source: main/branches/64_bit_Greenstone/greenstone2/runtime-src/src/oaiservr/oaiargs.cpp@ 23508

Last change on this file since 23508 was 23508, checked in by sjm84, 13 years ago

Committing 64 bit changes into the branch

  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1/**********************************************************************
2 *
3 * oaiargs.cpp --
4 *
5 * Copyright (C) 2004-2010 The New Zealand Digital Library Project
6 *
7 * A component of the Greenstone digital library software
8 * from the New Zealand Digital Library Project at the
9 * University of Waikato, New Zealand.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 *********************************************************************/
26
27#include "mglong.h"
28#include "oaiargs.h"
29#include "cgiutils.h"
30
31// read the arguments from a stream for an OAI action
32void oaiargs::readArgs(istream &in)
33{
34 unsigned char buffer[256];
35 unsigned char *end = buffer + 256;
36 unsigned char *here = end;
37 unsigned char *start, *equals;
38
39 // if no parameters, return now.
40 if (in.eof()) {
41 cerr << "Aborting on EOF" << endl;
42 return;
43 }
44
45 // fill the buffer with the stream
46 this->fillBuffer(in, buffer, &here, end);
47
48 // start at the beginning of the buffer, read
49 // the stream until exhausted
50 here = buffer;
51 while (*here != '\0')
52 {
53 // each iteration of the outer loop reads a single argument
54
55 // we start an argument and continue until the buffer is
56 // exhausted or the next argument is found
57 start = here;
58 equals = NULL;
59 while (*here != '&' && *here != '\0') {
60 // remember where the equals sign was if necessary
61 if (equals == NULL && *here == '=') {
62 equals = here;
63 }
64
65 // move to the next character
66 ++here;
67
68 // and refill the buffer as necessary
69 if (here == end) {
70 this->fillBuffer(in, buffer, &here, end);
71 }
72 }
73
74 // we've now got the scope of one argument, where it begins, ends
75 // and where the equals sign is; we now split it up accordingly
76 // to create the actual text_t type elements for the label and the
77 // value.
78 text_t label, value;
79 if (equals == NULL) {
80 label.setcarr((char *) start, (uintptr_t) here - (uintptr_t) start);
81 value = label;
82 }
83 else {
84 label.setcarr((char *) start, (uintptr_t) equals - (uintptr_t) start);
85 ++equals;
86 value.setcarr((char *) equals, (uintptr_t) here - (uintptr_t) equals);
87 }
88
89 decode_cgi_arg(value);
90
91 // output to the screen for tracing purposes
92 // cerr << label << "?" << value << endl;
93
94 // if the argument already exists, raise an error
95 if (this->arguments.count(label) > 0) {
96 this->duplicateArg = true;
97 }
98 else {
99 // add this argument to the argument map
100 this->arguments[label] = value;
101 }
102
103 // don't choke on an argument separator; swallow it so we don't get
104 // inifinite loops above
105 if (*here == '&') {
106 ++here;
107 }
108 }
109}
110
111void oaiargs::fillBuffer(istream &in, unsigned char *buffer, unsigned char **currentPtr, unsigned char *end)
112{
113 unsigned char *fill = buffer;
114
115 // copy out the remaining buffer space
116 while (*currentPtr != end) {
117 *fill = **currentPtr;
118 *currentPtr++;
119 }
120
121 // set the buffer appropriately
122 *currentPtr = fill;
123
124 // refill the buffer
125 in.get((char *)*currentPtr, (char *)end - (char *)*currentPtr);
126}
127
Note: See TracBrowser for help on using the repository browser.