summaryrefslogtreecommitdiff
path: root/tools/gbagfx/util.c
diff options
context:
space:
mode:
authorYamaArashi <shadow962@live.com>2015-11-13 21:57:22 -0800
committerYamaArashi <shadow962@live.com>2015-11-13 21:57:22 -0800
commit4c733f3811d2b1bd35bf0474a79490706a4cd845 (patch)
tree0eea3502ae34b55611a6920490ed32b479f6b244 /tools/gbagfx/util.c
parent3d1b6597f3fc22547fe2021275b1818a92093ab8 (diff)
pokemon PNGs and palettes
Diffstat (limited to 'tools/gbagfx/util.c')
-rw-r--r--tools/gbagfx/util.c140
1 files changed, 140 insertions, 0 deletions
diff --git a/tools/gbagfx/util.c b/tools/gbagfx/util.c
new file mode 100644
index 000000000..180b93d9b
--- /dev/null
+++ b/tools/gbagfx/util.c
@@ -0,0 +1,140 @@
+// Copyright (c) 2015 YamaArashi
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+#include <errno.h>
+#include <limits.h>
+#include "global.h"
+#include "util.h"
+
+bool ParseNumber(char *s, char **end, int radix, int *intValue)
+{
+ char *localEnd;
+
+ if (end == NULL)
+ end = &localEnd;
+
+ errno = 0;
+
+ const long longValue = strtol(s, end, radix);
+
+ if (*end == s)
+ return false; // not a number
+
+ if ((longValue == LONG_MIN || longValue == LONG_MAX) && errno == ERANGE)
+ return false;
+
+ if (longValue > INT_MAX)
+ return false;
+
+ if (longValue < INT_MIN)
+ return false;
+
+ *intValue = (int)longValue;
+
+ return true;
+}
+
+char *GetFileExtension(char *path)
+{
+ char *extension = path;
+
+ while (*extension != 0)
+ extension++;
+
+ while (extension > path && *extension != '.')
+ extension--;
+
+ if (extension == path)
+ return NULL;
+
+ extension++;
+
+ if (*extension == 0)
+ return NULL;
+
+ return extension;
+}
+
+void ExpectFileExtension(char *path, char *expectedExtension)
+{
+ char *extension = GetFileExtension(path);
+
+ if (extension == NULL)
+ FATAL_ERROR("\"%s\" has no file extension.\n", path);
+
+ if (strcmp(extension, expectedExtension) != 0)
+ FATAL_ERROR("Unexpected file extension \"%s\". Expected \"%s\".\n", extension, expectedExtension);
+}
+
+void AddFileExtension(char *path, char *extension)
+{
+ int pathLength = strlen(path);
+ int extensionLength = strlen(extension);
+
+ if (pathLength + 1 + extensionLength > GBAGFX_MAX_PATH)
+ FATAL_ERROR("\"%s\" is too long a path to add the extension \"%s\".\n", path, extension);
+
+ path[pathLength] = '.';
+ memcpy(path + pathLength + 1, extension, extensionLength);
+ path[pathLength + 1 + extensionLength] = 0;
+}
+
+void RemoveFileExtension(char *path)
+{
+ char *extension = GetFileExtension(path);
+
+ if (extension == NULL)
+ FATAL_ERROR("\"%s\" doesn't have a file extension.\n", path);
+
+ extension--;
+
+ *extension = 0;
+}
+
+void ChangeFileExtension(char *path, char *extension)
+{
+ RemoveFileExtension(path);
+ AddFileExtension(path, extension);
+}
+
+unsigned char *ReadWholeFile(char *path, int *size)
+{
+ FILE *fp = fopen(path, "rb");
+
+ if (fp == NULL)
+ FATAL_ERROR("Failed to open \"%s\" for reading.\n", path);
+
+ fseek(fp, 0, SEEK_END);
+
+ *size = ftell(fp);
+
+ unsigned char *buffer = malloc(*size);
+
+ if (buffer == NULL)
+ FATAL_ERROR("Failed to allocate memory for reading \"%s\".\n", path);
+
+ rewind(fp);
+
+ if (fread(buffer, *size, 1, fp) != 1)
+ FATAL_ERROR("Failed to read \"%s\".\n", path);
+
+ fclose(fp);
+
+ return buffer;
+}
+
+void WriteWholeFile(char *path, void *buffer, int bufferSize)
+{
+ FILE *fp = fopen(path, "wb");
+
+ if (fp == NULL)
+ FATAL_ERROR("Failed to open \"%s\" for writing.\n", path);
+
+ if (fwrite(buffer, bufferSize, 1, fp) != 1)
+ FATAL_ERROR("Failed to write to \"%s\".\n", path);
+
+ fclose(fp);
+}