-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
125 lines (114 loc) · 3.8 KB
/
main.c
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(){
FILE* outf = fopen("vk_enum_str.h", "w");
FILE* inf = fopen("C:/VulkanSDK/1.2.176.1/Include/vulkan/vulkan_core.h", "r");
fprintf(outf, "#ifndef _VK_ENUM_STR_H_\n#define _VK_ENUM_STR_H_\n\n");
// Find largest word
int max = 0;
int length = 0;
char c = 0;
do{
c = getc(inf);
while(c != EOF && c != ' ' && c != '\n' && c != '\t'){
length += 1;
c = getc(inf);
}
if(length > max) max = length;
length = 0;
} while(c != EOF);
rewind(inf);
char* word = malloc(sizeof(char) * max + 1);
char* value = malloc(sizeof(char) * max + 1);
int value_length = 0;
memset(word, '\0', max + 1);
memset(value, '\0', max + 1);
// Identify enums and write their names and possible values to file
do{
c = getc(inf);
while(c != EOF && c != ' ' && c != '\n' && c != '\t'){
word[length] = c;
length += 1;
c = getc(inf);
}
if(strcmp("enum", word) == 0){
memset(word, '\0', max + 1);
length = 0;
while((c == ' ' || c == '\n' || c == '\t') && c != EOF){
c = getc(inf);
}
while(c != ' ' && c != '\n' && c != '\t' && c != EOF){
word[length] = c;
length += 1;
c = getc(inf);
}
fprintf(outf, "#define STR_VK");
for(int i = 2; i < length; i ++){
if(islower(word[i - 1]) && isupper(word[i])){
fprintf(outf, "_");
}
fprintf(outf, "%c", toupper(word[i]));
}
fprintf(outf, "(x) (\\\n");
memset(word, '\0', max + 1);
length = 0;
do{
while((c == ' ' || c == '\n' || c == '\t' || c == '{') && c != EOF){
c = getc(inf);
}
while(c != ' ' && c != '\n' && c != '\t' && c != EOF){
word[length] = c;
length += 1;
c = getc(inf);
}
// Catch ifdef's and endif's
if(word[0] == '#') {
while(c != '\n'){
c = getc(inf);
}
memset(word, '\0', max + 1);
length = 0;
continue;
}
while(c == ' ' || c == '='){
c = getc(inf);
}
while(c != ' ' && c != '\n' && c != '\t' && c != ',' && c != EOF){
value[value_length] = c;
value_length += 1;
c = getc(inf);
}
int bad = 0;
for(int i = 0; i < value_length; i ++){
if(!isxdigit(value[i]) && value[i] != 'x' && value[i] != '-'){
bad = 1;
break;
}
}
if(!bad) fprintf(outf, "\tx==%s?\"%s\":\\\n", value, word + 3);
memset(word, '\0', max + 1);
length = 0;
memset(value, '\0', max + 1);
value_length = 0;
while(c != '\n'){
c = getc(inf);
}
c = getc(inf);
while((c == ' ' || c == '\n' || c == '\t') && c != EOF){
c = getc(inf);
}
} while(c != '}');
fprintf(outf, "\t\"ENUM_UNKNOWN\")\n\n");
}
memset(word, '\0', max + 1);
length = 0;
} while(c != EOF);
fprintf(outf, "#endif\n");
free(word);
free(value);
fclose(inf);
fclose(outf);
return 0;
}