summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: b914682d6c5f47f49eb32688c2323d7546185d10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <matte/logger.h>
#include <matte/types.h>
#include <matte/list.h>
#include "code_point.h"
#include "html_token.hpp"
#include "tokenizer.hpp"
#include <argp.h>

using namespace matte;

const char *argp_program_version = "html_parser 0.1";
const char *argp_program_bug_address = "<matthew@matthewkosarek.xyz>";
static char doc[] = "A description of your program.";
static char args_doc[] = "-f [FILENAME]...";
static struct argp_option options[] = { 
    { "file", 'f', "FILE", 0, "File to parse."},
    { 0 } 
};

struct Arguments {
    char* filename;
};

static error_t parse_opt(int key, char *arg, struct argp_state *state) {
    Arguments* a = (Arguments*)state->input;
    switch (key) {
    case 'f': {
        a->filename = arg;
        break;
    }
    case ARGP_KEY_ARG:
        return 0;
    default:
        return ARGP_ERR_UNKNOWN;
    }
    return 0;
}

static struct argp argp = { options, parse_opt, args_doc, doc, 0, 0, 0 };

int main(int argc, char *argv[]) {
    Arguments arguments;
    arguments.filename = nullptr;
    auto error = argp_parse(&argp, argc, argv, 0, 0, &arguments);

    if (arguments.filename == nullptr) {
        exit(EXIT_FAILURE);
    }

    FILE* file = fopen(arguments.filename, "rb");
    if (file == NULL) {
        exit(EXIT_FAILURE);
    }

    code_point_t wc;
    code_point_t buffer[1024];
    size_t ptr = 0;
    while ((wc=fgetwc(file))!=WEOF) {
        buffer[ptr++] = wc;
    }

    buffer[ptr] = '\0';
    fclose(file);

    Tokenizer tokenizer = create(buffer);
    while (true) {
        auto token = read_next(&tokenizer);
        token.print();
        if (token.type == HtmlTokenType_EOF) {
            break;
        }
    }

    return EXIT_SUCCESS;
}