uxmlparse.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2008 by egnite GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*
  34. * \file pro/uxmlparse.c
  35. * \brief Micro XML low level parser routines.
  36. *
  37. * \verbatim
  38. * $Id: uxmlparse.c 2889 2010-02-07 18:20:25Z Astralix $
  39. * \endverbatim
  40. */
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <ctype.h>
  44. #include <pro/uxml.h>
  45. /*!
  46. * \addtogroup xgUXML
  47. */
  48. /*@{*/
  49. /*!
  50. * \brief Retrieve the next token from an XML tag.
  51. *
  52. * \param data Pointer to the next character within the string to parse.
  53. * \param tkn Pointer to the buffer that receives the next token.
  54. * \param size Size of the token buffer.
  55. *
  56. * \return Pointer to the character following the one parsed last.
  57. */
  58. char *UxmlParseTag(char *data, char *tkn, size_t size)
  59. {
  60. char *rc = NULL;
  61. /* Skip leading spaces. */
  62. while (isspace((unsigned char)*data)) {
  63. data++;
  64. }
  65. if (*data) {
  66. rc = data;
  67. if (isalnum((unsigned char)*rc)) {
  68. /* Collect name or number (well, almost). */
  69. while (isalnum((unsigned char)*rc) || *rc == '-' || *rc == ':' || *rc == '_') {
  70. if (size > 1) {
  71. size--;
  72. *tkn++ = *rc;
  73. }
  74. rc++;
  75. }
  76. } else if (*rc == '"' || *rc == '\'') {
  77. /* Collect quoted string. */
  78. rc++;
  79. while (*rc != *data) {
  80. if (size > 1) {
  81. size--;
  82. *tkn++ = *rc;
  83. }
  84. rc++;
  85. }
  86. rc++;
  87. } else {
  88. /* Return anything else as a single character. */
  89. *tkn++ = *rc++;
  90. }
  91. *tkn = 0;
  92. }
  93. return rc;
  94. }
  95. /*!
  96. * \brief Check if name matches filter list.
  97. *
  98. * \param name String to check against list.
  99. * \param filt Array of filter strings.
  100. *
  101. * \return 1 if the name matches at list one filter string. Otherwise
  102. * 0 is returned.
  103. */
  104. int UxmlFilterMatch(char *name, char **filt)
  105. {
  106. if (filt == NULL) {
  107. return 1;
  108. }
  109. while (*filt) {
  110. if (strcasecmp(name, *filt) == 0) {
  111. return 1;
  112. }
  113. filt++;
  114. }
  115. return 0;
  116. }
  117. /*@}*/