diff --git a/src/sxmlc.c b/src/sxmlc.c index 35adddd..e4b4775 100644 --- a/src/sxmlc.c +++ b/src/sxmlc.c @@ -57,12 +57,12 @@ FILE* sx_fopen(const SXML_CHAR* filename, const SXML_CHAR* mode) { FILE* ret = NULL; - int is_unicode = false; + int is_unicode = FALSE; const char* p; - + for (p = filename; p && *p; p++) { if (sx_isunicode(*p)) { - is_unicode = true; + is_unicode = TRUE; break; } } @@ -136,7 +136,7 @@ int XML_register_user_tag(TagType tag_type, SXML_CHAR* start, SXML_CHAR* end) if (start == NULL || end == NULL || *start != C2SX('<')) return -1; - le = sx_strlen(end); + le = (int)sx_strlen(end); if (end[le-1] != C2SX('>')) return -1; @@ -149,7 +149,7 @@ int XML_register_user_tag(TagType tag_type, SXML_CHAR* start, SXML_CHAR* end) p[i].tag_type = tag_type; p[i].start = start; p[i].end = end; - p[i].len_start = sx_strlen(start); + p[i].len_start = (int)sx_strlen(start); p[i].len_end = le; _user_tags.tags = p; _user_tags.n_tags = n; @@ -167,14 +167,14 @@ int XML_unregister_user_tag(int i_tag) if (_user_tags.n_tags == 1) pt = NULL; else { - pt = __malloc((_user_tags.n_tags - 1) * sizeof(_TAG)); + pt = __malloc((size_t)(_user_tags.n_tags - 1) * sizeof(_TAG)); if (pt == NULL) return -1; } - + if (pt != NULL) { memcpy(pt, _user_tags.tags, i_tag * sizeof(_TAG)); - memcpy(&pt[i_tag], &_user_tags.tags[i_tag + 1], (_user_tags.n_tags - i_tag - 1) * sizeof(_TAG)); + memcpy(&pt[i_tag], &_user_tags.tags[i_tag + 1], (size_t)(_user_tags.n_tags - i_tag - 1) * sizeof(_TAG)); } if (_user_tags.tags != NULL) __free(_user_tags.tags); @@ -209,58 +209,58 @@ int XML_get_registered_user_tag(TagType tag_type) */ static int _add_node(XMLNode*** children_array, int* len_array, XMLNode* node) { - XMLNode** pt = __realloc(*children_array, (*len_array+1) * sizeof(XMLNode*)); - + XMLNode** pt = __realloc(*children_array, (size_t)(*len_array+1) * sizeof(XMLNode*)); + if (pt == NULL) return -1; - + pt[*len_array] = node; *children_array = pt; - + return (*len_array)++; } int XMLNode_init(XMLNode* node) { if (node == NULL) - return false; - + return FALSE; + if (node->init_value == XML_INIT_DONE) - return true; /*(void)XMLNode_free(node);*/ + return TRUE; /*(void)XMLNode_free(node);*/ node->tag = NULL; node->text = NULL; - + node->attributes = NULL; node->n_attributes = 0; - + node->father = NULL; node->children = NULL; node->n_children = 0; - + node->tag_type = TAG_NONE; - node->active = true; + node->active = TRUE; node->init_value = XML_INIT_DONE; - return true; + return TRUE; } XMLNode* XMLNode_allocN(int n) { int i; XMLNode* p; - + if (n <= 0) return NULL; - + p = __calloc(n, sizeof(XMLNode)); if (p == NULL) return NULL; for (i = 0; i < n; i++) (void)XMLNode_init(&p[i]); - + return p; } @@ -269,14 +269,14 @@ XMLNode* XMLNode_new(const TagType tag_type, const SXML_CHAR* tag, const SXML_CH XMLNode* node = XMLNode_alloc(); if (node == NULL) return NULL; - + if (!XMLNode_set_tag(node, tag) || (text != NULL && !XMLNode_set_text(node, text))) { __free(node); return NULL; } - + node->tag_type = tag_type; - + return node; } @@ -303,8 +303,8 @@ XMLNode* XMLNode_dup(const XMLNode* node, int copy_children) int XMLNode_free(XMLNode* node) { - CHECK_NODE(node, false); - + CHECK_NODE(node, FALSE); + if (node->tag != NULL) { __free(node->tag); node->tag = NULL; @@ -313,34 +313,42 @@ int XMLNode_free(XMLNode* node) XMLNode_remove_text(node); XMLNode_remove_all_attributes(node); XMLNode_remove_children(node); - + node->tag_type = TAG_NONE; - return true; + return TRUE; } int XMLNode_copy(XMLNode* dst, const XMLNode* src, int copy_children) { int i; - + if (dst == NULL || (src != NULL && src->init_value != XML_INIT_DONE)) - return false; - + return FALSE; + (void)XMLNode_free(dst); /* 'dst' is freed first */ - + /* NULL 'src' resets 'dst' */ if (src == NULL) - return true; - + return TRUE; + /* Tag */ if (src->tag != NULL) { + #ifdef _MSC_VER + dst->tag = _strdup(src->tag); + #else dst->tag = sx_strdup(src->tag); + #endif if (dst->tag == NULL) goto copy_err; } /* Text */ if (dst->text != NULL) { + #ifdef _MSC_VER + dst->text = _strdup(src->text); + #else dst->text = sx_strdup(src->text); + #endif if (dst->text == NULL) goto copy_err; } @@ -350,8 +358,13 @@ int XMLNode_copy(XMLNode* dst, const XMLNode* src, int copy_children) if (dst->attributes== NULL) goto copy_err; dst->n_attributes = src->n_attributes; for (i = 0; i < src->n_attributes; i++) { + #ifdef _MSC_VER + dst->attributes[i].name = _strdup(src->attributes[i].name); + dst->attributes[i].value = _strdup(src->attributes[i].value); + #else dst->attributes[i].name = sx_strdup(src->attributes[i].name); dst->attributes[i].value = sx_strdup(src->attributes[i].value); + #endif if (dst->attributes[i].name == NULL || dst->attributes[i].value == NULL) goto copy_err; dst->attributes[i].active = src->attributes[i].active; } @@ -361,63 +374,67 @@ int XMLNode_copy(XMLNode* dst, const XMLNode* src, int copy_children) dst->father = src->father; dst->user = src->user; dst->active = src->active; - + /* Copy children if required (and there are any) */ if (copy_children && src->n_children > 0) { dst->children = __calloc(src->n_children, sizeof(XMLNode*)); if (dst->children == NULL) goto copy_err; dst->n_children = src->n_children; for (i = 0; i < src->n_children; i++) { - if (!XMLNode_copy(dst->children[i], src->children[i], true)) goto copy_err; + if (!XMLNode_copy(dst->children[i], src->children[i], TRUE)) goto copy_err; } } - - return true; - + + return TRUE; + copy_err: (void)XMLNode_free(dst); - - return false; + + return FALSE; } int XMLNode_set_active(XMLNode* node, int active) { - CHECK_NODE(node, false); + CHECK_NODE(node, FALSE); node->active = active; - return true; + return TRUE; } int XMLNode_set_tag(XMLNode* node, const SXML_CHAR* tag) { SXML_CHAR* newtag; if (node == NULL || tag == NULL || node->init_value != XML_INIT_DONE) - return false; - + return FALSE; + +#ifdef _MSC_VER + newtag = _strdup(tag); +#else newtag = sx_strdup(tag); +#endif if (newtag == NULL) - return false; + return FALSE; if (node->tag != NULL) __free(node->tag); node->tag = newtag; - return true; + return TRUE; } int XMLNode_set_type(XMLNode* node, const TagType tag_type) { - CHECK_NODE(node, false); + CHECK_NODE(node, FALSE); switch (tag_type) { case TAG_ERROR: case TAG_END: case TAG_PARTIAL: case TAG_NONE: - return false; + return FALSE; default: node->tag_type = tag_type; - return true; + return TRUE; } } @@ -425,22 +442,31 @@ int XMLNode_set_attribute(XMLNode* node, const SXML_CHAR* attr_name, const SXML_ { XMLAttribute* pt; int i; - + if (node == NULL || attr_name == NULL || attr_name[0] == NULC || node->init_value != XML_INIT_DONE) return -1; - + i = XMLNode_search_attribute(node, attr_name, 0); if (i >= 0) { /* Attribute found: update it */ SXML_CHAR* value = NULL; + #ifdef _MSC_VER + if (attr_value != NULL && (value = _strdup(attr_value)) == NULL) + #else if (attr_value != NULL && (value = sx_strdup(attr_value)) == NULL) + #endif return -1; pt = node->attributes; if (pt[i].value != NULL) __free(pt[i].value); pt[i].value = value; } else { /* Attribute not found: add it */ + #ifdef _MSC_VER + SXML_CHAR* name = _strdup(attr_name); + SXML_CHAR* value = (attr_value == NULL ? NULL : _strdup(attr_value)); + #else SXML_CHAR* name = sx_strdup(attr_name); - SXML_CHAR* value = (attr_value == NULL ? NULL : sx_strdup(attr_value)); + SXML_CHAR* value = (attr_value == NULL ? NULL : sx_strdup(attr_value)); + #endif if (name == NULL || (value == NULL && attr_value != NULL)) { if (value != NULL) __free(value); @@ -449,7 +475,7 @@ int XMLNode_set_attribute(XMLNode* node, const SXML_CHAR* attr_name, const SXML_ return -1; } i = node->n_attributes; - pt = __realloc(node->attributes, (i+1) * sizeof(XMLAttribute)); + pt = __realloc(node->attributes, (size_t)(i+1) * sizeof(XMLAttribute)); if (pt == NULL) { if (value != NULL) __free(value); @@ -459,7 +485,7 @@ int XMLNode_set_attribute(XMLNode* node, const SXML_CHAR* attr_name, const SXML_ pt[i].name = name; pt[i].value = value; - pt[i].active = true; + pt[i].active = TRUE; node->attributes = pt; node->n_attributes = i + 1; } @@ -471,27 +497,35 @@ int XMLNode_get_attribute_with_default(XMLNode* node, const SXML_CHAR* attr_name { XMLAttribute* pt; int i; - + if (node == NULL || attr_name == NULL || attr_name[0] == NULC || attr_value == NULL || node->init_value != XML_INIT_DONE) - return false; - + return FALSE; + i = XMLNode_search_attribute(node, attr_name, 0); if (i >= 0) { pt = node->attributes; if (pt[i].value != NULL) { + #ifdef _MSC_VER + *attr_value = _strdup(pt[i].value); + #else *attr_value = sx_strdup(pt[i].value); + #endif if (*attr_value == NULL) - return false; + return FALSE; } else - *attr_value = NULL; /* NULL but returns 'true' as 'NULL' is the actual attribute value */ + *attr_value = NULL; /* NULL but returns 'TRUE' as 'NULL' is the actual attribute value */ } else if (default_attr_value != NULL) { + #ifdef _MSC_VER + *attr_value = _strdup(default_attr_value); + #else *attr_value = sx_strdup(default_attr_value); + #endif if (*attr_value == NULL) - return false; + return FALSE; } else *attr_value = NULL; - return true; + return TRUE; } int XMLNode_get_attribute_count(const XMLNode* node) @@ -509,14 +543,14 @@ int XMLNode_get_attribute_count(const XMLNode* node) int XMLNode_search_attribute(const XMLNode* node, const SXML_CHAR* attr_name, int i_search) { int i; - + if (node == NULL || attr_name == NULL || attr_name[0] == NULC || i_search < 0 || i_search >= node->n_attributes) return -1; - + for (i = i_search; i < node->n_attributes; i++) if (node->attributes[i].active && !sx_strcmp(node->attributes[i].name, attr_name)) return i; - + return -1; } @@ -525,12 +559,12 @@ int XMLNode_remove_attribute(XMLNode* node, int i_attr) XMLAttribute* pt; if (node == NULL || node->init_value != XML_INIT_DONE || i_attr < 0 || i_attr >= node->n_attributes) return -1; - + /* Before modifying first see if we run out of memory */ if (node->n_attributes == 1) pt = NULL; else { - pt = __malloc((node->n_attributes - 1) * sizeof(XMLAttribute)); + pt = __malloc((size_t)(node->n_attributes - 1) * sizeof(XMLAttribute)); if (pt == NULL) return -1; } @@ -538,16 +572,16 @@ int XMLNode_remove_attribute(XMLNode* node, int i_attr) /* Can't fail anymore, free item */ if (node->attributes[i_attr].name != NULL) __free(node->attributes[i_attr].name); if (node->attributes[i_attr].value != NULL) __free(node->attributes[i_attr].value); - + if (pt != NULL) { memcpy(pt, node->attributes, i_attr * sizeof(XMLAttribute)); - memcpy(&pt[i_attr], &node->attributes[i_attr + 1], (node->n_attributes - i_attr - 1) * sizeof(XMLAttribute)); + memcpy(&pt[i_attr], &node->attributes[i_attr + 1], (size_t)(node->n_attributes - i_attr - 1) * sizeof(XMLAttribute)); } if (node->attributes != NULL) __free(node->attributes); node->attributes = pt; node->n_attributes--; - + return node->n_attributes; } @@ -555,7 +589,7 @@ int XMLNode_remove_all_attributes(XMLNode* node) { int i; - CHECK_NODE(node, false); + CHECK_NODE(node, FALSE); if (node->attributes != NULL) { for (i = 0; i < node->n_attributes; i++) { @@ -569,13 +603,13 @@ int XMLNode_remove_all_attributes(XMLNode* node) } node->n_attributes = 0; - return true; + return TRUE; } int XMLNode_set_text(XMLNode* node, const SXML_CHAR* text) { SXML_CHAR* p; - CHECK_NODE(node, false); + CHECK_NODE(node, FALSE); if (text == NULL) { /* We want to remove it => free node text */ if (node->text != NULL) { @@ -583,30 +617,34 @@ int XMLNode_set_text(XMLNode* node, const SXML_CHAR* text) node->text = NULL; } - return true; + return TRUE; } +#ifdef _MSC_VER + p = _strdup(text); +#else p = sx_strdup(text); +#endif if (p == NULL) - return false; + return FALSE; if (node->text != NULL) __free(node->text); node->text = p; - return true; + return TRUE; } int XMLNode_add_child(XMLNode* node, XMLNode* child) { if (node == NULL || child == NULL || node->init_value != XML_INIT_DONE || child->init_value != XML_INIT_DONE) - return false; - + return FALSE; + if (_add_node(&node->children, &node->n_children, child) >= 0) { node->tag_type = TAG_FATHER; child->father = node; - return true; + return TRUE; } else - return false; + return FALSE; } int XMLNode_insert_child(XMLNode* node, XMLNode* child, int index) @@ -632,21 +670,21 @@ int XMLNode_insert_child(XMLNode* node, XMLNode* child, int index) for (j = node->n_children - 1; j >= i; j--) node->children[j] = node->children[j-1]; node->children[i] = child; /* Set it */ - return true; + return TRUE; } else - return false; + return FALSE; } - return false; /* Oops! */ + return FALSE; /* Oops! */ } int XMLNode_move_child(XMLNode* node, int from, int to) { XMLNode* nfrom; - CHECK_NODE(node, false); + CHECK_NODE(node, FALSE); if (from < 0 || from >= node->n_children) - return false; + return FALSE; if (to < 0) /* Before first => first */ to = 0; if (to >= node->n_children) /* After last => last */ @@ -664,7 +702,7 @@ int XMLNode_move_child(XMLNode* node, int from, int to) } node->children[to] = nfrom; - return true; + return TRUE; } @@ -677,7 +715,7 @@ int XMLNode_get_children_count(const XMLNode* node) for (i = n = 0; i < node->n_children; i++) if (node->children[i]->active) n++; - + return n; } @@ -704,17 +742,17 @@ int XMLNode_get_index(const XMLNode* node) XMLNode* XMLNode_get_child(const XMLNode* node, int i_child) { int i; - + if (node == NULL || node->init_value != XML_INIT_DONE || i_child < 0 || i_child >= node->n_children) return NULL; - + for (i = 0; i < node->n_children; i++) { if (!node->children[i]->active) i_child++; else if (i == i_child) return node->children[i]; } - + return NULL; } @@ -725,7 +763,7 @@ int XMLNode_remove_child(XMLNode* node, int i_child, int free_child) if (node == NULL || node->init_value != XML_INIT_DONE || i_child < 0 || i_child >= node->n_children) return -1; - + /* Lookup 'i_child'th active child */ for (i = 0; i < node->n_children; i++) { if (!node->children[i]->active) @@ -740,7 +778,7 @@ int XMLNode_remove_child(XMLNode* node, int i_child, int free_child) if (node->n_children == 1) { pt = NULL; } else { - pt = __malloc((node->n_children - 1) * sizeof(XMLNode*)); + pt = __malloc((size_t)(node->n_children - 1) * sizeof(XMLNode*)); if (pt == NULL) return -1; } @@ -749,10 +787,10 @@ int XMLNode_remove_child(XMLNode* node, int i_child, int free_child) (void)XMLNode_free(node->children[i_child]); if (free_child) __free(node->children[i_child]); - + if (pt != NULL) { memcpy(pt, node->children, i_child * sizeof(XMLNode*)); - memcpy(&pt[i_child], &node->children[i_child + 1], (node->n_children - i_child - 1) * sizeof(XMLNode*)); + memcpy(&pt[i_child], &node->children[i_child + 1], (size_t)(node->n_children - i_child - 1) * sizeof(XMLNode*)); } if (node->children != NULL) __free(node->children); @@ -760,7 +798,7 @@ int XMLNode_remove_child(XMLNode* node, int i_child, int free_child) node->n_children--; if (node->n_children == 0) node->tag_type = TAG_SELF; - + return node->n_children; } @@ -768,7 +806,7 @@ int XMLNode_remove_children(XMLNode* node) { int i; - CHECK_NODE(node, false); + CHECK_NODE(node, FALSE); if (node->children != NULL) { for (i = 0; i < node->n_children; i++) @@ -780,8 +818,8 @@ int XMLNode_remove_children(XMLNode* node) node->children = NULL; } node->n_children = 0; - - return true; + + return TRUE; } int XMLNode_equal(const XMLNode* node1, const XMLNode* node2) @@ -789,13 +827,13 @@ int XMLNode_equal(const XMLNode* node1, const XMLNode* node2) int i, j; if (node1 == node2) - return true; + return TRUE; if (node1 == NULL || node2 == NULL || node1->init_value != XML_INIT_DONE || node2->init_value != XML_INIT_DONE) - return false; + return FALSE; if (sx_strcmp(node1->tag, node2->tag)) - return false; + return FALSE; /* Test all attributes from 'node1' */ for (i = 0; i < node1->n_attributes; i++) { @@ -803,9 +841,9 @@ int XMLNode_equal(const XMLNode* node1, const XMLNode* node2) continue; j = XMLNode_search_attribute(node2, node1->attributes[i].name, 0); if (j < 0) - return false; + return FALSE; if (sx_strcmp(node1->attributes[i].value, node2->attributes[j].value)) - return false; + return FALSE; } /* Test other attributes from 'node2' that might not be in 'node1' */ @@ -814,12 +852,12 @@ int XMLNode_equal(const XMLNode* node1, const XMLNode* node2) continue; j = XMLNode_search_attribute(node1, node2->attributes[i].name, 0); if (j < 0) - return false; + return FALSE; if (sx_strcmp(node2->attributes[i].name, node1->attributes[j].name)) - return false; + return FALSE; } - return true; + return TRUE; } XMLNode* XMLNode_next_sibling(const XMLNode* node) @@ -852,12 +890,12 @@ static XMLNode* _XMLNode_next(const XMLNode* node, int in_children) return node2; /* Check next uncle */ - return _XMLNode_next(node->father, false); + return _XMLNode_next(node->father, FALSE); } XMLNode* XMLNode_next(const XMLNode* node) { - return _XMLNode_next(node, true); + return _XMLNode_next(node, TRUE); } /* --- XMLDoc methods --- */ @@ -865,7 +903,7 @@ XMLNode* XMLNode_next(const XMLNode* node) int XMLDoc_init(XMLDoc* doc) { if (doc == NULL) - return false; + return FALSE; doc->filename[0] = NULC; memset(&doc->bom, 0, sizeof(doc->bom)); @@ -875,15 +913,15 @@ int XMLDoc_init(XMLDoc* doc) doc->i_root = -1; doc->init_value = XML_INIT_DONE; - return true; + return TRUE; } int XMLDoc_free(XMLDoc* doc) { int i; - + if (doc == NULL || doc->init_value != XML_INIT_DONE) - return false; + return FALSE; for (i = 0; i < doc->n_nodes; i++) { (void)XMLNode_free(doc->nodes[i]); @@ -894,24 +932,24 @@ int XMLDoc_free(XMLDoc* doc) doc->n_nodes = 0; doc->i_root = -1; - return true; + return TRUE; } int XMLDoc_set_root(XMLDoc* doc, int i_root) { if (doc == NULL || doc->init_value != XML_INIT_DONE || i_root < 0 || i_root >= doc->n_nodes) - return false; - + return FALSE; + doc->i_root = i_root; - - return true; + + return TRUE; } int XMLDoc_add_node(XMLDoc* doc, XMLNode* node) { if (doc == NULL || node == NULL || doc->init_value != XML_INIT_DONE) return -1; - + if (_add_node(&doc->nodes, &doc->n_nodes, node) < 0) return -1; @@ -925,24 +963,24 @@ int XMLDoc_remove_node(XMLDoc* doc, int i_node, int free_node) { XMLNode** pt; if (doc == NULL || doc->init_value != XML_INIT_DONE || i_node < 0 || i_node > doc->n_nodes) - return false; + return FALSE; /* Before modifying first see if we run out of memory */ if (doc->n_nodes == 1) pt = NULL; else { - pt = __malloc((doc->n_nodes - 1) * sizeof(XMLNode*)); + pt = __malloc((size_t)(doc->n_nodes - 1) * sizeof(XMLNode*)); if (pt == NULL) - return false; + return FALSE; } /* Can't fail anymore, free item */ (void)XMLNode_free(doc->nodes[i_node]); if (free_node) __free(doc->nodes[i_node]); - + if (pt != NULL) { memcpy(pt, &doc->nodes[i_node], i_node * sizeof(XMLNode*)); - memcpy(&pt[i_node], &doc->nodes[i_node + 1], (doc->n_nodes - i_node - 1) * sizeof(XMLNode*)); + memcpy(&pt[i_node], &doc->nodes[i_node + 1], (size_t)(doc->n_nodes - i_node - 1) * sizeof(XMLNode*)); } if (doc->nodes != NULL) @@ -950,7 +988,7 @@ int XMLDoc_remove_node(XMLDoc* doc, int i_node, int free_node) doc->nodes = pt; doc->n_nodes--; - return true; + return TRUE; } /* @@ -967,7 +1005,7 @@ static int _count_new_char_line(const SXML_CHAR* str, int nb_char_tab, int cur_s else cur_sz_line++; } - + return cur_sz_line; } static int _print_formatting(const XMLNode* node, FILE* f, const SXML_CHAR* tag_sep, const SXML_CHAR* child_sep, int nb_char_tab, int cur_sz_line) @@ -982,7 +1020,7 @@ static int _print_formatting(const XMLNode* node, FILE* f, const SXML_CHAR* tag_ cur_sz_line = _count_new_char_line(child_sep, nb_char_tab, cur_sz_line); } } - + return cur_sz_line; } @@ -993,7 +1031,7 @@ static int _XMLNode_print_header(const XMLNode* node, FILE* f, const SXML_CHAR* if (node == NULL || f == NULL || !node->active || node->tag == NULL || node->tag[0] == NULC) return -1; - + /* Special handling of DOCTYPE */ if (node->tag_type == TAG_DOCTYPE) { /* Search for an unescaped '[' in the DOCTYPE definition, in which case the end delimiter should be ']>' instead of '>' */ @@ -1006,7 +1044,7 @@ static int _XMLNode_print_header(const XMLNode* node, FILE* f, const SXML_CHAR* for (i = 0; i < NB_SPECIAL_TAGS; i++) { if (node->tag_type == _spec[i].tag_type) { sx_fprintf(f, C2SX("%s%s%s"), _spec[i].start, node->tag, _spec[i].end); - cur_sz_line += sx_strlen(_spec[i].start) + sx_strlen(node->tag) + sx_strlen(_spec[i].end); + cur_sz_line += (int)(sx_strlen(_spec[i].start) + sx_strlen(node->tag) + sx_strlen(_spec[i].end)); return cur_sz_line; } } @@ -1015,11 +1053,11 @@ static int _XMLNode_print_header(const XMLNode* node, FILE* f, const SXML_CHAR* for (i = 0; i < _user_tags.n_tags; i++) { if (node->tag_type == _user_tags.tags[i].tag_type) { sx_fprintf(f, C2SX("%s%s%s"), _user_tags.tags[i].start, node->tag, _user_tags.tags[i].end); - cur_sz_line += sx_strlen(_user_tags.tags[i].start) + sx_strlen(node->tag) + sx_strlen(_user_tags.tags[i].end); + cur_sz_line += (int)(sx_strlen(_user_tags.tags[i].start) + sx_strlen(node->tag) + sx_strlen(_user_tags.tags[i].end)); return cur_sz_line; } } - + /* Print tag name */ cur_sz_line += sx_fprintf(f, C2SX("<%s"), node->tag); @@ -1029,7 +1067,7 @@ static int _XMLNode_print_header(const XMLNode* node, FILE* f, const SXML_CHAR* for (i = 0; i < node->n_attributes; i++) { if (!node->attributes[i].active) continue; - cur_sz_line += sx_strlen(node->attributes[i].name) + sx_strlen(node->attributes[i].value) + 3; + cur_sz_line += (int)(sx_strlen(node->attributes[i].name) + sx_strlen(node->attributes[i].value) + 3); if (sz_line > 0 && cur_sz_line > sz_line) { cur_sz_line = _print_formatting(node, f, tag_sep, child_sep, nb_char_tab, cur_sz_line); /* Add extra separator, as if new line was a child of the previous one */ @@ -1041,13 +1079,13 @@ static int _XMLNode_print_header(const XMLNode* node, FILE* f, const SXML_CHAR* /* Attribute name */ cur_sz_line = _count_new_char_line(attr_sep, nb_char_tab, cur_sz_line); sx_fprintf(f, C2SX("%s%s="), attr_sep, node->attributes[i].name); - + /* Attribute value */ (void)sx_fputc(XML_DEFAULT_QUOTE, f); cur_sz_line += fprintHTML(f, node->attributes[i].value) + 2; (void)sx_fputc(XML_DEFAULT_QUOTE, f); } - + /* End the tag if there are no children and no text */ if (node->n_children == 0 && (node->text == NULL || node->text[0] == NULC)) { cur_sz_line += sx_fprintf(f, C2SX("/>")); @@ -1061,14 +1099,14 @@ static int _XMLNode_print_header(const XMLNode* node, FILE* f, const SXML_CHAR* int XMLNode_print_header(const XMLNode* node, FILE* f, int sz_line, int nb_char_tab) { - return _XMLNode_print_header(node, f, NULL, NULL, NULL, sz_line, 0, nb_char_tab) < 0 ? false : true; + return _XMLNode_print_header(node, f, NULL, NULL, NULL, sz_line, 0, nb_char_tab) < 0 ? FALSE : TRUE; } static int _XMLNode_print(const XMLNode* node, FILE* f, const SXML_CHAR* tag_sep, const SXML_CHAR* child_sep, const SXML_CHAR* attr_sep, int keep_text_spaces, int sz_line, int cur_sz_line, int nb_char_tab, int depth) { int i; SXML_CHAR* p; - + if (node != NULL && node->tag_type==TAG_TEXT) { /* Text has to be printed: check if it is only spaces */ if (!keep_text_spaces) { for (p = node->text; p != NULL && *p != NULC && sx_isspace(*p); p++) ; /* 'p' points to first non-space character, or to '\0' if only spaces */ @@ -1081,16 +1119,16 @@ static int _XMLNode_print(const XMLNode* node, FILE* f, const SXML_CHAR* tag_sep if (node == NULL || f == NULL || !node->active || node->tag == NULL || node->tag[0] == NULC) return -1; - + if (nb_char_tab <= 0) nb_char_tab = 1; - + /* Print formatting */ if (depth < 0) /* UGLY HACK: 'depth' forced negative on very first line so we don't print an extra 'tag_sep' (usually "\n" when pretty-printing) */ depth = 0; else cur_sz_line = _print_formatting(node, f, tag_sep, child_sep, nb_char_tab, cur_sz_line); - + _XMLNode_print_header(node, f, tag_sep, child_sep, attr_sep, sz_line, cur_sz_line, nb_char_tab); if (node->text != NULL && node->text[0] != NULC) { @@ -1102,11 +1140,11 @@ static int _XMLNode_print(const XMLNode* node, FILE* f, const SXML_CHAR* tag_sep if (*p != NULC) cur_sz_line += fprintHTML(f, node->text); } else if (node->n_children <= 0) /* Everything has already been printed */ return cur_sz_line; - + /* Recursively print children */ for (i = 0; i < node->n_children; i++) (void)_XMLNode_print(node->children[i], f, tag_sep, child_sep, attr_sep, keep_text_spaces, sz_line, cur_sz_line, nb_char_tab, depth+1); - + /* Print tag end after children */ /* Print formatting */ if (node->n_children > 0) @@ -1124,10 +1162,10 @@ int XMLNode_print_attr_sep(const XMLNode* node, FILE* f, const SXML_CHAR* tag_se int XMLDoc_print_attr_sep(const XMLDoc* doc, FILE* f, const SXML_CHAR* tag_sep, const SXML_CHAR* child_sep, const SXML_CHAR* attr_sep, int keep_text_spaces, int sz_line, int nb_char_tab) { int i, depth, cur_sz_line; - + if (doc == NULL || f == NULL || doc->init_value != XML_INIT_DONE) - return false; - + return FALSE; + /* Write BOM if it exist */ if (doc->sz_bom > 0) fwrite(doc->bom, sizeof(unsigned char), doc->sz_bom, f); @@ -1138,7 +1176,7 @@ int XMLDoc_print_attr_sep(const XMLDoc* doc, FILE* f, const SXML_CHAR* tag_sep, } /* TODO: Find something more graceful than 'depth=-1', even though everyone knows I probably never will ;) */ - return true; + return TRUE; } /* --- */ @@ -1149,13 +1187,13 @@ int XML_parse_attribute_to(const SXML_CHAR* str, int to, XMLAttribute* xmlattr) int i, n0, n1, remQ = 0; int ret = 1; SXML_CHAR quote = '\0'; - + if (str == NULL || xmlattr == NULL) return 0; if (to < 0) - to = sx_strlen(str) - 1; - + to = (int)sx_strlen(str) - 1; + /* Search for the '=' */ /* 'n0' is where the attribute name stops, 'n1' is where the attribute value starts */ for (n0 = 0; n0 != to && str[n0] != C2SX('=') && !sx_isspace(str[n0]); n0++) ; /* Search for '=' or a space */ @@ -1167,13 +1205,17 @@ int XML_parse_attribute_to(const SXML_CHAR* str, int to, XMLAttribute* xmlattr) quote = str[n1]; remQ = 1; } - - xmlattr->name = __malloc((n0+1)*sizeof(SXML_CHAR)); - xmlattr->value = __malloc((to+1 - n1 - 2*remQ + 1) * sizeof(SXML_CHAR)); /* 2*remQ because we expect 2 quotes */ - xmlattr->active = true; + + xmlattr->name = __malloc((size_t)(n0+1)*sizeof(SXML_CHAR)); + xmlattr->value = __malloc((size_t)(to+1 - n1 - 2*remQ + 1) * sizeof(SXML_CHAR)); /* 2*remQ because we expect 2 quotes */ + xmlattr->active = TRUE; if (xmlattr->name != NULL && xmlattr->value != NULL) { /* Copy name */ + #ifdef _MSC_VER + strncpy_s(xmlattr->name, (size_t)(n0 + 1) * sizeof(SXML_CHAR), str, n0); + #else sx_strncpy(xmlattr->name, str, n0); + #endif xmlattr->name[n0] = NULC; /* (void)str_unescape(xmlattr->name); do not unescape the name */ /* Copy value (p starts after the quote (if any) and stops at the end of 'str' @@ -1186,7 +1228,7 @@ int XML_parse_attribute_to(const SXML_CHAR* str, int to, XMLAttribute* xmlattr) ret = 2; /* Quote at the beginning but not at the end: probable presence of '>' inside attribute value, so we need to read more data! */ } else ret = 0; - + if (ret == 0) { if (xmlattr->name != NULL) { __free(xmlattr->name); @@ -1197,7 +1239,7 @@ int XML_parse_attribute_to(const SXML_CHAR* str, int to, XMLAttribute* xmlattr) xmlattr->value = NULL; } } - + return ret; } @@ -1209,10 +1251,15 @@ static TagType _parse_special_tag(const SXML_CHAR* str, int len, _TAG* tag, XMLN if (sx_strncmp(str + len - tag->len_end, tag->end, tag->len_end)) /* There probably is a '>' inside the tag */ return TAG_PARTIAL; - node->tag = __malloc((len - tag->len_start - tag->len_end + 1)*sizeof(SXML_CHAR)); + node->tag = __malloc((size_t)(len - tag->len_start - tag->len_end + 1)*sizeof(SXML_CHAR)); if (node->tag == NULL) return TAG_ERROR; - sx_strncpy(node->tag, str + tag->len_start, len - tag->len_start - tag->len_end); +#ifdef _MSC_VER + strncpy_s(node->tag, (size_t)(len - tag->len_start - tag->len_end + 1) * sizeof(SXML_CHAR), + str + tag->len_start, (size_t)(len - tag->len_start - tag->len_end)); +#else + sx_strncpy(node->tag, str + tag->len_start, (size_t)(len - tag->len_start - tag->len_end)); +#endif node->tag[len - tag->len_start - tag->len_end] = NULC; node->tag_type = tag->tag_type; @@ -1229,11 +1276,11 @@ TagType XML_parse_1string(const SXML_CHAR* str, XMLNode* xmlnode) SXML_CHAR *p; XMLAttribute* pt; int n, nn, len, rc, tag_end = 0; - + if (str == NULL || xmlnode == NULL) return TAG_ERROR; - len = sx_strlen(str); - + len = (int)sx_strlen(str); + /* Check for malformed string */ if (str[0] != C2SX('<') || str[len-1] != C2SX('>')) return TAG_NONE; /* Syntax error */ @@ -1257,17 +1304,21 @@ TagType XML_parse_1string(const SXML_CHAR* str, XMLNode* xmlnode) return TAG_PARTIAL; nn = 1; } - xmlnode->tag = __malloc((len - 9 - nn)*sizeof(SXML_CHAR)); /* 'len' - "" + '\0' */ + xmlnode->tag = __malloc((size_t)(len - 9 - nn)*sizeof(SXML_CHAR)); /* 'len' - "" + '\0' */ if (xmlnode->tag == NULL) return TAG_ERROR; - sx_strncpy(xmlnode->tag, &str[9], len - 10 - nn); + #ifdef _MSC_VER + strncpy_s(xmlnode->tag, (size_t)(len - 9 - nn) * sizeof(SXML_CHAR), &str[9], (size_t)(len - 10 - nn)); + #else + sx_strncpy(xmlnode->tag, &str[9], (size_t)(len - 10 - nn)); + #endif xmlnode->tag[len - 10 - nn] = NULC; xmlnode->tag_type = TAG_DOCTYPE; return TAG_DOCTYPE; } } - + /* Test user tags */ for (nn = 0; nn < _user_tags.n_tags; nn++) { n = _parse_special_tag(str, len, &_user_tags.tags[nn], xmlnode); @@ -1280,24 +1331,28 @@ TagType XML_parse_1string(const SXML_CHAR* str, XMLNode* xmlnode) if (str[1] == C2SX('/')) tag_end = 1; - + /* tag starts at index 1 (or 2 if tag end) and ends at the first space or '/>' */ for (n = 1 + tag_end; str[n] != NULC && str[n] != C2SX('>') && str[n] != C2SX('/') && !sx_isspace(str[n]); n++) ; - xmlnode->tag = __malloc((n - tag_end)*sizeof(SXML_CHAR)); + xmlnode->tag = __malloc((size_t)(n - tag_end)*sizeof(SXML_CHAR)); if (xmlnode->tag == NULL) return TAG_ERROR; - sx_strncpy(xmlnode->tag, &str[1 + tag_end], n - 1 - tag_end); +#ifdef _MSC_VER + strncpy_s(xmlnode->tag, (size_t)(n - tag_end) * sizeof(SXML_CHAR), &str[1 + tag_end], (size_t)(n - 1 - tag_end)); +#else + sx_strncpy(xmlnode->tag, &str[1 + tag_end], (size_t)(n - 1 - tag_end)); +#endif xmlnode->tag[n - 1 - tag_end] = NULC; if (tag_end) { xmlnode->tag_type = TAG_END; return TAG_END; } - + /* Here, 'n' is the position of the first space after tag name */ while (n < len) { /* Skips spaces */ while (sx_isspace(str[n])) n++; - + /* Check for XML end ('>' or '/>') */ if (str[n] == C2SX('>')) { /* Tag with children */ TagType type = (str[n-1] == '/' ? TAG_SELF : TAG_FATHER); /* TODO: Find something better to cope with */ @@ -1308,28 +1363,28 @@ TagType XML_parse_1string(const SXML_CHAR* str, XMLNode* xmlnode) xmlnode->tag_type = TAG_SELF; return TAG_SELF; } - + /* New attribute found */ p = sx_strchr(str+n, C2SX('=')); if (p == NULL) goto parse_err; - pt = __realloc(xmlnode->attributes, (xmlnode->n_attributes + 1) * sizeof(XMLAttribute)); + pt = __realloc(xmlnode->attributes, (size_t)(xmlnode->n_attributes + 1) * sizeof(XMLAttribute)); if (pt == NULL) goto parse_err; - + pt[xmlnode->n_attributes].name = NULL; pt[xmlnode->n_attributes].value = NULL; - pt[xmlnode->n_attributes].active = false; + pt[xmlnode->n_attributes].active = FALSE; xmlnode->n_attributes++; xmlnode->attributes = pt; while (*++p != NULC && sx_isspace(*p)) ; /* Skip spaces */ if (isquote(*p)) { /* Attribute value starts with a quote, look for next one, ignoring protected ones with '\' */ - for (nn = p-str+1; str[nn] && str[nn] != *p; nn++) { /* CHECK UNICODE "nn = p-str+1" */ + for (nn = (int)(p-str+1); str[nn] && str[nn] != *p; nn++) { /* CHECK UNICODE "nn = p-str+1" */ /* if (str[nn] == C2SX('\\')) nn++; [bugs:#7]: '\' is valid in values */ } nn++; //* Skip quote */ } else { /* Attribute value stops at first space or end of XML string */ - for (nn = p-str+1; str[nn] != NULC && !sx_isspace(str[nn]) && str[nn] != C2SX('/') && str[nn] != C2SX('>'); nn++) ; /* Go to the end of the attribute value */ /* CHECK UNICODE */ + for (nn = (int)(p-str+1); str[nn] != NULC && !sx_isspace(str[nn]) && str[nn] != C2SX('/') && str[nn] != C2SX('>'); nn++) ; /* Go to the end of the attribute value */ /* CHECK UNICODE */ } - + /* Here 'str[nn]' is the character after value */ /* the attribute definition ('attrName="attrVal"') is between 'str[n]' and 'str[nn]' */ rc = XML_parse_attribute_to(&str[n], nn - n, &xmlnode->attributes[xmlnode->n_attributes - 1]); @@ -1338,7 +1393,7 @@ TagType XML_parse_1string(const SXML_CHAR* str, XMLNode* xmlnode) XMLNode_remove_attribute(xmlnode, xmlnode->n_attributes - 1); return TAG_ERROR; /* was TAG_PARTIAL */ } - + n = nn + 1; /* Go to next attribute */ if (str[nn] == C2SX('>')) { /* ... or we migh have reached the end if no space is between the attribute value and the ">" or "/>" */ TagType type = (str[nn-1] == '/' ? TAG_SELF : TAG_FATHER); /* TODO: Find something better to cope with */ @@ -1346,9 +1401,9 @@ TagType XML_parse_1string(const SXML_CHAR* str, XMLNode* xmlnode) return type; } } - + sx_fprintf(stderr, C2SX("\nWE SHOULD NOT BE HERE!\n[%s]\n\n"), str); - + parse_err: (void)XMLNode_free(xmlnode); @@ -1358,23 +1413,31 @@ TagType XML_parse_1string(const SXML_CHAR* str, XMLNode* xmlnode) static int _parse_data_SAX(void* in, const DataSourceType in_type, const SAX_Callbacks* sax, SAX_Data* sd) { SXML_CHAR *line = NULL, *txt_end, *p; +#ifdef _MSC_VER + XMLNode node = {0}; +#else XMLNode node; +#endif int ret, exit, sz, n0, ncr; +#ifdef _MSC_VER + TagType tag_type = {0}; +#else TagType tag_type; +#endif int (*meos)(void* ds) = (in_type == DATA_SOURCE_BUFFER ? (int(*)(void*))_beob : (int(*)(void*))sx_feof); if (sax->start_doc != NULL && !sax->start_doc(sd)) - return true; + return TRUE; if (sax->all_event != NULL && !sax->all_event(XML_EVENT_START_DOC, NULL, (SXML_CHAR*)sd->name, 0, sd)) - return true; + return TRUE; - ret = true; - exit = false; + ret = TRUE; + exit = FALSE; sd->line_num = 1; /* Line counter, starts at 1 */ sz = 0; /* 'line' buffer size */ node.init_value = 0; (void)XMLNode_init(&node); - while ((n0 = read_line_alloc(in, in_type, &line, &sz, 0, NULC, C2SX('>'), true, C2SX('\n'), &ncr)) != 0) { + while ((n0 = read_line_alloc(in, in_type, &line, &sz, 0, NULC, C2SX('>'), TRUE, C2SX('\n'), &ncr)) != 0) { (void)XMLNode_free(&node); for (p = line; *p != NULC && sx_isspace(*p) && p - line < n0; p++) ; /* Checks if text is only spaces */ if (*p == NULC || p - line >= n0) @@ -1383,10 +1446,10 @@ static int _parse_data_SAX(void* in, const DataSourceType in_type, const SAX_Cal /* Get text for 'father' (i.e. what is before '<') */ while ((txt_end = sx_strchr(line, C2SX('<'))) == NULL) { /* '<' was not found, indicating a probable '>' inside text (should have been escaped with '>' but we'll handle that ;)) */ - int n1 = read_line_alloc(in, in_type, &line, &sz, n0, 0, C2SX('>'), true, C2SX('\n'), &ncr); /* Go on reading the file from current position until next '>' */ + int n1 = read_line_alloc(in, in_type, &line, &sz, n0, 0, C2SX('>'), TRUE, C2SX('\n'), &ncr); /* Go on reading the file from current position until next '>' */ sd->line_num += ncr; if (n1 <= n0) { - ret = false; + ret = FALSE; if (sax->on_error == NULL && sax->all_event == NULL) { sx_fprintf(stderr, C2SX("%s:%d: MEMORY ERROR.\n"), sd->name, sd->line_num); } else { @@ -1400,7 +1463,7 @@ static int _parse_data_SAX(void* in, const DataSourceType in_type, const SAX_Cal n0 = n1; } if (txt_end == NULL) { /* Missing tag start */ - ret = false; + ret = FALSE; if (sax->on_error == NULL && sax->all_event == NULL) { sx_fprintf(stderr, C2SX("%s:%d: ERROR: Unexpected end character '>', without matching '<'!\n"), sd->name, sd->line_num); } else { @@ -1434,7 +1497,7 @@ static int _parse_data_SAX(void* in, const DataSourceType in_type, const SAX_Cal switch (tag_type = XML_parse_1string(txt_end, &node)) { case TAG_ERROR: /* Memory error */ - ret = false; + ret = FALSE; if (sax->on_error == NULL && sax->all_event == NULL) { sx_fprintf(stderr, C2SX("%s:%d: MEMORY ERROR.\n"), sd->name, sd->line_num); } else { @@ -1444,9 +1507,9 @@ static int _parse_data_SAX(void* in, const DataSourceType in_type, const SAX_Cal break; } break; - + case TAG_NONE: /* Syntax error */ - ret = false; + ret = FALSE; p = sx_strchr(txt_end, C2SX('\n')); if (p != NULL) *p = NULC; @@ -1474,10 +1537,10 @@ static int _parse_data_SAX(void* in, const DataSourceType in_type, const SAX_Cal default: /* Add 'node' to 'father' children */ /* If the line looks like a comment (or CDATA) but is not properly finished, loop until we find the end. */ while (tag_type == TAG_PARTIAL) { - int n1 = read_line_alloc(in, in_type, &line, &sz, n0, NULC, C2SX('>'), true, C2SX('\n'), &ncr); /* Go on reading the file from current position until next '>' */ + int n1 = read_line_alloc(in, in_type, &line, &sz, n0, NULC, C2SX('>'), TRUE, C2SX('\n'), &ncr); /* Go on reading the file from current position until next '>' */ sd->line_num += ncr; if (n1 <= n0) { - ret = false; + ret = FALSE; if (sax->on_error == NULL && sax->all_event == NULL) { sx_fprintf(stderr, C2SX("%s:%d: SYNTAX ERROR.\n"), sd->name, sd->line_num); } else { @@ -1492,7 +1555,7 @@ static int _parse_data_SAX(void* in, const DataSourceType in_type, const SAX_Cal txt_end = sx_strchr(line, C2SX('<')); /* In case 'line' has been moved by the '__realloc' in 'read_line_alloc' */ tag_type = XML_parse_1string(txt_end, &node); if (tag_type == TAG_ERROR) { - ret = false; + ret = FALSE; if (sax->on_error == NULL && sax->all_event == NULL) { sx_fprintf(stderr, C2SX("%s:%d: PARSE ERROR.\n"), sd->name, sd->line_num); } else { @@ -1504,7 +1567,7 @@ static int _parse_data_SAX(void* in, const DataSourceType in_type, const SAX_Cal break; } } - if (ret == false) + if (ret == FALSE) break; if (sax->start_node != NULL && (exit = !sax->start_node(&node, sd))) break; @@ -1518,9 +1581,9 @@ static int _parse_data_SAX(void* in, const DataSourceType in_type, const SAX_Cal } break; } - if (exit == true) /* Return false when exit is requested */ - ret = false; - if (ret == false || meos(in)) + if (exit == TRUE) /* Return FALSE when exit is requested */ + ret = FALSE; + if (ret == FALSE || meos(in)) break; } __free(line); @@ -1537,7 +1600,7 @@ static int _parse_data_SAX(void* in, const DataSourceType in_type, const SAX_Cal int SAX_Callbacks_init(SAX_Callbacks* sax) { if (sax == NULL) - return false; + return FALSE; sax->start_doc = NULL; sax->start_node = NULL; @@ -1547,7 +1610,7 @@ int SAX_Callbacks_init(SAX_Callbacks* sax) sax->end_doc = NULL; sax->all_event = NULL; - return true; + return TRUE; } int DOMXMLDoc_doc_start(SAX_Data* sd) @@ -1558,7 +1621,7 @@ int DOMXMLDoc_doc_start(SAX_Data* sd) dom->error = PARSE_ERR_NONE; dom->line_error = 0; - return true; + return TRUE; } int DOMXMLDoc_node_start(const XMLNode* node, SAX_Data* sd) @@ -1567,8 +1630,8 @@ int DOMXMLDoc_node_start(const XMLNode* node, SAX_Data* sd) XMLNode* new_node; int i; - if ((new_node = XMLNode_dup(node, true)) == NULL) goto node_start_err; /* No real need to put 'true' for 'XMLNode_dup', but cleaner */ - + if ((new_node = XMLNode_dup(node, TRUE)) == NULL) goto node_start_err; /* No real need to put 'TRUE' for 'XMLNode_dup', but cleaner */ + if (dom->current == NULL) { if ((i = _add_node(&dom->doc->nodes, &dom->doc->n_nodes, new_node)) < 0) goto node_start_err; @@ -1581,7 +1644,7 @@ int DOMXMLDoc_node_start(const XMLNode* node, SAX_Data* sd) new_node->father = dom->current; dom->current = new_node; - return true; + return TRUE; node_start_err: dom->error = PARSE_ERR_MEMORY; @@ -1589,7 +1652,7 @@ int DOMXMLDoc_node_start(const XMLNode* node, SAX_Data* sd) (void)XMLNode_free(new_node); __free(new_node); - return false; + return FALSE; } int DOMXMLDoc_node_end(const XMLNode* node, SAX_Data* sd) @@ -1606,12 +1669,12 @@ int DOMXMLDoc_node_end(const XMLNode* node, SAX_Data* sd) dom->error = PARSE_ERR_UNEXPECTED_NODE_END; dom->line_error = sd->line_num; - return false; + return FALSE; } dom->current = dom->current->father; - return true; + return TRUE; } int DOMXMLDoc_node_text(SXML_CHAR* text, SAX_Data* sd) @@ -1622,52 +1685,67 @@ int DOMXMLDoc_node_text(SXML_CHAR* text, SAX_Data* sd) /* Keep text, even if it is only spaces */ #if 0 while(*p != NULC && sx_isspace(*p++)) ; - if (*p == NULC) return true; /* Only spaces */ + if (*p == NULC) return TRUE; /* Only spaces */ #endif /* If there is no current node to add text to, raise an error, except if text is only spaces, in which case it is probably just formatting */ if (dom->current == NULL) { while(*p != NULC && sx_isspace(*p)) p++; if (*p == NULC) /* Only spaces => probably pretty-printing */ - return true; + return TRUE; dom->error = PARSE_ERR_TEXT_OUTSIDE_NODE; dom->line_error = sd->line_num; - return false; /* There is some "real" text => raise an error */ + return FALSE; /* There is some "real" text => raise an error */ } if (dom->text_as_nodes) { XMLNode* new_node = XMLNode_allocN(1); + #ifdef _MSC_VER + if (new_node == NULL || (new_node->text = _strdup(text)) == NULL + #else if (new_node == NULL || (new_node->text = sx_strdup(text)) == NULL + #endif || _add_node(&dom->current->children, &dom->current->n_children, new_node) < 0) { dom->error = PARSE_ERR_MEMORY; dom->line_error = sd->line_num; (void)XMLNode_free(new_node); __free(new_node); - return false; + return FALSE; } new_node->tag_type = TAG_TEXT; new_node->father = dom->current; /*dom->current->tag_type = TAG_FATHER; // OS: should parent field be forced to be TAG_FATHER? now it has at least one TAG_TEXT child. I decided not to enforce this for backward-compatibility related to tag_types*/ - return true; + return TRUE; } else { /* Old behaviour: concatenate text to the previous one */ /* 'p' will point at the new text */ if (dom->current->text == NULL) { + #ifdef _MSC_VER + p = _strdup(text); + #else p = sx_strdup(text); + #endif } else { - p = __realloc(dom->current->text, (sx_strlen(dom->current->text) + sx_strlen(text) + 1)*sizeof(SXML_CHAR)); + #ifdef _MSC_VER + const size_t len = (sx_strlen(dom->current->text) + sx_strlen(text) + 1) * sizeof(SXML_CHAR); + p = __realloc(dom->current->text, len); + if (p != NULL) + strcat_s(p, len, text); + #else + p = __realloc(dom->current->text, (sx_strlen(dom->current->text) + sx_strlen(text) + 1) * sizeof(SXML_CHAR)); if (p != NULL) sx_strcat(p, text); + #endif } if (p == NULL) { dom->error = PARSE_ERR_MEMORY; dom->line_error = sd->line_num; - return false; + return FALSE; } - + dom->current->text = p; } - return true; + return TRUE; } int DOMXMLDoc_parse_error(ParseError error_num, int line_number, SAX_Data* sd) @@ -1679,7 +1757,7 @@ int DOMXMLDoc_parse_error(ParseError error_num, int line_number, SAX_Data* sd) /* Complete error message will be displayed in 'DOMXMLDoc_doc_end' callback */ - return false; /* Stop on error */ + return FALSE; /* Stop on error */ } int DOMXMLDoc_doc_end(SAX_Data* sd) @@ -1704,13 +1782,13 @@ int DOMXMLDoc_doc_end(SAX_Data* sd) dom->doc = NULL; } - return true; + return TRUE; } int SAX_Callbacks_init_DOM(SAX_Callbacks* sax) { if (sax == NULL) - return false; + return FALSE; sax->start_doc = DOMXMLDoc_doc_start; sax->start_node = DOMXMLDoc_node_start; @@ -1720,15 +1798,23 @@ int SAX_Callbacks_init_DOM(SAX_Callbacks* sax) sax->end_doc = DOMXMLDoc_doc_end; sax->all_event = NULL; - return true; + return TRUE; } int XMLDoc_parse_file_SAX(const SXML_CHAR* filename, const SAX_Callbacks* sax, void* user) { +#ifdef _MSC_VER + FILE* f = NULL; +#else FILE* f; +#endif int ret; +#ifdef _MSC_VER + SAX_Data sd = {0}; +#else SAX_Data sd; - SXML_CHAR* fmode = +#endif + SXML_CHAR* fmode = #ifndef SXMLC_UNICODE C2SX("rt"); #else @@ -1738,11 +1824,15 @@ int XMLDoc_parse_file_SAX(const SXML_CHAR* filename, const SAX_Callbacks* sax, v if (sax == NULL || filename == NULL || filename[0] == NULC) - return false; + return FALSE; +#ifdef _MSC_VER + fopen_s(&f, filename, fmode); +#else f = sx_fopen(filename, fmode); +#endif if (f == NULL) - return false; + return FALSE; /* Microsoft's 'ftell' returns invalid position for Unicode text files (see http://connect.microsoft.com/VisualStudio/feedback/details/369265/ftell-ftell-nolock-incorrectly-handling-unicode-text-translation) However, we're opening the file as binary in Unicode so we don't fall into that case... @@ -1761,9 +1851,13 @@ int XMLDoc_parse_file_SAX(const SXML_CHAR* filename, const SAX_Callbacks* sax, v would read 2 bytes for 1 character, which would not work on "plain" files. */ if (bom == BOM_NONE || bom == BOM_UTF_8) { sx_fclose(f); + #ifdef _MSC_VER + fopen_s(&f, filename, C2SX("rt")); + #else f = sx_fopen(filename, C2SX("rt")); + #endif if (f == NULL) - return false; + return FALSE; if (bom == BOM_UTF_8) freadBOM(f, NULL, NULL); /* Skip the UTF-8 BOM that was found */ } @@ -1771,7 +1865,7 @@ int XMLDoc_parse_file_SAX(const SXML_CHAR* filename, const SAX_Callbacks* sax, v /* Unicode BOM when Unicode support has not been compiled in. */ else { sx_fclose(f); - return false; + return FALSE; } #endif @@ -1783,11 +1877,24 @@ int XMLDoc_parse_file_SAX(const SXML_CHAR* filename, const SAX_Callbacks* sax, v int XMLDoc_parse_buffer_SAX_len(const SXML_CHAR* buffer, int buffer_len, const SXML_CHAR* name, const SAX_Callbacks* sax, void* user) { - DataSourceBuffer dsb = { buffer, buffer_len, 0 }; +#ifdef __CODEGEARC__ SAX_Data sd; + DataSourceBuffer dsb; + dsb.buf = buffer; + dsb.buf_len = buffer_len; + dsb.cur_pos = 0; +#else + DataSourceBuffer dsb = { buffer, buffer_len, 0 }; + + #ifdef _MSC_VER + SAX_Data sd = {0}; + #else + SAX_Data sd; + #endif +#endif if (sax == NULL || buffer == NULL) - return false; + return FALSE; sd.name = name; sd.user = user; @@ -1798,20 +1905,33 @@ int XMLDoc_parse_buffer_SAX_len(const SXML_CHAR* buffer, int buffer_len, const S int XMLDoc_parse_file_DOM_text_as_nodes(const SXML_CHAR* filename, XMLDoc* doc, int text_as_nodes) { +#ifdef _MSC_VER + DOM_through_SAX dom = {0}; +#else DOM_through_SAX dom; +#endif SAX_Callbacks sax; int ret; if (doc == NULL || filename == NULL || filename[0] == NULC || doc->init_value != XML_INIT_DONE) - return false; + return FALSE; +#ifdef _MSC_VER + strncpy_s(doc->filename, SXMLC_MAX_PATH - 1, filename, SXMLC_MAX_PATH - 1); +#else sx_strncpy(doc->filename, filename, SXMLC_MAX_PATH - 1); +#endif doc->filename[SXMLC_MAX_PATH - 1] = NULC; /* Read potential BOM on file */ { /* In Unicode, open the file as binary so that further 'fgetwc' read all bytes */ + #ifdef _MSC_VER + FILE* f = NULL; + fopen_s(&f, filename, C2SX("rb")); + #else FILE* f = sx_fopen(filename, C2SX("rb")); + #endif if (f != NULL) { #if defined(SXMLC_UNICODE) && (defined(WIN32) || defined(WIN64)) /*setvbuf(f, NULL, _IONBF, 0);*/ @@ -1839,12 +1959,16 @@ int XMLDoc_parse_file_DOM_text_as_nodes(const SXML_CHAR* filename, XMLDoc* doc, int XMLDoc_parse_buffer_DOM_text_as_nodes(const SXML_CHAR* buffer, const SXML_CHAR* name, XMLDoc* doc, int text_as_nodes) { +#ifdef _MSC_VER + DOM_through_SAX dom = {0}; +#else DOM_through_SAX dom; +#endif SAX_Callbacks sax; int ret; if (doc == NULL || buffer == NULL || doc->init_value != XML_INIT_DONE) - return false; + return FALSE; dom.doc = doc; dom.current = NULL; @@ -1940,16 +2064,16 @@ static struct _html_special_dict { int _beob(DataSourceBuffer* ds) { if (ds == NULL || ds->buf[ds->cur_pos] == NULC || ds->cur_pos >= ds->buf_len) - return true; + return TRUE; - return false; + return FALSE; } int _bgetc(DataSourceBuffer* ds) { if (_beob(ds)) return EOF; - + return (int)(ds->buf[ds->cur_pos++]); } @@ -1987,16 +2111,16 @@ int read_line_alloc(void* in, DataSourceType in_type, SXML_CHAR** line, int* sz_ int n, ret; int (*mgetc)(void* ds) = (in_type == DATA_SOURCE_BUFFER ? (int(*)(void*))_bgetc : (int(*)(void*))sx_fgetc); int (*meos)(void* ds) = (in_type == DATA_SOURCE_BUFFER ? (int(*)(void*))_beob : (int(*)(void*))sx_feof); - + if (in == NULL || line == NULL) return 0; - + if (to == NULC) to = C2SX('\n'); /* Search for character 'from' */ if (interest_count != NULL) *interest_count = 0; - while (true) { + while (TRUE) { /* Reaching EOF before 'to' char is not an error but should trigger 'line' alloc and init to '' */ c = mgetc(in); ch = (SXML_CHAR)c; @@ -2008,10 +2132,10 @@ int read_line_alloc(void* in, DataSourceType in_type, SXML_CHAR** line, int* sz_ if (ch == from || from == NULC) break; } - + if (sz_line == NULL) sz_line = &init_sz; - + if (*line == NULL || *sz_line == 0) { if (*sz_line == 0) *sz_line = MEM_INCR_RLA; *line = __malloc(*sz_line*sizeof(SXML_CHAR)); @@ -2022,7 +2146,7 @@ int read_line_alloc(void* in, DataSourceType in_type, SXML_CHAR** line, int* sz_ i0 = 0; if (i0 >= *sz_line) return 0; - + n = i0; if (c == CEOF) { /* EOF reached before 'to' char => return the empty string */ (*line)[n] = NULC; @@ -2041,7 +2165,7 @@ int read_line_alloc(void* in, DataSourceType in_type, SXML_CHAR** line, int* sz_ } (*line)[n] = NULC; ret = 0; - while (true) { + while (TRUE) { if ((c = mgetc(in)) == CEOF) { /* EOF or error */ (*line)[n] = NULC; ret = meos(in) ? n : 0; @@ -2050,7 +2174,8 @@ int read_line_alloc(void* in, DataSourceType in_type, SXML_CHAR** line, int* sz_ ch = (SXML_CHAR)c; if (interest_count != NULL && ch == interest) (*interest_count)++; - (*line)[n] = ch; + if (n < *sz_line) + (*line)[n] = ch; if (ch != to || (keep_fromto && to != NULC && ch == to)) /* If we reached the 'to' character and we keep it, we still need to add the extra '\0' */ n++; if (n >= *sz_line) { /* Too many characters for our line => realloc some more */ @@ -2062,20 +2187,21 @@ int read_line_alloc(void* in, DataSourceType in_type, SXML_CHAR** line, int* sz_ } else *line = pt; } - (*line)[n] = NULC; /* If we reached the 'to' character and we want to strip it, 'n' hasn't changed and 'line[n]' (which is 'to') will be replaced by '\0' */ + if (line && sz_line && n < *sz_line) + (*line)[n] = NULC; /* If we reached the 'to' character and we want to strip it, 'n' hasn't changed and 'line[n]' (which is 'to') will be replaced by '\0' */ if (ch == to) { ret = n; break; } } - + #if 0 /* Automatic buffer resize is deactivated */ /* Resize line to the exact size */ pt = __realloc(*line, (n+1)*sizeof(SXML_CHAR)); if (pt != NULL) *line = pt; #endif - + return ret; } @@ -2094,14 +2220,18 @@ SXML_CHAR* strcat_alloc(SXML_CHAR** src1, const SXML_CHAR* src2) if (src2 == NULL || *src2 == NULC) return *src1; - n = (*src1 == NULL ? 0 : sx_strlen(*src1)) + sx_strlen(src2) + 1; + n = (*src1 == NULL ? 0 : (int)sx_strlen(*src1)) + (int)sx_strlen(src2) + 1; cat = __realloc(*src1, n*sizeof(SXML_CHAR)); if (cat == NULL) return NULL; if (*src1 == NULL) *cat = NULC; *src1 = cat; +#ifdef _MSC_VER + strcat_s(*src1, n * sizeof(SXML_CHAR), src2); +#else sx_strcat(*src1, src2); +#endif return *src1; } @@ -2110,22 +2240,22 @@ SXML_CHAR* strip_spaces(SXML_CHAR* str, SXML_CHAR repl_sq) { SXML_CHAR* p; int i, len; - + /* 'p' to the first non-space */ for (p = str; *p != NULC && sx_isspace(*p); p++) ; /* No need to search for 'protect' as it is not a space */ - len = sx_strlen(str); + len = (int)sx_strlen(str); for (i = len-1; i >= 0 && sx_isspace(str[i]); i--) ; if (i >= 0 && str[i] == C2SX('\\')) /* If last non-space is the protection, keep the last space */ i++; str[i+1] = NULC; /* New end of string to last non-space */ - + if (repl_sq == NULC) { if (p == str && i == len) return str; /* Nothing to do */ for (i = 0; (str[i] = *p) != NULC; i++, p++) ; /* Copy 'p' to 'str' */ return str; } - + /* Squeeze all spaces with 'repl_sq' */ i = 0; while (*p != NULC) { @@ -2139,7 +2269,7 @@ SXML_CHAR* strip_spaces(SXML_CHAR* str, SXML_CHAR repl_sq) } } str[i] = NULC; - + return str; } @@ -2165,13 +2295,13 @@ int split_left_right(SXML_CHAR* str, SXML_CHAR sep, int* l0, int* l1, int* i_sep SXML_CHAR quote = '\0'; if (str == NULL) - return false; + return FALSE; if (i_sep != NULL) *i_sep = -1; if (!ignore_spaces) /* No sense of ignore quotes if spaces are to be kept */ - ignore_quotes = false; + ignore_quotes = FALSE; /* Parse left part */ @@ -2209,7 +2339,7 @@ int split_left_right(SXML_CHAR* str, SXML_CHAR sep, int* l0, int* l1, int* i_sep *r1 = is-1; if (i_sep != NULL) *i_sep = (str[is] == NULC ? -1 : is); - return true; + return TRUE; } /* Parse right part */ @@ -2228,7 +2358,7 @@ int split_left_right(SXML_CHAR* str, SXML_CHAR sep, int* l0, int* l1, int* i_sep break; } if (ignore_quotes && str[n1--] != quote) /* Quote is not the same than earlier, '--' is not to take it into account */ - return false; + return FALSE; if (!ignore_spaces) while (str[++n1]) ; /* Jump down the end of the string */ @@ -2237,7 +2367,7 @@ int split_left_right(SXML_CHAR* str, SXML_CHAR sep, int* l0, int* l1, int* i_sep if (r1 != NULL) *r1 = n1; - return true; + return TRUE; } BOM_TYPE freadBOM(FILE* f, unsigned char* bom, int* sz_bom) @@ -2330,14 +2460,14 @@ BOM_TYPE freadBOM(FILE* f, unsigned char* bom, int* sz_bom) int has_html(SXML_CHAR* html) { if (html == NULL || *html == NULC) - return false; + return FALSE; do { if (*html++ == C2SX('&')) - return true; + return TRUE; } while (*html); - - return false; + + return FALSE; } SXML_CHAR* html2str(SXML_CHAR* html, SXML_CHAR* str) @@ -2350,7 +2480,7 @@ SXML_CHAR* html2str(SXML_CHAR* html, SXML_CHAR* str) if (str == NULL) str = html; - + /* Look for '&' and matches it to any of the recognized HTML pattern. */ /* If found, replaces the '&' by the corresponding char. */ /* 'p2' is the char to analyze, 'p1' is where to insert it */ @@ -2360,11 +2490,11 @@ SXML_CHAR* html2str(SXML_CHAR* html, SXML_CHAR* str) *pd = *ps; continue; } - + for (i = 0; HTML_SPECIAL_DICT[i].chr; i++) { if (sx_strncmp(ps, HTML_SPECIAL_DICT[i].html, HTML_SPECIAL_DICT[i].html_len)) continue; - + *pd = HTML_SPECIAL_DICT[i].chr; ps += HTML_SPECIAL_DICT[i].html_len-1; break; @@ -2374,7 +2504,7 @@ SXML_CHAR* html2str(SXML_CHAR* html, SXML_CHAR* str) *pd = *ps; } *pd = NULC; - + return str; } @@ -2399,7 +2529,11 @@ SXML_CHAR* str2html(SXML_CHAR* str, SXML_CHAR* html) for (ps = str, pd = html; *ps; ps++, pd++) { for (i = 0; HTML_SPECIAL_DICT[i].chr; i++) { if (*ps == HTML_SPECIAL_DICT[i].chr) { + #ifdef _MSC_VER + strcpy_s(pd, strlen(pd), HTML_SPECIAL_DICT[i].html); + #else sx_strcpy(pd, HTML_SPECIAL_DICT[i].html); + #endif pd += HTML_SPECIAL_DICT[i].html_len - 1; break; } @@ -2415,7 +2549,7 @@ SXML_CHAR* str2html(SXML_CHAR* str, SXML_CHAR* html) int strlen_html(SXML_CHAR* str) { int i, j, n; - + if (str == NULL) return 0; @@ -2438,7 +2572,7 @@ int fprintHTML(FILE* f, SXML_CHAR* str) { SXML_CHAR* p; int i, n; - + for (p = str, n = 0; *p != NULC; p++) { for (i = 0; HTML_SPECIAL_DICT[i].chr; i++) { if (*p != HTML_SPECIAL_DICT[i].chr) @@ -2454,7 +2588,7 @@ int fprintHTML(FILE* f, SXML_CHAR* str) n++; } } - + return n; } @@ -2463,14 +2597,14 @@ int regstrcmp(SXML_CHAR* str, SXML_CHAR* pattern) SXML_CHAR *p, *s; if (str == NULL && pattern == NULL) - return true; + return TRUE; if (str == NULL || pattern == NULL) - return false; + return FALSE; p = pattern; s = str; - while (true) { + while (TRUE) { switch (*p) { /* Any character matches, go to next one */ case C2SX('?'): @@ -2492,16 +2626,14 @@ int regstrcmp(SXML_CHAR* str, SXML_CHAR* pattern) /* NULL character on pattern has to be matched by 'str' */ case 0: - return *s ? false : true; + return *s ? FALSE : TRUE; default: if (*p == C2SX('\\')) /* Escape character */ p++; if (*p++ != *s++) /* Characters do not match */ - return false; + return FALSE; break; } } - - return false; } diff --git a/src/sxmlc.h b/src/sxmlc.h index 7f0ea0f..9f429de 100644 --- a/src/sxmlc.h +++ b/src/sxmlc.h @@ -124,12 +124,12 @@ extern "C" { #define MEM_INCR_RLA (256*sizeof(SXML_CHAR)) /* Initial buffer size and increment for memory reallocations */ #endif -#ifndef false -#define false 0 +#ifndef FALSE +#define FALSE 0 #endif -#ifndef true -#define true 1 +#ifndef TRUE +#define TRUE 1 #endif #define NULC ((SXML_CHAR)C2SX('\0')) @@ -190,7 +190,7 @@ typedef enum _TagType { typedef struct _XMLAttribute { SXML_CHAR* name; /**< The attribute name. */ SXML_CHAR* value; /**< The attribute value. */ - int active; /**< `true` if the attribute is active. */ + int active; /**< `TRUE` if the attribute is active. */ } XMLAttribute; /* Constant to know whether a struct has been initialized (XMLNode or XMLDoc) @@ -221,13 +221,13 @@ typedef struct _XMLNode { SXML_CHAR* text; /**< Text inside the node, or `NULL` if empty. */ XMLAttribute* attributes; /**< Array of attributes. */ int n_attributes; /**< Number of attributes *in `attributes` array* (might not be the number of *active* attributes). */ - + struct _XMLNode* father; /**< Pointer to father node. `NULL` if root. */ struct _XMLNode** children; /**< Array of children nodes. */ int n_children; /**< Number of nodes *in `children` array* (might not be the number of *active* children). */ - + TagType tag_type; /**< Node type. */ - int active; /**< 'true' to tell that node is active and should be displayed by 'XMLDoc_print_*()'. */ + int active; /**< 'TRUE' to tell that node is active and should be displayed by 'XMLDoc_print_*()'. */ void* user; /**< Pointer for user data associated to the node. */ @@ -254,7 +254,7 @@ typedef enum _BOM_TYPE { BOM_UTF_32BE = 0x0000feff, BOM_UTF_32LE = 0xfffe0000 } BOM_TYPE; - + /** * \brief An XML document, basically an array of `XMLNode`. * @@ -438,7 +438,7 @@ typedef struct _SAX_Callbacks { /** * \brief Helper function to initialize all `sax` members to `NULL`. * \param sax The callbacks structure to initialize. - * \return `false` is `sax` is NULL. + * \return `FALSE` is `sax` is NULL. */ int SAX_Callbacks_init(SAX_Callbacks* sax); @@ -561,15 +561,15 @@ int XMLNode_free(XMLNode* node); * \brief Copy a node to another one, optionally including its children. * \param dst The node receiving the copy. N.B. thtat the node is freed first! * \param src The node to duplicate. If `NULL`, `dst` is freed and initialized. - * \param copy_children `true` to include `src` children (recursive copy). - * \return `false` in case of memory error or if `dst` is `NULL` or `src` uninitialized. + * \param copy_children `TRUE` to include `src` children (recursive copy). + * \return `FALSE` in case of memory error or if `dst` is `NULL` or `src` uninitialized. */ int XMLNode_copy(XMLNode* dst, const XMLNode* src, int copy_children); /** * \brief Duplicate a node, potentially with its children. * \param node The node to duplicate. - * \param copy_children `true` to include `src` children (recursive copy). + * \param copy_children `TRUE` to include `src` children (recursive copy). * \return `NULL` if not enough memory, or a pointer to the new node otherwise. */ XMLNode* XMLNode_dup(const XMLNode* node, int copy_children); @@ -577,7 +577,7 @@ XMLNode* XMLNode_dup(const XMLNode* node, int copy_children); /** * \brief Set the active/inactive state of `node`. * - * Set `active` to `true` to activate `node` and all its children, and enable its use + * Set `active` to `TRUE` to activate `node` and all its children, and enable its use * in other functions (e.g. `XMLDoc_print()`, ...). */ int XMLNode_set_active(XMLNode* node, int active); @@ -586,13 +586,13 @@ int XMLNode_set_active(XMLNode* node, int active); * \brief Set node tag. * \param node The node to set. * \param tag The tag to set in `node`. A *copy* of `tag` will be assigned to `node->tag`, using `strdup()`. - * \return `false` for memory error, `true` otherwise. + * \return `FALSE` for memory error, `TRUE` otherwise. */ int XMLNode_set_tag(XMLNode* node, const SXML_CHAR* tag); /** * \brief Set the node type to one of `TagType` or any user-registered tag. - * \return 'false' when the node or the 'tag_type' is invalid. + * \return 'FALSE' when the node or the 'tag_type' is invalid. */ int XMLNode_set_type(XMLNode* node, const TagType tag_type); @@ -613,7 +613,7 @@ int XMLNode_set_attribute(XMLNode* node, const SXML_CHAR* attr_name, const SXML_ * \param attr_value A pointer receiving a *copy* of the attribute value (from `strdup()`). * \param default_attr_value If `attr_name` does not exist in `node`, a *copy* (from `strdup()`) * of this string will be stored in `attr_value`. - * \return `false` when the `node` is invalid, `attr_name` is NULL or empty, or `attr_value` is NULL. + * \return `FALSE` when the `node` is invalid, `attr_name` is NULL or empty, or `attr_value` is NULL. */ int XMLNode_get_attribute_with_default(XMLNode* node, const SXML_CHAR* attr_name, const SXML_CHAR** attr_value, const SXML_CHAR* default_attr_value); @@ -647,7 +647,7 @@ int XMLNode_remove_all_attributes(XMLNode* node); /** * Set node text to a copy of `text` (from `strdup()`), or remove text if set to `NULL`. - * \return `true` when successful, `false` on error. + * \return `TRUE` when successful, `FALSE` on error. */ int XMLNode_set_text(XMLNode* node, const SXML_CHAR* text); @@ -659,7 +659,7 @@ int XMLNode_set_text(XMLNode* node, const SXML_CHAR* text); /** * \brief Add a child to a node. - * \return `false` for memory problem, `true` otherwise. + * \return `FALSE` for memory problem, `TRUE` otherwise. */ int XMLNode_add_child(XMLNode* node, XMLNode* child); @@ -669,7 +669,7 @@ int XMLNode_add_child(XMLNode* node, XMLNode* child); * \param child The node to insert. * \param index The insert position: if `index <= 0`: will be the first child (0). * If `index >= child->father->n_children`: will be the last child. - * \return 'false' if 'node' is not initialized, 'true' otherwise. + * \return 'FALSE' if 'node' is not initialized, 'TRUE' otherwise. */ int XMLNode_insert_child(XMLNode* node, XMLNode* child, int index); @@ -690,7 +690,7 @@ int XMLNode_insert_child(XMLNode* node, XMLNode* child, int index); * \param from Position of the node to move. * \param to Position to move to. Moved to first position if `to <= 0` or last position * if `to >= node->n_children`. - * \return `false` if `node` is not initialized or `from` is invalid. `true` otherwise. + * \return `FALSE` if `node` is not initialized or `from` is invalid. `TRUE` otherwise. */ int XMLNode_move_child(XMLNode* node, int from, int to); @@ -723,8 +723,8 @@ XMLNode* XMLNode_get_child(const XMLNode* node, int i_child); * \brief Remove the `i_child`th *active* child of the node. * \param node The node. * \param i_child The active node index to retrieve. - * \param free_child if `true`, free the child node itself (and its children, recursively). - * This parameter is usually `true` but should be `false` when child nodes are pointers + * \param free_child if `TRUE`, free the child node itself (and its children, recursively). + * This parameter is usually `TRUE` but should be `FALSE` when child nodes are pointers * to local or global variables instead of user-allocated memory. * \return the new number of children or -1 on invalid arguments. */ @@ -733,14 +733,14 @@ int XMLNode_remove_child(XMLNode* node, int i_child, int free_child); /** * \brief Remove (and frees) all children from the node. * \param node The node. - * \return `true`. + * \return `TRUE`. */ int XMLNode_remove_children(XMLNode* node); /** * \param node1 The first node to test. * \param node2 The second node to test. - * \return `true` if `node1` is the same as `node2` (i.e. same tag, same active attributes) + * \return `TRUE` if `node1` is the same as `node2` (i.e. same tag, same active attributes) * but *not necessarily* the same children. */ int XMLNode_equal(const XMLNode* node1, const XMLNode* node2); @@ -768,14 +768,14 @@ XMLNode* XMLNode_next(const XMLNode* node); /** * \brief Initializes an already-allocated XML document. * \param doc The document to initialize. - * \return `false` if `doc` is NULL. + * \return `FALSE` if `doc` is NULL. */ int XMLDoc_init(XMLDoc* doc); /** * \brief Free an XML document, including all of its nodes, recursively. * \param doc The document to initialize. - * \return `false` if `doc` was not initialized. + * \return `FALSE` if `doc` was not initialized. */ int XMLDoc_free(XMLDoc* doc); @@ -783,7 +783,7 @@ int XMLDoc_free(XMLDoc* doc); * \brief Set the new document root node. * \param doc The document to initialize. * \param i_root The element index to set as root. - * \return `false` if `doc` is not initialized or `i_root` is invalid, `true` otherwise. + * \return `FALSE` if `doc` is not initialized or `i_root` is invalid, `TRUE` otherwise. */ int XMLDoc_set_root(XMLDoc* doc, int i_root); @@ -801,10 +801,10 @@ int XMLDoc_add_node(XMLDoc* doc, XMLNode* node); * \brief Remove a node from the document root nodes. Inactive nodes can be removed like this. * \param doc The XML document. * \param i_node The node index to remove - * \param free_node if `true`, free the node itself. This parameter is usually `true` - * but should be 'false' when the node is a pointer to local or global variable instead of + * \param free_node if `TRUE`, free the node itself. This parameter is usually `TRUE` + * but should be 'FALSE' when the node is a pointer to local or global variable instead of * user-allocated memory. - * \return `true` if node was removed or `false` if `doc` or `i_node` is invalid. + * \return `TRUE` if node was removed or `FALSE` if `doc` or `i_node` is invalid. */ int XMLDoc_remove_node(XMLDoc* doc, int i_node, int free_node); @@ -845,7 +845,7 @@ int XMLDoc_remove_node(XMLDoc* doc, int i_node, int free_node); * node remainder will be output to extra lines. * \param nb_char_tab How many characters should be counted for a tab when counting characters * in the line. It usually is 8 or 4, but at least 1. - * \return `false` on invalid arguments (`NULL` `node` or `f`), `true` otherwise. + * \return `FALSE` on invalid arguments (`NULL` `node` or `f`), `TRUE` otherwise. */ int XMLNode_print_attr_sep(const XMLNode* node, FILE* f, const SXML_CHAR* tag_sep, const SXML_CHAR* child_sep, const SXML_CHAR* attr_sep, int keep_text_spaces, int sz_line, int nb_char_tab); @@ -856,7 +856,7 @@ int XMLNode_print_attr_sep(const XMLNode* node, FILE* f, const SXML_CHAR* tag_se /** * \brief Print the node "header": ``, spanning it on several lines if needed. - * \return `false` on invalid arguments (`NULL` `node` or `f`), `true` otherwise. + * \return `FALSE` on invalid arguments (`NULL` `node` or `f`), `TRUE` otherwise. */ int XMLNode_print_header(const XMLNode* node, FILE* f, int sz_line, int nb_char_tab); @@ -873,7 +873,7 @@ int XMLDoc_print_attr_sep(const XMLDoc* doc, FILE* f, const SXML_CHAR* tag_sep, * \param filename The file to parse. * \param doc The document to parse into. * \param text_as_nodes should be non-zero to put text into separate TAG_TEXT nodes. - * \return `false` in case of error (memory or unavailable filename, malformed document), `true` otherwise. + * \return `FALSE` in case of error (memory or unavailable filename, malformed document), `TRUE` otherwise. */ int XMLDoc_parse_file_DOM_text_as_nodes(const SXML_CHAR* filename, XMLDoc* doc, int text_as_nodes); @@ -888,7 +888,7 @@ int XMLDoc_parse_file_DOM_text_as_nodes(const SXML_CHAR* filename, XMLDoc* doc, * \param name The buffer name (to identify several buffers if run concurrently). * \param doc The document to parse into. * \param text_as_nodes should be non-zero to put text into separate TAG_TEXT nodes. - * \return `false` in case of error (memory or unavailable filename, malformed document), `true` otherwise. + * \return `FALSE` in case of error (memory or unavailable filename, malformed document), `TRUE` otherwise. */ int XMLDoc_parse_buffer_DOM_text_as_nodes(const SXML_CHAR* buffer, const SXML_CHAR* name, XMLDoc* doc, int text_as_nodes); @@ -902,8 +902,8 @@ int XMLDoc_parse_buffer_DOM_text_as_nodes(const SXML_CHAR* buffer, const SXML_CH * \param filename The file to parse. * \param sax The SAX callbacks that will be called by the parser on each XML event. * \param user A user-given pointer that will be given back to all callbacks. - * \return `false` in case of error (memory or unavailable filename, malformed document) or when requested - * by a SAX callback. `true` otherwise. + * \return `FALSE` in case of error (memory or unavailable filename, malformed document) or when requested + * by a SAX callback. `TRUE` otherwise. */ int XMLDoc_parse_file_SAX(const SXML_CHAR* filename, const SAX_Callbacks* sax, void* user); @@ -914,15 +914,15 @@ int XMLDoc_parse_file_SAX(const SXML_CHAR* filename, const SAX_Callbacks* sax, v * \param name An optional buffer name. * \param sax The SAX callbacks that will be called by the parser on each XML event. * \param user A user-given pointer that will be given back to all callbacks. - * \return `false` in case of error (memory or unavailable filename, malformed document) or when requested - * by a SAX callback. `true` otherwise. + * \return `FALSE` in case of error (memory or unavailable filename, malformed document) or when requested + * by a SAX callback. `TRUE` otherwise. */ int XMLDoc_parse_buffer_SAX_len(const SXML_CHAR* buffer, int buffer_len, const SXML_CHAR* name, const SAX_Callbacks* sax, void* user); /** * \brief For backward compatibility (buffer length is `strlen(buffer)`). */ -#define XMLDoc_parse_buffer_SAX(buffer, name, sax, user) XMLDoc_parse_buffer_SAX_len(buffer, sx_strlen(buffer), name, sax, user) +#define XMLDoc_parse_buffer_SAX(buffer, name, sax, user) XMLDoc_parse_buffer_SAX_len(buffer, (int)sx_strlen(buffer), name, sax, user) /** * \brief Parse an XML file using the DOM implementation. @@ -1012,13 +1012,13 @@ SXML_CHAR* str_unescape(SXML_CHAR* str); * \param i_sep will contain the index of the separator in `str`, or -1 if not found. * \param r0 will contain the index of the left part first character in `str`. * \param r1 will contain the index of left part last character in `str`. - * \param ignore_spaces Is `true`, computed indexes will not take into account potential + * \param ignore_spaces Is `TRUE`, computed indexes will not take into account potential * spaces around the separator as well as before left part and after right part, so * `"name=val"` will be equivalent to `"name = val"`. - * \param ignore_quotes If `true`, quotes (`"` or `'`) will not be taken into account when parsing left + * \param ignore_quotes If `TRUE`, quotes (`"` or `'`) will not be taken into account when parsing left * and right members, so `"name = 'val'"` will be equivalent to `"name = val"`. * - * \return `false` when `str` is malformed, `true` when splitting was successful. + * \return `FALSE` when `str` is malformed, `TRUE` when splitting was successful. */ int split_left_right(SXML_CHAR* str, SXML_CHAR sep, int* l0, int* l1, int* i_sep, int* r0, int* r1, int ignore_spaces, int ignore_quotes); @@ -1040,7 +1040,7 @@ BOM_TYPE freadBOM(FILE* f, unsigned char* bom, int* sz_bom); * * \param html The string to check. * - * \returns `false` if `html` does not contain the HTML escape character. + * \returns `FALSE` if `html` does not contain the HTML escape character. */ int has_html(SXML_CHAR* html); diff --git a/src/sxmlsearch.c b/src/sxmlsearch.c index 2dd7442..f9fc47d 100644 --- a/src/sxmlsearch.c +++ b/src/sxmlsearch.c @@ -53,7 +53,7 @@ REGEXPR_COMPARE XMLSearch_set_regexpr_compare(REGEXPR_COMPARE fct) int XMLSearch_init(XMLSearch* search) { if (search == NULL) - return false; + return FALSE; search->tag = NULL; search->text = NULL; @@ -63,8 +63,8 @@ int XMLSearch_init(XMLSearch* search) search->prev = NULL; search->stop_at = INVALID_XMLNODE_POINTER; /* Because 'NULL' can be a valid value */ search->init_value = XML_INIT_DONE; - - return true; + + return TRUE; } int XMLSearch_free(XMLSearch* search, int free_next) @@ -72,7 +72,7 @@ int XMLSearch_free(XMLSearch* search, int free_next) int i; if (search == NULL || search->init_value != XML_INIT_DONE) - return false; + return FALSE; if (search->tag != NULL) { __free(search->tag); @@ -97,47 +97,55 @@ int XMLSearch_free(XMLSearch* search, int free_next) } if (free_next && search->next != NULL) { - (void)XMLSearch_free(search->next, true); + (void)XMLSearch_free(search->next, TRUE); __free(search->next); search->next = NULL; } search->init_value = 0; /* Something not XML_INIT_DONE, otherwise we'll go into 'XMLSearch_free' again */ (void)XMLSearch_init(search); - return true; + return TRUE; } int XMLSearch_search_set_tag(XMLSearch* search, const SXML_CHAR* tag) { if (search == NULL) - return false; + return FALSE; if (tag == NULL) { if (search->tag != NULL) { __free(search->tag); search->tag = NULL; } - return true; + return TRUE; } +#ifdef _MSC_VER + search->tag = _strdup(tag); +#else search->tag = sx_strdup(tag); +#endif return (search->tag != NULL); } int XMLSearch_search_set_text(XMLSearch* search, const SXML_CHAR* text) { if (search == NULL) - return false; + return FALSE; if (text == NULL) { if (search->text != NULL) { __free(search->text); search->text = NULL; } - return true; + return TRUE; } +#ifdef _MSC_VER + search->text = _strdup(text); +#else search->text = sx_strdup(text); +#endif return (search->text != NULL); } @@ -154,8 +162,13 @@ int XMLSearch_search_add_attribute(XMLSearch* search, const SXML_CHAR* attr_name if (attr_name == NULL || attr_name[0] == NULC) return -1; +#ifdef _MSC_VER + name = _strdup(attr_name); + value = (attr_value == NULL ? NULL : _strdup(attr_value)); +#else name = sx_strdup(attr_name); value = (attr_value == NULL ? NULL : sx_strdup(attr_value)); +#endif if (name == NULL || (attr_value && value == NULL)) { if (value != NULL) __free(value); @@ -164,7 +177,7 @@ int XMLSearch_search_add_attribute(XMLSearch* search, const SXML_CHAR* attr_name } i = search->n_attributes; - pt = (XMLAttribute*)__realloc(search->attributes, (i + 1) * sizeof(XMLAttribute)); + pt = (XMLAttribute*)__realloc(search->attributes, (size_t)(i + 1) * sizeof(XMLAttribute)); if (pt == NULL) { if (value) __free(value); @@ -208,7 +221,7 @@ int XMLSearch_search_remove_attribute(XMLSearch* search, int i_attr) if (search->n_attributes == 1) pt = NULL; else { - pt = (XMLAttribute*)__malloc((search->n_attributes - 1) * sizeof(XMLAttribute)); + pt = (XMLAttribute*)__malloc((size_t)(search->n_attributes - 1) * sizeof(XMLAttribute)); if (pt == NULL) return -1; } @@ -219,7 +232,7 @@ int XMLSearch_search_remove_attribute(XMLSearch* search, int i_attr) if (pt != NULL) { memcpy(pt, search->attributes, i_attr * sizeof(XMLAttribute)); - memcpy(&pt[i_attr], &search->attributes[i_attr + 1], (search->n_attributes - i_attr - 1) * sizeof(XMLAttribute)); + memcpy(&pt[i_attr], &search->attributes[i_attr + 1], (size_t)(search->n_attributes - i_attr - 1) * sizeof(XMLAttribute)); } if (search->attributes) __free(search->attributes); @@ -232,15 +245,15 @@ int XMLSearch_search_remove_attribute(XMLSearch* search, int i_attr) int XMLSearch_search_set_children_search(XMLSearch* search, XMLSearch* children_search) { if (search == NULL) - return false; + return FALSE; if (search->next != NULL) - XMLSearch_free(search->next, true); + XMLSearch_free(search->next, TRUE); search->next = children_search; children_search->prev = search; - return true; + return TRUE; } SXML_CHAR* XMLSearch_get_XPath_string(const XMLSearch* search, SXML_CHAR** xpath, SXML_CHAR quote) @@ -254,7 +267,11 @@ SXML_CHAR* XMLSearch_get_XPath_string(const XMLSearch* search, SXML_CHAR** xpath /* NULL 'search' is an empty string */ if (search == NULL) { + #ifdef _MSC_VER + *xpath = _strdup(C2SX("")); + #else *xpath = sx_strdup(C2SX("")); + #endif if (*xpath == NULL) return NULL; @@ -270,20 +287,20 @@ SXML_CHAR* XMLSearch_get_XPath_string(const XMLSearch* search, SXML_CHAR** xpath if (s->n_attributes > 0 || (s->text != NULL && s->text[0] != NULC)) if (strcat_alloc(xpath, C2SX("[")) == NULL) goto err; - fill = false; /* '[' has not been filled with text yet, no ", " separator should be added */ + fill = FALSE; /* '[' has not been filled with text yet, no ", " separator should be added */ if (s->text != NULL && s->text[0] != NULC) { if (strcat_alloc(xpath, C2SX(".=")) == NULL) goto err; if (strcat_alloc(xpath, squote) == NULL) goto err; if (strcat_alloc(xpath, s->text) == NULL) goto err; if (strcat_alloc(xpath, squote) == NULL) goto err; - fill = true; + fill = TRUE; } for (i = 0; i < s->n_attributes; i++) { if (fill) { if (strcat_alloc(xpath, C2SX(", ")) == NULL) goto err; } else - fill = true; /* filling is being performed */ + fill = TRUE; /* filling is being performed */ if (strcat_alloc(xpath, C2SX("@")) == NULL) goto err; if (strcat_alloc(xpath, s->attributes[i].name) == NULL) goto err; if (s->attributes[i].value == NULL) continue; @@ -311,7 +328,7 @@ SXML_CHAR* XMLSearch_get_XPath_string(const XMLSearch* search, SXML_CHAR** xpath Extract search information from 'xpath', where 'xpath' represents a single node (i.e. no '/' inside, except escaped ones), stripped from lead and tail '/'. tag[.=text, @attrib="value"] with potential spaces around '=' and ','. - Return 'false' if parsing failed, 'true' for success. + Return 'FALSE' if parsing failed, 'TRUE' for success. This is an internal function so we assume that arguments are valid (non-NULL). */ static int _init_search_from_1XPath(SXML_CHAR* xpath, XMLSearch* search) @@ -330,10 +347,10 @@ static int _init_search_from_1XPath(SXML_CHAR* xpath, XMLSearch* search) ret = XMLSearch_search_set_tag(search, xpath); *p = c; if (!ret) - return false; + return FALSE; if (*p == NULC) - return true; + return TRUE; /* Here, '*p' is '[', we have to parse either text or attribute names/values until ']' */ for (p++; *p && *p != C2SX(']'); p++) { @@ -341,11 +358,11 @@ static int _init_search_from_1XPath(SXML_CHAR* xpath, XMLSearch* search) cc = *q; if (*q == C2SX(',') || *q == C2SX(']')) *q = NULC; - ret = true; + ret = TRUE; switch (*p) { case C2SX('.'): /* '.[ ]=[ ]["']...["']' to search for text */ - if (!split_left_right(p, C2SX('='), &l0, &l1, &is, &r0, &r1, true, true)) - return false; + if (!split_left_right(p, C2SX('='), &l0, &l1, &is, &r0, &r1, TRUE, TRUE)) + return FALSE; c = p[r1+1]; p[r1+1] = NULC; ret = XMLSearch_search_set_text(search, &p[r0]); @@ -355,13 +372,13 @@ static int _init_search_from_1XPath(SXML_CHAR* xpath, XMLSearch* search) /* Attribute name, possibly '@attrib[[ ]=[ ]"value"]' */ case C2SX('@'): - if (!split_left_right(++p, '=', &l0, &l1, &is, &r0, &r1, true, true)) - return false; + if (!split_left_right(++p, '=', &l0, &l1, &is, &r0, &r1, TRUE, TRUE)) + return FALSE; c = p[l1+1]; c1 = p[r1+1]; p[l1+1] = NULC; p[r1+1] = NULC; - ret = (XMLSearch_search_add_attribute(search, &p[l0], (is < 0 ? NULL : &p[r0]), true) < 0 ? false : true); /* 'is' < 0 when there is no '=' (i.e. check for attribute presence only */ + ret = (XMLSearch_search_add_attribute(search, &p[l0], (is < 0 ? NULL : &p[r0]), TRUE) < 0 ? FALSE : TRUE); /* 'is' < 0 when there is no '=' (i.e. check for attribute presence only */ p[l1+1] = c; p[r1+1] = c1; p += r1-1; /* Jump to next value */ @@ -372,10 +389,10 @@ static int _init_search_from_1XPath(SXML_CHAR* xpath, XMLSearch* search) } *q = cc; /* Restore ',' separator if any */ if (!ret) - return false; + return FALSE; } - return true; + return TRUE; } int XMLSearch_init_from_XPath(const SXML_CHAR* xpath, XMLSearch* search) @@ -385,30 +402,34 @@ int XMLSearch_init_from_XPath(const SXML_CHAR* xpath, XMLSearch* search) SXML_CHAR c; if (!XMLSearch_init(search)) - return false; + return FALSE; /* NULL or empty xpath is an empty (initialized only) search */ if (xpath == NULL || *xpath == NULC) - return true; + return TRUE; search1 = NULL; /* Search struct to add the xpath portion to */ search2 = search; /* Search struct to be filled from xpath portion */ +#ifdef _MSC_VER + tag = tag0 = _strdup(xpath); /* Create a copy of 'xpath' to be able to patch it (or segfault if 'xpath' is const, cnacu6o Sergey@sourceforge!) */ +#else tag = tag0 = sx_strdup(xpath); /* Create a copy of 'xpath' to be able to patch it (or segfault if 'xpath' is const, cnacu6o Sergey@sourceforge!) */ +#endif while (*tag != NULC) { if (search2 != search) { /* Allocate a new search when the original one (i.e. 'search') has already been filled */ search2 = (XMLSearch*)__calloc(1, sizeof(XMLSearch)); if (search2 == NULL) { __free(tag0); - (void)XMLSearch_free(search, true); - return false; + (void)XMLSearch_free(search, TRUE); + return FALSE; } } /* Skip all first '/' */ for (; *tag != NULC && *tag == C2SX('/'); tag++) ; if (*tag == NULC) { __free(tag0); - return false; + return FALSE; } /* Look for the end of tag name: after '/' (to get another tag) or end of string */ @@ -420,8 +441,8 @@ int XMLSearch_init_from_XPath(const SXML_CHAR* xpath, XMLSearch* search) *p = NULC; if (!_init_search_from_1XPath(tag, search2)) { __free(tag0); - (void)XMLSearch_free(search, true); - return false; + (void)XMLSearch_free(search, TRUE); + return FALSE; } *p = c; @@ -435,31 +456,31 @@ int XMLSearch_init_from_XPath(const SXML_CHAR* xpath, XMLSearch* search) } __free(tag0); - return true; + return TRUE; } static int _attribute_matches(XMLAttribute* to_test, XMLAttribute* pattern) { if (to_test == NULL && pattern == NULL) - return true; + return TRUE; if (to_test == NULL || pattern == NULL) - return false; - + return FALSE; + /* No test on name => match */ if (pattern->name == NULL || pattern->name[0] == NULC) - return true; + return TRUE; /* Test on name fails => no match */ if (!regstrcmp_search(to_test->name, pattern->name)) - return false; + return FALSE; /* No test on value => match */ if (pattern->value == NULL) - return true; + return TRUE; /* Test on value according to pattern "equal" attribute */ - return regstrcmp_search(to_test->value, pattern->value) == pattern->active ? true : false; + return regstrcmp_search(to_test->value, pattern->value) == pattern->active ? TRUE : FALSE; } int XMLSearch_node_matches(const XMLNode* node, const XMLSearch* search) @@ -467,22 +488,22 @@ int XMLSearch_node_matches(const XMLNode* node, const XMLSearch* search) int i, j; if (node == NULL) - return false; + return FALSE; if (search == NULL) - return true; + return TRUE; /* No comments, prolog, or such type of nodes are tested */ if (node->tag_type != TAG_FATHER && node->tag_type != TAG_SELF) - return false; + return FALSE; /* Check tag */ if (search->tag != NULL && !regstrcmp_search(node->tag, search->tag)) - return false; + return FALSE; /* Check text */ if (search->text != NULL && !regstrcmp_search(node->text, search->text)) - return false; + return FALSE; /* Check attributes */ if (search->attributes != NULL) { @@ -494,7 +515,7 @@ int XMLSearch_node_matches(const XMLNode* node, const XMLSearch* search) break; } if (j >= node->n_attributes) /* All attributes where scanned without a successful match */ - return false; + return FALSE; } } @@ -504,9 +525,9 @@ int XMLSearch_node_matches(const XMLNode* node, const XMLSearch* search) /* TODO: Should a node match if search has no more 'prev' search and node father is still below the initial search ? Depends if XPath started with "//" (=> yes) or "/" (=> no). - if (search->prev == NULL && node->father != search->from) return false; ? */ - - return true; + if (search->prev == NULL && node->father != search->from) return FALSE; ? */ + + return TRUE; } XMLNode* XMLSearch_next(const XMLNode* from, XMLSearch* search) @@ -547,7 +568,7 @@ static SXML_CHAR* _get_XPath(const XMLNode* node, SXML_CHAR** xpath) SXML_CHAR* p; brackets = 0; - sz_xpath = sx_strlen(node->tag); + sz_xpath = (int)sx_strlen(node->tag); if (node->text != NULL) { sz_xpath += strlen_html(node->text) + 4; /* 4 = '.=""' */ brackets = 2; /* Text has to be displayed => add '[]' */ @@ -564,11 +585,23 @@ static SXML_CHAR* _get_XPath(const XMLNode* node, SXML_CHAR** xpath) if (*xpath == NULL) return NULL; +#ifdef _MSC_VER + strcpy_s(*xpath, sz_xpath * sizeof(SXML_CHAR), node->tag); +#else sx_strcpy(*xpath, node->tag); +#endif if (node->text != NULL) { + #ifdef _MSC_VER + strcat_s(*xpath, sz_xpath * sizeof(SXML_CHAR), C2SX("[.=\"")); + #else sx_strcat(*xpath, C2SX("[.=\"")); + #endif (void)str2html(node->text, &(*xpath[sx_strlen(*xpath)])); + #ifdef _MSC_VER + strcat_s(*xpath, sz_xpath * sizeof(SXML_CHAR), C2SX("\"")); + #else sx_strcat(*xpath, C2SX("\"")); + #endif n = 1; /* Indicates '[' has been put */ } else n = 0; @@ -578,24 +611,46 @@ static SXML_CHAR* _get_XPath(const XMLNode* node, SXML_CHAR** xpath) continue; if (n == 0) { + #ifdef _MSC_VER + strcat_s(*xpath, sz_xpath * sizeof(SXML_CHAR), C2SX("[")); + #else sx_strcat(*xpath, C2SX("[")); + #endif n = 1; } else + #ifdef _MSC_VER + strcat_s(*xpath, sz_xpath * sizeof(SXML_CHAR), C2SX(", ")); + p = &(*xpath)[strlen(*xpath)]; + #else sx_strcat(*xpath, C2SX(", ")); - p = &(*xpath)[sx_strlen(*xpath)]; + p = &(*xpath)[sx_strlen(*xpath)]; + #endif /* Standard and Unicode versions of 'sprintf' do not have the same signature! :( */ + #ifdef _MSC_VER + sprintf_s(p, + strlen(p), + #else sx_sprintf(p, + #endif #ifdef SXMLC_UNICODE sz_xpath, #endif C2SX("@%s=%c"), node->attributes[i].name, XML_DEFAULT_QUOTE); (void)str2html(node->attributes[i].value, p); + #ifdef _MSC_VER + strcat_s(*xpath, sz_xpath * sizeof(SXML_CHAR), C2SX("\"")); + #else sx_strcat(*xpath, C2SX("\"")); + #endif } if (n > 0) + #ifdef _MSC_VER + strcat_s(*xpath, sz_xpath * sizeof(SXML_CHAR), C2SX("]")); + #else sx_strcat(*xpath, C2SX("]")); + #endif return *xpath; } @@ -629,7 +684,11 @@ SXML_CHAR* XMLNode_get_XPath(XMLNode* node, SXML_CHAR** xpath, int incl_parents) xp = xparent; parent = parent->father; } while (parent != NULL); +#ifdef _MSC_VER + if ((*xpath = _strdup(C2SX("/"))) == NULL || strcat_alloc(xpath, xp) == NULL) goto xp_err; +#else if ((*xpath = sx_strdup(C2SX("/"))) == NULL || strcat_alloc(xpath, xp) == NULL) goto xp_err; +#endif return *xpath; diff --git a/src/sxmlsearch.h b/src/sxmlsearch.h index c990bbc..c84b6cd 100644 --- a/src/sxmlsearch.h +++ b/src/sxmlsearch.h @@ -51,8 +51,8 @@ typedef struct _XMLSearch { /**< `attribute->name`, no matter what its value is. */ /**< If `attribute->value` is not NULL, a matching node should have an attribute */ /**< `attribute->name` with the corresponding value `attribute->value`. */ - /**< When `attribute->value` is not NULL, the `attribute->active` should be `true` */ - /**< to specify that values should be equal, or `false` to specify that values should */ + /**< When `attribute->value` is not NULL, the `attribute->active` should be `TRUE` */ + /**< to specify that values should be equal, or `FALSE` to specify that values should */ /**< be different. */ int n_attributes; /**< The size of `attributes`array. */ @@ -74,7 +74,7 @@ typedef struct _XMLSearch { * The default regex function can be overriden by user code through `XMLSearch_set_regexpr_compare()`. * \param str The string to match on `pattern`. * \param pattern The pattern to match `str` to. - * \return `true` if `str` matches `pattern`. + * \return `TRUE` if `str` matches `pattern`. */ typedef int (*REGEXPR_COMPARE)(SXML_CHAR* str, SXML_CHAR* pattern); @@ -91,22 +91,22 @@ REGEXPR_COMPARE XMLSearch_set_regexpr_compare(REGEXPR_COMPARE fct); /** * \brief Initialize an empty search. No memory freeing is performed. * \param search The search parameters. - * \return `false` when `search` is NULL. + * \return `FALSE` when `search` is NULL. */ int XMLSearch_init(XMLSearch* search); /** * \brief Free all search members except for the `search->next` member that should be freed - * by its creator, unless `free_next` is `true`. + * by its creator, unless `free_next` is `TRUE`. * - * It is recommended that `free_next` is positioned to `true` only when the creator did not + * It is recommended that `free_next` is positioned to `TRUE` only when the creator did not * handle the whole memory allocation chain, e.g. when using `XMLSearch_init_from_XPath()` * that allocates all search structs. * * \param search The search parameters. - * \param free_next `false` in order *not* to free the `search->next` structures. + * \param free_next `FALSE` in order *not* to free the `search->next` structures. * - * \return `false` when `search` is NULL. + * \return `FALSE` when `search` is NULL. */ int XMLSearch_free(XMLSearch* search, int free_next); @@ -115,7 +115,7 @@ int XMLSearch_free(XMLSearch* search, int free_next); * \param search The search parameters. * \param tag should be NULL or empty to search for any node (e.g. search based on attributes * only). In this case, the previous tag is freed. - * \return `true` upon successful completion, `false` for memory error. + * \return `TRUE` upon successful completion, `FALSE` for memory error. */ int XMLSearch_search_set_tag(XMLSearch* search, const SXML_CHAR* tag); @@ -126,8 +126,8 @@ int XMLSearch_search_set_tag(XMLSearch* search, const SXML_CHAR* tag); * \param attr_value should be NULL to test for attribute presence only * (no test on value). An empty string means the attribute should exist * with an empty value. - * \param value_equal should be specified to test for attribute value equality (`true`) or - * difference (`false`). + * \param value_equal should be specified to test for attribute value equality (`TRUE`) or + * difference (`FALSE`). * \return the index of the new attribute, or -1 for memory error. */ int XMLSearch_search_add_attribute(XMLSearch* search, const SXML_CHAR* attr_name, const SXML_CHAR* attr_value, int value_equal); @@ -154,7 +154,7 @@ int XMLSearch_search_remove_attribute(XMLSearch* search, int i_attr); * \param text should be NULL or empty to search for any node (e.g. search based on attributes * only). In this case, the previous text is freed. * - * \return `true` upon successful completion, `false` for memory error. + * \return `TRUE` upon successful completion, `FALSE` for memory error. */ int XMLSearch_search_set_text(XMLSearch* search, const SXML_CHAR* text); @@ -177,7 +177,7 @@ int XMLSearch_search_set_text(XMLSearch* search, const SXML_CHAR* text); * \param children_search The search parameters to be applied to children of nodes * matching `search`. * - * \return `true` when association has been made, `false` when an error occurred. + * \return `TRUE` when association has been made, `FALSE` when an error occurred. */ int XMLSearch_search_set_children_search(XMLSearch* search, XMLSearch* children_search); @@ -190,7 +190,7 @@ int XMLSearch_search_set_children_search(XMLSearch* search, XMLSearch* children_ * \param quote is the quote character to be used (e.g. `"` or `'`). If '\0', * `XML_DEFAULT_QUOTE` will be used. * - * \return `false` for a memory problem, `true` otherwise. + * \return `FALSE` for a memory problem, `TRUE` otherwise. */ SXML_CHAR* XMLSearch_get_XPath_string(const XMLSearch* search, SXML_CHAR** xpath, SXML_CHAR quote); @@ -204,7 +204,7 @@ SXML_CHAR* XMLSearch_get_XPath_string(const XMLSearch* search, SXML_CHAR** xpath * \param search The search parameters. * * - * \return `true` when `search` was correctly initialized, `false` in case of memory + * \return `TRUE` when `search` was correctly initialized, `FALSE` in case of memory * problem or malformed `xpath`. */ int XMLSearch_init_from_XPath(const SXML_CHAR* xpath, XMLSearch* search); @@ -218,7 +218,7 @@ int XMLSearch_init_from_XPath(const SXML_CHAR* xpath, XMLSearch* search); * \param node The node to test. `tag_type` should be `TAG_FATHER` or `TAG_SELF` only. * \param search The search parameters. * - * \return `false` when `node` does not match or for invalid arguments, `true` + * \return `FALSE` when `node` does not match or for invalid arguments, `TRUE` * if `node` is a match. */ int XMLSearch_node_matches(const XMLNode* node, const XMLSearch* search); @@ -263,7 +263,7 @@ SXML_CHAR* XMLNode_get_XPath(XMLNode* node, SXML_CHAR** xpath, int incl_parents) * \param str The string to check. * \param pattern can use wildcads such as `*` (any potentially empty string) or * `?` (any character) and use `\` as an escape character. - * \returns `true` when `str` matches `pattern`, `false` otherwise. + * \returns `TRUE` when `str` matches `pattern`, `FALSE` otherwise. */ int regstrcmp(SXML_CHAR* str, SXML_CHAR* pattern);