57fba7d7e768bf3eaac7844e893f70af9e519c3d
[bluesky.git] / libs3-1.4 / src / testsimplexml.c
1 /** **************************************************************************
2  * testsimplexml.c
3  * 
4  * Copyright 2008 Bryan Ischo <bryan@ischo.com>
5  * 
6  * This file is part of libs3.
7  * 
8  * libs3 is free software: you can redistribute it and/or modify it under the
9  * terms of the GNU General Public License as published by the Free Software
10  * Foundation, version 3 of the License.
11  *
12  * In addition, as a special exception, the copyright holders give
13  * permission to link the code of this library and its programs with the
14  * OpenSSL library, and distribute linked combinations including the two.
15  *
16  * libs3 is distributed in the hope that it will be useful, but WITHOUT ANY
17  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License version 3
22  * along with libs3, in a file named COPYING.  If not, see
23  * <http://www.gnu.org/licenses/>.
24  *
25  ************************************************************************** **/
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <time.h>
31 #include "simplexml.h"
32
33 static S3Status simpleXmlCallback(const char *elementPath, const char *data,
34                                   int dataLen, void *callbackData)
35 {
36     (void) callbackData;
37
38     printf("[%s]: [%.*s]\n", elementPath, dataLen, data);
39
40     return S3StatusOK;
41 }
42
43
44 // The only argument allowed is a specification of the random seed to use
45 int main(int argc, char **argv)
46 {
47     if (argc > 1) {
48         char *arg = argv[1];
49         int seed = 0;
50         while (*arg) {
51             seed *= 10;
52             seed += (*arg++ - '0');
53         }
54         
55         srand(seed);
56     }
57     else {
58         srand(time(0));
59     }
60
61     SimpleXml simpleXml;
62
63     simplexml_initialize(&simpleXml, &simpleXmlCallback, 0);
64
65     // Read chunks of 10K from stdin, and then feed them in random chunks
66     // to simplexml_add
67     char inbuf[10000];
68
69     int amt_read;
70     while ((amt_read = fread(inbuf, 1, sizeof(inbuf), stdin)) > 0) {
71         char *buf = inbuf;
72         while (amt_read) {
73             int amt = (rand() % amt_read) + 1;
74             S3Status status = simplexml_add(&simpleXml, buf, amt);
75             if (status != S3StatusOK) {
76                 fprintf(stderr, "ERROR: Parse failure: %d\n", status);
77                 simplexml_deinitialize(&simpleXml);
78                 return -1;
79             }
80             buf += amt, amt_read -= amt;
81         }
82     }
83
84     simplexml_deinitialize(&simpleXml);
85
86     return 0;
87 }