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

Last change on this file since 22285 was 21424, checked in by davidb, 14 years ago

When testing on a 64-bit architecture, compile failed with error: cast from 'unsigned char*' to 'int' loses precision. Code seems to be doing a distance calculation between pointers in memory. Have changed these typecasts to (long)

  • Property svn:keywords set to Author Date Id Revision
File size: 2.6 KB
Line 
1#include "oaiargs.h"
2#include "cgiutils.h"
3
4// read the arguments from a stream for an OAI action
5void oaiargs::readArgs(istream &in)
6{
7 unsigned char buffer[256];
8 unsigned char *end = buffer + 256;
9 unsigned char *here = end;
10 unsigned char *start, *equals;
11
12 // if no parameters, return now.
13 if (in.eof()) {
14 cerr << "Aborting on EOF" << endl;
15 return;
16 }
17
18 // fill the buffer with the stream
19 this->fillBuffer(in, buffer, &here, end);
20
21 // start at the beginning of the buffer, read
22 // the stream until exhausted
23 here = buffer;
24 while (*here != '\0')
25 {
26 // each iteration of the outer loop reads a single argument
27
28 // we start an argument and continue until the buffer is
29 // exhausted or the next argument is found
30 start = here;
31 equals = NULL;
32 while (*here != '&' && *here != '\0') {
33 // remember where the equals sign was if necessary
34 if (equals == NULL && *here == '=') {
35 equals = here;
36 }
37
38 // move to the next character
39 ++here;
40
41 // and refill the buffer as necessary
42 if (here == end) {
43 this->fillBuffer(in, buffer, &here, end);
44 }
45 }
46
47 // we've now got the scope of one argument, where it begins, ends
48 // and where the equals sign is; we now split it up accordingly
49 // to create the actual text_t type elements for the label and the
50 // value.
51 text_t label, value;
52 if (equals == NULL) {
53 label.setcarr((char *) start, (long) here - (long) start);
54 value = label;
55 }
56 else {
57 label.setcarr((char *) start, (long) equals - (long) start);
58 ++equals;
59 value.setcarr((char *) equals, (long) here - (long) equals);
60 }
61
62 decode_cgi_arg(value);
63
64 // output to the screen for tracing purposes
65 // cerr << label << "?" << value << endl;
66
67 // if the argument already exists, raise an error
68 if (this->arguments.count(label) > 0) {
69 this->duplicateArg = true;
70 }
71 else {
72 // add this argument to the argument map
73 this->arguments[label] = value;
74 }
75
76 // don't choke on an argument separator; swallow it so we don't get
77 // inifinite loops above
78 if (*here == '&') {
79 ++here;
80 }
81 }
82}
83
84void oaiargs::fillBuffer(istream &in, unsigned char *buffer, unsigned char **currentPtr, unsigned char *end)
85{
86 unsigned char *fill = buffer;
87
88 // copy out the remaining buffer space
89 while (*currentPtr != end) {
90 *fill = **currentPtr;
91 *currentPtr++;
92 }
93
94 // set the buffer appropriately
95 *currentPtr = fill;
96
97 // refill the buffer
98 in.get((char *)*currentPtr, (char *)end - (char *)*currentPtr);
99}
100
Note: See TracBrowser for help on using the repository browser.