Skip to content

Commit 3770549

Browse files
author
hewei.it
committedSep 27, 2021
format code
1 parent 1661606 commit 3770549

21 files changed

+138
-116
lines changed
 

‎cpputil/hmain.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ typedef struct main_ctx_s {
2727
char** os_argv;
2828
char** save_argv;
2929
char* cmdline;
30-
keyval_t arg_kv;
31-
StringList arg_list;
30+
keyval_t arg_kv;
31+
hv::StringList arg_list;
3232

3333
// env
3434
int envc;

‎cpputil/hpath.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "hpath.h"
22

3+
#include "hplatform.h"
4+
35
bool HPath::exists(const char* path) {
46
return access(path, 0) == 0;
57
}

‎cpputil/hpath.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <string> // for std::string
55

66
#include "hexport.h"
7-
#include "hplatform.h" // for stat
87

98
class HV_EXPORT HPath {
109
public:
@@ -17,10 +16,10 @@ class HV_EXPORT HPath {
1716
// dirname = /mnt/share/image
1817
// filename = test
1918
// suffixname = jpg
20-
static std::string basename(const std::string& str);
21-
static std::string dirname(const std::string& str);
22-
static std::string filename(const std::string& str);
23-
static std::string suffixname(const std::string& str);
19+
static std::string basename(const std::string& filepath);
20+
static std::string dirname(const std::string& filepath);
21+
static std::string filename(const std::string& filepath);
22+
static std::string suffixname(const std::string& filepath);
2423

2524
static std::string join(const std::string& dir, const std::string& filename);
2625
};

‎cpputil/hstring.cpp

+24-20
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ static inline int vscprintf(const char* fmt, va_list ap) {
99
return vsnprintf(NULL, 0, fmt, ap);
1010
}
1111

12-
string asprintf(const char* fmt, ...) {
12+
namespace hv {
13+
14+
std::string asprintf(const char* fmt, ...) {
1315
va_list ap;
1416
va_start(ap, fmt);
1517
int len = vscprintf(fmt, ap);
1618
va_end(ap);
1719

18-
string str;
20+
std::string str;
1921
str.reserve(len+1);
2022
// must resize to set str.size
2123
str.resize(len);
@@ -27,7 +29,7 @@ string asprintf(const char* fmt, ...) {
2729
return str;
2830
}
2931

30-
StringList split(const string& str, char delim) {
32+
StringList split(const std::string& str, char delim) {
3133
/*
3234
std::stringstream ss;
3335
ss << str;
@@ -52,7 +54,7 @@ StringList split(const string& str, char delim) {
5254
return res;
5355
}
5456

55-
hv::KeyValue splitKV(const string& str, char kv_kv, char k_v) {
57+
hv::KeyValue splitKV(const std::string& str, char kv_kv, char k_v) {
5658
enum {
5759
s_key,
5860
s_value,
@@ -87,26 +89,26 @@ hv::KeyValue splitKV(const string& str, char kv_kv, char k_v) {
8789
return kvs;
8890
}
8991

90-
string trim(const string& str, const char* chars) {
91-
string::size_type pos1 = str.find_first_not_of(chars);
92-
if (pos1 == string::npos) return "";
92+
std::string trim(const std::string& str, const char* chars) {
93+
std::string::size_type pos1 = str.find_first_not_of(chars);
94+
if (pos1 == std::string::npos) return "";
9395

94-
string::size_type pos2 = str.find_last_not_of(chars);
96+
std::string::size_type pos2 = str.find_last_not_of(chars);
9597
return str.substr(pos1, pos2-pos1+1);
9698
}
9799

98-
string trimL(const string& str, const char* chars) {
99-
string::size_type pos = str.find_first_not_of(chars);
100-
if (pos == string::npos) return "";
100+
std::string trimL(const std::string& str, const char* chars) {
101+
std::string::size_type pos = str.find_first_not_of(chars);
102+
if (pos == std::string::npos) return "";
101103
return str.substr(pos);
102104
}
103105

104-
string trimR(const string& str, const char* chars) {
105-
string::size_type pos = str.find_last_not_of(chars);
106+
std::string trimR(const std::string& str, const char* chars) {
107+
std::string::size_type pos = str.find_last_not_of(chars);
106108
return str.substr(0, pos+1);
107109
}
108110

109-
string trim_pairs(const string& str, const char* pairs) {
111+
std::string trim_pairs(const std::string& str, const char* pairs) {
110112
const char* s = str.c_str();
111113
const char* e = str.c_str() + str.size() - 1;
112114
const char* p = pairs;
@@ -121,15 +123,17 @@ string trim_pairs(const string& str, const char* pairs) {
121123
return is_pair ? str.substr(1, str.size()-2) : str;
122124
}
123125

124-
string replace(const string& str, const string& find, const string& rep) {
125-
string::size_type pos = 0;
126-
string::size_type a = find.size();
127-
string::size_type b = rep.size();
126+
std::string replace(const std::string& str, const std::string& find, const std::string& rep) {
127+
std::string::size_type pos = 0;
128+
std::string::size_type a = find.size();
129+
std::string::size_type b = rep.size();
128130

129-
string res(str);
130-
while ((pos = res.find(find, pos)) != string::npos) {
131+
std::string res(str);
132+
while ((pos = res.find(find, pos)) != std::string::npos) {
131133
res.replace(pos, a, rep);
132134
pos += b;
133135
}
134136
return res;
135137
}
138+
139+
} // end namespace hv

‎cpputil/hstring.h

+17-16
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
#include <sstream>
77

88
#include "hexport.h"
9-
#include "hbase.h"
9+
#include "hplatform.h"
1010
#include "hmap.h"
1111

12-
using std::string;
13-
typedef std::vector<string> StringList;
12+
#define SPACE_CHARS " \t\r\n"
13+
#define PAIR_CHARS "{}[]()<>\"\"\'\'``"
14+
15+
namespace hv {
16+
17+
typedef std::vector<std::string> StringList;
1418

1519
// std::map<std::string, std::string, StringCaseLess>
1620
class StringCaseLess : public std::less<std::string> {
@@ -20,7 +24,6 @@ class StringCaseLess : public std::less<std::string> {
2024
}
2125
};
2226

23-
namespace hv {
2427
// NOTE: low-version NDK not provide std::to_string
2528
template<typename T>
2629
HV_INLINE std::string to_string(const T& t) {
@@ -36,20 +39,18 @@ HV_INLINE T from_string(const std::string& str) {
3639
iss >> t;
3740
return t;
3841
}
39-
}
40-
41-
#define SPACE_CHARS " \t\r\n"
42-
#define PAIR_CHARS "{}[]()<>\"\"\'\'``"
4342

44-
HV_EXPORT string asprintf(const char* fmt, ...);
43+
HV_EXPORT std::string asprintf(const char* fmt, ...);
4544
// x,y,z
46-
HV_EXPORT StringList split(const string& str, char delim = ',');
45+
HV_EXPORT StringList split(const std::string& str, char delim = ',');
4746
// user=amdin&pswd=123456
48-
HV_EXPORT hv::KeyValue splitKV(const string& str, char kv_kv = '&', char k_v = '=');
49-
HV_EXPORT string trim(const string& str, const char* chars = SPACE_CHARS);
50-
HV_EXPORT string trimL(const string& str, const char* chars = SPACE_CHARS);
51-
HV_EXPORT string trimR(const string& str, const char* chars = SPACE_CHARS);
52-
HV_EXPORT string trim_pairs(const string& str, const char* pairs = PAIR_CHARS);
53-
HV_EXPORT string replace(const string& str, const string& find, const string& rep);
47+
HV_EXPORT hv::KeyValue splitKV(const std::string& str, char kv_kv = '&', char k_v = '=');
48+
HV_EXPORT std::string trim(const std::string& str, const char* chars = SPACE_CHARS);
49+
HV_EXPORT std::string trimL(const std::string& str, const char* chars = SPACE_CHARS);
50+
HV_EXPORT std::string trimR(const std::string& str, const char* chars = SPACE_CHARS);
51+
HV_EXPORT std::string trim_pairs(const std::string& str, const char* pairs = PAIR_CHARS);
52+
HV_EXPORT std::string replace(const std::string& str, const std::string& find, const std::string& rep);
53+
54+
} // end namespace hv
5455

5556
#endif // HV_STRING_H_

‎cpputil/iniparser.cpp

+27-28
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "hfile.h"
1010
#include "hbase.h"
1111

12+
using namespace hv;
13+
1214
/**********************************
1315
# div
1416
@@ -29,8 +31,8 @@ class IniNode {
2931
INI_NODE_TYPE_DIV,
3032
INI_NODE_TYPE_SPAN,
3133
} type;
32-
string label; // section|key|comment
33-
string value;
34+
std::string label; // section|key|comment
35+
std::string value;
3436
std::list<IniNode*> children;
3537

3638
virtual ~IniNode() {
@@ -56,7 +58,7 @@ class IniNode {
5658
}
5759
}
5860

59-
IniNode* Get(const string& label, Type type = INI_NODE_TYPE_KEY_VALUE) {
61+
IniNode* Get(const std::string& label, Type type = INI_NODE_TYPE_KEY_VALUE) {
6062
for (auto pNode : children) {
6163
if (pNode->type == type && pNode->label == label) {
6264
return pNode;
@@ -71,22 +73,22 @@ class IniSection : public IniNode {
7173
IniSection() : IniNode(), section(label) {
7274
type = INI_NODE_TYPE_SECTION;
7375
}
74-
string &section;
76+
std::string &section;
7577
};
7678

7779
class IniKeyValue : public IniNode {
7880
public:
7981
IniKeyValue() : IniNode(), key(label) {
8082
type = INI_NODE_TYPE_KEY_VALUE;
8183
}
82-
string &key;
84+
std::string &key;
8385
};
8486

8587
class IniComment : public IniNode {
8688
public:
8789
IniComment() : IniNode(), comment(label) {
8890
}
89-
string &comment;
91+
std::string &comment;
9092
};
9193

9294
IniParser::IniParser() {
@@ -131,11 +133,9 @@ int IniParser::LoadFromMem(const char* data) {
131133
ss << data;
132134
std::string strLine;
133135
int line = 0;
134-
string::size_type pos;
136+
std::string::size_type pos;
135137

136-
string content;
137-
string comment;
138-
string strDiv;
138+
std::string content, comment, strDiv;
139139
IniNode* pScopeNode = root_;
140140
IniNode* pNewNode = NULL;
141141
while (std::getline(ss, strLine)) {
@@ -151,7 +151,7 @@ int IniParser::LoadFromMem(const char* data) {
151151
// trim_comment
152152
comment = "";
153153
pos = content.find_first_of(_comment);
154-
if (pos != string::npos) {
154+
if (pos != std::string::npos) {
155155
comment = content.substr(pos);
156156
content = content.substr(0, pos);
157157
}
@@ -184,7 +184,7 @@ int IniParser::LoadFromMem(const char* data) {
184184
}
185185
} else {
186186
pos = content.find_first_of(_delim);
187-
if (pos != string::npos) {
187+
if (pos != std::string::npos) {
188188
// key-value
189189
pNewNode = new IniNode;
190190
pNewNode->type = IniNode::INI_NODE_TYPE_KEY_VALUE;
@@ -218,7 +218,7 @@ int IniParser::LoadFromMem(const char* data) {
218218
return 0;
219219
}
220220

221-
void IniParser::DumpString(IniNode* pNode, string& str) {
221+
void IniParser::DumpString(IniNode* pNode, std::string& str) {
222222
if (pNode == NULL) return;
223223

224224
if (pNode->type != IniNode::INI_NODE_TYPE_SPAN) {
@@ -256,8 +256,8 @@ void IniParser::DumpString(IniNode* pNode, string& str) {
256256
}
257257
}
258258

259-
string IniParser::DumpString() {
260-
string str;
259+
std::string IniParser::DumpString() {
260+
std::string str;
261261
DumpString(root_, str);
262262
return str;
263263
}
@@ -267,7 +267,7 @@ int IniParser::Save() {
267267
}
268268

269269
int IniParser::SaveAs(const char* filepath) {
270-
string str = DumpString();
270+
std::string str = DumpString();
271271
if (str.length() == 0) {
272272
return 0;
273273
}
@@ -281,7 +281,7 @@ int IniParser::SaveAs(const char* filepath) {
281281
return 0;
282282
}
283283

284-
string IniParser::GetValue(const string& key, const string& section) {
284+
std::string IniParser::GetValue(const std::string& key, const std::string& section) {
285285
if (root_ == NULL) return "";
286286

287287
IniNode* pSection = root_;
@@ -296,7 +296,7 @@ string IniParser::GetValue(const string& key, const string& section) {
296296
return pKV->value;
297297
}
298298

299-
void IniParser::SetValue(const string& key, const string& value, const string& section) {
299+
void IniParser::SetValue(const std::string& key, const std::string& value, const std::string& section) {
300300
if (root_ == NULL) {
301301
root_ = new IniNode;
302302
}
@@ -323,35 +323,34 @@ void IniParser::SetValue(const string& key, const string& value, const string& s
323323
}
324324

325325
template<>
326-
HV_EXPORT bool IniParser::Get(const string& key, const string& section, bool defvalue) {
327-
string str = GetValue(key, section);
326+
HV_EXPORT bool IniParser::Get(const std::string& key, const std::string& section, bool defvalue) {
327+
std::string str = GetValue(key, section);
328328
return str.empty() ? defvalue : getboolean(str.c_str());
329329
}
330330

331331
template<>
332-
HV_EXPORT int IniParser::Get(const string& key, const string& section, int defvalue) {
333-
string str = GetValue(key, section);
332+
HV_EXPORT int IniParser::Get(const std::string& key, const std::string& section, int defvalue) {
333+
std::string str = GetValue(key, section);
334334
return str.empty() ? defvalue : atoi(str.c_str());
335335
}
336336

337337
template<>
338-
HV_EXPORT float IniParser::Get(const string& key, const string& section, float defvalue) {
339-
string str = GetValue(key, section);
338+
HV_EXPORT float IniParser::Get(const std::string& key, const std::string& section, float defvalue) {
339+
std::string str = GetValue(key, section);
340340
return str.empty() ? defvalue : atof(str.c_str());
341341
}
342342

343343
template<>
344-
HV_EXPORT void IniParser::Set(const string& key, const bool& value, const string& section) {
344+
HV_EXPORT void IniParser::Set(const std::string& key, const bool& value, const std::string& section) {
345345
SetValue(key, value ? "true" : "false", section);
346346
}
347347

348348
template<>
349-
HV_EXPORT void IniParser::Set(const string& key, const int& value, const string& section) {
349+
HV_EXPORT void IniParser::Set(const std::string& key, const int& value, const std::string& section) {
350350
SetValue(key, asprintf("%d", value), section);
351351
}
352352

353353
template<>
354-
HV_EXPORT void IniParser::Set(const string& key, const float& value, const string& section) {
354+
HV_EXPORT void IniParser::Set(const std::string& key, const float& value, const std::string& section) {
355355
SetValue(key, asprintf("%f", value), section);
356356
}
357-

0 commit comments

Comments
 (0)
Please sign in to comment.