diff options
author | Ian C <ianc@noddybox.co.uk> | 2019-05-28 14:11:25 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2019-05-28 14:11:25 +0000 |
commit | 507031a6e6379c9f809a69c7acc80f6d352f5544 (patch) | |
tree | a10b86f6dba357fb1246598d9f4c4d494a9081ed /util.c | |
parent | c6556f860ffa70d309a01bd29beb8b9203430bc2 (diff) |
CPC code now loads disk image contents. Directory manipulation still to do.
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 172 |
1 files changed, 172 insertions, 0 deletions
@@ -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 +*/ |