upnpreplyparse.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* $Id: upnpreplyparse.c,v 1.10 2008/02/21 13:05:27 nanard Exp $ */
  2. /* MiniUPnP project
  3. * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
  4. * (c) 2006 Thomas Bernard
  5. * This software is subject to the conditions detailed
  6. * in the LICENCE file provided within the distribution */
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include "upnpreplyparse.h"
  11. #include "minixml.h"
  12. static void
  13. NameValueParserStartElt(void * d, const char * name, int l)
  14. {
  15. struct NameValueParserData * data = (struct NameValueParserData *)d;
  16. if(l>63)
  17. l = 63;
  18. memcpy(data->curelt, name, l);
  19. data->curelt[l] = '\0';
  20. }
  21. static void
  22. NameValueParserGetData(void * d, const char * datas, int l)
  23. {
  24. struct NameValueParserData * data = (struct NameValueParserData *)d;
  25. struct NameValue * nv;
  26. nv = malloc(sizeof(struct NameValue));
  27. if(l>63)
  28. l = 63;
  29. strncpy(nv->name, data->curelt, 64);
  30. nv->name[63] = '\0';
  31. memcpy(nv->value, datas, l);
  32. nv->value[l] = '\0';
  33. LIST_INSERT_HEAD( &(data->head), nv, entries);
  34. }
  35. void
  36. ParseNameValue(const char * buffer, int bufsize,
  37. struct NameValueParserData * data)
  38. {
  39. struct xmlparser parser;
  40. LIST_INIT(&(data->head));
  41. /* init xmlparser object */
  42. parser.xmlstart = buffer;
  43. parser.xmlsize = bufsize;
  44. parser.data = data;
  45. parser.starteltfunc = NameValueParserStartElt;
  46. parser.endeltfunc = 0;
  47. parser.datafunc = NameValueParserGetData;
  48. parser.attfunc = 0;
  49. parsexml(&parser);
  50. }
  51. void
  52. ClearNameValueList(struct NameValueParserData * pdata)
  53. {
  54. struct NameValue * nv;
  55. while((nv = pdata->head.lh_first) != NULL)
  56. {
  57. LIST_REMOVE(nv, entries);
  58. free(nv);
  59. }
  60. }
  61. char *
  62. GetValueFromNameValueList(struct NameValueParserData * pdata,
  63. const char * Name)
  64. {
  65. struct NameValue * nv;
  66. char * p = NULL;
  67. for(nv = pdata->head.lh_first;
  68. (nv != NULL) && (p == NULL);
  69. nv = nv->entries.le_next)
  70. {
  71. if(strcmp(nv->name, Name) == 0)
  72. p = nv->value;
  73. }
  74. return p;
  75. }
  76. #if 0
  77. /* useless now that minixml ignores namespaces by itself */
  78. char *
  79. GetValueFromNameValueListIgnoreNS(struct NameValueParserData * pdata,
  80. const char * Name)
  81. {
  82. struct NameValue * nv;
  83. char * p = NULL;
  84. char * pname;
  85. for(nv = pdata->head.lh_first;
  86. (nv != NULL) && (p == NULL);
  87. nv = nv->entries.le_next)
  88. {
  89. pname = strrchr(nv->name, ':');
  90. if(pname)
  91. pname++;
  92. else
  93. pname = nv->name;
  94. if(strcmp(pname, Name)==0)
  95. p = nv->value;
  96. }
  97. return p;
  98. }
  99. #endif
  100. /* debug all-in-one function
  101. * do parsing then display to stdout */
  102. #ifdef DEBUG
  103. void
  104. DisplayNameValueList(char * buffer, int bufsize)
  105. {
  106. struct NameValueParserData pdata;
  107. struct NameValue * nv;
  108. ParseNameValue(buffer, bufsize, &pdata);
  109. for(nv = pdata.head.lh_first;
  110. nv != NULL;
  111. nv = nv->entries.le_next)
  112. {
  113. printf("%s = %s\n", nv->name, nv->value);
  114. }
  115. ClearNameValueList(&pdata);
  116. }
  117. #endif