diff --git a/example.c b/example.c index 4a47c93..0934321 100644 --- a/example.c +++ b/example.c @@ -49,7 +49,7 @@ int main(int argc, char** argv) { xl_node_t* n; for(i = 1; i < argc; i++) { - xemil_t* h = xl_open_file(argv[i]); + xemil_t* h = xl_open_file(argv[i]); h->new_text = 1; if(h != NULL) { printf("%s:\n", argv[i]); diff --git a/include/xemil.h b/include/xemil.h index 33961aa..1d5359a 100644 --- a/include/xemil.h +++ b/include/xemil.h @@ -5,6 +5,7 @@ #include #include #include +#include #define XLDECL extern @@ -55,7 +56,7 @@ struct xemil { xl_driver_t* driver; void* drv_opaque; void* drv_arg; - int new_text; + int new_text; xl_node_t* pre; xl_node_t* root; }; @@ -66,12 +67,9 @@ extern "C" { /* core.c */ XLDECL xemil_t* xl_open(xl_driver_t* driver, void* arg); - -XLDECL int xl_parse(xemil_t* handle); - -XLDECL void xl_close(xemil_t* handle); - -XLDECL char* xl_get_attribute(xl_node_t* node, const char* key); +XLDECL int xl_parse(xemil_t* handle); +XLDECL void xl_close(xemil_t* handle); +XLDECL char* xl_get_attribute(xl_node_t* node, const char* key); /* file.c */ XLDECL xl_driver_t* xl_driver_file; @@ -80,23 +78,18 @@ XLDECL xemil_t* xl_open_file(const char* filename); /* util.c */ XLDECL char* xl_util_trim(const char* str); - XLDECL char* xl_util_strdup(const char* str); +XLDECL char* xl_util_strvacat(const char* str, ...); /* unicode.c */ XLDECL int xl_unicode_count(unsigned char c); - XLDECL int xl_unicode_8_to_32(const char* input, int* output); - XLDECL int xl_unicode_32_to_8(const int input, char* output); /* array.c */ XLDECL void xl_array_push(int** array, int value); - -XLDECL int xl_array_length(int** array); - +XLDECL int xl_array_length(int** array); XLDECL void xl_array_pop(int** array); - XLDECL void xl_array_free(int** array); #ifdef __cplusplus diff --git a/src/core.c b/src/core.c index bb066f6..b5c246d 100644 --- a/src/core.c +++ b/src/core.c @@ -206,22 +206,22 @@ int xl_parse(xemil_t* handle) { n->text[strlen(n->text) - 2] = 0; } } else if(strlen(node) > 8 && memcmp(&node[1], "[CDATA[", 7) == 0 && node[strlen(node) - 1] == ']' && node[strlen(node) - 2] == ']') { - char* old; - char* text; + char* old; + char* text; xl_node_t* last; - if(handle->new_text){ + if(handle->new_text) { old = NULL; - + last = current->first_child; - if(last != NULL){ + if(last != NULL) { while(last->next != NULL) last = last->next; - - if(last->type == XL_NODE_TEXT){ + + if(last->type == XL_NODE_TEXT) { old = last->text; } } - }else{ + } else { old = current->text; } @@ -238,30 +238,33 @@ int xl_parse(xemil_t* handle) { text[strlen(text) - 2] = 0; } - if(handle->new_text){ - if(last == NULL){ + if(handle->new_text) { + if(last == NULL) { xl_node_t* n; - + last = malloc(sizeof(*last)); - + memset(last, 0, sizeof(*last)); - - last->type = XL_NODE_TEXT; + + last->type = XL_NODE_TEXT; last->parent = current; - + n = current->first_child; - if(n == NULL){ + if(n == NULL) { current->first_child = last; - }else{ + } else { while(n->next != NULL) n = n->next; - - n->next = last; + + n->next = last; last->prev = n; } } - + last->text = text; - }else{ + + if(current->text != NULL) free(current->text); + current->text = xl_util_strdup(text); + } else { current->text = text; } } else { @@ -283,11 +286,15 @@ int xl_parse(xemil_t* handle) { nest_level--; - if(handle->new_text){ + if(handle->new_text) { xl_node_t* n = current->first_child; - while(n != NULL){ - if(n->type == XL_NODE_TEXT){ - if(n->text != NULL){ + + if(current->text != NULL) free(current->text); + current->text = NULL; + + while(n != NULL) { + if(n->type == XL_NODE_TEXT) { + if(n->text != NULL) { str = xl_util_trim(n->text); free(n->text); n->text = str; @@ -307,13 +314,21 @@ int xl_parse(xemil_t* handle) { if(n != NULL && n->prev == NULL && n->parent != NULL) n->parent->first_child = n; continue; + } else { + char* old = current->text; + if(old == NULL) { + current->text = xl_util_strdup(n->text); + } else { + current->text = xl_util_strvacat(old, "\n", n->text, NULL); + free(old); + } } } } n = n->next; } - }else{ + } else { if(current->text != NULL) { str = xl_util_trim(current->text); free(current->text); @@ -469,24 +484,24 @@ int xl_parse(xemil_t* handle) { TAKE_AS_NODE(cp); } else if(STATE == STATE_INITIAL) { if(current != NULL) { - char* text; - char* old; + char* text; + char* old; xl_node_t* last; - if(handle->new_text){ + if(handle->new_text) { old = NULL; last = current->first_child; - if(last != NULL){ + if(last != NULL) { while(last->next != NULL) last = last->next; - if(last->type == XL_NODE_TEXT){ + if(last->type == XL_NODE_TEXT) { old = last->text; - }else{ + } else { last = NULL; } } - }else{ + } else { old = current->text; } @@ -509,30 +524,30 @@ int xl_parse(xemil_t* handle) { text[new] = 0; } - if(handle->new_text){ - if(last == NULL){ + if(handle->new_text) { + if(last == NULL) { xl_node_t* n; last = malloc(sizeof(*last)); memset(last, 0, sizeof(*last)); - last->type = XL_NODE_TEXT; + last->type = XL_NODE_TEXT; last->parent = current; n = current->first_child; - if(n == NULL){ + if(n == NULL) { current->first_child = last; - }else{ + } else { while(n->next != NULL) n = n->next; - n->next = last; + n->next = last; last->prev = n; } } last->text = text; - }else{ + } else { current->text = text; } } @@ -574,10 +589,10 @@ void xl_close(xemil_t* handle) { free(handle); } -char* xl_get_attribute(xl_node_t* node, const char* key){ +char* xl_get_attribute(xl_node_t* node, const char* key) { xl_attribute_t* attr = node->first_attribute; - while(attr != NULL){ + while(attr != NULL) { if(strcmp(attr->key, key) == 0) return attr->value; attr = attr->next; diff --git a/src/util.c b/src/util.c index 59e2b9a..fa55c08 100644 --- a/src/util.c +++ b/src/util.c @@ -75,10 +75,31 @@ char* xl_util_trim(const char* str) { return s; } -char* xl_util_strdup(const char* str){ +char* xl_util_strdup(const char* str) { char* s = malloc(strlen(str) + 1); strcpy(s, str); return s; } + +char* xl_util_strvacat(const char* str, ...) { + va_list va; + int len = 0; + const char* s; + char* r; + + len += strlen(str); + va_start(va, str); + while((s = va_arg(va, const char*)) != NULL) len += strlen(s); + va_end(va); + + r = malloc(len + 1); + + strcpy(r, str); + va_start(va, str); + while((s = va_arg(va, const char*)) != NULL) strcat(r, s); + va_end(va); + + return r; +}