-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.c
70 lines (53 loc) · 1.16 KB
/
helpers.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
#include "helpers.h"
/* convert a string to integer - return -1 if the string isn't a valid representation of
an integer or if value is too big\small */
int strToInt(char *param)
{
char *validate;
int result;
result = strtol(param, &validate, 10);
if (result == LONG_MIN || result == LONG_MAX || *validate != '\0' || result < 0)
return -1;
return result;
}
/* check for an empty string (return 1 if true) */
int isEmpty(char *command)
{
int i;
if (command == '\0' || strcmp(command, "\n") == 0)
return 1;
for (i = 0; i < strlen(command) - 1; i++)
if (!isspace(command[i]))
return 0;
return 1;
}
/* remove whitespace before string */
char *ltrim(char *str)
{
if(str && *str) {
char *cur = str;
while(*cur && isspace(*cur))
++cur;
if(str != cur)
memmove(str, cur, strlen(cur) + 1);
}
return str;
}
/* remove whitespace after string */
char *rtrim(char *str)
{
if(str && *str) {
char *cur = str + strlen(str) - 1;
while(cur != str && isspace(*cur))
--cur;
cur[isspace(*cur) ? 0 : 1] = '\0';
}
return str;
}
/* remove whitespace before and after a string */
char *trim(char *str)
{
rtrim(str);
ltrim(str);
return str;
}