The LevAWC Project
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Pages
utils.c
Go to the documentation of this file.
1 /*
2  * _____
3  * ANSI / ___/
4  * / /__
5  * \___/
6  *
7  * Filename: utils.c
8  * Author : Dan Levin
9  * Date : Mon Feb 09 07:58:29 2015
10  * Version : 0.51
11  * ---
12  * Description: Miscellanenous utility functions
13  *
14  * Date Revision message
15  * 150331 This code ready for version 0.51
16  *
17  */
22 #include <stdio.h>
23 #include <stdlib.h>
24 
25 #include "utils.h"
26 
27 #ifndef OK
28 #define OK 0
29 #endif
30 
31 #ifndef TRUE
32 #define TRUE 1
33 #endif
34 
35 #ifndef FALSE
36 #define FALSE 0
37 #endif
38 
39 /* FUNCTION DEFINITIONS */
40 /* --- Function: int randint(int low, int high) --- */
41 int rand_int(int low, int high)
42 {
43  return low+rand()%(high-low+1);
44 }
45 
46 /* --- Function: int dn_up_lim(int maxval, int minval, int val) --- */
47 int dn_up_lim(int minval, int maxval, int val)
48 {
49  val = val < minval ? minval : val;
50  val = val > maxval ? maxval : val;
51 
52  return val;
53 }
54 
55 /* --- Function: int dn_lim(int minval, int val) --- */
56 int dn_lim(int minval, int val)
57 {
58  return val < minval ? minval : val;
59 }
60 
61 
62 /* --- Function: int up_lim(int maxval, int val) --- */
63 int up_lim(int maxval, int val)
64 {
65  return val > maxval ? maxval : val;
66 }
67 
68 /* --- Function: int maxval(int val1, int val2) --- */
69 int maxval(int val1, int val2)
70 {
71  return val1>val2 ? val1 : val2;
72 }
73 
74 /* --- Function: int minval(int val1, int val2) --- */
75 int minval(int val1, int val2)
76 {
77  return val1<val2 ? val1 : val2;
78 }
79 
80 /* --- Function: int isfloat2(char *c) --- */
81 int isfloat2(char *c)
82 {
83  int isnum=0, wfe=1;
84  int i=0;
85 
86  if (strlen(c)==0)
87  return(FALSE);
88 
89  if ((c[i]=='+') || (c[i]=='-'))
90  i++;
91  while ((c[i] >= '0') && (c[i] <= '9')) {
92  isnum = TRUE;
93  i++;
94  }
95  if (c[i]=='.') {
96  i++;
97  while ((c[i] >= '0') && (c[i] <= '9')) {
98  isnum = TRUE;
99  i++;
100  }
101  }
102  if ((c[i]=='e') || (c[i]=='E')) {
103  wfe = FALSE;
104  i++;
105  if ((c[i]=='+') || (c[i]=='-'))
106  i++;
107  while ((c[i] >= '0') && (c[i] <= '9')) {
108  wfe = TRUE;
109  i++;
110  }
111  }
112  if ((c[i]=='f') || (c[i]=='F') || (c[i]=='l') || (c[i]=='L'))
113  i++;
114 
115  if ((c[i]=='\0') && isnum && wfe)
116  return(TRUE);
117  else
118  return(FALSE);
119 }
120 
121 /* --- Function: int isfloat(char *str) --- */
122 int isfloat(char *str)
123 {
124  int i = 0;
125 
126  if ( str[i] == '-' )
127  i++;
128 
129  while( str[i] >= '0' && str[i] <= '9' )
130  i++;
131 
132  if (!str[i])
133  return TRUE;
134 
135  if (str[i] == '.' )
136  i++;
137 
138  while( str[i] >= '0' && str[i] <= '9' )
139  i++;
140 
141  if (!str[i])
142  return TRUE;
143 
144  return FALSE;
145 }
146 
147 /* --- Function: int isunsigned(char *str) --- */
148 int isunsigned(char *str)
149 {
150  int i = 0;
151 
152  if ( str[i] == '+' )
153  i++;
154 
155  while( str[i] >= '0' && str[i] <= '9' )
156  i++;
157 
158  if (!str[i])
159  return TRUE;
160 
161  return FALSE;
162 }
163 
164 /* --- Function: int isunsignedfloat(char *str) --- */
165 int isunsignedfloat(char *str)
166 {
167  int i = 0;
168 
169  if ( str[i] == '+' )
170  i++;
171 
172  while( str[i] >= '0' && str[i] <= '9' )
173  i++;
174 
175  if (!str[i])
176  return TRUE;
177 
178  if (str[i] == '.' )
179  i++;
180 
181  while( str[i] >= '0' && str[i] <= '9' )
182  i++;
183 
184  if (!str[i])
185  return TRUE;
186 
187  return FALSE;
188 }
189 
190 /* --- Function: char *strmove(char *to, char *from) --- */
191 char *strmove (char *to, char *from)
192 {
193  return memmove (to, from, strlen (from) + 1); /* + 1 because of termination */
194 }
195 
196 /* --- Function: char *strins (char *dest, const char *ins) --- */
197 char *strins (char *dest, const char *ins)
198 {
199  strmove (dest + strlen (ins), dest);
200  memcpy (dest, ins, strlen (ins));
201 
202  return dest;
203 }
204 
205 /* --- Function: char *strtrim(char *str, int (*left) (int), int (*right) (int)) --- */
206 char *strtrim(char *str, int (*left) (int), int (*right) (int))
207 {
208  if (left)
209  {
210  char *p = str;
211 
212  while (*p && left ((int) *p))
213  p++;
214 
215  if (p - str)
216  strmove (str, p);
217  }
218 
219  if (right)
220  {
221  char *p = strchr (str, 0);
222 
223  while ((p - 1) - str && right ((int) *(p - 1)))
224  p--;
225 
226  *p = 0;
227  }
228 
229  return str;
230 }
231 
232 /* --- Function: char *strtriml(char *str) --- */
233 char *strtriml (char *str)
234 {
235  return strtrim(str, isspace, NULL);
236 }
237 
238 /* --- Function: char *strtrimr(char *str) --- */
239 char *strtrimr(char *str)
240 {
241  return strtrim (str, NULL, isspace);
242 }
243 
244 /* --- Function: int menu(const int lo_sel, const int hi_sel) --- */
245 int menu(const char *menurow, const int lo_sel, const int hi_sel)
246 {
247  int selection;
248 
249  my_clearscrn();
250 
251  selection = read_int(menurow, lo_sel, hi_sel);
252 
253  return selection;
254 }
255 
256 /* --- Function: int read_int(char *prompt, int lo_val, int hi_val) --- */
257 int read_int(const char *prompt, const int lo_val, const int hi_val)
258 {
259  int retval, input, val_ok;
260 
261  /* Initialize 'val_ok' - this is crucial.. */
262  val_ok = FALSE;
263 
264  do
265  {
266  printf("\n%s", prompt);
267  /* Print tail to prompt - if 'lo_val' differ from 'hi_val'.. */
268  if (lo_val != hi_val)
269  printf("<%d-%d>+<Enter>: ", lo_val, hi_val);
270 
271  /* Read user input stdin.. */
272  retval = scanf("%d", &input);
273 
274  if (retval == 1) /* If valid integer input.. */
275  {
276  if (lo_val != hi_val)
277  {
278  /* Check whether input is within interval 'lo_val' to 'hi_val'.. */
279  val_ok = is_val_ok(input, lo_val, hi_val);
280  if (!val_ok)
281  printf("Invalid selection - use <%d> to <%d>...!", lo_val, hi_val);
282  }
283  else
284  val_ok =TRUE;
285  }
286  else
287  printf("Invalid input - integer only!");
288 
289  /* Flush keyb. buffer */
290  myflush(stdin);
291 
292  } while (retval == EOF || !val_ok);
293 
294  return input;
295 }
296 
297 /* --- Function: int is_val_ok(const int val, const int lo_val, const int hi_val) --- */
298 int is_val_ok(const int val, const int lo_val, const int hi_val)
299 {
300  return (val>=lo_val && val<=hi_val) ? TRUE : FALSE;
301 }
302 
303 /* --- Function: void my_clearscrn(void) --- */
304 void my_clearscrn(void)
305 {
306 #ifdef __unix__
307  system("clear");
308 #elif _WIN32
309  system("cls");
310 #endif
311 }
312 
313 /* --- Function: void prompt_and_pause(char *message) --- */
314 void prompt_and_pause(char *message)
315 {
316  int ch;
317 
318  printf("%s", message);
319  printf(" - Hit <Enter> to continue...");
320  ch = getchar();
321  if (ch == '\n')
322  ungetc(ch, stdin);
323  myflush(stdin);
324 }
325 
326 int read_char(const char *prompt, const int lo_val, const int hi_val, int (*char_ok)(int ch))
327 {
328  int retval, input, val_ok;
329 
330  /* Initialize 'val_ok' - this is crucial.. */
331  val_ok = FALSE;
332 
333  do
334  {
335  printf("\n%s", prompt);
336  /* Print tail to prompt - if 'lo_val' differ from 'hi_val'.. */
337  if (lo_val != hi_val)
338  printf("<%c-%c>+<Enter>: ", lo_val, hi_val);
339 
340  /* Read user input stdin.. */
341  /* retval = scanf("%c", &input); */
342  input = getchar();
343  retval = char_ok(input);
344 
345  if (retval != FALSE) /* If valid integer input.. */
346  {
347  if (lo_val != hi_val)
348  {
349  /* Check whether input is within interval 'lo_val' to 'hi_val'.. */
350  val_ok = is_val_ok(input, lo_val, hi_val);
351  if (!val_ok)
352  printf("Invalid selection - use <%c> to <%c>...!", lo_val, hi_val);
353  }
354  else
355  val_ok =TRUE;
356  }
357  else
358  printf("Invalid character input!");
359 
360  /* Flush keyb. buffer */
361  myflush(stdin);
362 
363  } while (retval == FALSE || !val_ok);
364 
365  return input;
366 }
367 
368 void myflush(FILE *in)
369 {
370  int ch;
371 
372  do
373  ch = fgetc(in);
374  while (ch != EOF && ch != '\n');
375 
376  clearerr (in);
377 }