domnode-expat.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. static const char version[] = "$Id$";
  2. /*
  3. * Copyright 2002, Meiosys SA (www.meiosys.com). All rights reserved.
  4. *
  5. * See the COPYING file for the terms of usage and distribution.
  6. */
  7. /* domnode - simple XML DOM-like interface
  8. * Copyright (c) 2002 Michael B. Allen <mballen@erols.com>
  9. *
  10. * The MIT License
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a
  13. * copy of this software and associated documentation files (the "Software"),
  14. * to deal in the Software without restriction, including without limitation
  15. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  16. * and/or sell copies of the Software, and to permit persons to whom the
  17. * Software is furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included
  20. * in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  26. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  27. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  28. * OTHER DEALINGS IN THE SOFTWARE.
  29. */
  30. #ifdef HAVE_CONFIG_H
  31. #include "config.h"
  32. #endif
  33. #include <sd/domnode.h>
  34. #include <sd/stack.h>
  35. #include <sd/malloc.h>
  36. #include <sd/error.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <ctype.h>
  40. #ifdef HAVE_LANGINFO_H
  41. # include <langinfo.h>
  42. #endif
  43. #include <assert.h>
  44. #include "expat.h"
  45. struct udata {
  46. char cdata[2048];
  47. size_t wptr;
  48. sd_stack_t* elements;
  49. sd_domnode_t* root;
  50. };
  51. /******************************************************************************/
  52. static struct udata* udata_new(void)
  53. {
  54. struct udata* this;
  55. this = sd_calloc(1, sizeof(*this));
  56. this->cdata[0] = 0;
  57. this->wptr = 0;
  58. this->elements = sd_stack_new(0);
  59. this->root = NULL;
  60. return this;
  61. }
  62. /******************************************************************************/
  63. static void udata_delete(struct udata* this)
  64. {
  65. if (!this)
  66. return;
  67. sd_stack_delete(this->elements, NULL);
  68. free(this);
  69. }
  70. /******************************************************************************/
  71. static void udata_pop_cdata(struct udata* this)
  72. {
  73. this->wptr = 0;
  74. memset(this->cdata, 0, sizeof(this->cdata));
  75. }
  76. /******************************************************************************/
  77. static int udata_push_cdata(struct udata* this, const XML_Char* s, int len)
  78. {
  79. XML_Char *str;
  80. if (!this || !s || !len)
  81. return -1;
  82. str = (XML_Char *)s + len;
  83. while (s < str && isspace(*s)) {
  84. s++;
  85. len--;
  86. }
  87. if (s == str) return -1;
  88. str = (XML_Char *)s + len - 1;
  89. while (str > s && isspace(*str)) {
  90. str--;
  91. len--;
  92. }
  93. if ((this->wptr + len) >= sizeof(this->cdata) - 1){
  94. sd_error("cdata buffer exceeded (maximum %d bytes)\n",
  95. sizeof(this->cdata));
  96. return -1;
  97. }
  98. strncpy(&this->cdata[this->wptr], s, len);
  99. this->wptr += len;
  100. this->cdata[this->wptr] = 0;
  101. return 0;
  102. }
  103. /******************************************************************************/
  104. static int foreach_delete(sd_domnode_t* anode, void* unused)
  105. {
  106. sd_domnode_delete(anode);
  107. return 0;
  108. }
  109. /******************************************************************************/
  110. extern sd_domnode_t*
  111. __sd_domnode_new(const char* name, const char* value, int is_elem)
  112. {
  113. sd_domnode_t* this;
  114. this = sd_calloc(1, sizeof(*this));
  115. this->name = name ? sd_strdup(name) : NULL;
  116. this->value = value ? sd_strdup(value): NULL;
  117. this->children = is_elem ? sd_list_new(10) : NULL;
  118. this->attrs = is_elem ? sd_list_new(10) : NULL;
  119. return this;
  120. }
  121. /******************************************************************************/
  122. extern sd_domnode_t* sd_domnode_new(const char* name, const char* value)
  123. {
  124. return __sd_domnode_new(name, value, 1);
  125. }
  126. /******************************************************************************/
  127. extern void sd_domnode_delete(sd_domnode_t* this)
  128. {
  129. if (!this)
  130. return;
  131. free((void*) this->name);
  132. free((void*) this->value);
  133. sd_list_foreach(this->children, (sd_list_func_t) foreach_delete, NULL);
  134. sd_list_delete(this->children);
  135. sd_list_foreach(this->attrs, (sd_list_func_t) foreach_delete, NULL);
  136. sd_list_delete(this->attrs);
  137. free(this);
  138. }
  139. /******************************************************************************/
  140. static void
  141. start_handler(struct udata* udata, const XML_Char* name, const XML_Char** atts)
  142. {
  143. sd_domnode_t* parent;
  144. sd_domnode_t* child;
  145. int i;
  146. if (!udata || !name || !atts)
  147. return;
  148. child = __sd_domnode_new(name, NULL, 1);
  149. for (i = 0; atts[i]; i += 2)
  150. sd_list_add(child->attrs, __sd_domnode_new(atts[i], atts[i + 1], 0));
  151. udata_pop_cdata(udata);
  152. /* root node has no parent. save it. */
  153. if ( (parent = sd_stack_peek(udata->elements)) == NULL)
  154. udata->root = child;
  155. else
  156. sd_list_add(parent->children, child);
  157. sd_stack_push(udata->elements, child);
  158. }
  159. /******************************************************************************/
  160. static void end_handler(struct udata* udata, const XML_Char* name)
  161. {
  162. udata_pop_cdata(udata);
  163. sd_stack_pop(udata->elements);
  164. }
  165. /******************************************************************************/
  166. static void cdata_handler(struct udata* udata, const XML_Char* s, int len)
  167. {
  168. sd_domnode_t* parent = sd_stack_peek(udata->elements);
  169. assert(parent != NULL);
  170. if (udata_push_cdata(udata, s, len) == -1)
  171. return;
  172. free((void*) parent->value);
  173. parent->value = strdup(udata->cdata);
  174. }
  175. /******************************************************************************/
  176. static void comment_handler(struct udata* udata, const XML_Char* s)
  177. {
  178. sd_domnode_t* parent = sd_stack_peek(udata->elements);
  179. assert(parent != NULL);
  180. sd_list_add(parent->children, __sd_domnode_new("#comment", s, 0));
  181. }
  182. /******************************************************************************/
  183. extern int sd_domnode_fread(sd_domnode_t* this, FILE* stream)
  184. {
  185. XML_Parser p;
  186. struct udata* udata;
  187. int ret = 0;
  188. if (!this || !stream)
  189. return -1;
  190. if ((p = XML_ParserCreate(NULL)) == NULL)
  191. return -1;
  192. udata = udata_new();
  193. XML_SetStartElementHandler (p, (XML_StartElementHandler) start_handler);
  194. XML_SetEndElementHandler (p, (XML_EndElementHandler) end_handler);
  195. XML_SetCharacterDataHandler (p, (XML_CharacterDataHandler) cdata_handler);
  196. XML_SetCommentHandler (p, (XML_CommentHandler) comment_handler);
  197. XML_SetUserData (p, udata);
  198. for ( ;; ) {
  199. size_t n;
  200. void* buf;
  201. int done;
  202. if ((buf = XML_GetBuffer(p, BUFSIZ)) == NULL) {
  203. ret = -1;
  204. break;
  205. }
  206. if ((n = fread(buf, 1, BUFSIZ, stream)) == 0 && ferror(stream)) {
  207. ret = -1;
  208. break;
  209. }
  210. if (!XML_ParseBuffer(p, n, (done = feof(stream)))) {
  211. sd_error("XML error: %s [%d:%d - %ld]\n",
  212. XML_ErrorString(XML_GetErrorCode(p)),
  213. XML_GetCurrentLineNumber(p),
  214. XML_GetCurrentColumnNumber(p),
  215. XML_GetCurrentByteIndex(p));
  216. ret = -1;
  217. break;
  218. }
  219. if (done)
  220. break;
  221. }
  222. if (udata->root)
  223. {
  224. free((void*) this->name);
  225. free((void*) this->value);
  226. sd_list_foreach(this->children, (sd_list_func_t) foreach_delete, NULL);
  227. sd_list_delete(this->children);
  228. sd_list_foreach(this->attrs, (sd_list_func_t) foreach_delete, NULL);
  229. sd_list_delete(this->attrs);
  230. this->name = udata->root->name;
  231. this->value = udata->root->value;
  232. this->children = udata->root->children;
  233. this->attrs = udata->root->attrs;
  234. free(udata->root);
  235. udata->root = NULL;
  236. }
  237. udata_delete(udata);
  238. XML_ParserFree(p);
  239. return ret;
  240. }
  241. /******************************************************************************/
  242. extern int
  243. sd_domnode_read(sd_domnode_t* this, const char* abuffer, size_t asize)
  244. {
  245. XML_Parser p;
  246. struct udata* udata;
  247. int ret = 0;
  248. if (!this)
  249. return -1;
  250. if ((p = XML_ParserCreate(NULL)) == NULL)
  251. return -1;
  252. udata = udata_new();
  253. XML_SetStartElementHandler (p, (XML_StartElementHandler) start_handler);
  254. XML_SetEndElementHandler (p, (XML_EndElementHandler) end_handler);
  255. XML_SetCharacterDataHandler (p, (XML_CharacterDataHandler) cdata_handler);
  256. XML_SetCommentHandler (p, (XML_CommentHandler) comment_handler);
  257. XML_SetUserData (p, udata);
  258. if (!XML_Parse(p, abuffer, asize, 1)) {
  259. sd_error("XML error: %s [%d:%d - %ld]\n",
  260. XML_ErrorString(XML_GetErrorCode(p)),
  261. XML_GetCurrentLineNumber(p),
  262. XML_GetCurrentColumnNumber(p),
  263. XML_GetCurrentByteIndex(p));
  264. ret = -1;
  265. }
  266. if (udata->root)
  267. {
  268. free((void*) this->name);
  269. free((void*) this->value);
  270. sd_list_foreach(this->children, (sd_list_func_t) foreach_delete, NULL);
  271. sd_list_delete(this->children);
  272. sd_list_foreach(this->attrs, (sd_list_func_t) foreach_delete, NULL);
  273. sd_list_delete(this->attrs);
  274. this->name = udata->root->name;
  275. this->value = udata->root->value;
  276. this->children = udata->root->children;
  277. this->attrs = udata->root->attrs;
  278. free(udata->root);
  279. udata->root = NULL;
  280. }
  281. udata_delete(udata);
  282. XML_ParserFree(p);
  283. return ret;
  284. }
  285. /******************************************************************************/
  286. static int _sd_domnode_fwrite(const sd_domnode_t* this, FILE* stream, int indent)
  287. {
  288. sd_list_iter_t* iter;
  289. int i;
  290. if (!this || !this->name || !stream)
  291. return -1;
  292. for (i = 0; i < indent; i++)
  293. fprintf(stream, " ");
  294. if (this->name && strcmp(this->name, "#comment") == 0) {
  295. fprintf(stream, "<!-- %s -->\n", this->value);
  296. return 0;
  297. }
  298. fprintf(stream, "<%s", this->name);
  299. for (iter = sd_list_begin(this->attrs); iter != sd_list_end(this->attrs);
  300. iter = sd_list_iter_next(iter)) {
  301. sd_domnode_t* node = iter->data;
  302. fprintf(stream, " %s=\"%s\"", node->name, node->value);
  303. }
  304. if (this->value || sd_list_get_nelem(this->children)) {
  305. fprintf(stream, ">\n");
  306. if (this->value) {
  307. for (i = 0; i < indent + 1; i++)
  308. fprintf(stream, " ");
  309. fprintf(stream, "%s\n", this->value);
  310. }
  311. for (iter = sd_list_begin(this->children); iter != sd_list_end(this->children);
  312. iter = sd_list_iter_next(iter)) {
  313. sd_domnode_t* node = iter->data;
  314. if (_sd_domnode_fwrite(node, stream, indent + 1) == -1)
  315. return -1;
  316. }
  317. for (i = 0; i < indent; i++)
  318. fprintf(stream, " ");
  319. fprintf(stream, "</%s>\n", this->name);
  320. } else {
  321. fprintf(stream, "/>\n");
  322. }
  323. return 0;
  324. }
  325. /******************************************************************************/
  326. extern int sd_domnode_fwrite(const sd_domnode_t* this, FILE* stream)
  327. {
  328. #ifdef HAVE_NL_LANGINFO
  329. fprintf(stream, "<?xml version=\"1.0\" encoding=\"%s\"?>\n\n",
  330. nl_langinfo(CODESET));
  331. #else
  332. fprintf(stream, "<?xml version=\"1.0\"?>\n\n");
  333. #endif
  334. return _sd_domnode_fwrite(this, stream, 0);
  335. }
  336. /******************************************************************************/
  337. extern int sd_domnode_load(sd_domnode_t* this, const char* afilename)
  338. {
  339. FILE* fp;
  340. int ret = 0;
  341. if ( (fp = fopen(afilename, "r")) == NULL)
  342. return -1;
  343. ret = sd_domnode_fread(this, fp);
  344. fclose(fp);
  345. return 0;
  346. }
  347. /******************************************************************************/
  348. extern int sd_domnode_store(const sd_domnode_t* this, const char* afilename)
  349. {
  350. FILE* fp;
  351. int ret = 0;
  352. if ( (fp = fopen(afilename, "w")) == NULL)
  353. return -1;
  354. ret = sd_domnode_fwrite(this, fp);
  355. fclose(fp);
  356. return 0;
  357. }
  358. /******************************************************************************/
  359. extern sd_domnode_t*
  360. sd_domnode_search(const sd_domnode_t* this, const char* name)
  361. {
  362. sd_list_iter_t* i;
  363. for (i = sd_list_begin(this->children); i != sd_list_end(this->children);
  364. i = sd_list_iter_next(i)) {
  365. sd_domnode_t* node = i->data;
  366. if (strcmp(node->name, name) == 0)
  367. return node;
  368. }
  369. for (i = sd_list_begin(this->attrs); i != sd_list_end(this->attrs);
  370. i = sd_list_iter_next(i)) {
  371. sd_domnode_t* node = i->data;
  372. if (strcmp(node->name, name) == 0)
  373. return node;
  374. }
  375. for (i = sd_list_begin(this->children); i != sd_list_end(this->children);
  376. i = sd_list_iter_next(i)) {
  377. sd_domnode_t* node = i->data;
  378. if ((node = sd_domnode_search(node, name)) != NULL)
  379. return node;
  380. }
  381. return NULL;
  382. }
  383. /******************************************************************************/
  384. extern sd_domnode_t*
  385. sd_domnode_attrs_put(sd_domnode_t* anode, sd_domnode_t* attr)
  386. {
  387. sd_list_iter_t* i;
  388. if (!anode || !anode->attrs || !attr || !attr->value)
  389. return NULL;
  390. if ( (i = sd_list_lookadd(anode->attrs, attr)) == sd_list_end(anode->attrs))
  391. return NULL;
  392. return i->data;
  393. }
  394. /******************************************************************************/
  395. extern sd_domnode_t*
  396. sd_domnode_attrs_get(const sd_domnode_t* anode, const char* name)
  397. {
  398. sd_list_iter_t* i;
  399. if (!anode || !anode->attrs || !name || !*name)
  400. return NULL;
  401. for (i = sd_list_begin(anode->attrs); i != sd_list_end(anode->attrs);
  402. i = sd_list_iter_next(i)) {
  403. sd_domnode_t* node = i->data;
  404. if (strcmp(node->name, name) == 0)
  405. return node;
  406. }
  407. return NULL;
  408. }
  409. /******************************************************************************/
  410. extern sd_domnode_t*
  411. sd_domnode_attrs_remove(sd_domnode_t* anode, const char* name)
  412. {
  413. sd_list_iter_t* i;
  414. if (!anode || !anode->attrs || !name || !*name)
  415. return NULL;
  416. for (i = sd_list_begin(anode->attrs); i != sd_list_end(anode->attrs);
  417. i = sd_list_iter_next(i)) {
  418. sd_domnode_t* node = i->data;
  419. if (strcmp(node->name, name) == 0) {
  420. sd_list_iter_del(i);
  421. return node;
  422. }
  423. }
  424. return NULL;
  425. }