summaryrefslogtreecommitdiff
path: root/tools/csv2bin/csv2bin.c
blob: 5c8b5111b5f65fdd724ea4b43a6f5537c6653ae9 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>

char * ReadWholeFile(char * filename) {
    FILE * infile = fopen(filename, "rb");
    if (infile == NULL) return NULL;
    fseek(infile, 0, SEEK_END);
    size_t fsize = ftell(infile);
    fseek(infile, 0, SEEK_SET);
    char * ret = malloc(fsize);
    if (ret != NULL) {
        if (fread(ret, 1, fsize, infile) != fsize) {
            free(ret);
            ret = NULL;
        }
    }
    fclose(infile);
    return ret;
}

int main(int argc, char ** argv) {
    int width = 4;
    char * buffer = NULL;
    char * endptr = NULL;
    FILE * outfile = NULL;
    char * infname = NULL;
    for (int i = 1; i < argc; i++) {
        if (strcmp(argv[i], "-w") == 0 || strcmp(argv[i], "--width") == 0) {
            ++i;
            width = strtol(argv[i], &endptr, 10);
            if (width == 0) {
                fprintf(stderr, "invalid integer value for %s: %s\n", argv[i - 1], argv[i]);
                return EXIT_FAILURE;
            }
        } else if (strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--output") == 0) {
            ++i;
            outfile = fopen(argv[i], "wb");
            if (outfile == NULL) {
                fprintf(stderr, "unable to open file '%s' for writing\n", argv[i]);
                return EXIT_FAILURE;
            }
        } else {
            if (buffer == NULL)
            {
                buffer = ReadWholeFile(argv[i]);
            }
            if (buffer == NULL)
            {
                fprintf(stderr, "invalid argument: %s\n", argv[i]);
                return EXIT_FAILURE;
            }
            infname = argv[i];
        }
    }
    if (buffer == NULL) {
        fprintf(stderr, "missing required argument: INFILE\n");
        return EXIT_FAILURE;
    }
    if (outfile == NULL) {
        char * infname_ext = strrchr(infname, '.');
        char * outfname = malloc(infname_ext - infname + 5);
        char * outfname_ext = stpncpy(outfname, argv[1], infname_ext - argv[1]);
        strcpy(outfname_ext, ".bin");
        outfile = fopen(outfname, "wb");
        if (outfile == NULL) {
            fprintf(stderr, "unable to open file '%s' for writing\n", outfname);
            return EXIT_FAILURE;
        }
        free(outfname);
    }
    uint32_t value;
    size_t size = 0;
    char * ptr = buffer;
    char * end = NULL;
    int lineno = 1;
    int colno = 1;
    while (1) {
        while (isspace(*ptr) || *ptr == ',') {
            if (*ptr == 0) break;
            if (*ptr == '\n')
            { lineno++; colno = 1; }
            if (*ptr == ',') colno++;
            ptr++;
        };
        if (*ptr == 0) break;
        value = strtoul(ptr, &end, 10);
        if (value == 0 && ptr == end) {
            fprintf(stderr, "syntax error: %d:%d\n", lineno, colno);
            return EXIT_FAILURE;
        }
        if (fwrite(&value, 1, width, outfile) != width) {
            fprintf(stderr, "write error\n");
            return EXIT_FAILURE;
        }
        ptr = end;
    }
    fclose(outfile);
    free(buffer);
    return EXIT_SUCCESS;
}