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
5858 fprintf(out, "0x%02X", value);
5959 }
6060
61+bool starts_with(const char* str, const char* prefix)
62+{
63+ return _strnicmp(prefix, str, strlen(prefix)) == 0;
64+}
65+
6166 int main(int argc, char** argv)
6267 {
6368 if (argc < 3) {
64− fprintf(stderr, "USAGE: %s {sym} {rsrc} [{namespace}] [addnull]\n\n"
65− " Creates {sym}.cpp/.h from the contents of {rsrc}\n",
66− argv[0]);
69+ fprintf(stderr, "\nUSAGE: EmbedFile {sym} {rsrc} [-namespace={ns1::ns2::}] [-addNull] [-useIncludeGuard]\n\n"
70+ " Creates {sym}.cpp/.h from the contents of {rsrc}\n\n");
6771 return EXIT_FAILURE;
6872 }
6973
7074 const char* sym = argv[1];
7175 FILE* in = open_or_exit(argv[2], "r");
72− const char* prefix = "";
76+ const char* namespaceArgument = "";
77+ const char* namespacePrefix = "";
7378 bool addNullByte = false;
79+ bool useIncludeGuard = false;
7480
75− // Third argument is optional C++ namespace prefix
76− if (argc > 3)
77− prefix = argv[3];
78−
79− // Fourth argument is optional command to add a null byte to resource
80− if (argc > 4)
81− addNullByte = _strcmpi(argv[4], "addnull") == 0;
81+ // Parse optional argumments
82+ for (int i = 3; i < argc; i++)
83+ {
84+ // 01234567890
85+ if (starts_with(argv[i], "-namespace=") && strlen(argv[i]) > 11)
86+ {
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+ }
8299
83100 // Create .H resource file
84101 char symfile[256];
@@ -87,16 +104,20 @@ int main(int argc, char** argv) · 2 unmodified lines
87104
88105 // Define #ifdef directive symbol used to include header file only once
89106 char symdirective[256];
90− const char* s = sym;
91− char* d = symdirective;
92107
93− while (*s) {
94− if (isupper(*s))
95− *d++ = '_';
96− *d++ = toupper(*s++);
97− }
108+ if (useIncludeGuard)
109+ {
110+ const char* s = sym;
111+ char* d = symdirective;
98112
99− *d = '\0';
113+ while (*s) {
114+ if (isupper(*s))
115+ *d++ = '_';
116+ *d++ = toupper(*s++);
117+ }
118+
119+ *d = '\0';
120+ }
100121
101122 char now[256];
102123 time_t rawtime;
@@ -107,17 +128,26 @@ int main(int argc, char** argv) · 4 unmodified lines
107128
108129 // Write header for .H resource file
109130 fprintf(out, "// Auto-generated on %s", now);
110− fprintf(out, "// EmbedFile.exe %s %s %s%s\n\n", sym, argv[2], prefix, addNullByte ? " addnull" : "");
111− fprintf(out, "#ifndef %s\n", symdirective);
112− fprintf(out, "#define %s\n\n", symdirective);
131+ fprintf(out, "// EmbedFile.exe %s %s %s%s%s\n\n", sym, argv[2], namespaceArgument, addNullByte ? " -addNull" : "", useIncludeGuard ? " -useIncludeGuard" : "");
132+
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+
113143 fprintf(out, "#include <cstdint>\n\n");
114144
115− if (strlen(prefix) > 0)
145+ if (strlen(namespacePrefix) > 0)
116146 {
117147 // When a C++ namespace prefix is provided, break each namespace
118148 // component out into separate namespace commands for the .H file
119149 char source[256];
120− strcpy_s(source, 256, prefix);
150+ strcpy_s(source, 256, namespacePrefix);
121151
122152 char* next_token;
123153 char* token = strtok_s(source, "::", &next_token);
@@ -144,7 +174,9 @@ int main(int argc, char** argv) · 20 unmodified lines
144174 fprintf(out, "const extern uint32_t %sLength;", sym);
145175 }
146176
147− fprintf(out, "\n\n#endif\n");
177+ if (useIncludeGuard)
178+ fprintf(out, "\n\n#endif\n");
179+
148180 fclose(out);
149181
150182 // Create .CPP resource file
@@ -153,9 +185,9 @@ int main(int argc, char** argv) · 2 unmodified lines
153185
154186 // Write header for .CPP resource file
155187 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" : "");
157189 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);
159191
160192 unsigned char buf[256];
161193 size_t nread;
@@ -177,7 +209,7 @@ int main(int argc, char** argv) · 15 unmodified lines
177209 write_byte(out, chars, '\0');
178210
179211 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);
181213
182214 fclose(in);
183215 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