Showing the changes to Source/Tools/EmbedFile/EmbedFile.cpp (1 of 20 files). Show all
Unified · Split
| @@ -0,0 +1,143 @@ | |||
| 1 | // EmbedFile.cpp : This file contains the 'main' function. Program execution begins and ends there. | ||
| 2 | // | ||
| 3 | |||
| 4 | #include "pch.h" | ||
| 5 | |||
| 6 | FILE* open_or_exit(const char* fname, const char* mode) | ||
| 7 | { | ||
| 8 | FILE* f; | ||
| 9 | errno_t err; | ||
| 10 | |||
| 11 | if ((err = fopen_s(&f, fname, mode)) != 0) { | ||
| 12 | perror(fname); | ||
| 13 | exit(EXIT_FAILURE); | ||
| 14 | } | ||
| 15 | |||
| 16 | return f; | ||
| 17 | } | ||
| 18 | |||
| 19 | int main(int argc, char** argv) | ||
| 20 | { | ||
| 21 | if (argc < 3) { | ||
| 22 | fprintf(stderr, "USAGE: %s {sym} {rsrc} [(namespace}]\n\n" | ||
| 23 | " Creates {sym}.cpp from the contents of {rsrc}\n", | ||
| 24 | argv[0]); | ||
| 25 | return EXIT_FAILURE; | ||
| 26 | } | ||
| 27 | |||
| 28 | const char* sym = argv[1]; | ||
| 29 | FILE* in = open_or_exit(argv[2], "r"); | ||
| 30 | char empty[1] = ""; | ||
| 31 | const char* prefix = ""; | ||
| 32 | |||
| 33 | if (argc > 3) | ||
| 34 | prefix = argv[3]; | ||
| 35 | |||
| 36 | char symfile[256]; | ||
| 37 | snprintf(symfile, sizeof(symfile), "%s.h", sym); | ||
| 38 | FILE* out = open_or_exit(symfile,"w"); | ||
| 39 | |||
| 40 | char symdirective[256]; | ||
| 41 | const char* s = sym; | ||
| 42 | char* d = symdirective; | ||
| 43 | |||
| 44 | while (*s) { | ||
| 45 | if (isupper(*s)) | ||
| 46 | *d++ = '_'; | ||
| 47 | *d++ = toupper(*s++); | ||
| 48 | } | ||
| 49 | |||
| 50 | *d = '\0'; | ||
| 51 | |||
| 52 | char now[256]; | ||
| 53 | time_t rawtime; | ||
| 54 | struct tm timeinfo {}; | ||
| 55 | time(&rawtime); | ||
| 56 | localtime_s(&timeinfo, &rawtime); | ||
| 57 | asctime_s(now, 256, &timeinfo); | ||
| 58 | |||
| 59 | fprintf(out, "// Auto-generated on %s", now); | ||
| 60 | fprintf(out, "// EmbedFile.exe %s %s %s\n\n", sym, argv[2], prefix); | ||
| 61 | fprintf(out, "#ifndef %s\n", symdirective); | ||
| 62 | fprintf(out, "#define %s\n\n", symdirective); | ||
| 63 | fprintf(out, "#include <cstdint>\n\n"); | ||
| 64 | |||
| 65 | if (strlen(prefix) > 0) | ||
| 66 | { | ||
| 67 | char source[256]; | ||
| 68 | strcpy_s(source, 256, prefix); | ||
| 69 | |||
| 70 | char* next_token; | ||
| 71 | char* token = strtok_s(source, "::", &next_token); | ||
| 72 | int32_t count = 0; | ||
| 73 | |||
| 74 | while (token) { | ||
| 75 | if (count++ > 0) | ||
| 76 | fprintf(out, " {\n"); | ||
| 77 | |||
| 78 | fprintf(out, "namespace %s", token); | ||
| 79 | token = strtok_s(nullptr, "::", &next_token); | ||
| 80 | } | ||
| 81 | |||
| 82 | fprintf(out, "\n{\n"); | ||
| 83 | fprintf(out, " const extern uint8_t %s[];\n", sym); | ||
| 84 | fprintf(out, " const extern uint32_t %sLength;\n", sym); | ||
| 85 | |||
| 86 | for (int32_t i = 0; i < count; i++) | ||
| 87 | fprintf(out, "}"); | ||
| 88 | } | ||
| 89 | else | ||
| 90 | { | ||
| 91 | fprintf(out, "const extern uint8_t %s[];\n", sym); | ||
| 92 | fprintf(out, "const extern uint32_t %sLength;", sym); | ||
| 93 | } | ||
| 94 | |||
| 95 | fprintf(out, "\n\n#endif\n"); | ||
| 96 | fclose(out); | ||
| 97 | |||
| 98 | snprintf(symfile, sizeof(symfile), "%s.cpp", sym); | ||
| 99 | out = open_or_exit(symfile,"w"); | ||
| 100 | |||
| 101 | fprintf(out, "// Auto-generated on %s", now); | ||
| 102 | fprintf(out, "// EmbedFile.exe %s %s %s\n\n", sym, argv[2], prefix); | ||
| 103 | fprintf(out, "#include \"%s.h\"\n\n", sym); | ||
| 104 | fprintf(out, "const uint8_t %s%s[] = {", prefix, sym); | ||
| 105 | |||
| 106 | unsigned char buf[256]; | ||
| 107 | size_t nread = 0; | ||
| 108 | int32_t chars = 0; | ||
| 109 | bool first = true; | ||
| 110 | |||
| 111 | do | ||
| 112 | { | ||
| 113 | nread = fread(buf, 1, sizeof(buf), in); | ||
| 114 | size_t i; | ||
| 115 | |||
| 116 | for (i = 0; i < nread; i++) | ||
| 117 | { | ||
| 118 | if (chars++ % 10 == 0) | ||
| 119 | { | ||
| 120 | if (!first) | ||
| 121 | fprintf(out, ","); | ||
| 122 | |||
| 123 | fprintf(out, "\n "); | ||
| 124 | } | ||
| 125 | else | ||
| 126 | { | ||
| 127 | fprintf(out, ", "); | ||
| 128 | } | ||
| 129 | |||
| 130 | fprintf(out, "0x%02X", buf[i]); | ||
| 131 | first = false; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | while (nread > 0); | ||
| 135 | |||
| 136 | fprintf(out, "\n};\n\n"); | ||
| 137 | fprintf(out, "const uint32_t %s%sLength = sizeof(%s%s);\n", prefix, sym, prefix, sym); | ||
| 138 | |||
| 139 | fclose(in); | ||
| 140 | fclose(out); | ||
| 141 | |||
| 142 | return EXIT_SUCCESS; | ||
| 143 | } | ||