-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cc
More file actions
157 lines (120 loc) · 2.14 KB
/
Copy pathUtils.cc
File metadata and controls
157 lines (120 loc) · 2.14 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdarg.h>
#include "dx.h"
#define IS_WHITE(c) ((c == ' ') || (c == '\t'))
bool Utils::isHexChar(char c) {
c = tolower(c);
if ((c >= '0') && (c <= '9')) {
return YES;
}
if ((c >= 'a') && (c <= 'f')) {
return YES;
}
return NO;
}
int Utils::hexVal(char c) {
c = tolower(c);
if ((c >= '0') && (c <= '9')) {
return c - '0';
}
if ((c >= 'a') && (c <= 'f')) {
return c - 'a' + 10;
}
return -1;
}
long Utils::hex2int(char *str) {
int res = 0;
while (*str) {
if (!isHexChar(*str)) {
throw ConversionException();
}
res = (res << 4) | hexVal(*str);
str++;
}
return res;
}
long Utils::parseAddress(char *str) {
long res;
try {
if ((strncmp(str, "0x", 2) == 0) || (strncmp(str, "0X", 2) == 0)) {
res = hex2int(&str[2]);
}
else if (strncmp(str, "$", 1) == 0) {
res = hex2int(&str[1]);
}
else {
res = atol(str);
}
}
catch (ConversionException e) {
res = -1;
}
return res;
}
bool Utils::isFile(const char *path) {
struct stat inf;
int res = stat(path, &inf);
if (res) {
return NO;
}
if (S_ISREG(inf.st_mode)) {
return YES;
}
return NO;
}
char *Utils::truncStr(char *s, char c) {
char *ch = strchr(s, c);
if (ch != NULL) {
*ch = '\0';
}
return s;
}
void Utils::ltrim(char *s) {
char *sp = s;
if (IS_WHITE(*sp)) {
/* Leading space(s) */
int i;
int j;
while (IS_WHITE(*sp)) {
sp++;
}
while (*sp) {
*s++ = *sp++;
}
*s = EOS;
}
}
void Utils::rtrim(char *s) {
int len = strlen(s)-1;
int i = len;
while ((i >= 0) && IS_WHITE(s[i])) {
i--;
}
if (i < len) {
s[i+1] = '\0';
}
}
void Utils::trim(char *s) {
ltrim(s);
rtrim(s);
}
char Utils::printableChar(char ch) {
if ((ch < ' ') || (ch > '~')) {
return '.';
}
return ch;
}
void Utils::abortf(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
exit(1);
}