testupnpreplyparse.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* $Id: testupnpreplyparse.c,v 1.4 2014/01/27 11:45:19 nanard Exp $ */
  2. /* MiniUPnP project
  3. * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
  4. * (c) 2006-2014 Thomas Bernard
  5. * This software is subject to the conditions detailed
  6. * in the LICENCE file provided within the distribution */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include "upnpreplyparse.h"
  11. int
  12. test_parsing(const char * buf, int len, FILE * f)
  13. {
  14. char line[1024];
  15. struct NameValueParserData pdata;
  16. int ok = 1;
  17. ParseNameValue(buf, len, &pdata);
  18. /* check result */
  19. if(f != NULL)
  20. {
  21. while(fgets(line, sizeof(line), f))
  22. {
  23. char * value;
  24. char * equal;
  25. char * parsedvalue;
  26. int l;
  27. l = strlen(line);
  28. while((l > 0) && ((line[l-1] == '\r') || (line[l-1] == '\n')))
  29. line[--l] = '\0';
  30. /* skip empty lines */
  31. if(l == 0)
  32. continue;
  33. equal = strchr(line, '=');
  34. if(equal == NULL)
  35. {
  36. fprintf(stderr, "Warning, line does not contain '=' : %s\n", line);
  37. continue;
  38. }
  39. *equal = '\0';
  40. value = equal + 1;
  41. parsedvalue = GetValueFromNameValueList(&pdata, line);
  42. if((parsedvalue == NULL) || (strcmp(parsedvalue, value) != 0))
  43. {
  44. fprintf(stderr, "Element <%s> : expecting value '%s', got '%s'\n",
  45. line, value, parsedvalue ? parsedvalue : "<null string>");
  46. ok = 0;
  47. }
  48. }
  49. }
  50. ClearNameValueList(&pdata);
  51. return ok;
  52. }
  53. int main(int argc, char * * argv)
  54. {
  55. FILE * f;
  56. char buffer[4096];
  57. int l;
  58. int ok;
  59. if(argc<2)
  60. {
  61. fprintf(stderr, "Usage: %s file.xml [file.namevalues]\n", argv[0]);
  62. return 1;
  63. }
  64. f = fopen(argv[1], "r");
  65. if(!f)
  66. {
  67. fprintf(stderr, "Error : can not open file %s\n", argv[1]);
  68. return 2;
  69. }
  70. l = fread(buffer, 1, sizeof(buffer)-1, f);
  71. fclose(f);
  72. f = NULL;
  73. buffer[l] = '\0';
  74. if(argc > 2)
  75. {
  76. f = fopen(argv[2], "r");
  77. if(!f)
  78. {
  79. fprintf(stderr, "Error : can not open file %s\n", argv[2]);
  80. return 2;
  81. }
  82. }
  83. #ifdef DEBUG
  84. DisplayNameValueList(buffer, l);
  85. #endif
  86. ok = test_parsing(buffer, l, f);
  87. if(f)
  88. {
  89. fclose(f);
  90. }
  91. return ok ? 0 : 3;
  92. }