testURI.c 2.9 KB

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