blob: d20989d4b352ffdffc13120c3282a370ce7e8cab [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 Veillard1bc8d852007-10-16 12:18:18 +000021static int debug = 0;
Daniel Veillarde77a9182000-04-05 19:12:29 +000022
Daniel Veillard220346d2001-12-07 11:33:54 +000023static void handleURI(const char *str) {
24 int ret;
25 xmlURIPtr uri;
26 xmlChar *res = NULL, *parsed = NULL;
27
28 uri = xmlCreateURI();
29
30 if (base == NULL) {
31 ret = xmlParseURIReference(uri, str);
32 if (ret != 0)
33 printf("%s : error %d\n", str, ret);
34 else {
Daniel Veillard1bc8d852007-10-16 12:18:18 +000035 if (debug) {
36 if (uri->scheme) printf("scheme: %s\n", uri->scheme);
37 if (uri->opaque) printf("opaque: %s\n", uri->opaque);
38 if (uri->authority) printf("authority: %s\n", uri->authority);
39 if (uri->server) printf("server: %s\n", uri->server);
40 if (uri->user) printf("user: %s\n", uri->user);
41 if (uri->port != 0) printf("port: %d\n", uri->port);
42 if (uri->path) printf("path: %s\n", uri->path);
43 if (uri->query) printf("query: %s\n", uri->query);
44 if (uri->fragment) printf("fragment: %s\n", uri->fragment);
45 if (uri->query_raw) printf("query_raw: %s\n", uri->query_raw);
46 if (uri->cleanup != 0) printf("cleanup\n");
47 }
Daniel Veillard220346d2001-12-07 11:33:54 +000048 xmlNormalizeURIPath(uri->path);
49 if (escape != 0) {
50 parsed = xmlSaveUri(uri);
51 res = xmlURIEscape(parsed);
William M. Brackc1939562003-08-05 15:52:22 +000052 printf("%s\n", (char *) res);
Daniel Veillard220346d2001-12-07 11:33:54 +000053
54 } else {
55 xmlPrintURI(stdout, uri);
56 printf("\n");
57 }
58 }
59 } else {
60 res = xmlBuildURI((xmlChar *)str, (xmlChar *) base);
61 if (res != NULL) {
William M. Brackc1939562003-08-05 15:52:22 +000062 printf("%s\n", (char *) res);
Daniel Veillard220346d2001-12-07 11:33:54 +000063 }
64 else
65 printf("::ERROR::\n");
66 }
67 if (res != NULL)
68 xmlFree(res);
69 if (parsed != NULL)
70 xmlFree(parsed);
71 xmlFreeURI(uri);
72}
73
74int main(int argc, char **argv) {
75 int i, arg = 1;
76
77 if ((argc > arg) && (argv[arg] != NULL) &&
Daniel Veillardb6e7fdb2001-02-02 17:07:32 +000078 ((!strcmp(argv[arg], "-base")) || (!strcmp(argv[arg], "--base")))) {
Daniel Veillarde77a9182000-04-05 19:12:29 +000079 arg++;
80 base = argv[arg];
81 if (base != NULL)
82 arg++;
83 }
Daniel Veillard220346d2001-12-07 11:33:54 +000084 if ((argc > arg) && (argv[arg] != NULL) &&
85 ((!strcmp(argv[arg], "-escape")) || (!strcmp(argv[arg], "--escape")))) {
86 arg++;
87 escape++;
88 }
Daniel Veillard1bc8d852007-10-16 12:18:18 +000089 if ((argc > arg) && (argv[arg] != NULL) &&
90 ((!strcmp(argv[arg], "-debug")) || (!strcmp(argv[arg], "--debug")))) {
91 arg++;
92 debug++;
93 }
Daniel Veillarde77a9182000-04-05 19:12:29 +000094 if (argv[arg] == NULL) {
95 char str[1024];
96
97 while (1) {
98 /*
99 * read one line in string buffer.
100 */
101 if (fgets (&str[0], sizeof (str) - 1, stdin) == NULL)
102 break;
103
104 /*
105 * remove the ending spaces
106 */
107 i = strlen(str);
108 while ((i > 0) &&
109 ((str[i - 1] == '\n') || (str[i - 1] == '\r') ||
110 (str[i - 1] == ' ') || (str[i - 1] == '\t'))) {
111 i--;
112 str[i] = 0;
113 }
Daniel Veillard220346d2001-12-07 11:33:54 +0000114 handleURI(str);
Daniel Veillarde77a9182000-04-05 19:12:29 +0000115 }
116 } else {
117 while (argv[arg] != NULL) {
Daniel Veillard220346d2001-12-07 11:33:54 +0000118 handleURI(argv[arg]);
Daniel Veillarde77a9182000-04-05 19:12:29 +0000119 arg++;
120 }
121 }
Daniel Veillarde77a9182000-04-05 19:12:29 +0000122 xmlMemoryDump();
Daniel Veillardb6e7fdb2001-02-02 17:07:32 +0000123 return(0);
Daniel Veillarde77a9182000-04-05 19:12:29 +0000124}