├── README.md ├── build-easycpp-Debug └── Makefile └── easycpp ├── easycpp.pro ├── easycpp.pro.user ├── easycpp ├── helpers │ ├── datetime.cpp │ ├── datetime.h │ ├── file.cpp │ ├── file.h │ ├── http.cpp │ ├── http.h │ ├── json.cpp │ ├── json.h │ ├── log.cpp │ ├── log.h │ ├── string.cpp │ ├── string.h │ ├── type.cpp │ └── type.h ├── libraries │ ├── exception.cpp │ ├── exception.h │ └── json.h └── models │ ├── redis.cpp │ ├── redis.h │ └── redis.hpp └── main.cpp /README.md: -------------------------------------------------------------------------------- 1 | #EasyCpp 2 | 一个用于网络开发的C++ 敏捷开发框架,大量按网络开发业务需求封装的辅助函数、类、模型 3 | 4 | 5 | ## 依赖库 (libraries) 6 | 7 | all boost库 8 | models/redis.hpp hiredis库 & libevent库 9 | 10 | ## 安装 (install) 11 | 12 | ``` 13 | unzip easycpp 14 | cd easycpp/build-easycpp-Debug 15 | make distclean 16 | make 17 | make install 18 | ``` 19 | 20 | ## 案例 (case) 21 | 高性能异步http服务器 [async-http-server](https://github.com/onanying/async-http-server) 22 | 23 | ## 头文件列表 (files) 24 | 25 | easycpp/helpers/datatime.h 26 | easycpp/helpers/file.h 27 | easycpp/helpers/http.h 28 | easycpp/helpers/json.h 29 | easycpp/helpers/log.h 30 | easycpp/helpers/string.h 31 | easycpp/helpers/type.h 32 | easycpp/libraries/exception.h 33 | easycpp/libraries/json.h 34 | easycpp/models/redis.h 35 | 36 | ## 函数列表 (function) 37 | 38 | - date 39 | - timestamp 40 | - file_put_contents 41 | - file_get_contents 42 | - file_exists 43 | - dirname 44 | - basename 45 | - mkdir 46 | - readdir 47 | - http_post 48 | - http_get 49 | - urlencode 50 | - urldecode 51 | - http_build_query 52 | - json_init 53 | - json_get_array 54 | - json_get_object 55 | - json_get_string 56 | - json_get_int 57 | - log_error 58 | - log_debug 59 | - log_info 60 | - str_replace 61 | - explode 62 | - implode 63 | - strval 64 | - intval 65 | 66 | ## 使用范例 (sample) 67 | 68 | ### 字符串辅助函数 69 | 70 | ```cpp 71 | /** 72 | * 字符串辅助函数 73 | */ 74 | #include 75 | #include 76 | #include 77 | #include 78 | 79 | using namespace easycpp; 80 | using namespace std; 81 | 82 | int main() 83 | { 84 | 85 | // 替换字符 86 | string s = "aaabbbaaa"; 87 | string ns = helpers::str_replace("b", "a", s); 88 | 89 | // 把字符串打散为数组 90 | string s1 = "aaa,bbb,ccc"; 91 | vector ary = helpers::explode(",", s1); 92 | 93 | return 0; 94 | } 95 | ``` 96 | ### 日期时间辅助函数 97 | 98 | ```cpp 99 | /** 100 | * 日期时间辅助函数 101 | */ 102 | #include 103 | #include 104 | #include 105 | 106 | using namespace easycpp; 107 | using namespace std; 108 | 109 | int main() 110 | { 111 | 112 | // 获取当前日期时间 113 | string datetime = helpers::date("%Y/%m/%d %X"); 114 | 115 | // 获取当前日期 116 | string date = helpers::date("%Y-%m-%d"); 117 | 118 | // 获取当前时间戳 119 | long time = helpers::timestamp(); 120 | 121 | return 0; 122 | } 123 | ``` 124 | ### 文件辅助函数 125 | 126 | ```cpp 127 | /** 128 | * 文件辅助函数 129 | */ 130 | #include 131 | #include 132 | #include 133 | 134 | using namespace easycpp; 135 | using namespace std; 136 | 137 | int main() 138 | { 139 | 140 | // 文件写入 141 | string s1 = "我的文本"; 142 | helpers::file_put_contents("test.txt", s1, FILE_APPEND); 143 | 144 | // 文件读取 145 | string s2 = helpers::file_get_contents("test.txt"); 146 | 147 | // 判断文件存在 148 | if(file_exists(file)){ 149 | // 存在 150 | } 151 | 152 | // 返回路径中的目录部分 153 | std::string path = helpers::dirname("log/info.log"); 154 | 155 | // 返回路径中的文件名部分 156 | std::string path = helpers::basename("log/info.log"); 157 | 158 | // 创建目录 159 | helpers::mkdir("log/2016"); 160 | 161 | // 返回目录中的所有文件的文件名 162 | vector filenames = helpers::readdir("cache"); 163 | 164 | return 0; 165 | } 166 | ``` 167 | ### 日志辅助函数 168 | 169 | ```cpp 170 | /** 171 | * 日志辅助函数 172 | */ 173 | #include 174 | #include 175 | #include 176 | 177 | using namespace easycpp; 178 | using namespace std; 179 | 180 | int main() 181 | { 182 | 183 | 184 | string tag = "send_error"; 185 | string msg = "uid[123],name[哈哈],sex[0]"; 186 | 187 | // 错误日志, 写入到文件 log/error.20161219.log 188 | helpers::log_error(tag, msg); 189 | 190 | // 信息日志, 写入到文件 log/info.20161219.log 191 | helpers::log_info(tag, msg); 192 | 193 | // 调试日志, 写入到文件 log/debug.20161219.log 194 | helpers::log_debug(tag, msg); 195 | 196 | // 带子目录, 写入到文件 log/2016/debug.20161219.log 197 | helpers::log_debug(tag, msg, "2016"); 198 | 199 | return 0; 200 | } 201 | ``` 202 | ### 类型转换辅助函数 203 | 204 | ```cpp 205 | /** 206 | * 类型转换辅助函数 207 | */ 208 | #include 209 | #include 210 | #include 211 | 212 | using namespace easycpp; 213 | using namespace std; 214 | 215 | int main() 216 | { 217 | 218 | // 数字转字符 219 | string s = helpers::strval(123); 220 | 221 | // 字符转数字 222 | int n = helpers::intval("123"); 223 | 224 | return 0; 225 | } 226 | ``` 227 | ### json辅助函数 228 | 229 | ```cpp 230 | /** 231 | * json辅助函数 232 | */ 233 | #include 234 | #include 235 | #include 236 | 237 | using namespace easycpp; 238 | using namespace std; 239 | 240 | int main() 241 | { 242 | 243 | try{ 244 | 245 | string json_str = "{\"name\":\"xiaohua\",\"sex\":0,\"more\":{\"height\":175},\"datas\":[{\"item\":1},{\"item\":2}]}"; 246 | 247 | // 初始化 248 | libraries::JsonObject js_obj = helpers::json_init(json_str); 249 | 250 | // 获取string 251 | string v1 = helpers::json_get_string(js_obj, "name"); 252 | cout << "name: " << v1 << endl; 253 | 254 | // 获取int 255 | int v2 = helpers::json_get_int(js_obj, "sex"); 256 | cout << "sex: " << v2 << endl; 257 | 258 | // 获取object 259 | libraries::JsonObject new_obj = helpers::json_get_object(js_obj, "more"); 260 | int v3 = helpers::json_get_int(new_obj, "height"); 261 | cout << "height: " << v3 << endl; 262 | 263 | // 获取array 264 | vector array = helpers::json_get_array(js_obj, "datas"); 265 | for (int i = 0; i < array.size(); ++i) { 266 | libraries::JsonObject &obj = array.at(i); 267 | int v4 = helpers::json_get_int(obj, "item"); 268 | cout << "item: " << v4 << endl; 269 | } 270 | 271 | } catch (exception &ex) { 272 | cout << ex.what() << endl; 273 | } 274 | 275 | return 0; 276 | } 277 | ``` 278 | ### http辅助函数 279 | 280 | ```cpp 281 | /** 282 | * http辅助函数 283 | */ 284 | #include 285 | #include 286 | #include 287 | 288 | using namespace easycpp; 289 | using namespace std; 290 | 291 | int main() 292 | { 293 | // 构建要post的数据 294 | libraries::JsonObject form_data; 295 | try{ 296 | form_data = helpers::json_init("{\"name\":\"小花\",\"sex\":\"0\"}"); 297 | } catch (std::exception &ex){ 298 | cout << ex.what() << endl; 299 | return 1; 300 | } 301 | // post请求 302 | string reponse_data; 303 | int status = helpers::http_post("http://0.0.0.0:8888/test.php?token=123456", form_data, reponse_data); 304 | if(status == 0){ 305 | cout << "Success:\n" << reponse_data << endl; 306 | }else{ 307 | cout << "Failed:\n" << reponse_data << endl; 308 | } 309 | 310 | // get请求 311 | string reponse_data2; 312 | int status2 = helpers::http_get("http://0.0.0.0:8888/test.php?token=123456", reponse_data2, 60); // 60秒后超时, 默认30秒 313 | if(status2 == 0){ 314 | cout << "Success:\n" << reponse_data2 << endl; 315 | }else{ 316 | cout << "Failed:\n" << reponse_data2 << endl; 317 | } 318 | 319 | // URLencode 320 | string str1 = "name=小花&sex=女&height=168"; 321 | cout << "URLencode:\n" << helpers::urlencode(str1) << endl; 322 | 323 | // URLdecode 324 | cout << "URLdecode:\n" << helpers::urldecode(str1) << endl; 325 | 326 | return 0; 327 | } 328 | ``` 329 | ### redis模型 330 | 331 | ```cpp 332 | /** 333 | * redis模型 334 | */ 335 | #include 336 | #include 337 | #include 338 | 339 | using namespace easycpp; 340 | using namespace std; 341 | 342 | int main() 343 | { 344 | 345 | try{ 346 | 347 | // 连接redis 348 | models::RedisModel redis("127.0.0.1", 6379, "123456"); 349 | 350 | /* string类型 */ 351 | // 设置string 352 | redis.setString("token", "b12cc6c0cc5b4875af4b1334ceac782b10"); 353 | // 设置string (带有效期) 354 | redis.setString("token", "b12cc6c0cc5b4875af4b1334ceac782b10", 60); 355 | // 获取string 356 | string s1 = redis.getString("token"); 357 | cout << "token: " << s1 << endl; 358 | 359 | /* Hash类型 */ 360 | // 赋值 361 | map array; 362 | array["name"] = "xiaohua"; 363 | array["height"] = "176"; 364 | // 设置Hash 365 | redis.setHash("user", array); 366 | // 设置Hash (带有效期) 367 | redis.setHash("user", array, 60); 368 | // 获取Hash 369 | map *array1 = redis.getHash("user"); 370 | for (map::iterator it = array1->begin(); it != array1->end(); ++it) { 371 | cout << "array key: " << it->first << endl; 372 | cout << "array val: " << it->second << endl; 373 | } 374 | 375 | /* List类型 */ 376 | // 插入列表头部 377 | redis.pushList("queue_user", "update_xiaohua_info"); 378 | redis.pushList("queue_user", "insert_xiaohua_info"); 379 | // 从列表尾部弹出一条数据 380 | string res = redis.pullList("queue_user"); 381 | cout << "list: " << res << endl; 382 | 383 | } catch (exception &ex) { 384 | cout << "ERROR: " << ex.what() << endl; 385 | } 386 | 387 | return 0; 388 | } 389 | ``` 390 | -------------------------------------------------------------------------------- /build-easycpp-Debug/Makefile: -------------------------------------------------------------------------------- 1 | ############################################################################# 2 | # Makefile for building: libeasycpp.so.1.0.0 3 | # Generated by qmake (2.01a) (Qt 4.8.7) on: ?? 8? 24 10:48:50 2016 4 | # Project: ../easycpp/easycpp.pro 5 | # Template: lib 6 | # Command: /usr/lib/x86_64-linux-gnu/qt4/bin/qmake -spec /usr/share/qt4/mkspecs/linux-g++-64 CONFIG+=debug CONFIG+=declarative_debug -o Makefile ../easycpp/easycpp.pro 7 | ############################################################################# 8 | 9 | ####### Compiler, tools and options 10 | 11 | CC = gcc 12 | CXX = g++ 13 | CFLAGS = -m64 -pipe -g -Wall -W -D_REENTRANT -fPIC $(DEFINES) 14 | CXXFLAGS = -m64 -pipe -g -Wall -W -D_REENTRANT -fPIC $(DEFINES) 15 | INCPATH = -I/usr/local/include -I. -I../easycpp -I. 16 | LINK = g++ 17 | LFLAGS = -m64 -shared -Wl,-soname,libeasycpp.so.1 18 | LIBS = $(SUBLIBS) -lboost_system -lhiredis -levent 19 | AR = ar cqs 20 | RANLIB = 21 | TAR = tar -cf 22 | COMPRESS = gzip -9f 23 | COPY = cp -f 24 | SED = sed 25 | COPY_FILE = $(COPY) 26 | COPY_DIR = $(COPY) -r 27 | STRIP = strip 28 | INSTALL_FILE = install -m 644 -p 29 | INSTALL_DIR = $(COPY_DIR) 30 | INSTALL_PROGRAM = cp 31 | DEL_FILE = rm -f 32 | SYMLINK = ln -f -s 33 | DEL_DIR = rmdir 34 | MOVE = mv -f 35 | CHK_DIR_EXISTS= test -d 36 | MKDIR = mkdir -p 37 | 38 | ####### Output directory 39 | 40 | OBJECTS_DIR = ./ 41 | 42 | ####### Files 43 | 44 | SOURCES = ../easycpp/main.cpp \ 45 | ../easycpp/easycpp/helpers/string.cpp \ 46 | ../easycpp/easycpp/helpers/datetime.cpp \ 47 | ../easycpp/easycpp/helpers/file.cpp \ 48 | ../easycpp/easycpp/helpers/log.cpp \ 49 | ../easycpp/easycpp/helpers/type.cpp \ 50 | ../easycpp/easycpp/helpers/json.cpp \ 51 | ../easycpp/easycpp/libraries/exception.cpp \ 52 | ../easycpp/easycpp/models/redis.cpp \ 53 | ../easycpp/easycpp/helpers/http.cpp 54 | OBJECTS = main.o \ 55 | string.o \ 56 | datetime.o \ 57 | file.o \ 58 | log.o \ 59 | type.o \ 60 | json.o \ 61 | exception.o \ 62 | redis.o \ 63 | http.o 64 | QMAKE_TARGET = easycpp 65 | DESTDIR = 66 | TARGET = libeasycpp.so.1.0.0 67 | TARGETA = libeasycpp.a 68 | TARGETD = libeasycpp.so.1.0.0 69 | TARGET0 = libeasycpp.so 70 | TARGET1 = libeasycpp.so.1 71 | TARGET2 = libeasycpp.so.1.0 72 | 73 | first: all 74 | ####### Implicit rules 75 | 76 | .SUFFIXES: .o .c .cpp .cc .cxx .C 77 | 78 | .cpp.o: 79 | $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" 80 | 81 | .cc.o: 82 | $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" 83 | 84 | .cxx.o: 85 | $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" 86 | 87 | .C.o: 88 | $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" 89 | 90 | .c.o: 91 | $(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<" 92 | 93 | ####### Build rules 94 | 95 | all: $(TARGET) 96 | 97 | $(TARGET): $(OBJECTS) $(SUBLIBS) $(OBJCOMP) 98 | -$(DEL_FILE) $(TARGET) $(TARGET0) $(TARGET1) $(TARGET2) 99 | $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(LIBS) $(OBJCOMP) 100 | -ln -s $(TARGET) $(TARGET0) 101 | -ln -s $(TARGET) $(TARGET1) 102 | -ln -s $(TARGET) $(TARGET2) 103 | 104 | staticlib: $(TARGETA) 105 | 106 | $(TARGETA): $(OBJECTS) $(OBJCOMP) 107 | -$(DEL_FILE) $(TARGETA) 108 | $(AR) $(TARGETA) $(OBJECTS) 109 | 110 | clean: 111 | -$(DEL_FILE) $(OBJECTS) 112 | -$(DEL_FILE) *~ core *.core 113 | 114 | ####### Sub-libraries 115 | 116 | distclean: clean 117 | -$(DEL_FILE) $(TARGET) 118 | -$(DEL_FILE) $(TARGET0) $(TARGET1) $(TARGET2) $(TARGETA) 119 | -$(DEL_FILE) 120 | 121 | ####### Compile 122 | 123 | main.o: ../easycpp/main.cpp ../easycpp/easycpp/helpers/string.h \ 124 | ../easycpp/easycpp/helpers/datetime.h \ 125 | ../easycpp/easycpp/helpers/file.h \ 126 | ../easycpp/easycpp/helpers/log.h \ 127 | ../easycpp/easycpp/helpers/type.h \ 128 | ../easycpp/easycpp/helpers/json.h \ 129 | ../easycpp/easycpp/libraries/exception.h \ 130 | ../easycpp/easycpp/libraries/json.h \ 131 | ../easycpp/easycpp/helpers/http.h 132 | $(CXX) -c $(CXXFLAGS) $(INCPATH) -o main.o ../easycpp/main.cpp 133 | 134 | string.o: ../easycpp/easycpp/helpers/string.cpp ../easycpp/easycpp/helpers/string.h 135 | $(CXX) -c $(CXXFLAGS) $(INCPATH) -o string.o ../easycpp/easycpp/helpers/string.cpp 136 | 137 | datetime.o: ../easycpp/easycpp/helpers/datetime.cpp ../easycpp/easycpp/helpers/datetime.h 138 | $(CXX) -c $(CXXFLAGS) $(INCPATH) -o datetime.o ../easycpp/easycpp/helpers/datetime.cpp 139 | 140 | file.o: ../easycpp/easycpp/helpers/file.cpp ../easycpp/easycpp/helpers/file.h 141 | $(CXX) -c $(CXXFLAGS) $(INCPATH) -o file.o ../easycpp/easycpp/helpers/file.cpp 142 | 143 | log.o: ../easycpp/easycpp/helpers/log.cpp ../easycpp/easycpp/helpers/log.h \ 144 | ../easycpp/easycpp/helpers/file.h \ 145 | ../easycpp/easycpp/helpers/datetime.h 146 | $(CXX) -c $(CXXFLAGS) $(INCPATH) -o log.o ../easycpp/easycpp/helpers/log.cpp 147 | 148 | type.o: ../easycpp/easycpp/helpers/type.cpp ../easycpp/easycpp/helpers/type.h 149 | $(CXX) -c $(CXXFLAGS) $(INCPATH) -o type.o ../easycpp/easycpp/helpers/type.cpp 150 | 151 | json.o: ../easycpp/easycpp/helpers/json.cpp ../easycpp/easycpp/helpers/json.h \ 152 | ../easycpp/easycpp/libraries/exception.h \ 153 | ../easycpp/easycpp/libraries/json.h 154 | $(CXX) -c $(CXXFLAGS) $(INCPATH) -o json.o ../easycpp/easycpp/helpers/json.cpp 155 | 156 | exception.o: ../easycpp/easycpp/libraries/exception.cpp ../easycpp/easycpp/libraries/exception.h 157 | $(CXX) -c $(CXXFLAGS) $(INCPATH) -o exception.o ../easycpp/easycpp/libraries/exception.cpp 158 | 159 | redis.o: ../easycpp/easycpp/models/redis.cpp ../easycpp/easycpp/models/redis.h \ 160 | ../easycpp/easycpp/helpers/string.h \ 161 | ../easycpp/easycpp/helpers/type.h \ 162 | ../easycpp/easycpp/libraries/exception.h 163 | $(CXX) -c $(CXXFLAGS) $(INCPATH) -o redis.o ../easycpp/easycpp/models/redis.cpp 164 | 165 | http.o: ../easycpp/easycpp/helpers/http.cpp ../easycpp/easycpp/helpers/http.h \ 166 | ../easycpp/easycpp/helpers/json.h \ 167 | ../easycpp/easycpp/libraries/exception.h \ 168 | ../easycpp/easycpp/libraries/json.h \ 169 | ../easycpp/easycpp/helpers/type.h \ 170 | ../easycpp/easycpp/helpers/string.h 171 | $(CXX) -c $(CXXFLAGS) $(INCPATH) -o http.o ../easycpp/easycpp/helpers/http.cpp 172 | 173 | ####### Install 174 | 175 | install: 176 | $(INSTALL_PROGRAM) $(TARGETD) /usr/lib 177 | cd /usr/lib; ln -f -s $(TARGETD) $(TARGET0); ln -f -s $(TARGETD) $(TARGET1); ln -f -s $(TARGETD) $(TARGET2) 178 | $(MKDIR) /usr/local/include/$(QMAKE_TARGET) 179 | $(COPY_DIR) ../easycpp/easycpp/* /usr/local/include/$(QMAKE_TARGET) 180 | 181 | uninstall: FORCE 182 | 183 | FORCE: 184 | 185 | 186 | 187 | 188 | 189 | -------------------------------------------------------------------------------- /easycpp/easycpp.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2016-07-20T18:16:08 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core 8 | 9 | QT -= gui 10 | 11 | TARGET = easycpp 12 | CONFIG += console 13 | CONFIG -= app_bundle 14 | 15 | TEMPLATE = lib 16 | 17 | 18 | SOURCES += \ 19 | main.cpp \ 20 | easycpp/helpers/string.cpp \ 21 | easycpp/helpers/datetime.cpp \ 22 | easycpp/helpers/file.cpp \ 23 | easycpp/helpers/log.cpp \ 24 | easycpp/helpers/type.cpp \ 25 | easycpp/helpers/json.cpp \ 26 | easycpp/libraries/exception.cpp \ 27 | easycpp/models/redis.cpp \ 28 | easycpp/helpers/http.cpp 29 | 30 | 31 | INCLUDEPATH += /usr/local/include/ 32 | LIBS += -lboost_system -lhiredis -levent -lboost_filesystem 33 | 34 | HEADERS += \ 35 | easycpp/helpers/string.h \ 36 | easycpp/helpers/datetime.h \ 37 | easycpp/helpers/file.h \ 38 | easycpp/helpers/log.h \ 39 | easycpp/helpers/type.h \ 40 | easycpp/helpers/json.h \ 41 | easycpp/libraries/json.h \ 42 | easycpp/libraries/exception.h \ 43 | easycpp/models/redis.h \ 44 | easycpp/helpers/http.h 45 | -------------------------------------------------------------------------------- /easycpp/easycpp.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ProjectExplorer.Project.ActiveTarget 7 | 0 8 | 9 | 10 | ProjectExplorer.Project.EditorSettings 11 | 12 | true 13 | false 14 | true 15 | 16 | Cpp 17 | 18 | CppGlobal 19 | 20 | 21 | 22 | QmlJS 23 | 24 | QmlJSGlobal 25 | 26 | 27 | 2 28 | UTF-8 29 | false 30 | 4 31 | false 32 | true 33 | 1 34 | true 35 | 0 36 | true 37 | 0 38 | 8 39 | true 40 | 1 41 | true 42 | true 43 | true 44 | false 45 | 46 | 47 | 48 | ProjectExplorer.Project.PluginSettings 49 | 50 | 51 | 52 | ProjectExplorer.Project.Target.0 53 | 54 | 桌面 55 | 桌面 56 | {1e7fc4e4-8378-4d85-a7e3-17ef5899eac2} 57 | 0 58 | 0 59 | 0 60 | 61 | 62 | 63 | true 64 | qmake 65 | 66 | QtProjectManager.QMakeBuildStep 67 | false 68 | true 69 | 70 | false 71 | 72 | 73 | true 74 | Make 75 | 76 | Qt4ProjectManager.MakeStep 77 | 78 | -w 79 | -r 80 | 81 | false 82 | 83 | 84 | 85 | 2 86 | 构建 87 | 88 | ProjectExplorer.BuildSteps.Build 89 | 90 | 91 | 92 | true 93 | Make 94 | 95 | Qt4ProjectManager.MakeStep 96 | 97 | -w 98 | -r 99 | 100 | true 101 | clean 102 | 103 | 104 | 1 105 | 清理 106 | 107 | ProjectExplorer.BuildSteps.Clean 108 | 109 | 2 110 | false 111 | 112 | Debug 113 | 114 | Qt4ProjectManager.Qt4BuildConfiguration 115 | 2 116 | /home/github/EasyCpp-Framework/build-easycpp-Debug 117 | true 118 | 119 | 120 | 121 | 122 | true 123 | qmake 124 | 125 | QtProjectManager.QMakeBuildStep 126 | false 127 | true 128 | 129 | false 130 | 131 | 132 | true 133 | Make 134 | 135 | Qt4ProjectManager.MakeStep 136 | 137 | -w 138 | -r 139 | 140 | false 141 | 142 | 143 | 144 | 2 145 | 构建 146 | 147 | ProjectExplorer.BuildSteps.Build 148 | 149 | 150 | 151 | true 152 | Make 153 | 154 | Qt4ProjectManager.MakeStep 155 | 156 | -w 157 | -r 158 | 159 | true 160 | clean 161 | 162 | 163 | 1 164 | 清理 165 | 166 | ProjectExplorer.BuildSteps.Clean 167 | 168 | 2 169 | false 170 | 171 | Release 172 | 173 | Qt4ProjectManager.Qt4BuildConfiguration 174 | 0 175 | /home/code_pro/easycpp/build-easycpp-Release 176 | true 177 | 178 | 2 179 | 180 | 181 | 0 182 | 部署 183 | 184 | ProjectExplorer.BuildSteps.Deploy 185 | 186 | 1 187 | 在本地部署 188 | 189 | ProjectExplorer.DefaultDeployConfiguration 190 | 191 | 1 192 | 193 | 194 | true 195 | 196 | false 197 | false 198 | false 199 | false 200 | true 201 | 0.01 202 | 10 203 | true 204 | 25 205 | 206 | true 207 | valgrind 208 | 209 | 0 210 | 1 211 | 2 212 | 3 213 | 4 214 | 5 215 | 6 216 | 7 217 | 8 218 | 9 219 | 10 220 | 11 221 | 12 222 | 13 223 | 14 224 | 225 | 2 226 | 227 | 228 | 229 | false 230 | %{buildDir} 231 | 自定义执行档 232 | 233 | ProjectExplorer.CustomExecutableRunConfiguration 234 | 3768 235 | true 236 | false 237 | false 238 | false 239 | true 240 | 241 | 1 242 | 243 | 244 | 245 | ProjectExplorer.Project.TargetCount 246 | 1 247 | 248 | 249 | ProjectExplorer.Project.Updater.EnvironmentId 250 | {9f863264-3678-4123-b9ff-8977c67f50db} 251 | 252 | 253 | ProjectExplorer.Project.Updater.FileVersion 254 | 14 255 | 256 | 257 | -------------------------------------------------------------------------------- /easycpp/easycpp/helpers/datetime.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 日期时间辅助函数 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #include 7 | 8 | /// 获取当前日期 9 | std::string easycpp::helpers::date(std::string format, long timestamp) 10 | { 11 | time_t t = (timestamp == -1 ? time(0) : timestamp); 12 | char tmp[50]; 13 | strftime(tmp, sizeof(tmp), format.c_str(), localtime(&t)); 14 | return std::string(tmp); 15 | } 16 | 17 | /// 获取当前时间戳 18 | long easycpp::helpers::timestamp(){ 19 | time_t rawtime; 20 | return time(&rawtime); 21 | } 22 | -------------------------------------------------------------------------------- /easycpp/easycpp/helpers/datetime.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 日期时间辅助函数 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #ifndef EASYCPP_HELPERS_DATETIME_INCLUDED 7 | #define EASYCPP_HELPERS_DATETIME_INCLUDED 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | namespace easycpp { 14 | namespace helpers { 15 | 16 | /// 获取当前日期 17 | std::string date(std::string format, long timestamp = -1); 18 | 19 | /// 获取当前时间戳 20 | long timestamp(); 21 | 22 | } 23 | } 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /easycpp/easycpp/helpers/file.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 文件辅助函数 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #include 7 | 8 | const int FILE_REPLACE = 0; 9 | const int FILE_APPEND = 1; 10 | 11 | /// 文件写入 12 | void easycpp::helpers::file_put_contents(std::string file, std::string data, int mode) 13 | { 14 | // 判断文件存在 15 | if(!file_exists(file)){ 16 | // 新建目录 17 | std::string path = dirname(file); 18 | mkdir(path); 19 | } 20 | // 写入文件 21 | std::ofstream outdata; 22 | if(mode == FILE_REPLACE){ 23 | outdata.open(file.c_str()); 24 | } 25 | if(mode == FILE_APPEND){ 26 | outdata.open(file.c_str(), std::ios::app); 27 | } 28 | outdata << data; 29 | outdata.close(); 30 | } 31 | 32 | /// 文件读取 33 | std::string easycpp::helpers::file_get_contents(std::string path) 34 | { 35 | std::ifstream t(path.c_str(), std::ios::in | std::ios::binary); 36 | std::stringstream buffer; 37 | buffer << t.rdbuf(); 38 | std::string contents(buffer.str()); 39 | return contents; 40 | } 41 | 42 | /// 判断文件存在性 43 | bool easycpp::helpers::file_exists(std::string path) 44 | { 45 | // 取得当前目录 46 | boost::filesystem::path current_path = boost::filesystem::current_path(); 47 | // 生成文件路径 48 | boost::filesystem::path file_path = current_path / path; 49 | // 判断文件存在性 50 | if(boost::filesystem::exists(file_path)) 51 | { 52 | return true; 53 | } 54 | return false; 55 | } 56 | 57 | /// 返回路径中的目录部分 58 | std::string easycpp::helpers::dirname(std::string path) 59 | { 60 | std::vector file_array = helpers::explode("/", path); 61 | file_array.pop_back(); 62 | return helpers::implode("/", file_array); 63 | } 64 | 65 | /// 返回路径中的文件名部分 66 | std::string easycpp::helpers::basename(std::string path) 67 | { 68 | std::vector file_array = helpers::explode("/", path); 69 | std::string back_str = file_array.at(file_array.size() - 1); 70 | return back_str; 71 | } 72 | 73 | /// 创建目录 74 | void easycpp::helpers::mkdir(std::string path) 75 | { 76 | // 取得当前目录 77 | boost::filesystem::path current_path = boost::filesystem::current_path(); 78 | // 逐级建立目录 79 | std::vector array = helpers::explode("/", path); 80 | boost::filesystem::path dir_path = current_path; 81 | int size = array.size(); 82 | for(int i = 0; i < size; i++){ 83 | std::string dir = array.at(i); 84 | // 生成目录路径 85 | dir_path = dir_path / dir.c_str(); 86 | // 创建目录 87 | boost::filesystem::create_directory(dir_path); 88 | // 设置当前目录 89 | boost::filesystem::current_path(dir_path); 90 | } 91 | // 恢复默认当前目录 92 | boost::filesystem::current_path(current_path); 93 | } 94 | 95 | /// 返回目录中的所有文件的文件名 96 | std::vector easycpp::helpers::readdir(std::string path) 97 | { 98 | std::vector filenames; 99 | boost::filesystem::path dirPath(path); 100 | if (boost::filesystem::exists(dirPath)) { 101 | boost::filesystem::directory_iterator end_iter; 102 | for (boost::filesystem::directory_iterator iter(dirPath); iter!=end_iter; ++iter) { 103 | if (boost::filesystem::is_regular_file(iter->status())) { 104 | filenames.push_back(iter->path().string()); 105 | } 106 | } 107 | } 108 | return filenames; 109 | } 110 | -------------------------------------------------------------------------------- /easycpp/easycpp/helpers/file.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 文件辅助函数 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #ifndef EASYCPP_HELPERS_FILE_INCLUDED 7 | #define EASYCPP_HELPERS_FILE_INCLUDED 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | extern const int FILE_REPLACE; 17 | extern const int FILE_APPEND; 18 | 19 | namespace easycpp { 20 | namespace helpers { 21 | 22 | /// 文件写入 23 | void file_put_contents(std::string file, std::string data, int mode = FILE_APPEND); 24 | 25 | /// 文件读取 26 | std::string file_get_contents(std::string path); 27 | 28 | /// 判断文件存在性 29 | bool file_exists(std::string path); 30 | 31 | /// 返回路径中的目录部分 32 | std::string dirname(std::string path); 33 | 34 | /// 返回路径中的文件名部分 35 | std::string basename(std::string path); 36 | 37 | /// 创建目录 38 | void mkdir(std::string path); 39 | 40 | /// 返回目录中的所有文件的文件名 41 | std::vector readdir(std::string path); 42 | 43 | } 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /easycpp/easycpp/helpers/http.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * http辅助函数 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #include 7 | 8 | /// POST请求 9 | int easycpp::helpers::http_post(const std::string& host, const std::string& port, const std::string& page, easycpp::libraries::JsonObject& form_data, std::string& reponse_data, int timeout) 10 | { 11 | std::string query_data = http_build_query(form_data); // 生成http请求字符串 12 | try { 13 | boost::asio::ip::tcp::iostream stream; 14 | stream.expires_from_now(boost::posix_time::seconds(timeout)); 15 | stream.connect(host, port); 16 | stream << "POST " << page << " HTTP/1.0\r\n"; 17 | stream << "Host: " << host << ":" << port << "\r\n"; 18 | stream << "Accept: */*\r\n"; 19 | stream << "Content-Length: " << query_data.length() << "\r\n"; 20 | stream << "Content-Type: application/x-www-form-urlencoded\r\n"; 21 | stream << "Connection: close\r\n\r\n"; 22 | stream << query_data; 23 | stream.flush(); 24 | 25 | std::string status_code; 26 | std::string line; 27 | 28 | while (std::getline(stream, line) && line != "\r") { 29 | if(line.find("HTTP/") != std::string::npos){ 30 | status_code = line.substr(9, 3); 31 | } 32 | } 33 | while (std::getline(stream, line)) { 34 | reponse_data += line; 35 | } 36 | 37 | if (status_code.empty()) { 38 | reponse_data = "Invalid response"; 39 | return -1; 40 | } 41 | 42 | // 状态码非200, 不支持301/302等跳转 43 | if (status_code != "200") { 44 | reponse_data = "HTTP Status Code: " + status_code; 45 | } 46 | 47 | return easycpp::helpers::intval(status_code); 48 | } catch(std::exception& e) { 49 | reponse_data = e.what(); 50 | return -2; 51 | } 52 | } 53 | 54 | /// POST请求(重载) 55 | int easycpp::helpers::http_post(std::string url, easycpp::libraries::JsonObject& form_data, std::string& reponse_data, const int timeout) 56 | { 57 | // 去掉url中的协议头 58 | if (url.find("http://") != std::string::npos) { 59 | url = url.substr(7); 60 | } 61 | // 补充path 62 | if(url.find("/") == std::string::npos){ 63 | url.append("/"); 64 | } 65 | 66 | // 分隔符位置 67 | size_t path_index = url.find("/"); 68 | // 分隔符位置 69 | size_t port_index = url.find(":"); 70 | 71 | // 截取host字符串 72 | std::string host = url.substr(0, path_index); 73 | if(port_index != std::string::npos){ 74 | host = url.substr(0, port_index); 75 | } 76 | 77 | // 截取port字符串 78 | std::string port = "80"; 79 | if(port_index != std::string::npos){ 80 | port = url.substr(port_index + 1, path_index - port_index - 1); 81 | } 82 | 83 | // 截取path字符串 84 | std::string page = url.substr(path_index, url.length() - path_index); 85 | 86 | return http_post(host, port, page, form_data, reponse_data, timeout); 87 | } 88 | 89 | /// GET请求 90 | int easycpp::helpers::http_get(const std::string& host, const std::string& port, const std::string& page, std::string& reponse_data, const int timeout) 91 | { 92 | try { 93 | boost::asio::ip::tcp::iostream stream; 94 | stream.expires_from_now(boost::posix_time::seconds(timeout)); 95 | stream.connect(host, port); 96 | stream << "GET " << page << " HTTP/1.0\r\n"; 97 | stream << "Host: " << host << ":" << port << "\r\n"; 98 | stream << "Accept: */*\r\n"; 99 | stream << "Connection: close\r\n\r\n"; 100 | stream.flush(); 101 | 102 | std::string status_code; 103 | std::string line; 104 | 105 | while (std::getline(stream, line) && line != "\r") { 106 | if(line.find("HTTP/") != std::string::npos){ 107 | status_code = line.substr(9, 3); 108 | } 109 | } 110 | while (std::getline(stream, line)) { 111 | reponse_data += line; 112 | } 113 | 114 | if (status_code.empty()) { 115 | reponse_data = "Invalid response"; 116 | return -1; 117 | } 118 | 119 | // 状态码非200, 不支持301/302等跳转 120 | if (status_code != "200") { 121 | reponse_data = "HTTP Status Code: " + status_code; 122 | } 123 | 124 | return easycpp::helpers::intval(status_code); 125 | } catch(std::exception& e) { 126 | reponse_data = e.what(); 127 | return -2; 128 | } 129 | } 130 | 131 | /// GET请求(重载) 132 | int easycpp::helpers::http_get(std::string url, std::string& reponse_data, const int timeout) 133 | { 134 | // 去掉url中的协议头 135 | if (url.find("http://") != std::string::npos) { 136 | url = url.substr(7); 137 | } 138 | // 补充path 139 | if(url.find("/") == std::string::npos){ 140 | url.append("/"); 141 | } 142 | 143 | // 分隔符位置 144 | size_t path_index = url.find("/"); 145 | // 分隔符位置 146 | size_t port_index = url.find(":"); 147 | 148 | // 截取host字符串 149 | std::string host = url.substr(0, path_index); 150 | if(port_index != std::string::npos){ 151 | host = url.substr(0, port_index); 152 | } 153 | 154 | // 截取port字符串 155 | std::string port = "80"; 156 | if(port_index != std::string::npos){ 157 | port = url.substr(port_index + 1, path_index - port_index - 1); 158 | } 159 | 160 | // 截取page字符串 161 | std::string page = url.substr(path_index, url.length() - path_index); 162 | 163 | return http_get(host, port, page, reponse_data, timeout); 164 | } 165 | 166 | /// url编码 167 | std::string easycpp::helpers::urlencode(const std::string& str) 168 | { 169 | std::string src = str; 170 | char hex[] = "0123456789ABCDEF"; 171 | std::string result; 172 | for (size_t i = 0; i < src.size(); ++i) { 173 | unsigned char cc = src[i]; 174 | if (cc == ' ' || cc == '.' || (cc >= 48 && cc <= 57) || (cc >=97 && cc <= 122) || (cc >=65 && cc <= 90)) { 175 | if(cc == ' '){ 176 | result += '+'; 177 | }else{ 178 | result += cc; 179 | } 180 | } else { 181 | unsigned char c = static_cast(src[i]); 182 | result += '%'; 183 | result += hex[c / 16]; 184 | result += hex[c % 16]; 185 | } 186 | } 187 | return result; 188 | } 189 | 190 | /// url解码 191 | std::string easycpp::helpers::urldecode(const std::string& str) 192 | { 193 | std::string result; 194 | int hex = 0; 195 | for (size_t i = 0; i < str.length(); ++i) 196 | { 197 | switch (str[i]) 198 | { 199 | case '+': 200 | result += ' '; 201 | break; 202 | case '%': 203 | if (isxdigit(str[i + 1]) && isxdigit(str[i + 2])) 204 | { 205 | std::string hexStr = str.substr(i + 1, 2); 206 | hex = strtol(hexStr.c_str(), 0, 16); 207 | //字母和数字[0-9a-zA-Z]、一些特殊符号[$-_.+!*'(),] 、以及某些保留字[$&+,/:;=?@] 208 | //可以不经过编码直接用于URL 209 | if (!((hex >= 48 && hex <= 57) || //0-9 210 | (hex >=97 && hex <= 122) || //a-z 211 | (hex >=65 && hex <= 90) || //A-Z 212 | //一些特殊符号及保留字[$-_.+!*'(),] [$&+,/:;=?@] 213 | hex == 0x21 || hex == 0x24 || hex == 0x26 || hex == 0x27 || hex == 0x28 || hex == 0x29 214 | || hex == 0x2a || hex == 0x2b|| hex == 0x2c || hex == 0x2d || hex == 0x2e || hex == 0x2f 215 | || hex == 0x3A || hex == 0x3B|| hex == 0x3D || hex == 0x3f || hex == 0x40 || hex == 0x5f 216 | )) 217 | { 218 | result += char(hex); 219 | i += 2; 220 | } 221 | else result += '%'; 222 | }else { 223 | result += '%'; 224 | } 225 | break; 226 | default: 227 | result += str[i]; 228 | break; 229 | } 230 | } 231 | return result; 232 | } 233 | 234 | /// 生成http请求字符串 235 | std::string easycpp::helpers::http_build_query(easycpp::libraries::JsonObject &query_data) 236 | { 237 | std::string query_str; 238 | BOOST_FOREACH (easycpp::libraries::JsonValue &v, query_data) { 239 | std::string key = v.first; 240 | std::string val = easycpp::helpers::json_get_string(query_data, key); 241 | val = urlencode(val); 242 | query_str += ("&" + key + "=" + val); 243 | } 244 | return query_str.empty() ? query_str : query_str.substr(1, query_str.size()); 245 | } 246 | -------------------------------------------------------------------------------- /easycpp/easycpp/helpers/http.h: -------------------------------------------------------------------------------- 1 | /* 2 | * http辅助函数 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #ifndef EASYCPP_HELPERS_HTTP_INCLUDED 7 | #define EASYCPP_HELPERS_HTTP_INCLUDED 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | namespace easycpp { 22 | namespace helpers { 23 | 24 | /// POST请求 25 | int http_post(const std::string& host, const std::string& port, const std::string& page, easycpp::libraries::JsonObject& form_data, std::string& reponse_data, int timeout = 30); 26 | 27 | /// POST请求(重载) 28 | int http_post(std::string url, easycpp::libraries::JsonObject& form_data, std::string& reponse_data, const int timeout = 30); 29 | 30 | /// GET请求 31 | int http_get(const std::string& host, const std::string& port, const std::string& page, std::string& reponse_data, const int timeout = 30); 32 | 33 | /// GET请求(重载) 34 | int http_get(std::string url, std::string& reponse_data, const int timeout = 30); 35 | 36 | /// url编码 37 | std::string urlencode(const std::string& str); 38 | 39 | /// url解码 40 | std::string urldecode(const std::string& str); 41 | 42 | /// 生成http请求字符串 43 | std::string http_build_query(easycpp::libraries::JsonObject &query_data); 44 | 45 | } 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /easycpp/easycpp/helpers/json.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * json辅助函数 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #include 7 | 8 | /// 初始化json对象 9 | easycpp::libraries::JsonObject easycpp::helpers::json_init(const std::string json_str) 10 | { 11 | easycpp::libraries::JsonObject obj; 12 | std::stringstream ss(json_str); 13 | try { 14 | boost::property_tree::read_json(ss, obj); 15 | } catch (boost::property_tree::ptree_error& ex) { 16 | std::string err_str = "json_init error: " + json_str + "; "; 17 | err_str.append(ex.what()); 18 | throw easycpp::libraries::Exception(err_str); 19 | } 20 | return obj; 21 | } 22 | 23 | /// 从json对象中获取json数组 24 | std::vector easycpp::helpers::json_get_array(easycpp::libraries::JsonObject &obj, const std::string key) 25 | { 26 | std::vector obj_ary; 27 | try { 28 | easycpp::libraries::JsonObject array = obj.get_child(key); 29 | BOOST_FOREACH(easycpp::libraries::JsonValue &v, array) 30 | { 31 | std::stringstream s; 32 | write_json(s, v.second); 33 | easycpp::libraries::JsonObject new_obj = json_init(s.str()); 34 | obj_ary.push_back(new_obj); 35 | } 36 | } catch (boost::property_tree::ptree_error& ex) { 37 | std::string err_str = "json_get_array error: " + key + "; "; 38 | err_str.append(ex.what()); 39 | throw easycpp::libraries::Exception(err_str); 40 | } 41 | return obj_ary; 42 | } 43 | 44 | /// 从json对象中获取json对象 45 | easycpp::libraries::JsonObject easycpp::helpers::json_get_object(easycpp::libraries::JsonObject &obj, const std::string key) 46 | { 47 | easycpp::libraries::JsonObject obj_child; 48 | try { 49 | obj_child = obj.get_child(key); 50 | } catch (boost::property_tree::ptree_error& ex) { 51 | std::string err_str = "json_get_object error: " + key + "; "; 52 | err_str.append(ex.what()); 53 | throw easycpp::libraries::Exception(err_str); 54 | } 55 | return obj_child; 56 | } 57 | 58 | /// 从json对象中获取值, string 59 | std::string easycpp::helpers::json_get_string(easycpp::libraries::JsonObject &obj, const std::string key) 60 | { 61 | std::string val = ""; 62 | try { 63 | val = obj.get(key); 64 | } catch (boost::property_tree::ptree_error& ex) { 65 | std::string err_str = "json_get_string error: " + key + "; "; 66 | err_str.append(ex.what()); 67 | throw easycpp::libraries::Exception(err_str); 68 | } 69 | return val; 70 | } 71 | 72 | /// 从json对象中获取值 int 73 | int easycpp::helpers::json_get_int(easycpp::libraries::JsonObject &obj, const std::string key) 74 | { 75 | int val = 0; 76 | try { 77 | val = obj.get(key); 78 | } catch (boost::property_tree::ptree_error& ex) { 79 | std::string err_str = "json_get_int error: " + key + "; "; 80 | err_str.append(ex.what()); 81 | throw easycpp::libraries::Exception(err_str); 82 | } 83 | return val; 84 | } 85 | -------------------------------------------------------------------------------- /easycpp/easycpp/helpers/json.h: -------------------------------------------------------------------------------- 1 | /* 2 | * json辅助函数 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #ifndef EASYCPP_HELPERS_JSON_INCLUDED 7 | #define EASYCPP_HELPERS_JSON_INCLUDED 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | namespace easycpp { 21 | namespace helpers { 22 | 23 | /// 初始化json对象 24 | easycpp::libraries::JsonObject json_init(const std::string json_str); 25 | 26 | /// 从json对象中获取json数组 27 | std::vector json_get_array(easycpp::libraries::JsonObject &obj, const std::string key); 28 | 29 | /// 从json对象中获取json对象 30 | easycpp::libraries::JsonObject json_get_object(easycpp::libraries::JsonObject &obj, const std::string key); 31 | 32 | /// 从json对象中获取值, string 33 | std::string json_get_string(easycpp::libraries::JsonObject &obj, const std::string key); 34 | 35 | /// 从json对象中获取值 int 36 | int json_get_int(easycpp::libraries::JsonObject &obj, const std::string key); 37 | 38 | } 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /easycpp/easycpp/helpers/log.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 日志辅助函数 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #include 7 | 8 | /// 输出错误日志 9 | void easycpp::helpers::log_error(std::string tag, std::string msg, std::string path) 10 | { 11 | std::string data = easycpp::helpers::date("%Y/%m/%d %X")+ " | ERROR | " + tag + " | " + msg + "\n"; 12 | easycpp::helpers::file_put_contents((path == "" ? "" : path + "/") + "error." + easycpp::helpers::date("%Y%m%d") + ".log", data, FILE_APPEND); 13 | } 14 | 15 | /// 输出调试日志 16 | void easycpp::helpers::log_debug(std::string tag, std::string msg, std::string path) 17 | { 18 | std::string data = easycpp::helpers::date("%Y/%m/%d %X") + " | DEBUG | " + tag + " | " + msg + "\n"; 19 | easycpp::helpers::file_put_contents((path == "" ? "" : path + "/") + "debug." + easycpp::helpers::date("%Y%m%d") + ".log", data, FILE_APPEND); 20 | } 21 | 22 | /// 输出信息日志 23 | void easycpp::helpers::log_info(std::string tag, std::string msg, std::string path) 24 | { 25 | std::string data = easycpp::helpers::date("%Y/%m/%d %X") + " | INFO | " + tag + " | " + msg + "\n"; 26 | easycpp::helpers::file_put_contents((path == "" ? "" : path + "/") + "info." + easycpp::helpers::date("%Y%m%d") + ".log", data, FILE_APPEND); 27 | } 28 | -------------------------------------------------------------------------------- /easycpp/easycpp/helpers/log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 日志辅助函数 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #ifndef EASYCPP_HELPERS_LOG_INCLUDED 7 | #define EASYCPP_HELPERS_LOG_INCLUDED 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | 15 | namespace easycpp { 16 | namespace helpers { 17 | 18 | /// 输出错误日志 19 | void log_error(std::string tag, std::string msg, std::string path = ""); 20 | 21 | /// 输出调试日志 22 | void log_debug(std::string tag, std::string msg, std::string path = ""); 23 | 24 | /// 输出信息日志 25 | void log_info(std::string tag, std::string msg, std::string path = ""); 26 | 27 | } 28 | } 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /easycpp/easycpp/helpers/string.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 字符串辅助函数 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #include 7 | 8 | /// 替换字符 9 | std::string easycpp::helpers::str_replace(const std::string find, const std::string replace, std::string string) 10 | { 11 | if(find.empty() || string.empty()){ 12 | return string; 13 | } 14 | size_t pos = 0; 15 | while(true) { 16 | pos = string.find(find, pos); 17 | if (pos == std::string::npos) { 18 | break; 19 | } 20 | string.replace(pos, find.size(), replace); 21 | } 22 | return string; 23 | } 24 | 25 | /// 把字符串打散为数组 26 | std::vector easycpp::helpers::explode(const std::string separator, const std::string string) 27 | { 28 | std::vector array; 29 | if(separator.empty() || string.empty()){ 30 | return array; 31 | } 32 | size_t pos = 0; 33 | size_t start = 0; 34 | while(true) { 35 | pos = string.find(separator, pos); 36 | if (pos == std::string::npos) { 37 | std::string str = string.substr(start); 38 | array.push_back(str); 39 | break; 40 | } 41 | std::string str = string.substr(start, pos - start); 42 | array.push_back(str); 43 | pos++; 44 | start = pos; 45 | } 46 | return array; 47 | } 48 | 49 | /// 把数组元素组合为字符串 50 | std::string easycpp::helpers::implode(std::string separator, std::vector array) 51 | { 52 | std::string str; 53 | int size = array.size(); 54 | for(int i = 0; i 10 | #include 11 | #include 12 | 13 | namespace easycpp { 14 | namespace helpers { 15 | 16 | /// 替换字符 17 | std::string str_replace(const std::string find, const std::string replace, std::string string); 18 | 19 | /// 把字符串打散为数组 20 | std::vector explode(const std::string separator, const std::string string); 21 | 22 | /// 把数组元素组合为字符串 23 | std::string implode(std::string separator, std::vector array); 24 | 25 | } 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /easycpp/easycpp/helpers/type.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 类型转换辅助函数 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #include 7 | 8 | /// 转换为string类型 9 | std::string easycpp::helpers::strval(int var) 10 | { 11 | return boost::lexical_cast(var); 12 | } 13 | 14 | /// 转换为int类型, 通过string 15 | int easycpp::helpers::intval(std::string var) 16 | { 17 | return boost::lexical_cast(var); 18 | } 19 | 20 | /// 转换为int类型, 通过char数组 21 | int easycpp::helpers::intval(char *var) 22 | { 23 | return boost::lexical_cast(var); 24 | } 25 | -------------------------------------------------------------------------------- /easycpp/easycpp/helpers/type.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 类型转换辅助函数 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #ifndef EASYCPP_HELPERS_TYPE_INCLUDED 7 | #define EASYCPP_HELPERS_TYPE_INCLUDED 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | 14 | namespace easycpp { 15 | namespace helpers { 16 | 17 | /// 转换为string类型 18 | std::string strval(int var); 19 | 20 | /// 转换为int类型, 通过string 21 | int intval(std::string var); 22 | 23 | /// 转换为int类型, 通过char数组 24 | int intval(char *var); 25 | 26 | } 27 | } 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /easycpp/easycpp/libraries/exception.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 异常处理类 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #include 7 | 8 | easycpp::libraries::Exception::Exception(std::string &msg) 9 | { 10 | msg_ = msg; 11 | } 12 | 13 | easycpp::libraries::Exception::Exception(const char *msg) 14 | { 15 | msg_ = std::string(msg); 16 | } 17 | 18 | easycpp::libraries::Exception::~Exception() throw() 19 | { 20 | 21 | } 22 | 23 | const char* easycpp::libraries::Exception::what() const throw() 24 | { 25 | return msg_.c_str(); 26 | } 27 | -------------------------------------------------------------------------------- /easycpp/easycpp/libraries/exception.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 异常处理类 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #ifndef EASYCPP_LIBRARIES_EXCEPTION_INCLUDED 7 | #define EASYCPP_LIBRARIES_EXCEPTION_INCLUDED 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | namespace easycpp { 14 | namespace libraries { 15 | 16 | class Exception : public std::exception 17 | { 18 | public: 19 | Exception(std::string &msg); 20 | 21 | Exception(const char* msg); 22 | 23 | ~Exception() throw(); 24 | 25 | const char* what() const throw(); 26 | private: 27 | std::string msg_; 28 | }; 29 | 30 | } 31 | } 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /easycpp/easycpp/libraries/json.h: -------------------------------------------------------------------------------- 1 | /* 2 | * json处理类 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #ifndef EASYCPP_LIBRARIES_JSON_INCLUDED 7 | #define EASYCPP_LIBRARIES_JSON_INCLUDED 8 | 9 | #include 10 | 11 | namespace easycpp { 12 | namespace libraries { 13 | 14 | typedef boost::property_tree::ptree JsonObject; 15 | typedef boost::property_tree::ptree::value_type JsonValue; 16 | 17 | } 18 | } 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /easycpp/easycpp/models/redis.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * redis数据库模型类 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #include 7 | 8 | easycpp::models::RedisModel::RedisModel(std::string ip, int port, std::string passwd) 9 | { 10 | // 连接 11 | conn = redisConnect(ip.c_str(), port); 12 | if(conn->err){ 13 | std::string err_str("redis conn failed; "); 14 | err_str.append(conn->errstr); 15 | err_str += "; "; 16 | throw easycpp::libraries::Exception(err_str); 17 | }else{ 18 | // 校验密码 19 | redisReply* reply = (redisReply*)redisCommand(conn, "AUTH %s", passwd.c_str()); 20 | if(reply == NULL){ 21 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 22 | } 23 | int reply_type = reply->type; 24 | freeReplyObject(reply); 25 | if(reply_type == REDIS_REPLY_ERROR){ 26 | throw easycpp::libraries::Exception("redis auth failed; "); 27 | } 28 | } 29 | } 30 | 31 | easycpp::models::RedisModel::~RedisModel() 32 | { 33 | closeConn(); // 关闭连接 34 | array.clear(); // 清除数据 35 | } 36 | 37 | /// 获取字符串 38 | std::string easycpp::models::RedisModel::getString(std::string key) 39 | { 40 | key = "string:" + key; 41 | std::string cmd = "GET " + key; 42 | redisReply* reply = (redisReply*)redisCommand(conn, cmd.c_str()); 43 | // 重要错误, redis需重新连接 44 | if(reply == NULL){ 45 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 46 | } 47 | // 取数据 48 | std::string value = ""; 49 | if (reply->type == REDIS_REPLY_STRING) { 50 | value = reply->str; 51 | } 52 | freeReplyObject(reply); 53 | return value; 54 | } 55 | 56 | /// 设置字符串(带有效期) 57 | bool easycpp::models::RedisModel::setString(std::string key, std::string text, int expire) 58 | { 59 | // 设置数据 60 | key = "string:" + key; 61 | text = easycpp::helpers::str_replace(" ", "", text); 62 | std::string cmd = "SET " + key + " " + text; 63 | redisReply* reply = (redisReply*)redisCommand(conn, cmd.c_str()); 64 | // 重要错误, redis需重新连接 65 | if(reply == NULL){ 66 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 67 | } 68 | int reply_type = reply->type; 69 | freeReplyObject(reply); 70 | // 返回状态 71 | if (reply_type == REDIS_REPLY_ERROR) { 72 | return false; 73 | } 74 | // 设置有效时间(秒) 75 | if(expire > -1){ 76 | setExpire(key, expire); 77 | } 78 | // 返回状态 79 | return true; 80 | } 81 | 82 | /// 获取数组 83 | std::map* easycpp::models::RedisModel::getHash(std::string key) 84 | { 85 | key = "array:" + key; 86 | std::string cmd = "HGETALL " + key; 87 | redisReply* reply = (redisReply*)redisCommand(conn, cmd.c_str()); 88 | // 重要错误, redis需重新连接 89 | if(reply == NULL){ 90 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 91 | } 92 | array.clear(); // 清除之前的数据 93 | if (reply->type == REDIS_REPLY_ARRAY) { 94 | std::string key, val; 95 | for (size_t i = 0; i < reply->elements; i++) { 96 | if(i % 2 == 0){ 97 | key = reply->element[i]->str; 98 | }else{ 99 | val = reply->element[i]->str; 100 | array[key] = val; 101 | } 102 | } 103 | } 104 | freeReplyObject(reply); 105 | return &array; 106 | } 107 | 108 | /// 设置数组(带有效期) 109 | bool easycpp::models::RedisModel::setHash(std::string key, std::map &array, int expire) 110 | { 111 | // 设置数据 112 | key = "array:" + key; 113 | std::string cmd = "HMSET " + key; 114 | for (std::map::iterator it = array.begin(); it != array.end(); ++it) { 115 | std::string key = it->first; 116 | std::string val = it->second; 117 | val = easycpp::helpers::str_replace(" ", "", val); 118 | cmd = cmd + " " + key + " " + val; 119 | } 120 | redisReply* reply = (redisReply*)redisCommand(conn, cmd.c_str()); 121 | // 重要错误, redis需重新连接 122 | if(reply == NULL){ 123 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 124 | } 125 | int reply_type = reply->type; 126 | freeReplyObject(reply); 127 | // 返回状态 128 | if (reply_type == REDIS_REPLY_ERROR) { 129 | return false; 130 | } 131 | // 设置有效时间(秒) 132 | if(expire > -1){ 133 | setExpire(key, expire); 134 | } 135 | // 返回状态 136 | return true; 137 | } 138 | 139 | /// 往列表头部插入一条数据 140 | bool easycpp::models::RedisModel::pushList(std::string key, std::string text) 141 | { 142 | // 设置数据 143 | key = "list:" + key; 144 | text = easycpp::helpers::str_replace(" ", "", text); 145 | std::string cmd = "LPUSH " + key + " " + text; 146 | redisReply* reply = (redisReply*)redisCommand(conn, cmd.c_str()); 147 | // 重要错误, redis需重新连接 148 | if(reply == NULL){ 149 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 150 | } 151 | int reply_type = reply->type; 152 | freeReplyObject(reply); 153 | // 返回状态 154 | if (reply_type == REDIS_REPLY_ERROR) { 155 | return false; 156 | } 157 | return true; 158 | } 159 | 160 | /// 从列表尾部拉取一条数据(带堵塞超时) 161 | std::string easycpp::models::RedisModel::pullList(std::string key, int timeout) 162 | { 163 | key = "list:" + key; 164 | std::string cmd; 165 | if(timeout == -1){ 166 | cmd = "RPOP " + key; 167 | }else{ 168 | cmd = "BRPOP " + key + " " + easycpp::helpers::strval(timeout); 169 | } 170 | redisReply* reply = (redisReply*)redisCommand(conn, cmd.c_str()); 171 | // 重要错误, redis需重新连接 172 | if(reply == NULL){ 173 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 174 | } 175 | std::string value = ""; 176 | if (reply->type == REDIS_REPLY_STRING) { 177 | value = reply->str; 178 | } 179 | if (reply->type == REDIS_REPLY_ARRAY) { 180 | value = reply->element[1]->str; 181 | } 182 | freeReplyObject(reply); 183 | return value; 184 | } 185 | 186 | /// 关闭连接 187 | void easycpp::models::RedisModel::closeConn() 188 | { 189 | redisFree(conn); 190 | } 191 | 192 | /// 设置有效时间 193 | bool easycpp::models::RedisModel::setExpire(std::string key, int expire) 194 | { 195 | // 设置 196 | std::string cmd = "EXPIRE " + key + " " + easycpp::helpers::strval(expire); 197 | redisReply* reply = (redisReply*)redisCommand(conn, cmd.c_str()); 198 | // 重要错误, redis需重新连接 199 | if(reply == NULL){ 200 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 201 | } 202 | int reply_type = reply->type; 203 | freeReplyObject(reply); 204 | // 返回状态 205 | if (reply_type == REDIS_REPLY_ERROR) { 206 | return false; 207 | } 208 | return true; 209 | } 210 | -------------------------------------------------------------------------------- /easycpp/easycpp/models/redis.h: -------------------------------------------------------------------------------- 1 | /* 2 | * redis数据库模型类 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #ifndef EASYCPP_MODELS_REDIS_INCLUDED 7 | #define EASYCPP_MODELS_REDIS_INCLUDED 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | namespace easycpp { 20 | namespace models { 21 | 22 | /* 23 | * redis模型类 24 | */ 25 | class RedisModel 26 | { 27 | public: 28 | RedisModel(std::string ip, int port, std::string passwd); 29 | 30 | ~RedisModel(); 31 | 32 | /// 获取字符串 33 | std::string getString(std::string key); 34 | 35 | /// 设置字符串(带有效期) 36 | bool setString(std::string key, std::string text, int expire = -1); 37 | 38 | /// 获取数组 39 | std::map* getHash(std::string key); 40 | 41 | /// 设置数组(带有效期) 42 | bool setHash(std::string key, std::map &array, int expire = -1); 43 | 44 | /// 往列表头部插入一条数据 45 | bool pushList(std::string key, std::string text); 46 | 47 | /// 从列表尾部拉取一条数据(带堵塞超时) 48 | std::string pullList(std::string key, int timeout = -1); 49 | 50 | /// 设置有效时间 51 | bool setExpire(std::string key, int expire); 52 | 53 | private: 54 | redisContext* conn; 55 | 56 | std::map array; 57 | 58 | /// 关闭连接 59 | void closeConn(); 60 | }; 61 | 62 | } 63 | } 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /easycpp/easycpp/models/redis.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * redis数据库模型类 3 | * @author 刘健 59208859@qq.com 4 | */ 5 | 6 | #ifndef EASYCPP_MODELS_REDIS_INCLUDED 7 | #define EASYCPP_MODELS_REDIS_INCLUDED 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | namespace easycpp { 20 | namespace models { 21 | 22 | /* 23 | * redis哈希数据类 24 | */ 25 | class Kv 26 | { 27 | public: 28 | std::string key; 29 | std::string val; 30 | }; 31 | 32 | /* 33 | * redis模型类 34 | */ 35 | class RedisModel 36 | { 37 | public: 38 | RedisModel(std::string ip, int port, std::string passwd) 39 | { 40 | // 连接 41 | conn = redisConnect(ip.c_str(), port); 42 | if(conn->err){ 43 | std::string err_str("redis conn failed; "); 44 | err_str.append(conn->errstr); 45 | err_str += "; "; 46 | throw easycpp::libraries::Exception(err_str); 47 | }else{ 48 | // 校验密码 49 | redisReply* reply = (redisReply*)redisCommand(conn, "AUTH %s", passwd.c_str()); 50 | if(reply == NULL){ 51 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 52 | } 53 | int reply_type = reply->type; 54 | freeReplyObject(reply); 55 | if(reply_type == REDIS_REPLY_ERROR){ 56 | throw easycpp::libraries::Exception("redis auth failed; "); 57 | } 58 | } 59 | } 60 | 61 | ~RedisModel() 62 | { 63 | closeConn(); // 关闭连接 64 | array.clear(); // 清除数据 65 | } 66 | 67 | /// 获取字符串 68 | std::string getString(std::string key) 69 | { 70 | key = "string:" + key; 71 | std::string cmd = "GET " + key; 72 | redisReply* reply = (redisReply*)redisCommand(conn, cmd.c_str()); 73 | // 重要错误, redis需重新连接 74 | if(reply == NULL){ 75 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 76 | } 77 | // 取数据 78 | std::string value = ""; 79 | if (reply->type == REDIS_REPLY_STRING) { 80 | value = reply->str; 81 | } 82 | freeReplyObject(reply); 83 | return value; 84 | } 85 | 86 | /// 设置字符串(带有效期) 87 | bool setString(std::string key, std::string text, int expire = 0) 88 | { 89 | // 设置数据 90 | key = "string:" + key; 91 | easycpp::helpers::str_replace(" ", "", text); 92 | std::string cmd = "SET " + key + " " + text; 93 | redisReply* reply = (redisReply*)redisCommand(conn, cmd.c_str()); 94 | // 重要错误, redis需重新连接 95 | if(reply == NULL){ 96 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 97 | } 98 | int reply_type = reply->type; 99 | freeReplyObject(reply); 100 | // 返回状态 101 | if (reply_type == REDIS_REPLY_ERROR) { 102 | return false; 103 | } 104 | // 设置有效时间(秒) 105 | setExpire(key, expire); 106 | // 返回状态 107 | return true; 108 | } 109 | 110 | /// 获取数组 111 | std::vector* getArray(std::string key) 112 | { 113 | key = "array:" + key; 114 | std::string cmd = "HGETALL " + key; 115 | redisReply* reply = (redisReply*)redisCommand(conn, cmd.c_str()); 116 | // 重要错误, redis需重新连接 117 | if(reply == NULL){ 118 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 119 | } 120 | array.clear(); // 清除之前的数据 121 | if (reply->type == REDIS_REPLY_ARRAY) { 122 | Kv tmp; 123 | for (size_t i = 0; i < reply->elements; i++) { 124 | if(i % 2 == 0){ 125 | tmp.key = reply->element[i]->str; 126 | }else{ 127 | tmp.val = reply->element[i]->str; 128 | array.push_back(tmp); 129 | } 130 | } 131 | } 132 | freeReplyObject(reply); 133 | return &array; 134 | } 135 | 136 | /// 设置数组(带有效期) 137 | bool setArray(std::string key, std::vector &array, int expire = 0) 138 | { 139 | // 设置数据 140 | key = "array:" + key; 141 | std::string cmd = "HMSET " + key; 142 | for (size_t i = 0; i < array.size(); i++) { 143 | Kv* kv = &array.at(i); 144 | easycpp::helpers::str_replace(" ", "", kv->val); 145 | cmd = cmd + " " + kv->key + " " + kv->val; 146 | } 147 | redisReply* reply = (redisReply*)redisCommand(conn, cmd.c_str()); 148 | // 重要错误, redis需重新连接 149 | if(reply == NULL){ 150 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 151 | } 152 | int reply_type = reply->type; 153 | freeReplyObject(reply); 154 | // 返回状态 155 | if (reply_type == REDIS_REPLY_ERROR) { 156 | return false; 157 | } 158 | // 设置有效时间(秒) 159 | setExpire(key, expire); 160 | // 返回状态 161 | return true; 162 | } 163 | 164 | /// 获取表格的一行数据 165 | std::vector* getRow(std::string table, std::string id) 166 | { 167 | std::string key = table + ":" + id; 168 | std::string cmd = "HGETALL " + key; 169 | redisReply* reply = (redisReply*)redisCommand(conn, cmd.c_str()); 170 | // 重要错误, redis需重新连接 171 | if(reply == NULL){ 172 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 173 | } 174 | array.clear(); // 清除之前的数据 175 | if (reply->type == REDIS_REPLY_ARRAY) { 176 | Kv tmp; 177 | for (size_t i = 0; i < reply->elements; i++) { 178 | if(i % 2 == 0){ 179 | tmp.key = reply->element[i]->str; 180 | }else{ 181 | tmp.val = reply->element[i]->str; 182 | array.push_back(tmp); 183 | } 184 | } 185 | } 186 | freeReplyObject(reply); 187 | return &array; 188 | } 189 | 190 | /// 设置表格的一行数据(带有效期) 191 | bool setRow(std::string table, std::string id, std::vector &array, int expire = 0) 192 | { 193 | // 设置数据 194 | std::string key = table + ":" + id; 195 | std::string cmd = "HMSET " + key; 196 | for (size_t i = 0; i < array.size(); i++) { 197 | Kv* kv = &array.at(i); 198 | easycpp::helpers::str_replace(" ", "", kv->val); 199 | cmd = cmd + " " + kv->key + " " + kv->val; 200 | } 201 | redisReply* reply = (redisReply*)redisCommand(conn, cmd.c_str()); 202 | // 重要错误, redis需重新连接 203 | if(reply == NULL){ 204 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 205 | } 206 | int reply_type = reply->type; 207 | freeReplyObject(reply); 208 | // 返回状态 209 | if (reply_type == REDIS_REPLY_ERROR) { 210 | return false; 211 | } 212 | // 设置有效时间(秒) 213 | setExpire(key, expire); 214 | // 返回状态 215 | return true; 216 | } 217 | 218 | /// 从哈希表获取指定key的值 219 | static std::string getHashItem(std::vector* array, std::string key) 220 | { 221 | for (size_t i = 0; i < array->size(); i++) { 222 | Kv* kv = &array->at(i); 223 | if (key == kv->key) { 224 | return kv->val; 225 | } 226 | } 227 | return ""; 228 | } 229 | 230 | /// 往列表头部插入一条数据 231 | bool pushList(std::string key, std::string text) 232 | { 233 | // 设置数据 234 | key = "list:" + key; 235 | easycpp::helpers::str_replace(" ", "", text); 236 | std::string cmd = "LPUSH " + key + " " + text; 237 | redisReply* reply = (redisReply*)redisCommand(conn, cmd.c_str()); 238 | // 重要错误, redis需重新连接 239 | if(reply == NULL){ 240 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 241 | } 242 | int reply_type = reply->type; 243 | freeReplyObject(reply); 244 | // 返回状态 245 | if (reply_type == REDIS_REPLY_ERROR) { 246 | return false; 247 | } 248 | return true; 249 | } 250 | 251 | /// 从列表尾部拉取一条数据(带堵塞超时) 252 | std::string pullList(std::string key, int timeout = 0) 253 | { 254 | key = "list:" + key; 255 | std::string cmd; 256 | if(timeout == 0){ 257 | cmd = "RPOP " + key; 258 | }else{ 259 | cmd = "BRPOP " + key + " " + easycpp::helpers::strval(timeout); 260 | } 261 | redisReply* reply = (redisReply*)redisCommand(conn, cmd.c_str()); 262 | // 重要错误, redis需重新连接 263 | if(reply == NULL){ 264 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 265 | } 266 | std::string value = ""; 267 | if (reply->type == REDIS_REPLY_STRING) { 268 | value = reply->str; 269 | } 270 | if (reply->type == REDIS_REPLY_ARRAY) { 271 | value = reply->element[1]->str; 272 | } 273 | freeReplyObject(reply); 274 | return value; 275 | } 276 | 277 | private: 278 | redisContext* conn; 279 | std::vector array; 280 | 281 | /// 关闭连接 282 | void closeConn() 283 | { 284 | redisFree(conn); 285 | } 286 | 287 | /// 设置有效时间 288 | bool setExpire(std::string key, int expire) 289 | { 290 | if(expire == 0){ 291 | return false; 292 | } 293 | // 设置 294 | std::string cmd = "EXPIRE " + key + " " + easycpp::helpers::strval(expire); 295 | redisReply* reply = (redisReply*)redisCommand(conn, cmd.c_str()); 296 | // 重要错误, redis需重新连接 297 | if(reply == NULL){ 298 | throw easycpp::libraries::Exception("redis cmd failed, please connect again; "); 299 | } 300 | int reply_type = reply->type; 301 | freeReplyObject(reply); 302 | // 返回状态 303 | if (reply_type == REDIS_REPLY_ERROR) { 304 | return false; 305 | } 306 | return true; 307 | } 308 | }; 309 | 310 | } 311 | } 312 | 313 | #endif 314 | -------------------------------------------------------------------------------- /easycpp/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace easycpp; 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | 19 | 20 | 21 | return 0; 22 | } 23 | --------------------------------------------------------------------------------