blob: 60abae023f2b1e38896d2a0843cfb5d2891b8813 [file] [log] [blame]
Daniel Veillarde77a9182000-04-05 19:12:29 +00001/*
2 * testURI.c : a small tester program for XML input.
3 *
4 * See Copyright for the status of this software.
5 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00006 * daniel@veillard.com
Daniel Veillarde77a9182000-04-05 19:12:29 +00007 */
8
Bjorn Reese70a9da52001-04-21 16:57:29 +00009#include "libxml.h"
Daniel Veillarde77a9182000-04-05 19:12:29 +000010
Daniel Veillarde77a9182000-04-05 19:12:29 +000011#include <string.h>
12#include <stdio.h>
13#include <stdarg.h>
14
Daniel Veillardbe803962000-06-28 23:40:59 +000015#include <libxml/xmlmemory.h>
Daniel Veillarde77a9182000-04-05 19:12:29 +000016#include <libxml/uri.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000017#include <libxml/globals.h>
Daniel Veillarde77a9182000-04-05 19:12:29 +000018
Daniel Veillard220346d2001-12-07 11:33:54 +000019static const char *base = NULL;
20static int escape = 0;
Daniel Veillarde77a9182000-04-05 19:12:29 +000021
Daniel Veillard220346d2001-12-07 11:33:54 +000022static 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", 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", 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
60int main(int argc, char **argv) {
61 int i, arg = 1;
62
63 if ((argc > arg) && (argv[arg] != NULL) &&
Daniel Veillardb6e7fdb2001-02-02 17:07:32 +000064 ((!strcmp(argv[arg], "-base")) || (!strcmp(argv[arg], "--base")))) {
Daniel Veillarde77a9182000-04-05 19:12:29 +000065 arg++;
66 base = argv[arg];
67 if (base != NULL)
68 arg++;
69 }
Daniel Veillard220346d2001-12-07 11:33:54 +000070 if ((argc > arg) && (argv[arg] != NULL) &&
71 ((!strcmp(argv[arg], "-escape")) || (!strcmp(argv[arg], "--escape")))) {
72 arg++;
73 escape++;
74 }
Daniel Veillarde77a9182000-04-05 19:12:29 +000075 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 Veillard220346d2001-12-07 11:33:54 +000095 handleURI(str);
Daniel Veillarde77a9182000-04-05 19:12:29 +000096 }
97 } else {
98 while (argv[arg] != NULL) {
Daniel Veillard220346d2001-12-07 11:33:54 +000099 handleURI(argv[arg]);
Daniel Veillarde77a9182000-04-05 19:12:29 +0000100 arg++;
101 }
102 }
Daniel Veillarde77a9182000-04-05 19:12:29 +0000103 xmlMemoryDump();
Daniel Veillardb6e7fdb2001-02-02 17:07:32 +0000104 return(0);
Daniel Veillarde77a9182000-04-05 19:12:29 +0000105}