-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_header.c
More file actions
111 lines (82 loc) · 1.64 KB
/
generate_header.c
File metadata and controls
111 lines (82 loc) · 1.64 KB
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
104
105
106
107
108
109
110
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "hash.h"
#define PREFIX "I_"
#define DEF_FILE "defs.dat"
#define HEADER_FILE "src/constants.h"
#define HEADER_NAME "CONSTANTS"
#define BUF_SIZE 256
void strip(char *);
void strupper(char *);
int
main(int argc, char *argv[])
{
char buf[BUF_SIZE+1];
FILE *file;
FILE *header;
size_t len;
unsigned long hash;
file = fopen(DEF_FILE, "r");
if(!file) {
perror(DEF_FILE);
exit(EXIT_FAILURE);
}
header = fopen(HEADER_FILE, "w");
if(!header) {
perror(HEADER_FILE);
exit(EXIT_FAILURE);
}
fputs("/*\n * NOTE: PLEASE do no edit; autogenerated.\n"
" * check +generate_header.c+ for more info.\n"
" */\n\n", header);
fprintf(header, "#ifndef _%s_H_\n#define _%s_H_\n\n",
HEADER_NAME, HEADER_NAME);
while(!feof(file)) {
fgets(&buf[0], BUF_SIZE, file);
strip(buf);
/* ignore lines starting with # */
if(buf[0]=='#')
continue;
len = strlen(buf);
if(!len)
continue;
hash = hash_f(buf);
strupper(buf);
fprintf(header, "#define %s%s %lu\n", PREFIX, buf, hash);
}
fprintf(header, "\n#endif\n");
fclose(file);
fclose(header);
return EXIT_SUCCESS;
}
/* reinventing the wheel */
void
strupper(char *s)
{
while(*s) {
*s = toupper(*s);
s++;
}
return;
}
void
strip(char *s)
{
char *ptr = s;
size_t len = strlen(s);
size_t size = 0;
if(!len)
return;
while(isblank(*ptr) || isspace(*ptr)) {
size++;
ptr++;
}
if(size)
memcpy(s, ptr, size);
ptr = s + len - 1;
while((isblank(*ptr) || isspace(*ptr)) && ptr != s) --ptr;
*(++ptr) = '\0';
return;
}