History

EmbedFile: Updated tool to default to producing headers with "#pragma once" leaving old-style include guards as an option

J. Ritchie Carroll committed 5 years ago

Commit de86c471 parent 48b95af1 file · +60 −28Browse files

Showing the changes to Source/Tools/EmbedFile/EmbedFile.cpp (1 of 1 file). Show all

Unified · Split

Source/Tools/EmbedFile/EmbedFile.cpp+60−28
@@ -58,27 +58,44 @@ void write_byte(FILE* out, int32_t& chars, unsigned char value) · 57 unmodified lines
58 fprintf(out, "0x%02X", value);58 fprintf(out, "0x%02X", value);
59}59}
60 60
61bool starts_with(const char* str, const char* prefix)
62{
63 return _strnicmp(prefix, str, strlen(prefix)) == 0;
64}
65
61int main(int argc, char** argv)66int main(int argc, char** argv)
62{67{
63 if (argc < 3) {68 if (argc < 3) {
64 fprintf(stderr, "USAGE: %s {sym} {rsrc} [{namespace}] [addnull]\n\n"69 fprintf(stderr, "\nUSAGE: EmbedFile {sym} {rsrc} [-namespace={ns1::ns2::}] [-addNull] [-useIncludeGuard]\n\n"
65 " Creates {sym}.cpp/.h from the contents of {rsrc}\n",70 " Creates {sym}.cpp/.h from the contents of {rsrc}\n\n");
66 argv[0]);
67 return EXIT_FAILURE;71 return EXIT_FAILURE;
68 }72 }
69 73
70 const char* sym = argv[1];74 const char* sym = argv[1];
71 FILE* in = open_or_exit(argv[2], "r");75 FILE* in = open_or_exit(argv[2], "r");
72 const char* prefix = "";76 const char* namespaceArgument = "";
77 const char* namespacePrefix = "";
73 bool addNullByte = false;78 bool addNullByte = false;
79 bool useIncludeGuard = false;
74 80
75 // Third argument is optional C++ namespace prefix81 // Parse optional argumments
76 if (argc > 3)82 for (int i = 3; i < argc; i++)
77 prefix = argv[3];83 {
78 84 // 01234567890
79 // Fourth argument is optional command to add a null byte to resource85 if (starts_with(argv[i], "-namespace=") && strlen(argv[i]) > 11)
80 if (argc > 4)86 {
81 addNullByte = _strcmpi(argv[4], "addnull") == 0;87 namespaceArgument = argv[i];
88 namespacePrefix = &namespaceArgument[11];
89 }
90 else if (_strcmpi(argv[i], "-addNull") == 0)
91 {
92 addNullByte = true;
93 }
94 else if (_strcmpi(argv[i], "-useIncludeGuard") == 0)
95 {
96 useIncludeGuard = true;
97 }
98 }
82 99
83 // Create .H resource file100 // Create .H resource file
84 char symfile[256];101 char symfile[256];
@@ -87,16 +104,20 @@ int main(int argc, char** argv) · 2 unmodified lines
87 104
88 // Define #ifdef directive symbol used to include header file only once105 // Define #ifdef directive symbol used to include header file only once
89 char symdirective[256];106 char symdirective[256];
90 const char* s = sym;
91 char* d = symdirective;
92 107
93 while (*s) {108 if (useIncludeGuard)
94 if (isupper(*s))109 {
95 *d++ = '_';110 const char* s = sym;
96 *d++ = toupper(*s++);111 char* d = symdirective;
97 }
98 112
99 *d = '\0';113 while (*s) {
114 if (isupper(*s))
115 *d++ = '_';
116 *d++ = toupper(*s++);
117 }
118
119 *d = '\0';
120 }
100 121
101 char now[256];122 char now[256];
102 time_t rawtime;123 time_t rawtime;
@@ -107,17 +128,26 @@ int main(int argc, char** argv) · 4 unmodified lines
107 128
108 // Write header for .H resource file129 // Write header for .H resource file
109 fprintf(out, "// Auto-generated on %s", now);130 fprintf(out, "// Auto-generated on %s", now);
110 fprintf(out, "// EmbedFile.exe %s %s %s%s\n\n", sym, argv[2], prefix, addNullByte ? " addnull" : "");131 fprintf(out, "// EmbedFile.exe %s %s %s%s%s\n\n", sym, argv[2], namespaceArgument, addNullByte ? " -addNull" : "", useIncludeGuard ? " -useIncludeGuard" : "");
111 fprintf(out, "#ifndef %s\n", symdirective);132
112 fprintf(out, "#define %s\n\n", symdirective);133 if (useIncludeGuard)
134 {
135 fprintf(out, "#ifndef %s\n", symdirective);
136 fprintf(out, "#define %s\n\n", symdirective);
137 }
138 else
139 {
140 fprintf(out, "#pragma once\n\n");
141 }
142
113 fprintf(out, "#include <cstdint>\n\n");143 fprintf(out, "#include <cstdint>\n\n");
114 144
115 if (strlen(prefix) > 0)145 if (strlen(namespacePrefix) > 0)
116 {146 {
117 // When a C++ namespace prefix is provided, break each namespace147 // When a C++ namespace prefix is provided, break each namespace
118 // component out into separate namespace commands for the .H file148 // component out into separate namespace commands for the .H file
119 char source[256];149 char source[256];
120 strcpy_s(source, 256, prefix);150 strcpy_s(source, 256, namespacePrefix);
121 151
122 char* next_token;152 char* next_token;
123 char* token = strtok_s(source, "::", &next_token);153 char* token = strtok_s(source, "::", &next_token);
@@ -144,7 +174,9 @@ int main(int argc, char** argv) · 20 unmodified lines
144 fprintf(out, "const extern uint32_t %sLength;", sym);174 fprintf(out, "const extern uint32_t %sLength;", sym);
145 }175 }
146 176
147 fprintf(out, "\n\n#endif\n");177 if (useIncludeGuard)
178 fprintf(out, "\n\n#endif\n");
179
148 fclose(out);180 fclose(out);
149 181
150 // Create .CPP resource file182 // Create .CPP resource file
@@ -153,9 +185,9 @@ int main(int argc, char** argv) · 2 unmodified lines
153 185
154 // Write header for .CPP resource file186 // Write header for .CPP resource file
155 fprintf(out, "// Auto-generated on %s", now);187 fprintf(out, "// Auto-generated on %s", now);
156 fprintf(out, "// EmbedFile.exe %s %s %s%s\n\n", sym, argv[2], prefix, addNullByte ? " addnull" : "");188 fprintf(out, "// EmbedFile.exe %s %s %s%s%s\n\n", sym, argv[2], namespaceArgument, addNullByte ? " -addNull" : "", useIncludeGuard ? " -useIncludeGuard" : "");
157 fprintf(out, "#include \"%s.h\"\n\n", sym);189 fprintf(out, "#include \"%s.h\"\n\n", sym);
158 fprintf(out, "const uint8_t %s%s[] = {", prefix, sym);190 fprintf(out, "const uint8_t %s%s[] = {", namespacePrefix, sym);
159 191
160 unsigned char buf[256];192 unsigned char buf[256];
161 size_t nread;193 size_t nread;
@@ -177,7 +209,7 @@ int main(int argc, char** argv) · 15 unmodified lines
177 write_byte(out, chars, '\0');209 write_byte(out, chars, '\0');
178 210
179 fprintf(out, "\n};\n\n");211 fprintf(out, "\n};\n\n");
180 fprintf(out, "const uint32_t %s%sLength = sizeof(%s%s);\n", prefix, sym, prefix, sym);212 fprintf(out, "const uint32_t %s%sLength = sizeof(%s%s);\n", namespacePrefix, sym, namespacePrefix, sym);
181 213
182 fclose(in);214 fclose(in);
183 fclose(out);215 fclose(out);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t