source: main/trunk/greenstone2/runtime-src/src/oaiservr/oaiargs.cpp@ 22739

Last change on this file since 22739 was 22739, checked in by mdewsnip, 14 years ago

Added copyright header to runtime-src/src/oaiserver/*.cpp and runtime-src/src/oaiserver/*.h.

  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
RevLine 
[22739]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
[8182]27#include "oaiargs.h"
28#include "cgiutils.h"
29
30// read the arguments from a stream for an OAI action
31void oaiargs::readArgs(istream &in)
32{
33 unsigned char buffer[256];
34 unsigned char *end = buffer + 256;
35 unsigned char *here = end;
36 unsigned char *start, *equals;
37
38 // if no parameters, return now.
39 if (in.eof()) {
40 cerr << "Aborting on EOF" << endl;
41 return;
42 }
43
44 // fill the buffer with the stream
45 this->fillBuffer(in, buffer, &here, end);
46
47 // start at the beginning of the buffer, read
48 // the stream until exhausted
49 here = buffer;
50 while (*here != '\0')
51 {
52 // each iteration of the outer loop reads a single argument
53
54 // we start an argument and continue until the buffer is
55 // exhausted or the next argument is found
56 start = here;
57 equals = NULL;
58 while (*here != '&' && *here != '\0') {
59 // remember where the equals sign was if necessary
60 if (equals == NULL && *here == '=') {
61 equals = here;
62 }
63
64 // move to the next character
[9608]65 ++here;
[8182]66
67 // and refill the buffer as necessary
68 if (here == end) {
69 this->fillBuffer(in, buffer, &here, end);
70 }
71 }
72
73 // we've now got the scope of one argument, where it begins, ends
74 // and where the equals sign is; we now split it up accordingly
75 // to create the actual text_t type elements for the label and the
76 // value.
77 text_t label, value;
78 if (equals == NULL) {
[21424]79 label.setcarr((char *) start, (long) here - (long) start);
[8182]80 value = label;
81 }
82 else {
[21424]83 label.setcarr((char *) start, (long) equals - (long) start);
[9608]84 ++equals;
[21424]85 value.setcarr((char *) equals, (long) here - (long) equals);
[8182]86 }
87
88 decode_cgi_arg(value);
89
90 // output to the screen for tracing purposes
91 // cerr << label << "?" << value << endl;
92
93 // if the argument already exists, raise an error
94 if (this->arguments.count(label) > 0) {
95 this->duplicateArg = true;
96 }
97 else {
98 // add this argument to the argument map
99 this->arguments[label] = value;
100 }
101
102 // don't choke on an argument separator; swallow it so we don't get
103 // inifinite loops above
104 if (*here == '&') {
[9608]105 ++here;
[8182]106 }
107 }
108}
109
110void oaiargs::fillBuffer(istream &in, unsigned char *buffer, unsigned char **currentPtr, unsigned char *end)
111{
112 unsigned char *fill = buffer;
113
114 // copy out the remaining buffer space
115 while (*currentPtr != end) {
116 *fill = **currentPtr;
[10336]117 *currentPtr++;
[8182]118 }
119
120 // set the buffer appropriately
121 *currentPtr = fill;
122
123 // refill the buffer
[8228]124 in.get((char *)*currentPtr, (char *)end - (char *)*currentPtr);
[8182]125}
126
Note: See TracBrowser for help on using the repository browser.