History

EmbedFile Tool: Added comments to EmbedFile tool used to create a portable embedded resources for C++ applications.

J. Ritchie Carroll committed 7 years ago

Commit 4d1a1af1 parent 948fa611 file · +44 −12Browse files

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

Unified · Split

Source/Tools/EmbedFile/EmbedFile.cpp+44−12
@@ -1,14 +1,39 @@
1−// EmbedFile.cpp : This file contains the 'main' function. Program execution begins and ends there.
1+//******************************************************************************************************
2+// EmbedFile.cpp - Gbtc
23 //
4+// Copyright � 2019, Grid Protection Alliance. All Rights Reserved.
5+//
6+// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
7+// the NOTICE file distributed with this work for additional information regarding copyright ownership.
8+// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
9+// file except in compliance with the License. You may obtain a copy of the License at:
10+//
11+// http://opensource.org/licenses/MIT
12+//
13+// Unless agreed to in writing, the subject software distributed under the License is distributed on an
14+// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
15+// License for the specific language governing permissions and limitations.
16+//
17+// Code Modification History:
18+// ----------------------------------------------------------------------------------------------------
19+// 02/02/2019 - J. Ritchie Carrikk
20+// Generated original version of source code.
21+//
22+//******************************************************************************************************
23+
24+/*
25+ * This application is used to create a portable embedded resource for a C++ application.
26+ * The program will take the file name specified as a command line argument and turn it
27+ * into a byte array where the array is declared in a .H file and defined in a .CPP file.
28+ */
329
430 #include "pch.h"
531
632 FILE* open_or_exit(const char* fname, const char* mode)
733 {
834 FILE* f;
9− errno_t err;
1035
11− if ((err = fopen_s(&f, fname, mode)) != 0) {
36+ if (fopen_s(&f, fname, mode) != 0) {
1237 perror(fname);
1338 exit(EXIT_FAILURE);
1439 }
@@ -16,11 +41,11 @@ FILE* open_or_exit(const char* fname, const char* mode) · 1 unmodified line
1641 return f;
1742 }
1843
19−void write_byte(FILE* out, int32_t& chars, bool& first, unsigned char value)
44+void write_byte(FILE* out, int32_t& chars, unsigned char value)
2045 {
2146 if (chars++ % 10 == 0)
2247 {
23− if (!first)
48+ if (chars > 1)
2449 fprintf(out, ",");
2550
2651 fprintf(out, "\n ");
@@ -31,34 +56,36 @@ void write_byte(FILE* out, int32_t& chars, bool& first, unsigned char value) · 4 unmodified lines
3156 }
3257
3358 fprintf(out, "0x%02X", value);
34− first = false;
3559 }
3660
3761 int main(int argc, char** argv)
3862 {
3963 if (argc < 3) {
4064 fprintf(stderr, "USAGE: %s {sym} {rsrc} [{namespace}] [addnull]\n\n"
41− " Creates {sym}.cpp from the contents of {rsrc}\n",
65+ " Creates {sym}.cpp/.h from the contents of {rsrc}\n",
4266 argv[0]);
4367 return EXIT_FAILURE;
4468 }
4569
4670 const char* sym = argv[1];
4771 FILE* in = open_or_exit(argv[2], "r");
48− char empty[1] = "";
4972 const char* prefix = "";
5073 bool addNullByte = false;
5174
75+ // Third argument is optional C++ namespace prefix
5276 if (argc > 3)
5377 prefix = argv[3];
5478
79+ // Fourth argument is optional command to add a null byte to resource
5580 if (argc > 4)
5681 addNullByte = _strcmpi(argv[4], "addnull") == 0;
5782
83+ // Create .H resource file
5884 char symfile[256];
5985 snprintf(symfile, sizeof(symfile), "%s.h", sym);
6086 FILE* out = open_or_exit(symfile,"w");
6187
88+ // Define #ifdef directive symbol used to include header file only once
6289 char symdirective[256];
6390 const char* s = sym;
6491 char* d = symdirective;
@@ -78,6 +105,7 @@ int main(int argc, char** argv) · 13 unmodified lines
78105 localtime_s(&timeinfo, &rawtime);
79106 asctime_s(now, 256, &timeinfo);
80107
108+ // Write header for .H resource file
81109 fprintf(out, "// Auto-generated on %s", now);
82110 fprintf(out, "// EmbedFile.exe %s %s %s%s\n\n", sym, argv[2], prefix, addNullByte ? " addnull" : "");
83111 fprintf(out, "#ifndef %s\n", symdirective);
@@ -86,6 +114,8 @@ int main(int argc, char** argv) · 2 unmodified lines
86114
87115 if (strlen(prefix) > 0)
88116 {
117+ // When a C++ namespace prefix is provided, break each namespace
118+ // component out into separate namespace commands for the .H file
89119 char source[256];
90120 strcpy_s(source, 256, prefix);
91121
@@ -117,32 +147,34 @@ int main(int argc, char** argv) · 25 unmodified lines
117147 fprintf(out, "\n\n#endif\n");
118148 fclose(out);
119149
150+ // Create .CPP resource file
120151 snprintf(symfile, sizeof(symfile), "%s.cpp", sym);
121152 out = open_or_exit(symfile,"w");
122153
154+ // Write header for .CPP resource file
123155 fprintf(out, "// Auto-generated on %s", now);
124156 fprintf(out, "// EmbedFile.exe %s %s %s%s\n\n", sym, argv[2], prefix, addNullByte ? " addnull" : "");
125157 fprintf(out, "#include \"%s.h\"\n\n", sym);
126158 fprintf(out, "const uint8_t %s%s[] = {", prefix, sym);
127159
128160 unsigned char buf[256];
129− size_t nread = 0;
161+ size_t nread;
130162 int32_t chars = 0;
131− bool first = true;
132163
164+ // Write resource bytes
133165 do
134166 {
135167 nread = fread(buf, 1, sizeof(buf), in);
136168 size_t i;
137169
138170 for (i = 0; i < nread; i++)
139− write_byte(out, chars, first, buf[i]);
171+ write_byte(out, chars, buf[i]);
140172 }
141173 while (nread > 0);
142174
143175 // Write NULL as last character, if requested
144176 if (addNullByte)
145− write_byte(out, chars, first, '\0');
177+ write_byte(out, chars, '\0');
146178
147179 fprintf(out, "\n};\n\n");
148180 fprintf(out, "const uint32_t %s%sLength = sizeof(%s%s);\n", prefix, sym, prefix, sym);

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