-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
220 lines (205 loc) · 5.72 KB
/
util.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
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "util.h"
//strip trailing whitespace and ctrl characters
char * CleanStringTR(char * sz)
{
//Easy. Just start at the end, and work back to first printing character.
char * p = sz;
//find the null
while(*p)++p;
//work backwards
while(p > sz)
{
if(!isgraph(*p)) *(p--) = 0;
else break;
}
return sz;
}
//Remove padding, CR, DESTRUCTIVE
char * CleanStringCR(char * sz)
{
//This modifies the input string by removing any
//leading or trailing whitespace, CR
char * p = sz; //Pointer to first char
char * q = sz; //Insertion point
int leadspace = 1; // Am I in leading whitepace?
while(*p)// While I haven't reached the null
{
if(!isgraph(*p)) //non-printing
{
if(isspace(*p))
{
if(leadspace)
p++; //drop leading whitespace
else
{
//Replace all whitepsace with spaces
*q=' ';
++q;
++p;
}
}
}
else
{
//Done with any lead space
leadspace = 0;
*q = *p;
++p;
++q;
}
}
*q = *p;//Copy final null.
//Get rid of trailing whitespace
sz = CleanStringTR(sz);
return sz; // Return modified string
}
//Remove padding, convert to lower case DESTRUCTIVE
char * CleanStringLC(char * sz)
{
//This modifies the input string by removing any
//leading or trailing whitespace and converting
//all uppercase characters to lowercase
sz = CleanStringCR(sz);
char * p = sz; //Pointer to first char
char * q = sz; //Insertion point
while(*p)// While I haven't reached the null
{
if(isspace(*p) || iscntrl(*p)) //whitespace
{
p++; //Ignore
}
else
{//tolower doesn't change non-uppercase
*q = tolower(*p);
++p;
++q;
}
}
*q = *p;//Copy final null.
return sz; // Return modified string
}
//Get an int from user
int GetInt(char * prompt)
{
int gotanint = 0;
int value = 0;
char * pos = NULL; //Pointer to location in buffer
char * buffer = (char *)malloc(INPUTBUFFSIZE);
if (!buffer)// DMA Failure
{
fprintf(stderr, "Error: DMA Failure.\n");
#ifdef _DEBUG
fflush(stdin); //Make sure there's nothing lurking in the buffer.
printf("Press Enter to Exit");
fgetc(stdin);
#endif
exit(EXIT_FAILURE);
}
do
{
printf("%s ",prompt);
fflush(stdout);
fflush(stdin);
if (!fgets(buffer, INPUTBUFFSIZE, stdin)) // Error talking to user
{
fprintf(stderr, "Error: Unable to get value from user.\n");
exit(EXIT_FAILURE);
#ifdef _DEBUG
fflush(stdin); //Make sure there's nothing lurking in the buffer.
printf("Press Enter to Exit");
fgetc(stdin);
#endif
}
value = strtol(buffer, &pos, 10); // Expect base 10. pos at final
//Was what we got an integer?
if (buffer[0] != '\n' && (*pos == '\n' || *pos == '\0'))
gotanint = 1; //looks good
} while (!gotanint);
//Free the memory
free(buffer);
return value;
}
//Get a y or n response from user. Y returns 1, else 0
//Will keep asking until it gets something intelligible.
int GetYorN(char * prompt)
{
int gotResponse = 0;
int retval = 0;
char * buffer = (char *)malloc(INPUTBUFFSIZE);
if (!buffer)// DMA Failure
{
fprintf(stderr, "Error: DMA Failure.\n");
#ifdef _DEBUG
fflush(stdin); //Make sure there's nothing lurking in the buffer.
printf("Press Enter to Exit");
fgetc(stdin);
exit(EXIT_FAILURE);
#endif
}
do
{
printf("%s ",prompt);
fflush(stdout);
fflush(stdin);
if (!fgets(buffer, INPUTBUFFSIZE, stdin)) // Error talking to user
{
fprintf(stderr, "Error: Unable to get value from user.\n");
#ifdef _DEBUG
fflush(stdin); //Make sure there's nothing lurking in the buffer.
printf("Press Enter to Exit");
fgetc(stdin);
exit(EXIT_FAILURE);
#endif
}
//We now have a response from the user. Is it what we're looking for?
buffer = CleanStringLC(buffer); //Convert to lcase, strip whitespace
if(!strcmp(buffer,"y") || !strcmp(buffer,"yes"))
{
gotResponse = 1;
retval = 1;
}
else if(!strcmp(buffer,"n") || !strcmp(buffer,"no"))
{
gotResponse = 1;
retval = 0;
}
else gotResponse = 0;
} while (!gotResponse);
//Free the memory
free(buffer);
return retval;
}
//Get a string from the user after a prompt
char * GetString(char * prompt, char * buffer, int buffersize)
{
//In this case, we will not acquire our own temporary buffer
//but use the one passed to us by the calling scope.
if (!buffer)// bad buffer location
{
fprintf(stderr, "Error: Buffer is a Null Pointer.\n");
#ifdef _DEBUG
fflush(stdin); //Make sure there's nothing lurking in the buffer.
printf("Press Enter to Exit");
fgetc(stdin);
exit(EXIT_FAILURE);
#endif
}
printf("%s ",prompt);
fflush(stdout);
fflush(stdin);
if (!fgets(buffer, buffersize, stdin)) // Error talking to user
{
fprintf(stderr, "Error: Unable to get value from user.\n");
#ifdef _DEBUG
fflush(stdin); //Make sure there's nothing lurking in the buffer.
printf("Press Enter to Exit");
fgetc(stdin);
exit(EXIT_FAILURE);
#endif
}
return CleanStringCR(buffer);
}