-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathutils.c
377 lines (300 loc) · 7.18 KB
/
utils.c
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* _____
* ANSI / ___/
* / /__
* \___/
*
* Filename: utils.c
* Author : Dan Levin
* Date : Mon Feb 09 07:58:29 2015
* Version : 0.51
* ---
* Description: Miscellanenous utility functions
*
* Date Revision message
* 150331 This code ready for version 0.51
*
*/
/**
* @file utils.c
**/
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"
#ifndef OK
#define OK 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/* FUNCTION DEFINITIONS */
/* --- Function: int randint(int low, int high) --- */
int rand_int(int low, int high)
{
return low+rand()%(high-low+1);
}
/* --- Function: int dn_up_lim(int maxval, int minval, int val) --- */
int dn_up_lim(int minval, int maxval, int val)
{
val = val < minval ? minval : val;
val = val > maxval ? maxval : val;
return val;
}
/* --- Function: int dn_lim(int minval, int val) --- */
int dn_lim(int minval, int val)
{
return val < minval ? minval : val;
}
/* --- Function: int up_lim(int maxval, int val) --- */
int up_lim(int maxval, int val)
{
return val > maxval ? maxval : val;
}
/* --- Function: int maxval(int val1, int val2) --- */
int maxval(int val1, int val2)
{
return val1>val2 ? val1 : val2;
}
/* --- Function: int minval(int val1, int val2) --- */
int minval(int val1, int val2)
{
return val1<val2 ? val1 : val2;
}
/* --- Function: int isfloat2(char *c) --- */
int isfloat2(char *c)
{
int isnum=0, wfe=1;
int i=0;
if (strlen(c)==0)
return(FALSE);
if ((c[i]=='+') || (c[i]=='-'))
i++;
while ((c[i] >= '0') && (c[i] <= '9')) {
isnum = TRUE;
i++;
}
if (c[i]=='.') {
i++;
while ((c[i] >= '0') && (c[i] <= '9')) {
isnum = TRUE;
i++;
}
}
if ((c[i]=='e') || (c[i]=='E')) {
wfe = FALSE;
i++;
if ((c[i]=='+') || (c[i]=='-'))
i++;
while ((c[i] >= '0') && (c[i] <= '9')) {
wfe = TRUE;
i++;
}
}
if ((c[i]=='f') || (c[i]=='F') || (c[i]=='l') || (c[i]=='L'))
i++;
if ((c[i]=='\0') && isnum && wfe)
return(TRUE);
else
return(FALSE);
}
/* --- Function: int isfloat(char *str) --- */
int isfloat(char *str)
{
int i = 0;
if ( str[i] == '-' )
i++;
while( str[i] >= '0' && str[i] <= '9' )
i++;
if (!str[i])
return TRUE;
if (str[i] == '.' )
i++;
while( str[i] >= '0' && str[i] <= '9' )
i++;
if (!str[i])
return TRUE;
return FALSE;
}
/* --- Function: int isunsigned(char *str) --- */
int isunsigned(char *str)
{
int i = 0;
if ( str[i] == '+' )
i++;
while( str[i] >= '0' && str[i] <= '9' )
i++;
if (!str[i])
return TRUE;
return FALSE;
}
/* --- Function: int isunsignedfloat(char *str) --- */
int isunsignedfloat(char *str)
{
int i = 0;
if ( str[i] == '+' )
i++;
while( str[i] >= '0' && str[i] <= '9' )
i++;
if (!str[i])
return TRUE;
if (str[i] == '.' )
i++;
while( str[i] >= '0' && str[i] <= '9' )
i++;
if (!str[i])
return TRUE;
return FALSE;
}
/* --- Function: char *strmove(char *to, char *from) --- */
char *strmove (char *to, char *from)
{
return memmove (to, from, strlen (from) + 1); /* + 1 because of termination */
}
/* --- Function: char *strins (char *dest, const char *ins) --- */
char *strins (char *dest, const char *ins)
{
strmove (dest + strlen (ins), dest);
memcpy (dest, ins, strlen (ins));
return dest;
}
/* --- Function: char *strtrim(char *str, int (*left) (int), int (*right) (int)) --- */
char *strtrim(char *str, int (*left) (int), int (*right) (int))
{
if (left)
{
char *p = str;
while (*p && left ((int) *p))
p++;
if (p - str)
strmove (str, p);
}
if (right)
{
char *p = strchr (str, 0);
while ((p - 1) - str && right ((int) *(p - 1)))
p--;
*p = 0;
}
return str;
}
/* --- Function: char *strtriml(char *str) --- */
char *strtriml (char *str)
{
return strtrim(str, isspace, NULL);
}
/* --- Function: char *strtrimr(char *str) --- */
char *strtrimr(char *str)
{
return strtrim (str, NULL, isspace);
}
/* --- Function: int menu(const int lo_sel, const int hi_sel) --- */
int menu(const char *menurow, const int lo_sel, const int hi_sel)
{
int selection;
my_clearscrn();
selection = read_int(menurow, lo_sel, hi_sel);
return selection;
}
/* --- Function: int read_int(char *prompt, int lo_val, int hi_val) --- */
int read_int(const char *prompt, const int lo_val, const int hi_val)
{
int retval, input, val_ok;
/* Initialize 'val_ok' - this is crucial.. */
val_ok = FALSE;
do
{
printf("\n%s", prompt);
/* Print tail to prompt - if 'lo_val' differ from 'hi_val'.. */
if (lo_val != hi_val)
printf("<%d-%d>+<Enter>: ", lo_val, hi_val);
/* Read user input stdin.. */
retval = scanf("%d", &input);
if (retval == 1) /* If valid integer input.. */
{
if (lo_val != hi_val)
{
/* Check whether input is within interval 'lo_val' to 'hi_val'.. */
val_ok = is_val_ok(input, lo_val, hi_val);
if (!val_ok)
printf("Invalid selection - use <%d> to <%d>...!", lo_val, hi_val);
}
else
val_ok =TRUE;
}
else
printf("Invalid input - integer only!");
/* Flush keyb. buffer */
myflush(stdin);
} while (retval == EOF || !val_ok);
return input;
}
/* --- Function: int is_val_ok(const int val, const int lo_val, const int hi_val) --- */
int is_val_ok(const int val, const int lo_val, const int hi_val)
{
return (val>=lo_val && val<=hi_val) ? TRUE : FALSE;
}
/* --- Function: void my_clearscrn(void) --- */
void my_clearscrn(void)
{
#ifdef __unix__
system("clear");
#elif _WIN32
system("cls");
#endif
}
/* --- Function: void prompt_and_pause(char *message) --- */
void prompt_and_pause(char *message)
{
int ch;
printf("%s", message);
printf(" - Hit <Enter> to continue...");
ch = getchar();
if (ch == '\n')
ungetc(ch, stdin);
myflush(stdin);
}
int read_char(const char *prompt, const int lo_val, const int hi_val, int (*char_ok)(int ch))
{
int retval, input, val_ok;
/* Initialize 'val_ok' - this is crucial.. */
val_ok = FALSE;
do
{
printf("\n%s", prompt);
/* Print tail to prompt - if 'lo_val' differ from 'hi_val'.. */
if (lo_val != hi_val)
printf("<%c-%c>+<Enter>: ", lo_val, hi_val);
/* Read user input stdin.. */
/* retval = scanf("%c", &input); */
input = getchar();
retval = char_ok(input);
if (retval != FALSE) /* If valid integer input.. */
{
if (lo_val != hi_val)
{
/* Check whether input is within interval 'lo_val' to 'hi_val'.. */
val_ok = is_val_ok(input, lo_val, hi_val);
if (!val_ok)
printf("Invalid selection - use <%c> to <%c>...!", lo_val, hi_val);
}
else
val_ok =TRUE;
}
else
printf("Invalid character input!");
/* Flush keyb. buffer */
myflush(stdin);
} while (retval == FALSE || !val_ok);
return input;
}
void myflush(FILE *in)
{
int ch;
do
ch = fgetc(in);
while (ch != EOF && ch != '\n');
clearerr (in);
}