aboutsummaryrefslogtreecommitdiff
path: root/src/header
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-02-20 19:04:21 +0100
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-03-30 08:20:21 +0200
commit4765653cb3af905a9b20f1e5f2277e50d801c43b (patch)
tree9c9f914e5ea9b19f2312cd43221df37d19dec2f3 /src/header
parentc9754ac4300abad20d942ad18bc5bf611a0b3a2e (diff)
downloadjancity-4765653cb3af905a9b20f1e5f2277e50d801c43b.tar.gz
Add metadata header to media files
The following properties are supported: - Sound: "loop". Must be either 0 or 1 - Images: "transparent". Must be either 0 or 1 These headers are only used for non-PS1 builds, since .TIM and .VAG files do already implement such information.
Diffstat (limited to 'src/header')
-rw-r--r--src/header/CMakeLists.txt5
-rw-r--r--src/header/inc/header.h18
-rw-r--r--src/header/src/header.c65
3 files changed, 88 insertions, 0 deletions
diff --git a/src/header/CMakeLists.txt b/src/header/CMakeLists.txt
new file mode 100644
index 0000000..e23d4fd
--- /dev/null
+++ b/src/header/CMakeLists.txt
@@ -0,0 +1,5 @@
+set(src "src/header.c")
+set(inc "inc")
+
+add_library(header ${src})
+target_include_directories(header PUBLIC ${inc})
diff --git a/src/header/inc/header.h b/src/header/inc/header.h
new file mode 100644
index 0000000..7514132
--- /dev/null
+++ b/src/header/inc/header.h
@@ -0,0 +1,18 @@
+#ifndef HEADER_H
+#define HEADER_H
+
+#include <stdbool.h>
+#include <stdio.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+int header_load_bool(FILE *f, const char *key, bool *value);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* HEADER_H */
diff --git a/src/header/src/header.c b/src/header/src/header.c
new file mode 100644
index 0000000..bfc6534
--- /dev/null
+++ b/src/header/src/header.c
@@ -0,0 +1,65 @@
+#include <header.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int header_load_bool(FILE *const f, const char *const key, bool *const value)
+{
+ for (size_t i = 0; i < strlen(key); i++)
+ {
+ const int ci = fgetc(f);
+
+ if (ferror(f) || ci != key[i])
+ {
+ fprintf(stderr, "%s: failed to read property %s\n",
+ __func__, key);
+ return -1;
+ }
+ }
+
+ {
+ const int ci = fgetc(f);
+
+ if (ferror(f) || ci != '=')
+ {
+ fprintf(stderr, "%s: expected '=' on property %s\n",
+ __func__, key);
+ return -1;
+ }
+ }
+
+ {
+ static const char exp[] = "1";
+ char str[sizeof exp];
+
+ for (char *c = str; c - str < sizeof str; c++)
+ {
+ const int ci = fgetc(f);
+
+ if (ferror(f) || ci == EOF)
+ {
+ fprintf(stderr, "%s: could not read value for property %s\n",
+ __func__, key);
+ return -1;
+ }
+
+ *c = ci;
+ }
+
+ errno = 0;
+ const unsigned long out = strtoul(str, NULL, 10);
+
+ if (errno || (out != 0 && out != 1))
+ {
+ fprintf(stderr, "%s: unexpected value for property %s: %lu\n",
+ __func__, key, out);
+ return -1;
+ }
+
+ *value = out;
+ }
+
+ return 0;
+}