X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=utils%2Fxmlgrep%2Fxmlgrep.c;h=23577b0c8d2415a25343817e413c1dcd33836cf1;hb=a89a28c4e62a63e05b6b889274fa848ea2bda007;hp=f163620e1692bd86f397047b7366b2981a0fb626;hpb=0654531fff30a47bc47ec6eea11812b856f05f66;p=flightgear.git diff --git a/utils/xmlgrep/xmlgrep.c b/utils/xmlgrep/xmlgrep.c index f163620e1..23577b0c8 100644 --- a/utils/xmlgrep/xmlgrep.c +++ b/utils/xmlgrep/xmlgrep.c @@ -5,11 +5,15 @@ #include #ifndef _MSC_VER # include +# include /* read */ #else # define strncasecmp strnicmp # include +# include #endif #include +#include /* fstat */ +#include /* open */ #include "xml.h" @@ -26,6 +30,7 @@ static int print_filenames = 0; static void free_and_exit(int i); +#define USE_BUFFER 0 #define NODE_NAME_LEN 256 #define STRING_LEN 2048 @@ -117,6 +122,7 @@ parse_option(char **args, int n, int max) { if (arg == 0) SHOW_NOVAL(opt); alen = strlen(arg)+1; + if (_root) free(_root); _root = malloc(alen); memcpy(_root, arg, alen); return 2; @@ -125,6 +131,7 @@ parse_option(char **args, int n, int max) { if (arg == 0) SHOW_NOVAL(opt); alen = strlen(arg)+1; + if (_element) free(_element); _element = malloc(alen); memcpy(_element, arg, alen); return 2; @@ -133,6 +140,7 @@ parse_option(char **args, int n, int max) { if (arg == 0) SHOW_NOVAL(opt); alen = strlen(arg)+1; + if (_value) free(_value); _value = malloc(alen); memcpy(_value, arg, alen); return 2; @@ -141,6 +149,7 @@ parse_option(char **args, int n, int max) { if (arg == 0) SHOW_NOVAL(opt); alen = strlen(arg)+1; + if (_print) free(_print); _print = malloc(alen); memcpy(_print, arg, alen); return 2; @@ -149,6 +158,7 @@ parse_option(char **args, int n, int max) { if (arg == 0) SHOW_NOVAL(opt); alen = strlen(arg)+1; + if (_attribute) free(_attribute); _attribute = malloc(alen); memcpy(_attribute, arg, alen); return 2; @@ -203,17 +213,23 @@ void walk_the_tree(size_t num, void *xid, char *tree) { if (xmlNodeGetPos(xid, xmid, _print, i) != 0) { - char value[STRING_LEN]; + char *value; - xmlCopyString(xmid, (char *)&value, STRING_LEN); - if (_value && _attribute) + value = xmlGetString(xmid); + if (_value && _attribute && value) { +#if 1 + char *a = xmlAttributeGetString(xmid, _attribute); + if (a && !strcmp(a, _value)) +#else if (!xmlAttributeCompareString(xmid, _attribute, _value)) +#endif { printf("%s: <%s %s=\"%s\">%s\n", _filenames[num], _print, _attribute, _value, value, _print); } + if (value) free(value); } else { @@ -306,7 +322,6 @@ void walk_the_tree(size_t num, void *xid, char *tree) } } - void grep_file(unsigned num) { void *xid; @@ -323,8 +338,9 @@ void grep_file(unsigned num) if (r) { size_t n = xmlErrorGetLineNo(xrid, 0); - char *s = xmlErrorGetString(xrid, 1); /* clear the error */ - printf("%s: at line %u: '%s'\n",_filenames[num], n, s); + size_t c = xmlErrorGetColumnNo(xrid, 0); + const char *s = xmlErrorGetString(xrid, 1); /* clear the error */ + printf("%s: at line %u, column %u: '%s'\n",_filenames[num], n,c, s); } free(xrid); @@ -337,9 +353,68 @@ void grep_file(unsigned num) xmlClose(xid); } + +void grep_file_buffer(unsigned num) +{ + struct stat st; + void *xid, *buf; + int fd, res; + + fd = open(_filenames[num], O_RDONLY); + if (fd == -1) + { + printf("read error opening file '%s'\n", _filenames[num]); + return; + } + + fstat(fd, &st); + buf = malloc(st.st_size); + if (!buf) + { + printf("unable to allocate enough memory for reading.\n"); + return; + } + + res = read(fd, buf, st.st_size); + if (res == -1) + { + printf("unable to read from file '%s'.\n", _filenames[num]); + return; + } + close(fd); + + xid = xmlInitBuffer(buf, st.st_size); + if (xid) + { + void *xrid = xmlMarkId(xid); + int r = 0; + + walk_the_tree(num, xrid, _root); + + r = xmlErrorGetNo(xrid, 0); + if (r) + { + size_t n = xmlErrorGetLineNo(xrid, 0); + size_t c = xmlErrorGetColumnNo(xrid, 0); + const char *s = xmlErrorGetString(xrid, 1); /* clear the error */ + printf("%s: at line %u, column %u: '%s'\n",_filenames[num], n,c, s); + } + + free(xrid); + } + else + { + fprintf(stderr, "Error reading file '%s'\n", _filenames[num]); + } + + xmlClose(xid); + free(buf); +} + int main (int argc, char **argv) { + unsigned int u; int i; if (argc == 1) @@ -354,8 +429,12 @@ main (int argc, char **argv) if (_root == 0) _root = (char *)_static_root; if (_element == 0) _element = (char *)_static_element; - for (i=0; i<_fcount; i++) - grep_file(i); + for (u=0; u<_fcount; u++) +#if USE_BUFFER + grep_file_buffer(u); +#else + grep_file(u); +#endif free_and_exit(0);