-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountInfo.cpp
More file actions
219 lines (189 loc) · 4.52 KB
/
AccountInfo.cpp
File metadata and controls
219 lines (189 loc) · 4.52 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "AccountInfo.h"
#include <vector>
#include <fstream>
using std::ifstream;
using std::vector;
#ifdef __LINUX__
#include <string.h>
#define stricmp strcasecmp
#else
#define stricmp _stricmp
#endif
//字符串trim函数
string trim(const string& str) {
string t = str;
t.erase(0, t.find_first_not_of(" \t\n\r"));
t.erase(t.find_last_not_of(" \t\n\r") + 1);
return t;
}
//字符串分割函数
vector<string> split(string str,string pattern)
{
string::size_type pos;
vector<std::string> result;
str+=pattern;//扩展字符串以方便操作
int size=str.size();
for(int i=0; i<size; i++)
{
pos=str.find(pattern,i);
if(pos<size)
{
std::string s=str.substr(i,pos-i);
s=trim(s);
result.push_back(s);
i=pos+pattern.size()-1;
}
}
return result;
}
#define APP_KEY_STR "appKey"
#define DEVELOPER_KEY_STR "developerKey"
#define CLOUD_URL_STR "cloudUrl"
#define CAP_KEY_STR "capKey"
AccountInfo *AccountInfo::instance_ = NULL;
AccountInfo *AccountInfo::GetInstance()
{
if (instance_ == NULL)
{
instance_ = new AccountInfo;
}
return instance_;
}
void AccountInfo::ReleaseInstance()
{
if (instance_ == NULL)
{
delete instance_;
instance_ = NULL;
}
}
AccountInfo::AccountInfo()
{
app_key_ = "";
developer_key_ = "";
cloud_url_ = "";
cap_key_ = "";
string path_prefix = "";
#ifdef _WIN32_WCE
path_prefix = "/SDMMC";
#endif
test_data_path_ = path_prefix + "testdata/";
data_path_ = path_prefix + "data/";
auth_path_ = path_prefix + "bin/";
logfile_path_ = path_prefix + "bin/";
}
AccountInfo::~AccountInfo()
{
}
//此处实现一个简单的配置文件读取操作,开发者可以自行选择是否使用
bool AccountInfo::LoadFromFile(const string &account_info_file)
{
//打开文件
ifstream fin;
fin.open(account_info_file.c_str());
if (!fin)
{
printf("get account info failed \n\t may be the file %s not exist!\n",account_info_file.c_str());
return false;
}
//按行遍历
string line;
while(getline(fin,line))
{
line = trim(line);
//为空或首字符为"#",则忽略
if (line.empty() || line[0] == '#' )
{
continue;
}
vector<string> key_value = split(line,"=");
if (stricmp(key_value[0].c_str(),APP_KEY_STR) == 0)
{
app_key_ = key_value[1];
}
if (stricmp(key_value[0].c_str(),DEVELOPER_KEY_STR) == 0)
{
developer_key_ = key_value[1];
}
if (stricmp(key_value[0].c_str(),CLOUD_URL_STR) == 0)
{
cloud_url_ = key_value[1];
}
if (stricmp(key_value[0].c_str(),CAP_KEY_STR) == 0)
{
cap_key_ = key_value[1];
}
}
//关闭文件
fin.close();
//有效性判断
if (!IsValid())
{
printf("get account info failed \n\t \
account info(%s,%s,%s,%s) \n\t \
some record info may be missed,\
please check the file %s!\n",
APP_KEY_STR,DEVELOPER_KEY_STR,CLOUD_URL_STR,CAP_KEY_STR,account_info_file.c_str());
return false;
}
return true;
}
bool AccountInfo::LoadFromCode()
{
//代码中赋值即可,无需AccountInfo.txt文件
//此时需要在此处填写正确的应用账号信息
app_key_ = "";
developer_key_ = "";
cloud_url_ = "";
cap_key_ = "";
//有效性判断
if (!IsValid())
{
printf("get account info failed \n\t \
account info(%s,%s,%s,%s) \n\t \
some record info may be missed!\n",
APP_KEY_STR,DEVELOPER_KEY_STR,CLOUD_URL_STR,CAP_KEY_STR);
return false;
}
return false;
}
bool AccountInfo::IsValid()
{
if (app_key_.empty() || developer_key_.empty() || cloud_url_.empty()||cap_key_.empty())
{
return false;
}
return true;
}
string AccountInfo::app_key()
{
return app_key_;
}
string AccountInfo::developer_key()
{
return developer_key_;
}
string AccountInfo::cloud_url()
{
return cloud_url_;
}
string AccountInfo::auth_path()
{
return auth_path_;
}
string AccountInfo::logfile_path()
{
return logfile_path_;
}
string AccountInfo::cap_key()
{
return cap_key_;
}
string AccountInfo::data_path()
{
return data_path_;
}
string AccountInfo::test_data_path()
{
return test_data_path_;
}