summaryrefslogtreecommitdiff
path: root/src/amalgamate.c
blob: 6064e91401eda3e8219d518fe9d0375713910cc2 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char const* argv[]) {
    static const char* aFilenames[] = {
        "src/http.h",
        "src/http.c",
        "src/http_backend_curl.c",
        "src/http_backend_dummy.c",
        "src/http_backend_winhttp.c",
        "src/http_next_header.c",
    };

    static const int nFilenames = sizeof(aFilenames) / sizeof(aFilenames[0]);

    FILE* fout = fopen("http.c", "wb");
    if (!fout) {
        fprintf(stderr, "error opening http.c: %s\n", strerror(errno));
        return 1;
    }

    static const int bufsize = 16*1024;
    char* buffer = malloc(bufsize);

    for (int i = 0; i < nFilenames; ++i) {
        FILE* fp = fopen(aFilenames[i], "rb");
        if (!fp) {
            fprintf(stderr, "error opening %s: %s\n", aFilenames[i], strerror(errno));
            return 1;
        }
        snprintf(buffer, bufsize, "\n/********** %s **********/\n\n", aFilenames[i]);
        if (fwrite(buffer, 1, strlen(buffer), fout) != strlen(buffer)) {
            fprintf(stderr, "error writing http.c: %s\n", strerror(errno));
            return 1;
        }
        while (fgets(buffer, bufsize, fp)) {
            size_t l = strlen(buffer);
            if (strncmp(buffer, "#include \"http.h\"", 17) != 0) {
                if (fwrite(buffer, 1, l, fout) != l) {
                    fprintf(stderr, "error writing http.c: %s\n", strerror(errno));
                    return 1;
                }
            }
        }
        fclose(fp);
    }

    fclose(fout);

    return 0;
}