1 | /*
|
---|
2 | * testURI.c : a small tester program for XML input.
|
---|
3 | *
|
---|
4 | * See Copyright for the status of this software.
|
---|
5 | *
|
---|
6 | * daniel@veillard.com
|
---|
7 | */
|
---|
8 |
|
---|
9 | #include "libxml.h"
|
---|
10 |
|
---|
11 | #include <string.h>
|
---|
12 | #include <stdio.h>
|
---|
13 | #include <stdarg.h>
|
---|
14 |
|
---|
15 | #include <libxml/xmlmemory.h>
|
---|
16 | #include <libxml/uri.h>
|
---|
17 | #include <libxml/globals.h>
|
---|
18 |
|
---|
19 | static const char *base = NULL;
|
---|
20 | static int escape = 0;
|
---|
21 |
|
---|
22 | static void handleURI(const char *str) {
|
---|
23 | int ret;
|
---|
24 | xmlURIPtr uri;
|
---|
25 | xmlChar *res = NULL, *parsed = NULL;
|
---|
26 |
|
---|
27 | uri = xmlCreateURI();
|
---|
28 |
|
---|
29 | if (base == NULL) {
|
---|
30 | ret = xmlParseURIReference(uri, str);
|
---|
31 | if (ret != 0)
|
---|
32 | printf("%s : error %d\n", str, ret);
|
---|
33 | else {
|
---|
34 | xmlNormalizeURIPath(uri->path);
|
---|
35 | if (escape != 0) {
|
---|
36 | parsed = xmlSaveUri(uri);
|
---|
37 | res = xmlURIEscape(parsed);
|
---|
38 | printf("%s\n", (char *) res);
|
---|
39 |
|
---|
40 | } else {
|
---|
41 | xmlPrintURI(stdout, uri);
|
---|
42 | printf("\n");
|
---|
43 | }
|
---|
44 | }
|
---|
45 | } else {
|
---|
46 | res = xmlBuildURI((xmlChar *)str, (xmlChar *) base);
|
---|
47 | if (res != NULL) {
|
---|
48 | printf("%s\n", (char *) res);
|
---|
49 | }
|
---|
50 | else
|
---|
51 | printf("::ERROR::\n");
|
---|
52 | }
|
---|
53 | if (res != NULL)
|
---|
54 | xmlFree(res);
|
---|
55 | if (parsed != NULL)
|
---|
56 | xmlFree(parsed);
|
---|
57 | xmlFreeURI(uri);
|
---|
58 | }
|
---|
59 |
|
---|
60 | int main(int argc, char **argv) {
|
---|
61 | int i, arg = 1;
|
---|
62 |
|
---|
63 | if ((argc > arg) && (argv[arg] != NULL) &&
|
---|
64 | ((!strcmp(argv[arg], "-base")) || (!strcmp(argv[arg], "--base")))) {
|
---|
65 | arg++;
|
---|
66 | base = argv[arg];
|
---|
67 | if (base != NULL)
|
---|
68 | arg++;
|
---|
69 | }
|
---|
70 | if ((argc > arg) && (argv[arg] != NULL) &&
|
---|
71 | ((!strcmp(argv[arg], "-escape")) || (!strcmp(argv[arg], "--escape")))) {
|
---|
72 | arg++;
|
---|
73 | escape++;
|
---|
74 | }
|
---|
75 | if (argv[arg] == NULL) {
|
---|
76 | char str[1024];
|
---|
77 |
|
---|
78 | while (1) {
|
---|
79 | /*
|
---|
80 | * read one line in string buffer.
|
---|
81 | */
|
---|
82 | if (fgets (&str[0], sizeof (str) - 1, stdin) == NULL)
|
---|
83 | break;
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * remove the ending spaces
|
---|
87 | */
|
---|
88 | i = strlen(str);
|
---|
89 | while ((i > 0) &&
|
---|
90 | ((str[i - 1] == '\n') || (str[i - 1] == '\r') ||
|
---|
91 | (str[i - 1] == ' ') || (str[i - 1] == '\t'))) {
|
---|
92 | i--;
|
---|
93 | str[i] = 0;
|
---|
94 | }
|
---|
95 | handleURI(str);
|
---|
96 | }
|
---|
97 | } else {
|
---|
98 | while (argv[arg] != NULL) {
|
---|
99 | handleURI(argv[arg]);
|
---|
100 | arg++;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | xmlMemoryDump();
|
---|
104 | return(0);
|
---|
105 | }
|
---|