diff options
author | John Mark Bell <jmb@netsurf-browser.org> | 2008-07-30 20:05:15 +0000 |
---|---|---|
committer | John Mark Bell <jmb@netsurf-browser.org> | 2008-07-30 20:05:15 +0000 |
commit | a324baae03e4018c2c1aa8e4692505f8fcbe7172 (patch) | |
tree | 1c0ee3e41d2284347c87a12682f023d9c83416dc /test/parse.c | |
parent | e697509c239a4dd22e4543aca5308b57b248e4f3 (diff) | |
download | libcss-a324baae03e4018c2c1aa8e4692505f8fcbe7172.tar.gz libcss-a324baae03e4018c2c1aa8e4692505f8fcbe7172.tar.bz2 |
Implement the first-stage parser.
Things missing:
1) Recovery after parse errors
2) Event emission (the locations of DEBUG_EVENTS are about right for this)
3) A second-stage parser
4) Any kind of testsuite
svn path=/trunk/libcss/; revision=4825
Diffstat (limited to 'test/parse.c')
-rw-r--r-- | test/parse.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/test/parse.c b/test/parse.c new file mode 100644 index 0000000..f24aa76 --- /dev/null +++ b/test/parse.c @@ -0,0 +1,79 @@ +#include <inttypes.h> +#include <stdio.h> + +#include <libcss/libcss.h> + +#include "charset/detect.h" +#include "utils/utils.h" + +#include "parse/parse.h" + +#include "testutils.h" + +static void *myrealloc(void *ptr, size_t len, void *pw) +{ + UNUSED(pw); + + return realloc(ptr, len); +} + +int main(int argc, char **argv) +{ + css_parser *parser; + FILE *fp; + size_t len, origlen; +#define CHUNK_SIZE (4096) + uint8_t buf[CHUNK_SIZE]; + css_error error; + + if (argc != 3) { + printf("Usage: %s <aliases_file> <filename>\n", argv[0]); + return 1; + } + + /* Initialise library */ + assert(css_initialise(argv[1], myrealloc, NULL) == CSS_OK); + + parser = css_parser_create((css_stylesheet *) 10, + "UTF-8", CSS_CHARSET_DICTATED, myrealloc, NULL); + assert(parser != NULL); + + fp = fopen(argv[2], "rb"); + if (fp == NULL) { + printf("Failed opening %s\n", argv[2]); + return 1; + } + + fseek(fp, 0, SEEK_END); + origlen = len = ftell(fp); + fseek(fp, 0, SEEK_SET); + + while (len >= CHUNK_SIZE) { + fread(buf, 1, CHUNK_SIZE, fp); + + error = css_parser_parse_chunk(parser, buf, CHUNK_SIZE); + assert(error == CSS_OK || error == CSS_NEEDDATA); + } + + if (len > 0) { + fread(buf, 1, len, fp); + + error = css_parser_parse_chunk(parser, buf, len); + assert(error == CSS_OK || error == CSS_NEEDDATA); + + len = 0; + } + + fclose(fp); + + assert(css_parser_completed(parser) == CSS_OK); + + css_parser_destroy(parser); + + assert(css_finalise(myrealloc, NULL) == CSS_OK); + + printf("PASS\n"); + + return 0; +} + |