Skip to content

Commit ec33d74

Browse files
committed
support props_path
1 parent 0bcced9 commit ec33d74

File tree

3 files changed

+229
-6
lines changed

3 files changed

+229
-6
lines changed

README.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ brew install boost
2626
```
2727
tar --bzip2 -xf boost_1_81_0.tar.bz2
2828
```
29-
3. 编译
29+
3. 编译 (注意权限, 如有报错可前面加sudo重试)
3030
```shell
3131
cd /usr/local/boost_1_81_0
3232
./bootstrap.sh
@@ -43,10 +43,24 @@ cd cpprestsdk
4343
git submodule update --init
4444
mkdir build
4545
cd build
46-
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug ..
46+
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug-DBOOST_ROOT=/usr/local/ boost_1_81_0 ..
4747
make -j 8
4848
make install
4949
```
50+
如果找不到openssl或boost 或其他错误,可尝试指定路径,关闭某些模块:
51+
```
52+
cmake .. \
53+
-DCMAKE_BUILD_TYPE=Debug \
54+
-DBUILD_SHARED_LIBS=ON \
55+
-DBUILD_SAMPLES=OFF \
56+
-DBUILD_TESTS=OFF \
57+
-DOPENSSL_ROOT_DIR=/opt/homebrew/opt/openssl@3 \
58+
-DOPENSSL_CRYPTO_LIBRARY=/opt/homebrew/opt/openssl@3/lib/libcrypto.dylib \
59+
-DOPENSSL_SSL_LIBRARY=/opt/homebrew/opt/openssl@3/lib/libssl.dylib \
60+
-DOPENSSL_INCLUDE_DIR=/opt/homebrew/opt/openssl@3/include \
61+
-DBOOST_ROOT=/usr/local/boost_1_81_0 \
62+
-DCMAKE_CXX_FLAGS="-Wno-error=null-pointer-subtraction"
63+
```
5064
5165
**说明**
5266
[参考文档](https://github.com/Microsoft/cpprestsdk/wiki/Getting-Started-Tutorial)
@@ -82,7 +96,8 @@ make install
8296
```
8397
3. 编译
8498
```
85-
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug ..
99+
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug \
100+
-DBOOST_ROOT=/usr/local/boost_1_81_0 ..
86101
make -j 6
87102
make install
88103
```
@@ -166,5 +181,24 @@ ln -s libicuuc.dylib libicuuc.70.dylib
166181
export LDFLAGS="-L/usr/local/opt/icu4c/lib"
167182
export CPPFLAGS="-I/usr/local/opt/icu4c/include"
168183
```
169-
184+
2. mac 系统编译 cpprestsdk 时,报错:
185+
```
186+
CMake Error at Release/cmake/cpprest_find_openssl.cmake:40 (list):
187+
list GET given empty list
188+
Call Stack (most recent call first):
189+
Release/cmake/cpprest_find_websocketpp.cmake:18 (cpprest_find_openssl)
190+
Release/src/CMakeLists.txt:68 (cpprest_find_websocketpp)
191+
```
192+
Fix 指定openssl路径:
193+
```
194+
cmake .. \
195+
-DCMAKE_BUILD_TYPE=Release \
196+
-DBUILD_SHARED_LIBS=ON \
197+
-DCMAKE_INSTALL_PREFIX=/usr/local \
198+
-DOPENSSL_ROOT_DIR=/opt/homebrew/opt/openssl@3 \
199+
-DOPENSSL_CRYPTO_LIBRARY=/opt/homebrew/opt/openssl@3/lib/libcrypto.dylib \
200+
-DOPENSSL_SSL_LIBRARY=/opt/homebrew/opt/openssl@3/lib/libssl.dylib \
201+
-DOPENSSL_INCLUDE_DIR=/opt/homebrew/opt/openssl@3/include \
202+
-DBOOST_ROOT=/usr/local/boost_1_81_0
203+
```
170204

include/tigerapi/client_config.h

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "easylogging++.h"
1212
#include "utils.h"
1313
#include "win32.h"
14+
#include <fstream>
15+
#include "properties.h"
1416

1517
using namespace std;
1618

@@ -19,8 +21,9 @@ namespace TIGER_API {
1921
public:
2022
ClientConfig(bool sandbox_debug = false) : sandbox_debug(sandbox_debug) {
2123
if (sandbox_debug) {
22-
server_url = SANDBOX_TIGER_SERVER_URL;
23-
server_public_key = SANDBOX_TIGER_PUBLIC_KEY;
24+
LOG(WARNING) << U("SANDBOX IS NOT SUPPORTED") << endl;
25+
// server_url = SANDBOX_TIGER_SERVER_URL;
26+
// server_public_key = SANDBOX_TIGER_PUBLIC_KEY;
2427
}
2528
};
2629

@@ -41,6 +44,10 @@ namespace TIGER_API {
4144
}
4245
};
4346

47+
ClientConfig(utility::string_t props_path) : props_path(props_path) {
48+
load_props();
49+
};
50+
4451
utility::string_t tiger_id;
4552
utility::string_t private_key;
4653
utility::string_t account;
@@ -53,6 +60,8 @@ namespace TIGER_API {
5360
utility::string_t socket_ca_certs;
5461
unsigned int send_interval = 10 * 1000;
5562
unsigned int receive_interval = 10 * 1000;
63+
utility::string_t token;
64+
utility::string_t props_path;
5665

5766
void check() {
5867
if (this->tiger_id.empty()) {
@@ -106,6 +115,49 @@ namespace TIGER_API {
106115
utility::string_t server_public_key = TIGER_PUBLIC_KEY;
107116
utility::string_t socket_url = TIGER_SOCKET_HOST;
108117
utility::string_t socket_port = TIGER_SOCKET_PORT;
118+
119+
void load_props() {
120+
if (props_path.empty()) {
121+
return;
122+
}
123+
124+
try {
125+
std::ifstream file(props_path);
126+
if (!file.is_open()) {
127+
LOG(ERROR) << U("Failed to open properties file: ") << props_path << endl;
128+
return;
129+
}
130+
131+
Properties props;
132+
props.load(file);
133+
134+
// 只在值为空时从配置文件加载
135+
if (tiger_id.empty()) {
136+
tiger_id = props.get_property(U("tiger_id"));
137+
}
138+
if (private_key.empty()) {
139+
private_key = props.get_property(U("private_key_pk1"));
140+
}
141+
if (account.empty()) {
142+
account = props.get_property(U("account"));
143+
}
144+
145+
// 检查是否为沙箱环境
146+
if (!sandbox_debug) {
147+
utility::string_t env = props.get_property(U("env"));
148+
std::transform(env.begin(), env.end(), env.begin(), ::toupper);
149+
if (env == U("SANDBOX")) {
150+
sandbox_debug = true;
151+
server_url = SANDBOX_TIGER_SERVER_URL;
152+
server_public_key = SANDBOX_TIGER_PUBLIC_KEY;
153+
socket_url = SANDBOX_TIGER_SOCKET_HOST;
154+
socket_port = SANDBOX_TIGER_SOCKET_PORT;
155+
}
156+
}
157+
} catch (const std::exception& e) {
158+
LOG(ERROR) << U("Failed to load properties file: ") << e.what() << endl;
159+
}
160+
}
109161
};
110162
}
111163

include/tigerapi/properties.h

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#ifndef TIGERAPI_PROPERTIES_H
2+
#define TIGERAPI_PROPERTIES_H
3+
4+
#include <string>
5+
#include <map>
6+
#include <fstream>
7+
#include <sstream>
8+
#include "utils.h"
9+
10+
namespace TIGER_API {
11+
class Properties {
12+
public:
13+
void load(std::ifstream& input) {
14+
std::string line;
15+
while (std::getline(input, line)) {
16+
// 跳过空行和注释行
17+
if (line.empty() || line[0] == '#' || line[0] == '!') {
18+
continue;
19+
}
20+
21+
// 查找第一个非转义的等号或冒号
22+
size_t pos = 0;
23+
while ((pos = line.find_first_of("=:", pos)) != std::string::npos) {
24+
if (pos == 0 || line[pos - 1] != '\\') {
25+
break;
26+
}
27+
pos++;
28+
}
29+
30+
if (pos != std::string::npos) {
31+
std::string key = trim(line.substr(0, pos));
32+
std::string value = trim(line.substr(pos + 1));
33+
34+
// 处理转义字符
35+
value = unescape(value);
36+
37+
// 存储为 utility::string_t
38+
properties[utility::conversions::to_string_t(key)] =
39+
utility::conversions::to_string_t(value);
40+
}
41+
}
42+
}
43+
44+
utility::string_t get_property(const utility::string_t& key) const {
45+
auto it = properties.find(key);
46+
if (it != properties.end()) {
47+
return it->second;
48+
}
49+
return utility::string_t();
50+
}
51+
52+
void store(std::ofstream& output) const {
53+
// 写入文件头注释
54+
output << "# Properties" << std::endl;
55+
output << "# " << get_current_datetime() << std::endl;
56+
output << std::endl;
57+
58+
// 写入所有属性
59+
for (const auto& pair : properties) {
60+
std::string key = utility::conversions::to_utf8string(pair.first);
61+
std::string value = utility::conversions::to_utf8string(pair.second);
62+
63+
// 转义特殊字符
64+
key = escape(key);
65+
value = escape(value);
66+
67+
output << key << "=" << value << std::endl;
68+
}
69+
}
70+
71+
private:
72+
std::map<utility::string_t, utility::string_t> properties;
73+
74+
static std::string trim(const std::string& str) {
75+
const std::string whitespace = " \t\n\r\f\v";
76+
size_t start = str.find_first_not_of(whitespace);
77+
if (start == std::string::npos) {
78+
return "";
79+
}
80+
size_t end = str.find_last_not_of(whitespace);
81+
return str.substr(start, end - start + 1);
82+
}
83+
84+
static std::string unescape(const std::string& str) {
85+
std::string result;
86+
bool escaped = false;
87+
88+
for (size_t i = 0; i < str.length(); i++) {
89+
if (escaped) {
90+
switch (str[i]) {
91+
case 'n': result += '\n'; break;
92+
case 't': result += '\t'; break;
93+
case 'r': result += '\r'; break;
94+
case '\\': result += '\\'; break;
95+
default: result += str[i]; break;
96+
}
97+
escaped = false;
98+
} else if (str[i] == '\\') {
99+
escaped = true;
100+
} else {
101+
result += str[i];
102+
}
103+
}
104+
105+
return result;
106+
}
107+
108+
static std::string escape(const std::string& str) {
109+
std::string result;
110+
for (char c : str) {
111+
switch (c) {
112+
case '\n': result += "\\n"; break;
113+
case '\t': result += "\\t"; break;
114+
case '\r': result += "\\r"; break;
115+
case '\\': result += "\\\\"; break;
116+
case '=': result += "\\="; break;
117+
case ':': result += "\\:"; break;
118+
case ' ': result += "\\ "; break;
119+
default: result += c; break;
120+
}
121+
}
122+
return result;
123+
}
124+
125+
static std::string get_current_datetime() {
126+
auto now = std::chrono::system_clock::now();
127+
auto time = std::chrono::system_clock::to_time_t(now);
128+
std::string timestamp(30, '\0');
129+
std::strftime(&timestamp[0], timestamp.size(),
130+
"%Y-%m-%d %H:%M:%S",
131+
std::localtime(&time));
132+
return timestamp;
133+
}
134+
};
135+
}
136+
137+
#endif //TIGERAPI_PROPERTIES_H

0 commit comments

Comments
 (0)