<libroxml  version="2.3.0" />
contact: tristan.lelong@libroxml.net
roxml.c
Go to the documentation of this file.
1 
26 #include "roxml-internal.h"
27 
28 void ROXML_API roxml_release(void *data)
29 {
30  memory_cell_t *ptr = &head_cell;
31  memory_cell_t *to_delete = NULL;
32 
33  if (data == RELEASE_LAST) {
34  while ((ptr->prev != NULL) && (ptr->prev->id != pthread_self())) {
35  ptr = ptr->prev;
36  }
37  if (ptr->prev == NULL) {
38  return;
39  }
40 
41  to_delete = ptr->prev;
42 
43  if (to_delete->next) {
44  to_delete->prev->next = to_delete->next;
45  to_delete->next->prev = to_delete->prev;
46  } else {
47  if (to_delete->prev != &head_cell) {
48  head_cell.prev = to_delete->prev;
49  } else {
50  head_cell.prev = NULL;
51  }
52  to_delete->prev->next = NULL;
53  }
54 
55  if (PTR_IS_STAR(to_delete)) {
56  int i = 0;
57  for (i = 0; i < to_delete->occ; i++) {
58  free(((void **)(to_delete->ptr))[i]);
59  }
60  }
61  if (to_delete->type != PTR_NONE) {
62  free(to_delete->ptr);
63  to_delete->type = PTR_NONE;
64  free(to_delete);
65  }
66  } else if (data == RELEASE_ALL) {
67  head_cell.prev = NULL;
68  while ((head_cell.next != NULL)) {
69  to_delete = head_cell.next;
70  if (to_delete->next) {
71  to_delete->next->prev = &head_cell;
72  }
73  head_cell.next = to_delete->next;
74 
75  if (PTR_IS_STAR(to_delete)) {
76  int i = 0;
77  for (i = 0; i < to_delete->occ; i++) {
78  free(((void **)(to_delete->ptr))[i]);
79  }
80  }
81  free(to_delete->ptr);
82  to_delete->ptr = NULL;
83  to_delete->type = PTR_NONE;
84  free(to_delete);
85  }
86  } else {
87  while ((ptr->next != NULL) && (ptr->next->ptr != data)) {
88  ptr = ptr->next;
89  }
90  if (ptr->next == NULL) {
91  return;
92  }
93 
94  to_delete = ptr->next;
95  if (to_delete->next) {
96  to_delete->next->prev = ptr;
97  } else {
98  if (ptr == &head_cell) {
99  head_cell.prev = NULL;
100  } else {
101  head_cell.prev = to_delete->prev;
102  }
103  }
104  ptr->next = to_delete->next;
105  if (PTR_IS_STAR(to_delete)) {
106  int i = 0;
107  for (i = 0; i < to_delete->occ; i++) {
108  free(((void **)(to_delete->ptr))[i]);
109  }
110  }
111  free(to_delete->ptr);
112  to_delete->type = PTR_NONE;
113  free(to_delete);
114  }
115  if (head_cell.next == &head_cell) {
116  head_cell.next = NULL;
117  }
118  if (head_cell.prev == &head_cell) {
119  head_cell.prev = NULL;
120  }
121 }
122 
123 char ROXML_API *roxml_get_content(node_t *n, char *buffer, int bufsize, int *size)
124 {
125  int total = 0;
126  char *content = buffer;
127 
128  if (n == NULL) {
129  if (size) {
130  *size = 0;
131  }
132  if (buffer) {
133  strcpy(buffer, "");
134  return buffer;
135  }
136  return NULL;
137  } else if (n->type & ROXML_ELM_NODE) {
138  node_t *ptr = n->chld;
139  while (ptr) {
140  if ((ptr->type & ROXML_NODE_TYPES) == ROXML_TXT_NODE) {
141  total += ptr->end - ptr->pos;
142  }
143  ptr = ptr->sibl;
144  }
145 
146  if (content == NULL) {
147  content = roxml_malloc(sizeof(char), total + 1, PTR_CHAR);
148  bufsize = total + 1;
149  }
150  if (content == NULL) {
151  return NULL;
152  }
153 
154  total = 0;
155  ptr = n->chld;
156  while (ptr) {
157  if ((ptr->type & ROXML_NODE_TYPES) == ROXML_TXT_NODE) {
158  int ret_len = 0;
159  int read_size = ptr->end - ptr->pos;
160 
161  if (total + read_size > bufsize - 1) {
162  read_size = bufsize - total - 1;
163  }
164  ret_len += roxml_read(ptr->pos, read_size, content + total, ptr);
165 
166  total += ret_len;
167  }
168  ptr = ptr->sibl;
169  }
170  } else {
171  node_t *target = n;
172  int read_size = 0;
173  int name_len = 0;
174  int spec_offset = 0;
175 
176  ROXML_GET_BASE_BUFFER(name);
177 
178  roxml_get_name(n, name, ROXML_BASE_LEN);
179  name_len = strlen(name);
180 
181  ROXML_PUT_BASE_BUFFER(name);
182 
183  if (n->type & ROXML_DOCTYPE_NODE) {
184  total = target->end - target->pos - name_len - 2;
185  spec_offset = target->pos + name_len + 2;
186  } else if (n->type & ROXML_TXT_NODE) {
187  total = target->end - target->pos;
188  spec_offset = target->pos;
189  } else if (n->type & ROXML_CMT_NODE) {
190  total = target->end - target->pos - 4;
191  spec_offset = target->pos + 4;
192  } else if (n->type & ROXML_PI_NODE) {
193  total = target->end - target->pos - name_len - 3;
194  spec_offset = target->pos + name_len + 3;
195  } else if (n->type & ROXML_ATTR_NODE) {
196  target = n->chld;
197  if (target) {
198  spec_offset = target->pos;
199  total = target->end - target->pos;
200  } else {
201  spec_offset = 0;
202  total = 0;
203  }
204  }
205 
206  if (content == NULL) {
207  content = roxml_malloc(sizeof(char), total + 1, PTR_CHAR);
208  bufsize = total + 1;
209  }
210  if (content == NULL) {
211  return NULL;
212  }
213 
214  read_size = total;
215  if (read_size > bufsize - 1) {
216  read_size = bufsize - 1;
217  }
218  total = roxml_read(spec_offset, read_size, content, target);
219  }
220 
221  content[total] = '\0';
222  if (size) {
223  *size = total + 1;
224  }
225  return content;
226 }
227 
228 char ROXML_API *roxml_get_name(node_t *n, char *buffer, int size)
229 {
230  int offset = 0;
231 
232  ROXML_GET_BASE_BUFFER(tmp_name);
233 
234  memset(tmp_name, 0, ROXML_BASE_LEN * sizeof(char));
235 
236  if (buffer) {
237  memset(buffer, 0, size * sizeof(char));
238  }
239 
240  if (n == NULL) {
241  if (buffer) {
242  strcpy(buffer, "");
243  }
244  ROXML_PUT_BASE_BUFFER(tmp_name);
245  return buffer;
246  }
247 
248  if (n->prnt == NULL) {
249  strcpy(tmp_name, "documentRoot");
250  } else if (n->type & ROXML_NS_NODE) {
251  roxml_ns_t *ns = (roxml_ns_t *) n->priv;
252  if (ns) {
253  strncpy(tmp_name, ns->alias, ROXML_BASE_LEN-1);
254  } else {
255  tmp_name[0] = '\0';
256  }
257  } else if ((n->type & ROXML_TXT_NODE) || (n->type & ROXML_CMT_NODE)) {
258  ROXML_PUT_BASE_BUFFER(tmp_name);
259  if (buffer) {
260  strcpy(buffer, "");
261  return buffer;
262  }
263  return NULL;
264  } else {
265  int count = 0;
266  int total = 0;
267  int spec_offset = 0;
268 
269  if (n->type & ROXML_PI_NODE) {
270  spec_offset = 2;
271  } else if (n->type & ROXML_DOCTYPE_NODE) {
272  spec_offset = 1;
273  }
274 
275  if ((total = roxml_read(n->pos + spec_offset, ROXML_BASE_LEN, tmp_name, n)) > 0) {
276  while (ROXML_WHITE(tmp_name[offset]) || tmp_name[offset] == '<') {
277  offset++;
278  }
279  count = offset;
280  } else {
281  count = 0;
282  }
283 
284  if (n->type & ROXML_PI_NODE) {
285  for (; count < total; count++) {
286  if (ROXML_WHITE(tmp_name[count])) {
287  break;
288  } else if ((tmp_name[count] == '?') && (tmp_name[count + 1] == '>')) {
289  break;
290  }
291  }
292  } else if (n->type & ROXML_ELM_NODE) {
293  for (; count < total; count++) {
294  if (ROXML_WHITE(tmp_name[count])) {
295  break;
296  } else if ((tmp_name[count] == '/') && (tmp_name[count + 1] == '>')) {
297  break;
298  } else if (tmp_name[count] == '>') {
299  break;
300  }
301  }
302  } else if (n->type & ROXML_ATTR_NODE) {
303  for (; count < total; count++) {
304  if (ROXML_WHITE(tmp_name[count])) {
305  break;
306  } else if (tmp_name[count] == '=') {
307  break;
308  } else if (tmp_name[count] == '>') {
309  break;
310  } else if ((tmp_name[count] == '/') && (tmp_name[count + 1] == '>')) {
311  break;
312  }
313  }
314  } else if (n->type & ROXML_DOCTYPE_NODE) {
315  for (; count < total; count++) {
316  if (ROXML_WHITE(tmp_name[count])) {
317  break;
318  } else if (tmp_name[count] == '>') {
319  break;
320  }
321  }
322  }
323  tmp_name[count] = '\0';
324  }
325 
326  if (buffer == NULL) {
327  buffer = (char *)roxml_malloc(sizeof(char), strlen(tmp_name) - offset + 1, PTR_CHAR);
328  strcpy(buffer, tmp_name + offset);
329  } else {
330  if (strlen(tmp_name) - offset < (unsigned int)size) {
331  size = strlen(tmp_name) - offset;
332  }
333  strncpy(buffer, tmp_name + offset, size);
334  }
335 
336  ROXML_PUT_BASE_BUFFER(tmp_name);
337 
338  return buffer;
339 }
340 
342 {
343  node_t *root = n;
344  if (root == NULL) {
345  return;
346  }
347  while (root->prnt != NULL) {
348  root = root->prnt;
349  }
350 
351  roxml_del_tree(root->chld);
352  roxml_del_tree(root->sibl);
353  if ((root->type & ROXML_FILE) == ROXML_FILE) {
354  fclose(root->src.fil);
355  }
356  roxml_free_node(root);
357 }
358 
360 {
361  node_t *ptr = n;
362  int nb = -1;
363  if (n) {
364  nb = 0;
365  if (ptr->chld) {
366  ptr = ptr->chld;
367  do {
368  if (ptr->type & type) {
369  nb++;
370  }
371  ptr = ptr->sibl;
372  } while (ptr);
373  }
374  if (type & ROXML_ATTR_NODE) {
375  ptr = n->attr;
376  while (ptr) {
377  nb++;
378  ptr = ptr->sibl;
379  }
380  }
381  }
382  return nb;
383 }
384 
385 node_t ROXML_API *roxml_get_nodes(node_t *n, int type, char *name, int nth)
386 {
387  node_t *ptr = NULL;
388 
389  if (n == NULL) {
390  return NULL;
391  }
392 
393  if (name == NULL) {
394  int count = 0;
395  if (n->ns && (type & ROXML_NS_NODE)) {
396  ptr = n->ns;
397  if (nth == 0) {
398  return ptr;
399  }
400  } else if (n->attr && (type & ROXML_ATTR_NODE)) {
401  ptr = n->attr;
402  if (nth == 0) {
403  return ptr;
404  }
405  while ((ptr->sibl) && (nth > count)) {
406  ptr = ptr->sibl;
407  count++;
408  }
409  } else {
410  ptr = n->chld;
411  while (ptr && !((ptr->type & ROXML_NODE_TYPES) & type)) {
412  ptr = ptr->sibl;
413  }
414  }
415  if (nth > count) {
416  ptr = n->chld;
417  while (ptr && !((ptr->type & ROXML_NODE_TYPES) & type)) {
418  ptr = ptr->sibl;
419  }
420  while (ptr && (ptr->sibl) && (nth > count)) {
421  ptr = ptr->sibl;
422  if ((ptr->type & ROXML_NODE_TYPES) & type) {
423  count++;
424  }
425  }
426  }
427  if (nth > count) {
428  return NULL;
429  }
430  } else {
431  if (n->attr && (type & ROXML_ATTR_NODE)) {
432  ptr = n->attr;
433  while (ptr) {
434  int ans = strcmp(roxml_get_name(ptr, NULL, 0), name);
436  if (ans == 0) {
437  return ptr;
438  }
439  ptr = ptr->sibl;
440  }
441  }
442  ptr = n->chld;
443  while (ptr) {
444  if ((ptr->type & ROXML_NODE_TYPES) & type) {
445  int ans = strcmp(roxml_get_name(ptr, NULL, 0), name);
447  if (ans == 0) {
448  return ptr;
449  }
450  }
451  ptr = ptr->sibl;
452  }
453  }
454  return ptr;
455 }
456 
458 {
459  node_t *attr = NULL;
460  node_t *chld = NULL;
461 
462  if (!n) {
463  return NULL;
464  }
465 
466  if (ns) {
467  node_t *common_parent = n;
468  while (common_parent && common_parent != ns->prnt) {
469  common_parent = common_parent->prnt;
470  }
471  if (common_parent != ns->prnt) {
472  return NULL;
473  }
474  }
475 
476  n->ns = ns;
477  chld = n->chld;
478  while (chld) {
479  roxml_set_ns(chld, ns);
480  chld = chld->sibl;
481  }
482 
483  attr = n->attr;
484  while (attr) {
485  if ((attr->type & ROXML_NS_NODE) == 0) {
486  attr->ns = ns;
487  }
488  attr = attr->sibl;
489  }
490 
491  return n;
492 }
493 
495 {
496  return roxml_get_nodes(n, ROXML_NS_NODE, NULL, 0);
497 }
498 
500 {
502 }
503 
505 {
506  return roxml_get_nodes(n, ROXML_PI_NODE, NULL, nth);
507 }
508 
510 {
512 }
513 
515 {
516  return roxml_get_nodes(n, ROXML_CMT_NODE, NULL, nth);
517 }
518 
520 {
522 }
523 
525 {
526  return roxml_get_nodes(n, ROXML_TXT_NODE, NULL, nth);
527 }
528 
530 {
532 }
533 
534 node_t ROXML_API *roxml_get_attr(node_t *n, char *name, int nth)
535 {
536  return roxml_get_nodes(n, ROXML_ATTR_NODE, name, nth);
537 }
538 
540 {
542 }
543 
544 node_t ROXML_API *roxml_get_chld(node_t *n, char *name, int nth)
545 {
546  return roxml_get_nodes(n, ROXML_ELM_NODE, name, nth);
547 }
548 
550 {
551  node_t *prev = NULL;
552  node_t *prev_elm = NULL;
553 
554  if (n && n->prnt) {
555  prev = n->prnt->chld;
556  while (prev && prev != n) {
557  if ((prev->type & ROXML_NODE_TYPES) == ROXML_ELM_NODE) {
558  prev_elm = prev;
559  }
560  prev = prev->sibl;
561  }
562  }
563  return prev_elm;
564 }
565 
567 {
568  if (n) {
569  while (n->sibl && (n->sibl->type & ROXML_NODE_TYPES) != ROXML_ELM_NODE) {
570  n = n->sibl;
571  }
572  return n->sibl;
573  }
574  return NULL;
575 }
576 
578 {
579  if (n) {
580  if (n->prnt == NULL) {
581  return n;
582  } else {
583  return n->prnt;
584  }
585  }
586  return NULL;
587 }
588 
590 {
591  node_t *root = NULL;
592  if (n) {
593  root = n;
594 
595  while (root->prnt)
596  root = root->prnt;
597  }
598  return root;
599 }
600 
602 {
603  if (n) {
604  return (n->type & ROXML_NODE_TYPES);
605  }
606  return 0;
607 }
608 
610 {
611  int idx = 1;
612  char name[256];
613  node_t *prnt;
614  node_t *first;
615 
616  if (n == NULL) {
617  return 0;
618  }
619 
620  roxml_get_name(n, name, 256);
621 
622  prnt = n->prnt;
623  if (!prnt) {
624  return 1;
625  }
626  first = prnt->chld;
627 
628  while ((first) && (first != n)) {
629  char twin[256];
630  roxml_get_name(first, twin, 256);
631  if (strcmp(name, twin) == 0) {
632  idx++;
633  }
634  first = first->sibl;
635  }
636 
637  return idx;
638 }
639 
641 {
642  FILE *file = NULL;
643  node_t *current_node = NULL;
644  if (fd < 0) {
645  return NULL;
646  }
647  file = fdopen(fd, "r");
648  if (file == NULL) {
649  return NULL;
650  }
651  current_node = roxml_create_node(0, file, ROXML_ELM_NODE | ROXML_FILE);
652  current_node = roxml_append_node(NULL, current_node);
653  return roxml_load(current_node, file, NULL);
654 }
655 
657 {
658  node_t *current_node = NULL;
659  FILE *file = fopen(filename, "rb");
660  if (file == NULL) {
661  return NULL;
662  }
663  current_node = roxml_create_node(0, file, ROXML_ELM_NODE | ROXML_FILE);
664  current_node = roxml_append_node(NULL, current_node);
665  return roxml_load(current_node, file, NULL);
666 }
667 
669 {
670  node_t *current_node = NULL;
671  if (buffer == NULL) {
672  return NULL;
673  }
674  current_node = roxml_create_node(0, buffer, ROXML_ELM_NODE | ROXML_BUFF);
675  current_node = roxml_append_node(NULL, current_node);
676  return roxml_load(current_node, NULL, buffer);
677 }
678 
679 node_t ROXML_API **roxml_xpath(node_t *n, char *path, int *nb_ans)
680 {
681  int count = 0;
682  node_t **node_set = NULL;
683 
684 #if(CONFIG_XML_XPATH_ENGINE==1)
685 
686  int index = 0;
687  xpath_node_t *xpath = NULL;
688  node_t *root = n;
689  char *full_path_to_find;
690 
691  if (n == NULL) {
692  if (nb_ans) {
693  *nb_ans = 0;
694  }
695  return NULL;
696  }
697 
698  root = roxml_get_root(n);
699 
700  full_path_to_find = strdup(path);
701 
702  index = roxml_parse_xpath(full_path_to_find, &xpath, 0);
703 
704  if (index >= 0) {
705  node_set = roxml_exec_xpath(root, n, xpath, index, &count);
706 
707  roxml_free_xpath(xpath, index);
708  free(full_path_to_find);
709 
710  if (count == 0) {
711  roxml_release(node_set);
712  node_set = NULL;
713  }
714  }
715 #endif /* CONFIG_XML_XPATH_ENGINE*/
716  if (nb_ans) {
717  *nb_ans = count;
718  }
719 
720  return node_set;
721 }
722 
724 {
725 #if(CONFIG_XML_READ_WRITE==1)
726  if (n == NULL)
727  return;
728 
729  if ((n->type & ROXML_ELM_NODE) ||
730  (n->type & ROXML_DOCTYPE_NODE) || (n->type & ROXML_PI_NODE) || (n->type & ROXML_CMT_NODE)) {
732  } else if (n->type & ROXML_ATTR_NODE) {
734  } else if (n->type & ROXML_TXT_NODE) {
736  }
737  roxml_free_node(n);
738 #endif /* CONFIG_XML_READ_WRITE */
739 }
740 
741 int ROXML_API roxml_commit_changes(node_t *n, char *dest, char **buffer, int human)
742 {
743  int len = 0;
744 
745 #if(CONFIG_XML_COMMIT_XML_TREE==1)
746 
747  int size = 0;
748 
749  if (n) {
750  FILE *fout = NULL;
751 
752  if (dest) {
753  fout = fopen(dest, "w");
754  } else if (buffer) {
755  *buffer = (char *)malloc(ROXML_BASE_LEN);
756  memset(*buffer, 0, ROXML_BASE_LEN);
757  }
758 
759  if (fout || buffer) {
760  len = ROXML_BASE_LEN;
761 
762  if ((n->prnt == NULL) || (n->prnt && n->prnt->prnt == NULL)) {
763  if (n->prnt) {
764  n = n->prnt->chld;
765  } else {
766  n = n->chld;
767  }
768  while (n) {
769  roxml_write_node(n, fout, buffer, human, 0, &size, &len);
770  n = n->sibl;
771  }
772  } else {
773  roxml_write_node(n, fout, buffer, human, 0, &size, &len);
774  }
775  if (buffer) {
776  char *ptr = NULL;
777  len -= ROXML_BASE_LEN;
778  ptr = *buffer + len;
779  len += strlen(ptr);
780  } else if (fout) {
781  len = ftell(fout);
782  fclose(fout);
783  }
784  }
785  }
786 #endif /* CONFIG_XML_COMMIT_XML_TREE */
787 
788  return len;
789 }
790 
791 node_t ROXML_API *roxml_add_node(node_t *parent, int position, int type, char *name, char *value)
792 {
793  node_t *new_node = NULL;
794 
795 #if(CONFIG_XML_READ_WRITE==1)
796 
797  int name_l = 0;
798  int end_node = 0;
799  int content_l = 0;
800  int content_pos = 0;
801  int end_content = 0;
802  char *buffer = NULL;
803 
804  if (parent) {
805  if (parent->type & ROXML_ATTR_NODE) {
806  if (((type & ROXML_TXT_NODE) == 0) || (parent->chld)) {
807  return NULL;
808  }
809  } else if ((parent->type & ROXML_ELM_NODE) == 0) {
810  if (parent->prnt && (parent->prnt->type & ROXML_ELM_NODE)) {
811  parent = parent->prnt;
812  } else {
813  return NULL;
814  }
815  }
816  }
817 
818  if (value) {
819  content_l = strlen(value);
820  }
821  if (name) {
822  name_l = strlen(name);
823  }
824 
825  if (type & ROXML_ATTR_NODE) {
826  int xmlns_l = 0;
827  if (!name || !value) {
828  return NULL;
829  }
830  if (type & ROXML_NS_NODE) {
831  if (name_l > 0) {
832  xmlns_l = 6;
833  } else {
834  xmlns_l = 5;
835  }
836  buffer = (char *)malloc(sizeof(char) * (name_l + content_l + xmlns_l + 4));
837  sprintf(buffer, "xmlns%s%s=\"%s\"", name_l ? ":" : "", name, value);
838  } else {
839  buffer = (char *)malloc(sizeof(char) * (name_l + content_l + 4));
840  sprintf(buffer, "%s=\"%s\"", name, value);
841  }
842  content_pos = name_l + 2 + xmlns_l;
843  end_node = name_l + 1 + xmlns_l;
844  end_content = name_l + content_l + 2 + xmlns_l;
845  } else if (type & ROXML_CMT_NODE) {
846  if (!value) {
847  return NULL;
848  }
849  buffer = (char *)malloc(sizeof(char) * (content_l + 8));
850  sprintf(buffer, "<!--%s-->", value);
851  content_pos = 0;
852  end_node = content_l + 4;
853  end_content = content_l + 4;
854  } else if (type & ROXML_PI_NODE) {
855  if (!name) {
856  return NULL;
857  }
858  if (content_l) {
859  buffer = (char *)malloc(sizeof(char) * (name_l + content_l + 8));
860  sprintf(buffer, "<?%s %s?>", name, value);
861  end_node = name_l + content_l + 3;
862  end_content = name_l + content_l + 5;
863  } else {
864  buffer = (char *)malloc(sizeof(char) * (name_l + 7));
865  sprintf(buffer, "<?%s?>", name);
866  end_node = name_l + 2;
867  end_content = name_l + 4;
868  }
869  content_pos = 0;
870  } else if (type & ROXML_TXT_NODE) {
871  if (!value) {
872  return NULL;
873  }
874  buffer = (char *)malloc(sizeof(char) * (content_l + 1));
875  sprintf(buffer, "%s", value);
876  content_pos = 0;
877  end_node = content_l + 1;
878  end_content = content_l + 1;
879  } else if (type & ROXML_ELM_NODE) {
880  if (!name) {
881  return NULL;
882  }
883  if (value) {
884  buffer = (char *)malloc(sizeof(char) * (name_l * 2 + content_l + 6));
885  sprintf(buffer, "<%s>%s</%s>", name, value, name);
886  content_pos = name_l + 2;
887  end_node = name_l + content_l + 2;
888  end_content = end_node;
889  } else {
890  buffer = (char *)malloc(sizeof(char) * (name_l + 5));
891  sprintf(buffer, "<%s />", name);
892  }
893  } else {
894  return NULL;
895  }
896 
897  new_node = roxml_create_node(0, buffer, type | ROXML_PENDING | ROXML_BUFF);
898  new_node->end = end_node;
899 
900  if (type & ROXML_NS_NODE) {
901  roxml_ns_t *ns = calloc(1, sizeof(roxml_ns_t) + name_l + 1);
902  ns->id = ROXML_NS_ID;
903  ns->alias = (char *)ns + sizeof(roxml_ns_t);
904  if (name)
905  strcpy(ns->alias, name);
906  new_node->priv = ns;
907  }
908 
909  if (((type & ROXML_ELM_NODE) && content_l) || (type & ROXML_ATTR_NODE)) {
910  node_t *new_txt = roxml_create_node(content_pos, buffer, ROXML_TXT_NODE | ROXML_PENDING | ROXML_BUFF);
911  roxml_append_node(new_node, new_txt);
912  new_txt->end = end_content;
913  }
914 
915  if (parent == NULL) {
916  xpath_tok_table_t *table = (xpath_tok_table_t *) calloc(1, sizeof(xpath_tok_table_t));
917  parent = roxml_create_node(0, NULL, ROXML_ELM_NODE | ROXML_PENDING | ROXML_BUFF);
918  parent->end = 1;
919 
920  table->id = ROXML_REQTABLE_ID;
921  table->ids[ROXML_REQTABLE_ID] = 1;
922 
923  pthread_mutex_init(&table->mut, NULL);
924  parent->priv = (void *)table;
925 
926  roxml_append_node(parent, new_node);
927  } else {
928  roxml_parent_node(parent, new_node, position);
929  }
930 #endif /* CONFIG_XML_READ_WRITE */
931 
932  return new_node;
933 }
int ROXML_INT roxml_read(int pos, int size, char *buffer, node_t *node)
read xml doc function
void ROXML_INT roxml_free_xpath(xpath_node_t *xpath, int nb)
xpath free function
int ROXML_API roxml_get_txt_nb(node_t *n)
text node number getter function
Definition: roxml.c:519
#define ROXML_NS_NODE
Definition: roxml.h:110
void ROXML_API roxml_close(node_t *n)
unload function
Definition: roxml.c:341
node_t structure
Definition: roxml-types.h:152
int ROXML_API roxml_get_nodes_nb(node_t *n, int type)
number of nodes getter function
Definition: roxml.c:359
node_t ROXML_API * roxml_get_txt(node_t *n, int nth)
text node getter function
Definition: roxml.c:524
struct node * attr
Definition: roxml-types.h:164
node_t ROXML_API * roxml_get_ns(node_t *n)
namespace getter function
Definition: roxml.c:494
node_t ROXML_API * roxml_get_parent(node_t *n)
parent getter function
Definition: roxml.c:577
#define PTR_CHAR
#define ROXML_ATTR_NODE
Definition: roxml.h:59
node_t ROXML_API * roxml_get_attr(node_t *n, char *name, int nth)
attribute getter function
Definition: roxml.c:534
node_t ROXML_API * roxml_get_nodes(node_t *n, int type, char *name, int nth)
nodes getter function
Definition: roxml.c:385
node_t ROXML_INT ** roxml_exec_xpath(node_t *root, node_t *n, xpath_node_t *xpath, int index, int *count)
real xpath execution
#define RELEASE_LAST
Definition: roxml.h:196
roxml_pos_t end
Definition: roxml-types.h:160
int ROXML_API roxml_get_chld_nb(node_t *n)
chlds number getter function
Definition: roxml.c:539
int ROXML_API roxml_get_attr_nb(node_t *n)
number of attribute getter function
Definition: roxml.c:529
unsigned char id
Definition: roxml-types.h:139
node_t ROXML_API * roxml_load_doc(char *filename)
load function for files
Definition: roxml.c:656
#define ROXML_PENDING
unsigned char id
Definition: roxml-types.h:113
#define ROXML_PI_NODE
Definition: roxml.h:102
void ROXML_INT roxml_del_txt_node(node_t *n)
text node deletion function
struct node * chld
Definition: roxml-types.h:162
node_t ROXML_API * roxml_get_chld(node_t *n, char *name, int nth)
chld getter function
Definition: roxml.c:544
memory cell structure
Definition: roxml-types.h:59
int ROXML_INT roxml_parse_xpath(char *path, xpath_node_t **xpath, int context)
xpath parsing function
node_t ROXML_API * roxml_get_pi(node_t *n, int nth)
process-instruction getter function
Definition: roxml.c:504
char * alias
Definition: roxml-types.h:141
node_t ROXML_API * roxml_get_prev_sibling(node_t *n)
prev sibling getter function
Definition: roxml.c:549
node_t ROXML_INT * roxml_load(node_t *current_node, FILE *file, char *buffer)
generic load function
#define ROXML_API
Definition: roxml.h:39
#define ROXML_DOCTYPE_NODE
Definition: roxml.h:134
void ROXML_API roxml_del_node(node_t *n)
node deletion function
Definition: roxml.c:723
struct node * sibl
Definition: roxml-types.h:161
node_t ROXML_API * roxml_add_node(node_t *parent, int position, int type, char *name, char *value)
add a node to the tree
Definition: roxml.c:791
pthread_mutex_t mut
Definition: roxml-types.h:115
#define ROXML_BASE_LEN
#define ROXML_BUFF
#define ROXML_CMT_NODE
Definition: roxml.h:94
void ROXML_INT * roxml_malloc(int size, int num, int type)
alloc memory function
int ROXML_API roxml_get_type(node_t *n)
node type function
Definition: roxml.c:601
void * src
Definition: roxml-types.h:157
int ROXML_API roxml_commit_changes(node_t *n, char *dest, char **buffer, int human)
sync function
Definition: roxml.c:741
node_t ROXML_API * roxml_load_fd(int fd)
load function for file descriptors
Definition: roxml.c:640
xpath node structure
Definition: roxml-types.h:95
int ROXML_API roxml_get_node_position(node_t *n)
node get position function
Definition: roxml.c:609
node_t ROXML_INT * roxml_create_node(int pos, void *src, int type)
internal function
char ROXML_API * roxml_get_content(node_t *n, char *buffer, int bufsize, int *size)
content getter function
Definition: roxml.c:123
node_t ROXML_API * roxml_set_ns(node_t *n, node_t *ns)
namespace setter function
Definition: roxml.c:457
roxml_pos_t pos
Definition: roxml-types.h:159
void ROXML_API roxml_release(void *data)
memory cleanning function
Definition: roxml.c:28
node_t ROXML_API * roxml_load_buf(char *buffer)
load function for buffers
Definition: roxml.c:668
struct node * ns
Definition: roxml-types.h:166
namespace structure
Definition: roxml-types.h:138
memory_cell_t head_cell
head of memory manager
node_t ROXML_INT * roxml_append_node(node_t *parent, node_t *n)
node append function
#define ROXML_FILE
void ROXML_INT roxml_del_tree(node_t *n)
internal function
#define PTR_NONE
node_t ROXML_API * roxml_get_root(node_t *n)
root getter function
Definition: roxml.c:589
struct memory_cell * prev
Definition: roxml-types.h:65
void ROXML_INT roxml_del_arg_node(node_t *n)
attribute node deletion function
void ROXML_INT roxml_free_node(node_t *n)
internal function
#define ROXML_ELM_NODE
Definition: roxml.h:78
#define ROXML_WHITE(n)
void * priv
Definition: roxml-types.h:167
int ROXML_API roxml_get_cmt_nb(node_t *n)
comments number getter function
Definition: roxml.c:509
struct node * prnt
Definition: roxml-types.h:163
char ROXML_API * roxml_get_name(node_t *n, char *buffer, int size)
name getter function
Definition: roxml.c:228
node_t ROXML_API * roxml_get_cmt(node_t *n, int nth)
comment getter function
Definition: roxml.c:514
#define ROXML_NODE_TYPES
Definition: roxml.h:158
unsigned char ids[256]
Definition: roxml-types.h:114
struct memory_cell * next
Definition: roxml-types.h:64
#define ROXML_TXT_NODE
Definition: roxml.h:86
unsigned short type
Definition: roxml-types.h:153
node_t ROXML_INT * roxml_parent_node(node_t *parent, node_t *n, int position)
node relocate function
#define PTR_IS_STAR(a)
node_t ROXML_API ** roxml_xpath(node_t *n, char *path, int *nb_ans)
exec path function
Definition: roxml.c:679
#define RELEASE_ALL
Definition: roxml.h:166
int ROXML_API roxml_get_pi_nb(node_t *n)
process-instruction number getter function
Definition: roxml.c:499
void ROXML_INT roxml_write_node(node_t *n, FILE *f, char **buf, int human, int lvl, int *offset, int *len)
tree write function
void ROXML_INT roxml_del_std_node(node_t *n)
node deletion function
xpath token structure
Definition: roxml-types.h:112
internal header for libroxml.so
node_t ROXML_API * roxml_get_next_sibling(node_t *n)
next sibling getter function
Definition: roxml.c:566