-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathutils.h
194 lines (161 loc) · 3.65 KB
/
utils.h
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
/*
* _____
* ANSI / ___/
* / /__
* \___/
*
* Filename: utils.h
* Author : Dan Levin
* Date : Sun Feb 08 18:41:33 2015
* Version : 0.51
* ---
* Description: Description: Miscellanenous utility functions
*
* Date Revision message
* 150331 This code ready for version 0.51
*
*/
/**
* @file utils.h
**/
#ifndef _UTILS_H_
#define _UTILS_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifdef __cplusplus
extern "C" {
#endif
/* SOME USEFUL MACROS */
/* Check if memory allocation failed - Usage: MALCHK(string); */
/**
* Macro for handling dynamic memory allocation failure
*
* Check if memory allocation failed - Usage: MALCHK(pointer to dynamically allocated memory);
*
**/
#define MALCHK(x) if(!x){fprintf(stderr, "Out of memory, file: %s, line: %d", __FILE__, __LINE__); exit(EXIT_FAILURE);}
/**
* Macro for handling file opening failure
*
* Check if a file was opened properly - Usage: FILCHK(fp);
*
**/
#define FILCHK(x) if(!x){fprintf(stderr, "File opening failure - in file: %s, line: %d", __FILE__, __LINE__); exit(EXIT_FAILURE);}
/* FUNCTION-DECLARATIONS */
/**
* Generate random integer values in an interval
*
**/
int rand_int(int low, int high);
/**
* Set both a lower and upper bounds for an integer value - i.e. force it into a given interval
*
**/
int dn_up_lim(int minval, int maxval, int val);
/**
* Set a lower bound for an integer value
*
**/
int dn_lim(int minval, int val);
/**
* Set an upper bound for an integer value
*
**/
int up_lim(int maxval, int val);
/**
* Return the largest of two integer values
*
**/
int maxval(int val1, int val2);
/**
* Return the smallest of two integer values
*
**/
int minval(int val1, int val2);
/**
* Determine if a string is a float..
*
**/
int isfloat2(char *c);
/**
* Determine if a string is a float..
*
**/
int isfloat(char *str);
/**
* Determine if a string is an unsigned integer..
*
**/
int isunsigned(char *str);
/**
* Determine if a string is an unsigned float..
*
**/
int isunsignedfloat(char *str);
/**
* Move a string in memory...
*
**/
char *strmove (char *to, char *from);
/**
* Insert a string into another..
*
**/
char *strins (char *dest, const char *ins);
/**
* Trim both ends of a string
*
**/
char *strtrim (char *str, int (*left) (int), int (*right) (int));
/**
* Trim left side(=skip blanks) of a string
*
**/
char *strtriml (char *str);
/**
* Trim rigt side(=skip trailing blanks) of a string
*
**/
char *strtrimr (char *str);
/**
* Read integer from stdin - with some error handling
*
**/
int read_int(const char *prompt, const int lo_val, const int hi_val);
/**
* Read character from stdin - with some error handling
*
**/
int read_char(const char *prompt, const int lo_val, const int hi_val, int (*char_ok)(int ch));
/**
* Rudimentary menu function
*
**/
int menu(const char *menurow, const int lo_sel, const int hi_sel);
/**
* Determine if a value is within a specified interval - or not
*
**/
int is_val_ok(const int val, const int lo_val, const int hi_val);
/**
* Clear screen - both in Windows and Linux
*
**/
void my_clearscrn(void);
/**
* Stop execution, show prompt - and wait for <Enter> keypress
*
**/
void prompt_and_pause(char *message);
/**
* Flush input/keyb buffer
*
**/
void myflush(FILE *in);
/* END-OF-FUNCTION-DECLARATIONS */
#ifdef __cplusplus
}
#endif
#endif /* _UTILS_H_ */