aboutsummaryrefslogtreecommitdiff
path: root/src_c/main.c
blob: 9cc71ed4d747c8c696403eb5dbabd7462fc24de7 (plain) (blame)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"


static int read_stdin(opcut_str_t *json) {
    size_t json_data_size = 0;
    json->data = NULL;
    json->len = 0;

    while (!(json->len < json_data_size)) {
        char *old_json_data = json->data;
        json_data_size += 4096;
        json->data = realloc(json->data, json_data_size);
        if (!json->data) {
            free(old_json_data);
            return OPCUT_ERROR;
        }
        json->len +=
            fread(json->data + json->len, 1, json_data_size - json->len, stdin);
    }

    return OPCUT_SUCCESS;
}


int main(int argc, char **argv) {
    char *method = NULL;
    for (size_t i = 1; i < argc - 1; ++i) {
        if (strcmp("--method", argv[i]) == 0)
            method = argv[++i];
    }

    opcut_str_t json;
    if (!read_stdin(&json))
        return OPCUT_ERROR;

    opcut_params_t params;
    if (!opcut_params_init(&params, &json)) {
        free(json.data);
        return OPCUT_ERROR;
    }

    opcut_result_t result = {.params = &params,
                             .used = NULL,
                             .used_len = 0,
                             .unused = NULL,
                             .unused_len = 0};

    size_t res = opcut_result_write(&result, stdout);

    opcut_params_destroy(&params);
    free(json.data);

    return res;
}