Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7638 views
1
/* namedump.c -- parse an alphabetically sorted list of PDF names
2
* and generate header files from it. */
3
4
#include <stdio.h>
5
#include <string.h>
6
#include <stdlib.h>
7
8
char buffer[256];
9
10
static char *get_line(FILE *in)
11
{
12
size_t l;
13
14
if (fgets(buffer, sizeof(buffer), in) == NULL)
15
{
16
buffer[0] = 0;
17
return buffer;
18
}
19
l = strlen(buffer);
20
while (l > 0 && buffer[l-1] <= ' ')
21
l--;
22
buffer[l] = 0;
23
24
return buffer;
25
}
26
27
28
int
29
main(int argc, char **argv)
30
{
31
FILE *in;
32
FILE *out_c;
33
FILE *out_h;
34
35
if (argc != 4)
36
{
37
fprintf(stderr, "Syntax:\nnamedump <in-file> <public header> <private header>\n");
38
return EXIT_FAILURE;
39
}
40
41
in = fopen(argv[1], "rb");
42
if (!in)
43
{
44
fprintf(stderr, "Failed to open '%s' for reading\n", argv[1]);
45
return EXIT_FAILURE;
46
}
47
48
out_h = fopen(argv[2], "wb");
49
if (!out_h)
50
{
51
fprintf(stderr, "Failed to open '%s' for writing\n", argv[2]);
52
return EXIT_FAILURE;
53
}
54
55
out_c = fopen(argv[3], "wb");
56
if (!out_c)
57
{
58
fprintf(stderr, "Failed to open '%s' for writing\n", argv[3]);
59
return EXIT_FAILURE;
60
}
61
62
fprintf(out_c, "char *PDF_NAMES[] =\n{\n\t\"\",\n");
63
64
fprintf(out_h, "enum\n{\n\tPDF_OBJ_ENUM__DUMMY,\n");
65
66
while (!feof(in))
67
{
68
char *line = get_line(in);
69
if (*line == 0)
70
continue;
71
72
fprintf(out_c, "\t\"%s\",\n", line);
73
74
{
75
char *l;
76
for (l = line; *l; l++)
77
{
78
if (*l == '.' || *l == '-')
79
*l = '_';
80
}
81
}
82
83
fprintf(out_h, "#define PDF_NAME_%s ((pdf_obj *)(intptr_t)PDF_OBJ_ENUM_NAME_%s)\n", line, line);
84
fprintf(out_h, "\tPDF_OBJ_ENUM_NAME_%s,\n", line);
85
}
86
87
fprintf(out_h, "#define PDF_OBJ_NAME__LIMIT ((pdf_obj *)(intptr_t)PDF_OBJ_ENUM_NAME__LIMIT)\n\tPDF_OBJ_ENUM_NAME__LIMIT,\n");
88
fprintf(out_h, "#define PDF_OBJ_FALSE ((pdf_obj *)(intptr_t)PDF_OBJ_ENUM_BOOL_FALSE)\n\tPDF_OBJ_ENUM_BOOL_FALSE = PDF_OBJ_ENUM_NAME__LIMIT,\n");
89
fprintf(out_h, "#define PDF_OBJ_TRUE ((pdf_obj *)(intptr_t)PDF_OBJ_ENUM_BOOL_TRUE)\n\tPDF_OBJ_ENUM_BOOL_TRUE,\n");
90
fprintf(out_h, "#define PDF_OBJ_NULL ((pdf_obj *)(intptr_t)PDF_OBJ_ENUM_NULL)\n\tPDF_OBJ_ENUM_NULL,\n");
91
fprintf(out_h, "#define PDF_OBJ__LIMIT ((pdf_obj *)(intptr_t)PDF_OBJ_ENUM__LIMIT)\n\tPDF_OBJ_ENUM__LIMIT\n};\n");
92
93
fprintf(out_c, "};\n");
94
95
fclose(out_c);
96
fclose(out_h);
97
fclose(in);
98
99
return EXIT_SUCCESS;
100
}
101
102