summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2014-11-14 23:07:28 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2014-11-15 18:34:31 -0800
commit8f523b0149f250f733d80357320f92ffbe2a8b8f (patch)
tree1df9572e60d88795ee992089eaf46b0fd7987ce1 /src/main.c
parent694fa11266741aa061477aaca627e0445ba20723 (diff)
Expose lower-level parsing API.
The new functions cmark_new_doc_parser, cmark_free_doc_parser, cmark_process_line, and cmark_finish allow you to feed lines one by one (possibly from several files) to the parser and call finish when you're done. This is now used in main for mulitple files.
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c70
1 files changed, 46 insertions, 24 deletions
diff --git a/src/main.c b/src/main.c
index 210328a..20a708b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -4,6 +4,7 @@
#include <errno.h>
#include "cmark.h"
#include "buffer.h"
+#include "debug.h"
#include "bench.h"
void print_usage()
@@ -27,23 +28,17 @@ static void print_document(node_block *document, bool ast)
}
}
-void parse_and_render(node_block *document, FILE *fp, bool ast)
-{
- document = cmark_parse_file(fp);
- start_timer();
- print_document(document, ast);
- end_timer("print_document");
- start_timer();
- cmark_free_blocks(document);
- end_timer("free_blocks");
-}
-
int main(int argc, char *argv[])
{
int i, numfps = 0;
bool ast = false;
int files[argc];
- node_block *document = NULL;
+ unsigned char buffer[4096];
+ cmark_doc_parser *parser;
+ size_t offset;
+ node_block *document;
+
+ parser = cmark_new_doc_parser();
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "--version") == 0) {
@@ -64,22 +59,49 @@ int main(int argc, char *argv[])
}
}
- if (numfps == 0) {
- parse_and_render(document, stdin, ast);
- } else {
- for (i = 0; i < numfps; i++) {
- FILE *fp = fopen(argv[files[i]], "r");
+ for (i = 0; i < numfps; i++) {
+ FILE *fp = fopen(argv[files[i]], "r");
+ if (fp == NULL) {
+ fprintf(stderr, "Error opening file %s: %s\n",
+ argv[files[i]], strerror(errno));
+ exit(1);
+ }
- if (fp == NULL) {
- fprintf(stderr, "Error opening file %s: %s\n",
- argv[files[i]], strerror(errno));
- exit(1);
- }
+ start_timer();
+ while (fgets((char *)buffer, sizeof(buffer), fp)) {
+ offset = strlen((char *)buffer);
+ cmark_process_line(parser, buffer, offset);
+ }
+ end_timer("processing lines");
- parse_and_render(document, fp, ast);
- fclose(fp);
+ fclose(fp);
+ }
+
+ if (numfps == 0) {
+ /*
+ document = cmark_parse_file(stdin);
+ print_document(document, ast);
+ exit(0);
+ */
+
+ while (fgets((char *)buffer, sizeof(buffer), stdin)) {
+ offset = strlen((char *)buffer);
+ cmark_process_line(parser, buffer, offset);
}
}
+ start_timer();
+ document = cmark_finish(parser);
+ end_timer("finishing document");
+ cmark_free_doc_parser(parser);
+
+ start_timer();
+ print_document(document, ast);
+ end_timer("print_document");
+
+ start_timer();
+ cmark_free_blocks(document);
+ end_timer("free_blocks");
+
return 0;
}