summaryrefslogtreecommitdiff
path: root/gen-ui-array.c
blob: 6cb7e26eb0d9c0f44e170a32f7c00ba434954bf6 (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
/*
 * This C program takes a filename and an array name, and prints to
 * stdout an array of the data contained in the file.
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>

int main(int argc, char *argv[])
{
    char *arrayname, *filename;
    unsigned char *src;
    struct stat stat_buf;
    int length, fd, i, n;

    if (argc != 3) {
        fprintf(stderr, "usage: %s [filename] [arrayname]\n", argv[0]);
        return 1;
    }
    
    filename = argv[1];
    arrayname = argv[2];
    
    /* open the file */

    fd = open(filename, O_RDONLY);
    if (fd < 0) {
        fprintf(stderr, "unable to open(2) %s (%s)\n",
                filename, strerror(errno));
        return 1;
    }

    /* get the length of the file */

    if (fstat(fd, &stat_buf) != 0) {
        fprintf(stderr, "unable to stat(2) %s (%s)\n",
                filename, strerror(errno));
        return 1;
    }

    length = stat_buf.st_size;

    /* map the file */

    src = mmap(0, length, PROT_READ, MAP_SHARED, fd, 0);
    if (src == (void *) -1) {
        fprintf(stderr, "unable to mmap(2) %s (%s)\n",
                filename, strerror(errno));
        return 1;
    }

    /* print the header */

    printf("/*\n");
    printf( " * THIS FILE IS AUTOMATICALLY GENERATED - DO NOT EDIT\n");
    printf( " *\n");
    printf( " * This file contains the contents of the file \"%s\"\n",
            filename);
    printf( " * as the byte array %s[].\n", arrayname);
    printf( " */\n\n");

    /* print a separate variable that stores the size */

    printf("const int %s_size = %d;\n\n", arrayname, length);

    /* print the array name */

    printf("const unsigned char %s[%d] = {\n", arrayname, length);

    for (i = 0, n = 0; i < length; i++, n++) {
        if (n == 0) printf("    ");
        printf("0x%02x, ", src[i]);
        if (n >= 11) { printf("\n"); n = -1; }
    }
    
    printf("\n};\n\n");

    /* unmap the file */

    munmap(src, length);

    /* close the file */

    close(fd);


    return 0;
}