-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
123 lines (109 loc) · 3.15 KB
/
utils.c
File metadata and controls
123 lines (109 loc) · 3.15 KB
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
#include "utils.h"
void get_token(char cmd[], char token[], int *idx, int size) {
/*
* generates the next token contained in cmd appearing after the idx
* Arguments:
* char cmd[] - the cmd string
* char token[] - the resultant token
* int *idx - pointer to the starting index in cmd
* int size - the max size of the cmd & token
*/
int i;
int comma=0; /* flag for detecting the first comma */
/* skip valid blanks after idx */
while(*idx<size-1 && ((!comma&&cmd[*idx]==',') || cmd[*idx]==' ' || cmd[*idx]=='\t') && cmd[*idx]!='\0') {
(*idx)++;
comma = 1;
}
/*
at this point, idx indicates one of the following:
i) first character of token (typical case)
ii) null char (end of the string)
iii) max size (end of the array)
*/
/* construct the token until the first delimiter is recognized */
i=0;
while(*idx<size-1 && i<size-1 && cmd[*idx]!=' ' && cmd[*idx]!='\t' && cmd[*idx]!='\0' && cmd[*idx]!=',') {
token[i] = cmd[*idx];
(*idx)++;
i++;
}
token[i] = '\0'; /* make token into a valid string */
}
void get_substr(char str[], char tok[], int lb, int ub) {
int i; /* scans tok */
int comma=0; /* flag for detecting the first comma */
int idx = lb; /* scans str */
/* skip valid blanks after idx */
while(idx<=ub && ((!comma&&str[idx]==',') || str[idx]==' ' || str[idx]=='\t') && str[idx]!='\0') {
idx++;
comma = 1;
}
/*
at this point, idx indicates one of the following:
i) first character of token (typical case)
ii) null char (end of the string)
iii) max size (end of the array)
*/
/* construct the token until the first delimiter is recognized */
i=0;
while(idx<=ub && str[idx]!=' ' && str[idx]!='\t' && str[idx]!='\0' && str[idx]!=',') {
tok[i] = str[idx];
idx++;
i++;
}
tok[i] = '\0'; /* make token into a valid string */
}
int hex_to_int(char digit) {
/*
* converts the hex char into int
* Arguments:
* char digit - hex in char
* Returns:
* the corresponding int if digit is a valid hex, and -1 otherwise
*/
if('0' <= digit && digit <= '9') return (int)(digit-'0');
else if('A' <= digit && digit <= 'F') return 10+(int)(digit-'A');
else if('a' <= digit && digit <= 'f') return 10+(int)(digit-'a');
else return -1;
}
int parse_op(char op[]) {
/*
* converts the hex string into int, if valid
* Arguments:
* char op[] - string consisting of hex digits
* Returns:
* the corresponding int if op is not null, -1 if null, and -2 if invalid
*/
int basis,idx,ret,valid=1;
const int len = strlen(op);
ret = 0;
basis = 1;
idx = len - 1; /* last idx of op */
while(idx>=0) {
/* check validity */
valid *= (('0'<=op[idx] && op[idx]<='9') || ('a'<=op[idx] && op[idx]<='f') || ('A'<=op[idx] && op[idx]<='F')) ? 1 : 0;
/* construct start address from op */
ret += hex_to_int(op[idx]) * basis;
basis *= 16;
idx--;
}
if(!valid) return -2;
else if(!len) return -1;
else return ret;
}
int parse_dec(char dec[]) {
// TODO returns -1 if invalid
int basis, sum, idx;
const int len = strlen(dec);
sum = 0;
basis = 1;
idx = len - 1;
while(idx>=0) {
if(dec[idx]<'0' || dec[idx]>'9') return -1;
sum += basis * (int)(dec[idx] - '0');
basis *= 10;
idx--;
}
return sum;
}