summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'util.c')
-rw-r--r--util.c172
1 files changed, 172 insertions, 0 deletions
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..7fbc632
--- /dev/null
+++ b/util.c
@@ -0,0 +1,172 @@
+/*
+
+ DiskImageTool - Tool for manipulating disk images - CPC DSK images
+
+ Copyright (C) 2019 Ian Cowburn (ianc@noddybox.co.uk)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+ -------------------------------------------------------------------------
+
+ Various utilities
+
+*/
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include "util.h"
+
+/* ---------------------------------------- PRIVATE ROUTINES
+*/
+static void Chomp(char *p)
+{
+ size_t l = strlen(p);
+
+ while(l > 0 && p[l-1] == '\n')
+ {
+ p[--l] = 0;
+ }
+}
+
+/* ---------------------------------------- PUBLIC INTERFACES
+*/
+
+void *Malloc(size_t size)
+{
+ void *p;
+
+ p = malloc(size);
+
+ if (!p)
+ {
+ perror("malloc");
+ exit(EXIT_FAILURE);
+ }
+
+ return p;
+}
+
+void *Realloc(void *p, size_t size)
+{
+ void *new;
+
+ new = realloc(p, size);
+
+ if (!new)
+ {
+ perror("realloc");
+ exit(EXIT_FAILURE);
+ }
+
+ return new;
+}
+
+int GetLine(const char *prompt, char *buff, size_t buffsize)
+{
+ buff[0] = 0;
+
+ while(!buff[0])
+ {
+ printf("%s> ", prompt);
+ fflush(stdout);
+
+ if (!fgets(buff, buffsize, stdin))
+ {
+ return 0;
+ }
+
+ Chomp(buff);
+ }
+
+ return 1;
+}
+
+void *Load(const char *path, size_t *size)
+{
+ FILE *fp;
+ void *mem = NULL;
+
+ *size = 0;
+
+ if ((fp = fopen(path, "rb")))
+ {
+ fseek(fp, 0, SEEK_END);
+ *size = ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+ mem = Malloc(*size);
+ fread(mem, 1, *size, fp);
+ fclose(fp);
+ }
+
+ return mem;
+}
+
+int Save(const char *path, const void *buff, size_t buffsize)
+{
+ FILE *fp;
+
+ if ((fp = fopen(path, "wb")))
+ {
+ fwrite(buff, 1, buffsize, fp);
+ fclose(fp);
+ }
+ else
+ {
+ return 0;
+ }
+
+ return 1;
+}
+
+int CompareMem(const void *mem, const char *str)
+{
+ return memcmp(mem, str, strlen(str));
+}
+
+char GetOption(const char *prompt, const char *allowed, char default_val)
+{
+ char buff[80];
+
+ printf("%s> ", prompt);
+ fflush(stdout);
+
+ while(fgets(buff, sizeof buff, stdin))
+ {
+ Chomp(buff);
+
+ if (buff[0])
+ {
+ if (strchr(allowed, buff[0]))
+ {
+ return buff[0];
+ }
+ }
+ else
+ {
+ if (default_val)
+ {
+ return default_val;
+ }
+ }
+ }
+
+ return 0;
+}
+
+
+/*
+vim: ai sw=4 ts=8 expandtab
+*/