aboutsummaryrefslogtreecommitdiffstats
path: root/utils
diff options
context:
space:
mode:
authoralexey.lysiuk <[email protected]>2017-05-05 13:29:52 +0300
committeralexey.lysiuk <[email protected]>2017-05-05 14:30:06 +0300
commit17dfaca43d4f72c1691a66a5124247f121d967d8 (patch)
tree2b9a80318f16e80b74d7f195250dfcde0c4d2a25 /utils
parent9c9ad2f60aead15416c8b582e20fadf847a13ce8 (diff)
Implement cross-platform embedding of HRTF data
Diffstat (limited to 'utils')
-rw-r--r--utils/native-tools/CMakeLists.txt8
-rw-r--r--utils/native-tools/bin2h.c100
2 files changed, 108 insertions, 0 deletions
diff --git a/utils/native-tools/CMakeLists.txt b/utils/native-tools/CMakeLists.txt
new file mode 100644
index 00000000..4713dc56
--- /dev/null
+++ b/utils/native-tools/CMakeLists.txt
@@ -0,0 +1,8 @@
+cmake_minimum_required(VERSION 3.0.2)
+project(native-tools)
+add_executable(bin2h bin2h.c)
+# Enforce no dressing for executable names, so the main script can find it
+set_target_properties(bin2h PROPERTIES OUTPUT_NAME bin2h)
+# Avoid configuration-dependent subdirectories while building with Visual Studio
+set_target_properties(bin2h PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}")
+set_target_properties(bin2h PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}")
diff --git a/utils/native-tools/bin2h.c b/utils/native-tools/bin2h.c
new file mode 100644
index 00000000..d9f5eb9a
--- /dev/null
+++ b/utils/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, "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;
+}