{
struct _root_id *rid = 0;
- if (buffer && (size>0))
+ if (buffer && (size > 0))
{
- rid->fd = -1;
- rid->start = (char *)buffer;
- rid->len = size;
- rid->name = 0;
+ rid = malloc(sizeof(struct _root_id));
+ if (rid)
+ {
+ rid->fd = -1;
+ rid->start = (char *)buffer;
+ rid->len = size;
+ rid->name = 0;
#ifndef XML_NONVALIDATING
- rid->info = 0;
+ rid->info = 0;
#endif
+ }
}
return (void *)rid;
assert(rid != 0);
assert(rid->name == 0);
- if (rid->fd == -1)
+ if (rid->fd != -1)
{
munmap(rid->start, rid->len);
close(rid->fd);
# include <stdlib.h>
#endif
#include <assert.h>
+#include <unistd.h> /* read */
+#include <sys/stat.h> /* fstat */
+#include <fcntl.h> /* open */
#include "xml.h"
static void free_and_exit(int i);
+#define USE_BUFFER 0
#define NODE_NAME_LEN 256
#define STRING_LEN 2048
}
}
-
void grep_file(unsigned num)
{
void *xid;
{
size_t n = xmlErrorGetLineNo(xrid, 0);
size_t c = xmlErrorGetColumnNo(xrid, 0);
- char *s = xmlErrorGetString(xrid, 1); /* clear the error */
+ const char *s = xmlErrorGetString(xrid, 1); /* clear the error */
printf("%s: at line %u, column %u: '%s'\n",_filenames[num], n,c, s);
}
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)
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);