-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
shg8
committed
Feb 22, 2024
1 parent
ef16ef8
commit 3e67418
Showing
4 changed files
with
64 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
FILE* open_or_exit(const char* fname, const char* mode) | ||
{ | ||
FILE* f = fopen(fname, mode); | ||
if (f == NULL) { | ||
perror(fname); | ||
exit(EXIT_FAILURE); | ||
} | ||
return f; | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
if (argc < 3) { | ||
fprintf(stderr, "USAGE: %s {sym} {rsrc}\n\n" | ||
" Creates {sym}.c from the contents of {rsrc}\n", | ||
argv[0]); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
const char* sym = argv[1]; | ||
FILE* in = open_or_exit(argv[2], "r"); | ||
FILE* out = open_or_exit(argv[3], "a"); | ||
fprintf(out, "static const unsigned char %s[] = {\n", sym); | ||
|
||
unsigned char buf[256]; | ||
size_t nread = 0; | ||
size_t linecount = 0; | ||
do { | ||
nread = fread(buf, 1, sizeof(buf), in); | ||
size_t i; | ||
for (i=0; i < nread; i++) { | ||
fprintf(out, "0x%02x, ", buf[i]); | ||
if (++linecount == 10) { fprintf(out, "\n"); linecount = 0; } | ||
} | ||
} while (nread > 0); | ||
if (linecount > 0) fprintf(out, "\n"); | ||
fprintf(out, "};\n"); | ||
fprintf(out, "static const size_t %s_len = sizeof(%s);\n\n",sym,sym); | ||
|
||
fclose(in); | ||
fclose(out); | ||
|
||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters