domnode.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright 2001-2003, Meiosys (www.meiosys.com). All rights reserved.
  3. *
  4. * See the COPYING file for the terms of usage and distribution.
  5. */
  6. #ifdef HAVE_CONFIG_H
  7. #include "config.h"
  8. #endif
  9. #include <tchar.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include "malloc.h"
  14. #include "error.h"
  15. #include "domnode.h"
  16. /* TODO: generic format support */
  17. #include "domnode-xml.h"
  18. static const TCHAR version[] = _T("$Id$");
  19. /******************************************************************************/
  20. extern sd_domnode_t* __sd_domnode_new(const TCHAR* a_name, const TCHAR* a_value, int is_elem)
  21. {
  22. sd_domnode_t* ptrThis;
  23. ptrThis = (sd_domnode_t*)sd_calloc(1, sizeof(*ptrThis));
  24. ptrThis->name = a_name ? sd_strdup(a_name) : 0;
  25. ptrThis->value = a_value ? sd_strdup(a_value): 0;
  26. ptrThis->children = is_elem ? sd_list_new(10) : 0;
  27. ptrThis->attrs = is_elem ? sd_list_new(10) : 0;
  28. return ptrThis;
  29. }
  30. /******************************************************************************/
  31. extern sd_domnode_t* sd_domnode_new(const TCHAR* a_name, const TCHAR* a_value)
  32. {
  33. return __sd_domnode_new(a_name, a_value, 1);
  34. }
  35. /******************************************************************************/
  36. static int foreach_delete(sd_domnode_t* a_node, void* unused)
  37. {
  38. sd_domnode_delete(a_node);
  39. return 0;
  40. }
  41. /******************************************************************************/
  42. static void domnode_clear(sd_domnode_t* ptrThis)
  43. {
  44. free((void*) ptrThis->name);
  45. free((void*) ptrThis->value);
  46. sd_list_foreach(ptrThis->children, (sd_list_func_t) foreach_delete, 0);
  47. sd_list_delete(ptrThis->children);
  48. sd_list_foreach(ptrThis->attrs, (sd_list_func_t) foreach_delete, 0);
  49. sd_list_delete(ptrThis->attrs);
  50. }
  51. /******************************************************************************/
  52. extern void sd_domnode_delete(sd_domnode_t* ptrThis)
  53. {
  54. if (!ptrThis)
  55. return;
  56. domnode_clear(ptrThis);
  57. free(ptrThis);
  58. }
  59. /******************************************************************************/
  60. static void domnode_update(sd_domnode_t* ptrThis, sd_domnode_t* a_node)
  61. {
  62. domnode_clear(ptrThis);
  63. ptrThis->name = a_node->name;
  64. ptrThis->value = a_node->value;
  65. ptrThis->children = a_node->children;
  66. ptrThis->attrs = a_node->attrs;
  67. /* Destroy ptrThis now empty node ! */
  68. free(a_node);
  69. }
  70. /******************************************************************************/
  71. extern int sd_domnode_fread(sd_domnode_t* ptrThis, FILE* a_stream)
  72. {
  73. int ret;
  74. sd_domnode_t* node;
  75. /* TODO: generic format support */
  76. if (! (ret = __sd_domnode_xml_fread(&node, a_stream)))
  77. domnode_update(ptrThis, node);
  78. return ret ? -1 : 0;
  79. }
  80. /******************************************************************************/
  81. extern int sd_domnode_read(sd_domnode_t* ptrThis, const TCHAR* a_buffer,size_t a_size)
  82. {
  83. int ret;
  84. sd_domnode_t* node;
  85. /* TODO: generic format support */
  86. if (! (ret = __sd_domnode_xml_read(&node, a_buffer, a_size)))
  87. domnode_update(ptrThis, node);
  88. return ret ? -1 : 0;
  89. }
  90. /******************************************************************************/
  91. extern int sd_domnode_write(sd_domnode_t* ptrThis, TCHAR** a_buffer,size_t* a_size)
  92. {
  93. /* TODO: generic format support */
  94. return __sd_domnode_xml_write(ptrThis, a_buffer, a_size);
  95. }
  96. /******************************************************************************/
  97. extern int sd_domnode_fwrite(const sd_domnode_t* ptrThis, FILE* a_stream)
  98. {
  99. /* TODO: generic format support */
  100. return __sd_domnode_xml_fwrite(ptrThis, a_stream);
  101. }
  102. /******************************************************************************/
  103. extern int sd_domnode_load(sd_domnode_t* ptrThis, const TCHAR* a_filename)
  104. {
  105. FILE* fp;
  106. int ret = 0;
  107. if ( (fp = _tfopen(a_filename, _T("r"))) == 0)
  108. return -1;
  109. ret = sd_domnode_fread(ptrThis, fp);
  110. fclose(fp);
  111. return ret;
  112. }
  113. /******************************************************************************/
  114. extern int sd_domnode_store(const sd_domnode_t* ptrThis, const TCHAR* afilename)
  115. {
  116. FILE* fp;
  117. int ret = 0;
  118. if ( (0 != _tfopen_s(&fp, afilename, _T("w"))))
  119. return -1;
  120. ret = sd_domnode_fwrite(ptrThis, fp);
  121. fclose(fp);
  122. return ret;
  123. }
  124. /******************************************************************************/
  125. extern sd_domnode_t* sd_domnode_search(const sd_domnode_t* ptrThis, const TCHAR* a_name)
  126. {
  127. sd_list_iter_t* i;
  128. for (i = sd_list_begin(ptrThis->children); i != sd_list_end(ptrThis->children);
  129. i = sd_list_iter_next(i)) {
  130. sd_domnode_t* node = (sd_domnode_t*)i->data;
  131. if (_tcscmp(node->name, a_name) == 0)
  132. return node;
  133. }
  134. for (i = sd_list_begin(ptrThis->attrs); i != sd_list_end(ptrThis->attrs);
  135. i = sd_list_iter_next(i)) {
  136. sd_domnode_t* node = (sd_domnode_t*)i->data;
  137. if (_tcscmp(node->name, a_name) == 0)
  138. return node;
  139. }
  140. for (i = sd_list_begin(ptrThis->children); i != sd_list_end(ptrThis->children);
  141. i = sd_list_iter_next(i)) {
  142. sd_domnode_t* node = (sd_domnode_t*)i->data;
  143. if ((node = sd_domnode_search(node, a_name)) != 0)
  144. return node;
  145. }
  146. return 0;
  147. }
  148. /******************************************************************************/
  149. extern sd_domnode_t* sd_domnode_attrs_put(sd_domnode_t* ptrThis,sd_domnode_t* a_attr)
  150. {
  151. sd_list_iter_t* i;
  152. if (!ptrThis || !ptrThis->attrs || !a_attr || !a_attr->value)
  153. return 0;
  154. if ((i = sd_list_lookadd(ptrThis->attrs, a_attr)) == sd_list_end(ptrThis->attrs))
  155. return 0;
  156. return (sd_domnode_t*)i->data;
  157. }
  158. /******************************************************************************/
  159. extern sd_domnode_t* sd_domnode_attrs_get(const sd_domnode_t* ptrThis, const TCHAR* a_name)
  160. {
  161. sd_list_iter_t* i;
  162. if (!ptrThis || !ptrThis->attrs || !a_name || !*a_name)
  163. return 0;
  164. for (i = sd_list_begin(ptrThis->attrs); i != sd_list_end(ptrThis->attrs);
  165. i = sd_list_iter_next(i)) {
  166. sd_domnode_t* node = (sd_domnode_t*)i->data;
  167. if (_tcscmp(node->name, a_name) == 0)
  168. return node;
  169. }
  170. return 0;
  171. }
  172. /******************************************************************************/
  173. extern sd_domnode_t* sd_domnode_attrs_remove(sd_domnode_t* ptrThis, const TCHAR* a_name)
  174. {
  175. sd_list_iter_t* i;
  176. if (!ptrThis || !ptrThis->attrs || !a_name || !*a_name)
  177. return 0;
  178. for (i = sd_list_begin(ptrThis->attrs); i != sd_list_end(ptrThis->attrs);
  179. i = sd_list_iter_next(i)) {
  180. sd_domnode_t* node = (sd_domnode_t*)i->data;
  181. if (_tcscmp(node->name, a_name) == 0) {
  182. sd_list_iter_del(i);
  183. return node;
  184. }
  185. }
  186. return 0;
  187. }