1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
*/
|