Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * testURI.c : a small tester program for XML input. |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 6 | * daniel@veillard.com |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 9 | #include "libxml.h" |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 10 | |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 11 | #include <string.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdarg.h> |
| 14 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 15 | #include <libxml/xmlmemory.h> |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 16 | #include <libxml/uri.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 17 | #include <libxml/globals.h> |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 18 | |
Daniel Veillard | 220346d | 2001-12-07 11:33:54 +0000 | [diff] [blame] | 19 | static const char *base = NULL; |
| 20 | static int escape = 0; |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 21 | |
Daniel Veillard | 220346d | 2001-12-07 11:33:54 +0000 | [diff] [blame] | 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); |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 38 | printf("%s\n", (char *) res); |
Daniel Veillard | 220346d | 2001-12-07 11:33:54 +0000 | [diff] [blame] | 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) { |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 48 | printf("%s\n", (char *) res); |
Daniel Veillard | 220346d | 2001-12-07 11:33:54 +0000 | [diff] [blame] | 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) && |
Daniel Veillard | b6e7fdb | 2001-02-02 17:07:32 +0000 | [diff] [blame] | 64 | ((!strcmp(argv[arg], "-base")) || (!strcmp(argv[arg], "--base")))) { |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 65 | arg++; |
| 66 | base = argv[arg]; |
| 67 | if (base != NULL) |
| 68 | arg++; |
| 69 | } |
Daniel Veillard | 220346d | 2001-12-07 11:33:54 +0000 | [diff] [blame] | 70 | if ((argc > arg) && (argv[arg] != NULL) && |
| 71 | ((!strcmp(argv[arg], "-escape")) || (!strcmp(argv[arg], "--escape")))) { |
| 72 | arg++; |
| 73 | escape++; |
| 74 | } |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 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 | } |
Daniel Veillard | 220346d | 2001-12-07 11:33:54 +0000 | [diff] [blame] | 95 | handleURI(str); |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 96 | } |
| 97 | } else { |
| 98 | while (argv[arg] != NULL) { |
Daniel Veillard | 220346d | 2001-12-07 11:33:54 +0000 | [diff] [blame] | 99 | handleURI(argv[arg]); |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 100 | arg++; |
| 101 | } |
| 102 | } |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 103 | xmlMemoryDump(); |
Daniel Veillard | b6e7fdb | 2001-02-02 17:07:32 +0000 | [diff] [blame] | 104 | return(0); |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 105 | } |