2018-04-24 21:50:49 +02:00
|
|
|
#include "exparse.h"
|
|
|
|
|
2018-04-24 22:04:23 +02:00
|
|
|
#include "util/log.h"
|
|
|
|
|
2018-04-24 21:50:49 +02:00
|
|
|
#include <expat.h>
|
|
|
|
|
|
|
|
int exparse_readfile(const char * filename) {
|
2018-04-24 22:04:23 +02:00
|
|
|
XML_Parser xp;
|
|
|
|
FILE *F;
|
|
|
|
int err = 1;
|
|
|
|
char buf[4096];
|
|
|
|
|
|
|
|
F = fopen(filename, "r");
|
|
|
|
if (!F) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
xp = XML_ParserCreate("UTF-8");
|
|
|
|
for (;;) {
|
|
|
|
size_t len = (int) fread(buf, 1, sizeof(buf), F);
|
|
|
|
int done;
|
|
|
|
|
|
|
|
if (ferror(F)) {
|
|
|
|
log_error("read error in %s", filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
done = feof(F);
|
|
|
|
if (XML_Parse(xp, buf, len, done) == XML_STATUS_ERROR) {
|
|
|
|
log_error("parse error at line %u of %s: %s",
|
|
|
|
XML_GetCurrentLineNumber(xp),
|
|
|
|
filename,
|
|
|
|
XML_ErrorString(XML_GetErrorCode(xp)));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (done) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
XML_ParserFree(xp);
|
|
|
|
fclose(F);
|
|
|
|
return err;
|
2018-04-24 21:50:49 +02:00
|
|
|
}
|