forked from ideawu/icomet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.h
More file actions
203 lines (178 loc) · 3.68 KB
/
strings.h
File metadata and controls
203 lines (178 loc) · 3.68 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
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
#ifndef UTIL_STRING_H
#define UTIL_STRING_H
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string>
#ifndef PRId64
#define PRId64 "lld"
#endif
#ifndef PRIu64
#define PRIu64 "llu"
#endif
inline static
int is_empty_str(const char *str){
const char *p = str;
while(*p && isspace(*p)){
p++;
}
return *p == '\0';
}
/* 返回左边不包含空白字符的字符串的指针 */
inline static
char *ltrim(const char *str){
const char *p = str;
while(*p && isspace(*p)){
p++;
}
return (char *)p;
}
/* 返回指向字符串结尾的指针, 会修改字符串内容 */
inline static
char *rtrim(char *str){
char *p;
p = str + strlen(str) - 1;
while(p >= str && isspace(*p)){
p--;
}
*(++p) = '\0';
return p;
}
/* 返回左边不包含空白字符的字符串的指针 */
inline static
char *trim(char *str){
char *p;
p = ltrim(str);
rtrim(p);
return p;
}
inline static
std::string real_dirname(const char *filepath){
std::string dir;
if(filepath[0] != '/'){
char buf[1024];
char *p = getcwd(buf, sizeof(buf));
if(p != NULL){
dir.append(p);
}
}
const char *p = strrchr(filepath, '/');
if(p != NULL){
dir.append("/");
dir.append(filepath, p - filepath);
}
return dir;
}
inline static
std::string hexmem(const void *p, int size){
std::string ret;
char buf[4];
for(int i=0; i<size; i++){
char c = ((char *)p)[i];
if(isalnum(c) || isprint(c)){
ret.append(1, c);
}else{
switch(c){
case '\r':
ret.append("\\r", 2);
break;
case '\n':
ret.append("\\n", 2);
break;
default:
sprintf(buf, "\\%02x", (unsigned char)c);
ret.append(buf, 3);
}
}
}
return ret;
}
// TODO: mem_printf("%5c%d%s", p, size);
static inline
void dump(const void *p, int size, const char *msg = NULL){
if(msg == NULL){
printf("dump <");
}else{
printf("%s <", msg);
}
std::string s = hexmem(p, size);
printf("%s>\n", s.c_str());
}
static inline
int str_to_int(const char *p, int size){
return atoi(std::string(p, size).c_str());
}
static inline
std::string int_to_str(int v){
char buf[21] = {0};
snprintf(buf, sizeof(buf), "%d", v);
return std::string(buf);
}
static inline
int64_t str_to_int64(const char *p, int size){
return atoll(std::string(p, size).c_str());
}
static inline
std::string int64_to_str(int64_t v){
char buf[21] = {0};
snprintf(buf, sizeof(buf), "%"PRId64"", v);
return std::string(buf);
}
static inline
int64_t str_to_uint64(const char *p, int size){
return strtol(std::string(p, size).c_str(), (char **)NULL, 10);
}
static inline
std::string uint64_to_str(uint64_t v){
char buf[21] = {0};
snprintf(buf, sizeof(buf), "%"PRIu64"", v);
return std::string(buf);
}
static inline
double str_to_double(const char *p, int size){
return atof(std::string(p, size).c_str());
}
static inline
std::string double_to_str(double v){
char buf[21] = {0};
if(v - floor(v) == 0){
snprintf(buf, sizeof(buf), "%.0f", v);
}else{
snprintf(buf, sizeof(buf), "%f", v);
}
return std::string(buf);
}
static inline
int parse_ip_port(const char *addr, std::string *ip, int *port){
char *sep = (char *)strchr(addr, ':');
if(!sep){
return -1;
}
ip->assign(addr, sep - addr);
*port = atoi(sep + 1);
if(*port < 0 || *port > 65535){
return -1;
}
return 0;
}
// is big endia. TODO: auto detect
#if 0
#define big_endian(v) (v)
#else
static inline
uint16_t big_endian(uint16_t v){
return (v>>8) | (v<<8);
}
static inline
uint32_t big_endian(uint32_t v){
return (v >> 24) | ((v >> 8) & 0xff00) | ((v << 8) & 0xff0000) | (v << 24);
}
static inline
uint64_t big_endian(uint64_t v){
uint32_t h = v >> 32;
uint32_t l = v & 0xffffffffull;
return big_endian(h) | ((uint64_t)big_endian(l) << 32);
}
#endif
#endif