diff options
author | Sven Gothel <[email protected]> | 2019-04-07 23:39:04 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2019-04-07 23:39:04 +0200 |
commit | 73233ce69919fc19c53ce8663c5b8cc05227f07e (patch) | |
tree | f2b6ccc1a14d7c387f33398a44ea4511d7ecb212 /native-tools/bin2h.c | |
parent | 8efa4c7ba5ee8eb399d31a9884e45f743d4625ad (diff) | |
parent | 99a55c445211fea77af6ab61cbc6a6ec4fbdc9b9 (diff) |
Merge branch 'v1.19' of git://repo.or.cz/openal-soft into v1.19v1.19
Diffstat (limited to 'native-tools/bin2h.c')
-rw-r--r-- | native-tools/bin2h.c | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/native-tools/bin2h.c b/native-tools/bin2h.c new file mode 100644 index 00000000..92f2b8a5 --- /dev/null +++ b/native-tools/bin2h.c @@ -0,0 +1,100 @@ + +#ifdef _MSC_VER +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +int main (int argc, char *argv[]) +{ + char* input_name; + FILE* input_file; + + char* output_name; + FILE* output_file; + + char* variable_name; + + if (4 != argc) + { + puts("Usage: bin2h [input] [output] [variable]"); + return EXIT_FAILURE; + } + + input_name = argv[1]; + output_name = argv[2]; + variable_name = argv[3]; + + input_file = fopen(input_name, "rb"); + + if (NULL == input_file) + { + printf("Could not open input file '%s': %s\n", input_name, strerror(errno)); + return EXIT_FAILURE; + } + + output_file = fopen(output_name, "w"); + + if (NULL == output_file) + { + printf("Could not open output file '%s': %s\n", output_name, strerror(errno)); + return EXIT_FAILURE; + } + + if (fprintf(output_file, "static const unsigned char %s[] = {", variable_name) < 0) + { + printf("Could not write to output file '%s': %s\n", output_name, strerror(ferror(output_file))); + return EXIT_FAILURE; + } + + while (0 == feof(input_file)) + { + unsigned char buffer[4096]; + size_t i, count = fread(buffer, 1, sizeof(buffer), input_file); + + if (sizeof(buffer) != count) + { + if (0 == feof(input_file) || 0 != ferror(input_file)) + { + printf("Could not read from input file '%s': %s\n", input_name, strerror(ferror(input_file))); + return EXIT_FAILURE; + } + } + + for (i = 0; i < count; ++i) + { + if ((i & 15) == 0) + { + if (fprintf(output_file, "\n ") < 0) + { + printf("Could not write to output file '%s': %s\n", output_name, strerror(ferror(output_file))); + return EXIT_FAILURE; + } + } + + if (fprintf(output_file, "0x%2.2x, ", buffer[i]) < 0) + { + printf("Could not write to output file '%s': %s\n", output_name, strerror(ferror(output_file))); + return EXIT_FAILURE; + } + + } + } + + if (fprintf(output_file, "\n};\n") < 0) + { + printf("Could not write to output file '%s': %s\n", output_name, strerror(ferror(output_file))); + return EXIT_FAILURE; + } + + if (fclose(output_file) < 0) + { + printf("Could not close output file '%s': %s\n", output_name, strerror(ferror(output_file))); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} |