jas_tvp.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2001-2002 Michael David Adams.
  3. * All rights reserved.
  4. */
  5. /* __START_OF_JASPER_LICENSE__
  6. *
  7. * JasPer License Version 2.0
  8. *
  9. * Copyright (c) 2001-2006 Michael David Adams
  10. * Copyright (c) 1999-2000 Image Power, Inc.
  11. * Copyright (c) 1999-2000 The University of British Columbia
  12. *
  13. * All rights reserved.
  14. *
  15. * Permission is hereby granted, free of charge, to any person (the
  16. * "User") obtaining a copy of this software and associated documentation
  17. * files (the "Software"), to deal in the Software without restriction,
  18. * including without limitation the rights to use, copy, modify, merge,
  19. * publish, distribute, and/or sell copies of the Software, and to permit
  20. * persons to whom the Software is furnished to do so, subject to the
  21. * following conditions:
  22. *
  23. * 1. The above copyright notices and this permission notice (which
  24. * includes the disclaimer below) shall be included in all copies or
  25. * substantial portions of the Software.
  26. *
  27. * 2. The name of a copyright holder shall not be used to endorse or
  28. * promote products derived from the Software without specific prior
  29. * written permission.
  30. *
  31. * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
  32. * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  33. * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
  34. * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  35. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  36. * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
  37. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  38. * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  39. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  40. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  41. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
  42. * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
  43. * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
  44. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
  45. * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
  46. * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
  47. * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
  48. * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
  49. * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
  50. * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
  51. * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
  52. * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
  53. * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
  54. * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
  55. * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
  56. * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
  57. *
  58. * __END_OF_JASPER_LICENSE__
  59. */
  60. /*
  61. * Tag-Value Parser Library
  62. *
  63. * $Id: jas_tvp.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  64. */
  65. /******************************************************************************\
  66. * Includes.
  67. \******************************************************************************/
  68. #include <assert.h>
  69. #include <stdio.h>
  70. #include <ctype.h>
  71. #include <stdlib.h>
  72. #include "jasper/jas_malloc.h"
  73. #include "jasper/jas_string.h"
  74. #include "jasper/jas_tvp.h"
  75. /******************************************************************************\
  76. * Macros.
  77. \******************************************************************************/
  78. /* Is the specified character valid for a tag name? */
  79. #define JAS_TVP_ISTAG(x) \
  80. (isalpha(x) || (x) == '_' || isdigit(x))
  81. /******************************************************************************\
  82. * Code for creating and destroying a tag-value parser.
  83. \******************************************************************************/
  84. jas_tvparser_t *jas_tvparser_create(const char *s)
  85. {
  86. jas_tvparser_t *tvp;
  87. if (!(tvp = jas_malloc(sizeof(jas_tvparser_t)))) {
  88. return 0;
  89. }
  90. if (!(tvp->buf = jas_strdup(s))) {
  91. jas_tvparser_destroy(tvp);
  92. return 0;
  93. }
  94. tvp->pos = tvp->buf;
  95. tvp->tag = 0;
  96. tvp->val = 0;
  97. return tvp;
  98. }
  99. void jas_tvparser_destroy(jas_tvparser_t *tvp)
  100. {
  101. if (tvp->buf) {
  102. jas_free(tvp->buf);
  103. }
  104. jas_free(tvp);
  105. }
  106. /******************************************************************************\
  107. * Main parsing code.
  108. \******************************************************************************/
  109. /* Get the next tag-value pair. */
  110. int jas_tvparser_next(jas_tvparser_t *tvp)
  111. {
  112. char *p;
  113. char *tag;
  114. char *val;
  115. /* Skip any leading whitespace. */
  116. p = tvp->pos;
  117. while (*p != '\0' && isspace(*p)) {
  118. ++p;
  119. }
  120. /* Has the end of the input data been reached? */
  121. if (*p == '\0') {
  122. /* No more tags are present. */
  123. tvp->pos = p;
  124. return 1;
  125. }
  126. /* Does the tag name begin with a valid character? */
  127. if (!JAS_TVP_ISTAG(*p)) {
  128. return -1;
  129. }
  130. /* Remember where the tag name begins. */
  131. tag = p;
  132. /* Find the end of the tag name. */
  133. while (*p != '\0' && JAS_TVP_ISTAG(*p)) {
  134. ++p;
  135. }
  136. /* Has the end of the input data been reached? */
  137. if (*p == '\0') {
  138. /* The value field is empty. */
  139. tvp->tag = tag;
  140. tvp->val = "";
  141. tvp->pos = p;
  142. return 0;
  143. }
  144. /* Is a value field not present? */
  145. if (*p != '=') {
  146. if (*p != '\0' && !isspace(*p)) {
  147. return -1;
  148. }
  149. *p++ = '\0';
  150. tvp->tag = tag;
  151. tvp->val = "";
  152. tvp->pos = p;
  153. return 0;
  154. }
  155. *p++ = '\0';
  156. val = p;
  157. while (*p != '\0' && !isspace(*p)) {
  158. ++p;
  159. }
  160. if (*p != '\0') {
  161. *p++ = '\0';
  162. }
  163. tvp->pos = p;
  164. tvp->tag = tag;
  165. tvp->val = val;
  166. return 0;
  167. }
  168. /******************************************************************************\
  169. * Code for querying the current tag/value.
  170. \******************************************************************************/
  171. /* Get the current tag. */
  172. char *jas_tvparser_gettag(jas_tvparser_t *tvp)
  173. {
  174. return tvp->tag;
  175. }
  176. /* Get the current value. */
  177. char *jas_tvparser_getval(jas_tvparser_t *tvp)
  178. {
  179. return tvp->val;
  180. }
  181. /******************************************************************************\
  182. * Miscellaneous code.
  183. \******************************************************************************/
  184. /* Lookup a tag by name. */
  185. jas_taginfo_t *jas_taginfos_lookup(jas_taginfo_t *taginfos, const char *name)
  186. {
  187. jas_taginfo_t *taginfo;
  188. taginfo = taginfos;
  189. while (taginfo->id >= 0) {
  190. if (!strcmp(taginfo->name, name)) {
  191. return taginfo;
  192. }
  193. ++taginfo;
  194. }
  195. return 0;
  196. }
  197. /* This function is simply for convenience. */
  198. /* One can avoid testing for the special case of a null pointer, by
  199. using this function. This function never returns a null pointer. */
  200. jas_taginfo_t *jas_taginfo_nonull(jas_taginfo_t *taginfo)
  201. {
  202. static jas_taginfo_t invalidtaginfo = {
  203. -1, 0
  204. };
  205. return taginfo ? taginfo : &invalidtaginfo;
  206. }