From 507031a6e6379c9f809a69c7acc80f6d352f5544 Mon Sep 17 00:00:00 2001 From: Ian C Date: Tue, 28 May 2019 14:11:25 +0000 Subject: CPC code now loads disk image contents. Directory manipulation still to do. --- util.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 util.c (limited to 'util.c') 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 . + + ------------------------------------------------------------------------- + + Various utilities + +*/ +#include +#include +#include +#include + +#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 +*/ -- cgit v1.2.3