Showing the changes to Source/Tools/EmbedFile/EmbedFile.cpp (1 of 1 file). Show all
Unified · Split
| @@ -58,27 +58,44 @@ void write_byte(FILE* out, int32_t& chars, unsigned char value) · 57 unmodified lines | ||
| 58 | 58 | fprintf(out, "0x%02X", value); |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | +bool starts_with(const char* str, const char* prefix) | |
| 62 | +{ | |
| 63 | + return _strnicmp(prefix, str, strlen(prefix)) == 0; | |
| 64 | +} | |
| 65 | + | |
| 61 | 66 | int main(int argc, char** argv) |
| 62 | 67 | { |
| 63 | 68 | 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"); | |
| 67 | 71 | return EXIT_FAILURE; |
| 68 | 72 | } |
| 69 | 73 | |
| 70 | 74 | const char* sym = argv[1]; |
| 71 | 75 | FILE* in = open_or_exit(argv[2], "r"); |
| 72 | − const char* prefix = ""; | |
| 76 | + const char* namespaceArgument = ""; | |
| 77 | + const char* namespacePrefix = ""; | |
| 73 | 78 | bool addNullByte = false; |
| 79 | + bool useIncludeGuard = false; | |
| 74 | 80 | |
| 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 | + } | |
| 82 | 99 | |
| 83 | 100 | // Create .H resource file |
| 84 | 101 | char symfile[256]; |
| @@ -87,16 +104,20 @@ int main(int argc, char** argv) · 2 unmodified lines | ||
| 87 | 104 | |
| 88 | 105 | // Define #ifdef directive symbol used to include header file only once |
| 89 | 106 | char symdirective[256]; |
| 90 | − const char* s = sym; | |
| 91 | − char* d = symdirective; | |
| 92 | 107 | |
| 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; | |
| 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 | 122 | char now[256]; |
| 102 | 123 | time_t rawtime; |
| @@ -107,17 +128,26 @@ int main(int argc, char** argv) · 4 unmodified lines | ||
| 107 | 128 | |
| 108 | 129 | // Write header for .H resource file |
| 109 | 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" : ""); | |
| 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 | + | |
| 113 | 143 | fprintf(out, "#include <cstdint>\n\n"); |
| 114 | 144 | |
| 115 | − if (strlen(prefix) > 0) | |
| 145 | + if (strlen(namespacePrefix) > 0) | |
| 116 | 146 | { |
| 117 | 147 | // When a C++ namespace prefix is provided, break each namespace |
| 118 | 148 | // component out into separate namespace commands for the .H file |
| 119 | 149 | char source[256]; |
| 120 | − strcpy_s(source, 256, prefix); | |
| 150 | + strcpy_s(source, 256, namespacePrefix); | |
| 121 | 151 | |
| 122 | 152 | char* next_token; |
| 123 | 153 | char* token = strtok_s(source, "::", &next_token); |
| @@ -144,7 +174,9 @@ int main(int argc, char** argv) · 20 unmodified lines | ||
| 144 | 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 | 180 | fclose(out); |
| 149 | 181 | |
| 150 | 182 | // Create .CPP resource file |
| @@ -153,9 +185,9 @@ int main(int argc, char** argv) · 2 unmodified lines | ||
| 153 | 185 | |
| 154 | 186 | // Write header for .CPP resource file |
| 155 | 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 | 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 | 192 | unsigned char buf[256]; |
| 161 | 193 | size_t nread; |
| @@ -177,7 +209,7 @@ int main(int argc, char** argv) · 15 unmodified lines | ||
| 177 | 209 | write_byte(out, chars, '\0'); |
| 178 | 210 | |
| 179 | 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 | 214 | fclose(in); |
| 183 | 215 | fclose(out); |