diff --git a/src/Husky/CMakeLists.txt b/src/Husky/CMakeLists.txt index 41daa97..281fd79 100644 --- a/src/Husky/CMakeLists.txt +++ b/src/Husky/CMakeLists.txt @@ -1,9 +1,8 @@ - +SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) -SET(LIBHUSKY_SRC Daemon.cpp ServerFrame.cpp) -INCLUDE_DIRECTORIES(../limonp) +SET(LIBHUSKY_SRC Daemon.cpp ServerFrame.cpp) ADD_LIBRARY(husky STATIC ${LIBHUSKY_SRC}) -INSTALL(TARGETS husky ARCHIVE DESTINATION lib/CppJieba) -INSTALL(FILES Daemon.h globals.h HttpReqInfo.hpp ServerFrame.h ThreadManager.hpp DESTINATION include/CppJieba/Husky) +INSTALL(TARGETS husky ARCHIVE DESTINATION lib/CppJieba/Husky) +INSTALL(FILES Daemon.h globals.h HttpReqInfo.hpp ServerFrame.h ThreadManager.hpp DESTINATION include/CppJieba/Husky) diff --git a/src/Husky/Daemon.h b/src/Husky/Daemon.h index 532765a..e1402b8 100644 --- a/src/Husky/Daemon.h +++ b/src/Husky/Daemon.h @@ -8,7 +8,7 @@ #include #include #include -#include +#include "Limonp/logger.hpp" #include "ServerFrame.h" namespace Husky diff --git a/src/Husky/HttpReqInfo.hpp b/src/Husky/HttpReqInfo.hpp index fe1cb2d..a664f5f 100644 --- a/src/Husky/HttpReqInfo.hpp +++ b/src/Husky/HttpReqInfo.hpp @@ -3,14 +3,14 @@ #include #include +#include "Limonp/logger.hpp" +#include "Limonp/str_functs.hpp" #include "globals.h" -#include -#include -#include namespace Husky { using namespace Limonp; + using namespace std; static const char* const KEY_METHOD = "METHOD"; static const char* const KEY_PATH = "PATH"; @@ -161,6 +161,8 @@ namespace Husky HashMap _headerMap; HashMap _methodGetMap; HashMap _methodPostMap; + //public: + friend ostream& operator<<(ostream& os, const HttpReqInfo& obj); private: bool _find(const HashMap& mp, const string& key, string& res)const { @@ -172,19 +174,6 @@ namespace Husky res = it->second; return true; } - public: - //string toString() const;// function for debug because of heavy time consuming - //string toString() const - //{ - // string res("{"); - // res += HashMapToString(_headerMap); - // res += ","; - // res += HashMapToString(_methodGetMap); - // res += ","; - // res += HashMapToString(_methodPostMap); - // res += "}"; - // return res; - //} private: bool _parseUrl(const string& url, HashMap& mp) { @@ -227,6 +216,11 @@ namespace Husky } }; + inline std::ostream& operator << (std::ostream& os, const Husky::HttpReqInfo& obj) + { + return os << obj._headerMap << obj._methodGetMap << obj._methodPostMap; + } + } #endif diff --git a/src/Husky/Limonp/ArgvContext.hpp b/src/Husky/Limonp/ArgvContext.hpp new file mode 100644 index 0000000..8be15f1 --- /dev/null +++ b/src/Husky/Limonp/ArgvContext.hpp @@ -0,0 +1,90 @@ +/************************************ + * file enc : ascii + * author : wuyanyi09@gmail.com + ************************************/ + +#ifndef LIMONP_ARGV_FUNCTS_H +#define LIMONP_ARGV_FUNCTS_H + +#include +#include +#include "str_functs.hpp" +#include "map_functs.hpp" + +namespace Limonp +{ + using namespace std; + class ArgvContext + { + public : + ArgvContext(int argc, const char* const * argv) + { + + for(int i = 0; i < argc; i++) + { + if(strStartsWith(argv[i], "-")) + { + if(i + 1 < argc && !strStartsWith(argv[i + 1], "-")) + { + _mpss[argv[i]] = argv[i+1]; + i++; + } + else + { + _sset.insert(argv[i]); + } + } + else + { + _args.push_back(argv[i]); + } + } + } + ~ArgvContext(){}; + public: + friend ostream& operator << (ostream& os, const ArgvContext& args); + string operator [](uint i) + { + if(i < _args.size()) + { + return _args[i]; + } + return ""; + } + string operator [](const string& key) + { + map::const_iterator it = _mpss.find(key); + if(it != _mpss.end()) + { + return it->second; + } + return ""; + } + public: + bool hasKey(const string& key) + { + if(_mpss.find(key) != _mpss.end() || _sset.find(key) != _sset.end()) + { + return true; + } + return false; + } + private: + vector _args; + map _mpss; + set _sset; + + }; + + inline ostream& operator << (ostream& os, const ArgvContext& args) + { + return os< +#include +#include +#include +#include "logger.hpp" + +namespace Limonp +{ + using namespace std; + class MysqlClient + { + public: + typedef vector< vector > RowsType; + private: + const char * const HOST; + const unsigned int PORT; + const char * const USER; + const char * const PASSWD; + const char * const DB; + const char * const CHARSET; + public: + MysqlClient(const char* host, uint port, const char* user, const char* passwd, const char* db, const char* charset = "utf8"): HOST(host), PORT(port), USER(user), PASSWD(passwd), DB(db), CHARSET(charset){ _conn = NULL;}; + ~MysqlClient(){dispose();}; + public: + bool init() + { + //cout< vec; + for(uint i = 0; i < num_fields; i ++) + { + row[i] ? vec.push_back(row[i]) : vec.push_back("NULL"); + } + rows.push_back(vec); + } + mysql_free_result(result); + return true; + } + + private: + MYSQL * _conn; + + }; +} + +#endif diff --git a/src/Husky/Limonp/cast_functs.hpp b/src/Husky/Limonp/cast_functs.hpp new file mode 100644 index 0000000..cf8c54f --- /dev/null +++ b/src/Husky/Limonp/cast_functs.hpp @@ -0,0 +1,87 @@ +#ifndef LIMONP_CAST_FUNCTS_H +#define LIMONP_CAST_FUNCTS_H + +namespace Limonp +{ + //logical and or + static const int sign_32 = 0xC0000000; + static const int exponent_32 = 0x07800000; + static const int mantissa_32 = 0x007FE000; + static const int sign_exponent_32 = 0x40000000; + static const int loss_32 = 0x38000000; + + static const short sign_16 = (short)0xC000; + static const short exponent_16 = (short)0x3C00; + static const short mantissa_16 = (short)0x03FF; + static const short sign_exponent_16 = (short)0x4000; + static const int exponent_fill_32 = 0x38000000; + + //infinite + static const short infinite_16 = (short) 0x7FFF; + static const short infinitesmall_16 = (short) 0x0000; + + inline float intBitsToFloat(unsigned int x) + { + union + { + float f; + int i; + }u; + u.i = x; + return u.f; + } + + inline int floatToIntBits(float f) + { + union + { + float f; + int i ; + }u; + u.f = f; + return u.i; + } + + inline short floatToShortBits(float f) + { + int fi = floatToIntBits(f); + + // 提取关键信息 + short sign = (short) ((unsigned int)(fi & sign_32) >> 16); + short exponent = (short) ((unsigned int)(fi & exponent_32) >> 13); + short mantissa = (short) ((unsigned int)(fi & mantissa_32) >> 13); + // 生成编码结果 + short code = (short) (sign | exponent | mantissa); + // 无穷大量、无穷小量的处理 + if ((fi & loss_32) > 0 && (fi & sign_exponent_32) > 0) { + // 当指数符号为1时(正次方),且左234位为1,返回无穷大量 + return (short) (code | infinite_16); + } + if (((fi & loss_32) ^ loss_32) > 0 && (fi & sign_exponent_32) == 0) { + // 当指数符号位0时(负次方),且左234位为0(与111异或>0),返回无穷小量 + return infinitesmall_16; + } + + return code; + } + + inline float shortBitsToFloat(short s) + { + /* + * 指数空余3位:若符号位为1,补0;若符号位为0,补1。 尾数位在后补0(13个) + */ + int sign = ((int) (s & sign_16)) << 16; + int exponent = ((int) (s & exponent_16)) << 13; + // 指数符号位为0,234位补1 + if ((s & sign_exponent_16) == 0 && s != 0) { + exponent |= exponent_fill_32; + } + int mantissa = ((int) (s & mantissa_16)) << 13; + // 生成解码结果 + int code = sign | exponent | mantissa; + return intBitsToFloat(code); + + } +} + +#endif diff --git a/src/Husky/Limonp/config.hpp b/src/Husky/Limonp/config.hpp new file mode 100644 index 0000000..3e45e6d --- /dev/null +++ b/src/Husky/Limonp/config.hpp @@ -0,0 +1,107 @@ +/************************************ + * file enc : utf8 + * author : wuyanyi09@gmail.com + ************************************/ +#ifndef LIMONP_CONFIG_H +#define LIMONP_CONFIG_H + + +#include +#include +#include +#include "logger.hpp" +#include "str_functs.hpp" + +namespace Limonp +{ + using std::map; + using std::string; + using std::cout; + using std::endl; + using std::ifstream; + class Config + { + public: + Config(){_isInit = false;}; + ~Config(){}; + bool init(const string& configFile) + { + if(_isInit) + { + LogFatal("already have been initialized. "); + return false; + } + ifstream ifile(configFile.c_str()); + if(!ifile) + { + LogFatal("open configFile[%s] failed.", configFile.c_str()); + return false; + } + string line, key, value; + vector vecBuf; + while(getline(ifile, line)) + { + //line = _stripComment(line); + if(line.empty()) + { + continue; + } + vecBuf.clear(); + if(!splitStr(line, vecBuf, "=") || 2 != vecBuf.size()) + { + LogFatal("line[%s] is illegal.", line.c_str()); + return false; + } + key = vecBuf[0]; + value = vecBuf[1]; + if(_map.end() != _map.find(key)) + { + LogFatal("key[%s] already exists.", key.c_str()); + return false; + } + _map[key] = value; + } + ifile.close(); + _isInit = true; + return true; + } + void display() + { + for(map::iterator it = _map.begin(); it != _map.end(); it++) + { + cout<<"("<first<<","<second<<")"< _map; + bool _isInit; + + }; +} + +namespace Limonp +{ + extern Config gConfig; +} + +#endif diff --git a/src/Husky/Limonp/io_functs.hpp b/src/Husky/Limonp/io_functs.hpp new file mode 100644 index 0000000..0128e62 --- /dev/null +++ b/src/Husky/Limonp/io_functs.hpp @@ -0,0 +1,82 @@ +/************************************ + * file enc : utf8 + * author : wuyanyi09@gmail.com +************************************/ +#ifndef LIMONP_IO_FUNCTS_H +#define LIMONP_IO_FUNCTS_H +#include +#include +#include +namespace Limonp +{ + using namespace std; + inline string loadFile2Str(const char * const filepath) + { + ifstream in(filepath); + if(!in) + { + return ""; + } + istreambuf_iterator beg(in), end; + string str(beg, end); + in.close(); + return str; + } + + inline void loadStr2File(const char * const filename, ios_base::openmode mode, const string& str) + { + ofstream out(filename, mode); + ostreambuf_iterator itr (out); + copy(str.begin(), str.end(), itr); + out.close(); + } + + inline int ReadFromFile(const char * fileName, char* buf, int maxCount, const char* mode) + { + FILE* fp = fopen(fileName, mode); + if (!fp) + return 0; + int ret; + fgets(buf, maxCount, fp) ? ret = 1 : ret = 0; + fclose(fp); + return ret; + } + + inline int WriteStr2File(const char* fileName, const char* buf, const char* mode) + { + FILE* fp = fopen(fileName, mode); + if (!fp) + return 0; + int n = fprintf(fp, "%s", buf); + fclose(fp); + return n; + } + + inline bool checkFileExist(const string& filePath) + { + fstream _file; + _file.open(filePath.c_str(), ios::in); + if(_file) + return true; + return false; + } + + inline bool createDir(const string& dirPath, bool p = true) + { + string dir_str(dirPath); + string cmd = "mkdir"; + if(p) + { + cmd += " -p"; + } + cmd += " " + dir_str; + int res = system(cmd.c_str()); + return res; + } + + inline bool checkDirExist(const string& dirPath) + { + return checkFileExist(dirPath); + } +} +#endif diff --git a/src/Husky/Limonp/logger.hpp b/src/Husky/Limonp/logger.hpp new file mode 100644 index 0000000..ed1a3c2 --- /dev/null +++ b/src/Husky/Limonp/logger.hpp @@ -0,0 +1,78 @@ +/************************************ + * file enc : utf8 + * author : wuyanyi09@gmail.com + ************************************/ +#ifndef LIMONP_LOGGER_H +#define LIMONP_LOGGER_H + +#include +#include +#include +#include +#include +#include +#include "io_functs.hpp" +#include "str_functs.hpp" + +#define LogDebug(fmt, ...) Logger::LoggingF(LL_DEBUG, __FILE__, __LINE__, fmt, ## __VA_ARGS__) +#define LogInfo(fmt, ...) Logger::LoggingF(LL_INFO, __FILE__, __LINE__, fmt, ## __VA_ARGS__) +#define LogWarn(fmt, ...) Logger::LoggingF(LL_WARN, __FILE__, __LINE__, fmt, ## __VA_ARGS__) +#define LogError(fmt, ...) Logger::LoggingF(LL_ERROR, __FILE__, __LINE__, fmt, ## __VA_ARGS__) +#define LogFatal(fmt, ...) Logger::LoggingF(LL_FATAL, __FILE__, __LINE__, fmt, ## __VA_ARGS__) + + +namespace Limonp +{ + using namespace std; + enum {LL_DEBUG = 0, LL_INFO = 1, LL_WARN = 2, LL_ERROR = 3, LL_FATAL = 4, LEVEL_ARRAY_SIZE = 5, CSTR_BUFFER_SIZE = 1024}; + static const char * LOG_LEVEL_ARRAY[LEVEL_ARRAY_SIZE]= {"DEBUG","INFO","WARN","ERROR","FATAL"}; + static const char * LOG_FORMAT = "%s %s:%d %s %s\n"; + static const char * LOG_TIME_FORMAT = "%Y-%m-%d %H:%M:%S"; + + class Logger + { + public: + static bool Logging(uint level, const string& msg, const char* fileName, int lineNo) + { + if(level > LL_FATAL) + { + cerr<<"level's value is out of range"< -1 && n < size) { + msg.resize(n); + break; + } + if (n > -1) + size = n + 1; + else + size *= 2; + } + return Logging(level, msg, fileName, lineNo); + } + }; +} + +#endif diff --git a/src/Husky/Limonp/macro_def.hpp b/src/Husky/Limonp/macro_def.hpp new file mode 100644 index 0000000..58fe835 --- /dev/null +++ b/src/Husky/Limonp/macro_def.hpp @@ -0,0 +1,22 @@ +#ifndef LIMONP_MACRO_DEF_H +#define LIMONP_MACRO_DEF_H + +#define XX_GET_SET(varType, varName, funName)\ +private: varType varName;\ +public: inline varType get##funName(void) const {return varName;}\ +public: inline void set##funName(varType var) {varName = var;} + +#define XX_GET(varType, varName, funName)\ +private: varType varName;\ +public: inline varType get##funName(void) const {return varName;} + +#define XX_SET(varType, varName, funName)\ +private: varType varName;\ +public: inline void set##funName(varType var) {varName = var;} + +#define XX_GET_SET_BY_REF(varType, varName, funName)\ +private: varType varName;\ +public: inline const varType& get##funName(void) const {return varName;}\ +public: inline void set##funName(const varType& var){varName = var;} + +#endif diff --git a/src/Husky/Limonp/map_functs.hpp b/src/Husky/Limonp/map_functs.hpp new file mode 100644 index 0000000..44d472c --- /dev/null +++ b/src/Husky/Limonp/map_functs.hpp @@ -0,0 +1,116 @@ +/************************************ + * file enc : ascii + * author : wuyanyi09@gmail.com + ************************************/ + + +#ifndef LIMONP_MAP_FUNCTS_H +#define LIMONP_MAP_FUNCTS_H + +#include +#include +#include +#include + +#include +#define HashMap std::tr1::unordered_map + +namespace Limonp +{ + using namespace std; + + + //template + // string setToString(const set& st) + // { + // if(st.empty()) + // { + // return "{}"; + // } + // stringstream ss; + // ss<<'{'; + // typename set::const_iterator it = st.begin(); + // ss<<*it; + // it++; + // while(it != st.end()) + // { + // ss<<", "<<*it; + // it++; + // } + // ss<<'}'; + // return ss.str(); + // } + + //template + // string mapToString(const map& mp) + // { + // if(mp.empty()) + // { + // return "{}"; + // } + // stringstream ss; + // ss<<'{'; + // typename map::const_iterator it = mp.begin(); + // ss<first<<": "<second; + // it++; + // while(it != mp.end()) + // { + // ss<<", "<first<<": "<second; + // it++; + // } + // ss<<'}'; + // return ss.str(); + // } + + //template + // string HashMapToString(const HashMap& mp) + // { + // if(mp.empty()) + // { + // return "{}"; + // } + // stringstream ss; + // ss<<'{'; + // typename HashMap::const_iterator it = mp.begin(); + // ss<first<<": "<second; + // it++; + // while(it != mp.end()) + // { + // ss<<", "<first<<": "<second; + // it++; + // } + // ss<<'}'; + // return ss.str(); + // } + //template + // string pairToString(const pair& p) + // { + // stringstream ss; + // ss< + vT getMap(const map& mp, const kT & key, const vT & defaultVal) + { + typename map::const_iterator it; + it = mp.find(key); + if(mp.end() == it) + { + return defaultVal; + } + return it->second; + } + + template + void map2Vec(const map& mp, vector > & res) + { + typename map::const_iterator it = mp.begin(); + for(; it != mp.end(); it++) + { + res.push_back(*it); + } + } +} + +#endif diff --git a/src/Husky/Limonp/std_outbound.hpp b/src/Husky/Limonp/std_outbound.hpp new file mode 100644 index 0000000..ab3e5c3 --- /dev/null +++ b/src/Husky/Limonp/std_outbound.hpp @@ -0,0 +1,101 @@ +#ifndef LIMONP_STD_OUTBOUND_H +#define LIMONP_STD_OUTBOUND_H + +#include +#include +#include + +namespace std +{ + template + ostream& operator << (ostream& os, const vector& vec) + { + if(vec.empty()) + { + return os << "[]"; + } + os<<"[\""< + ostream& operator << (ostream& os, const pair& pr) + { + os << pr.first << ":" << pr.second ; + return os; + } + + + template + string& operator << (string& str, const T& obj) + { + stringstream ss; + ss << obj; // call ostream& operator << (ostream& os, + return str = ss.str(); + } + + template + ostream& operator << (ostream& os, const map& mp) + { + if(mp.empty()) + { + os<<"{}"; + return os; + } + os<<'{'; + typename map::const_iterator it = mp.begin(); + os<<*it; + it++; + while(it != mp.end()) + { + os<<", "<<*it; + it++; + } + os<<'}'; + return os; + } + template + ostream& operator << (ostream& os, const std::tr1::unordered_map& mp) + { + if(mp.empty()) + { + return os << "{}"; + } + os<<'{'; + typename std::tr1::unordered_map::const_iterator it = mp.begin(); + os<<*it; + it++; + while(it != mp.end()) + { + os<<", "<<*it++; + } + return os<<'}'; + } + + template + ostream& operator << (ostream& os, const set& st) + { + if(st.empty()) + { + os << "{}"; + return os; + } + os<<'{'; + typename set::const_iterator it = st.begin(); + os<<*it; + it++; + while(it != st.end()) + { + os<<", "<<*it; + it++; + } + os<<'}'; + return os; + } +} + +#endif diff --git a/src/Husky/Limonp/str_functs.hpp b/src/Husky/Limonp/str_functs.hpp new file mode 100644 index 0000000..8be5366 --- /dev/null +++ b/src/Husky/Limonp/str_functs.hpp @@ -0,0 +1,257 @@ +/************************************ + * file enc : ascii + * author : wuyanyi09@gmail.com + ************************************/ +#ifndef LIMONP_STR_FUNCTS_H +#define LIMONP_STR_FUNCTS_H +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "std_outbound.hpp" +#include "map_functs.hpp" + +#define print(x) cout<<(x)< -1 && n < size) { + str.resize(n); + return str; + } + if (n > -1) + size = n + 1; + else + size *= 2; + } + return str; + } + + inline void string_format(string& res, const char* fmt, ...) + { + int size = 256; + va_list ap; + while (1) { + res.resize(size); + va_start(ap, fmt); + int n = vsnprintf((char *)res.c_str(), size, fmt, ap); + va_end(ap); + if (n > -1 && n < size) { + res.resize(n); + return; + } + if (n > -1) + size = n + 1; + else + size *= 2; + } + } + + //inline bool joinStr(const vector& src, string& dest, const string& connectorStr) + //{ + // if(src.empty()) + // { + // return false; + // } + // for(uint i = 0; i < src.size() - 1; i++) + // { + // dest += src[i]; + // dest += connectorStr; + // } + // dest += src[src.size() - 1]; + // return true; + //} + + //inline string joinStr(const vector& source, const string& connector) + //{ + // string res; + // joinStr(source, res, connector); + // return res; + //} + + template + void join(T begin, T end, string& res, const string& connector) + { + if(begin == end) + { + return; + } + stringstream ss; + ss<<*begin; + begin++; + while(begin != end) + { + ss << connector << *begin; + begin ++; + } + res = ss.str(); + } + + template + string join(T begin, T end, const string& connector) + { + string res; + join(begin ,end, res, connector); + return res; + } + + + + inline bool splitStr(const string& src, vector& res, const string& pattern) + { + if(src.empty()) + { + return false; + } + res.clear(); + + size_t start = 0; + size_t end = 0; + while(start < src.size()) + { + end = src.find_first_of(pattern, start); + if(string::npos == end) + { + res.push_back(src.substr(start)); + return true; + } + res.push_back(src.substr(start, end - start)); + if(end == src.size() - 1) + { + res.push_back(""); + break; + } + start = end + 1; + } + return true; + } + + inline string& upper(string& str) + { + transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper); + return str; + } + + inline string& lower(string& str) + { + transform(str.begin(), str.end(), str.begin(), (int (*)(int))tolower); + return str; + } + + inline std::string <rim(std::string &s) + { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(std::isspace)))); + return s; + } + + inline std::string &rtrim(std::string &s) + { + s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun(std::isspace))).base(), s.end()); + return s; + } + + inline std::string &trim(std::string &s) + { + return ltrim(rtrim(s)); + } + + + inline uint16_t twocharToUint16(char high, char low) + { + return (((uint16_t(high) & 0x00ff ) << 8) | (uint16_t(low) & 0x00ff)); + } + + inline pair uint16ToChar2(uint16_t in) + { + pair res; + res.first = (in>>8) & 0x00ff; //high + res.second = (in) & 0x00ff; //low + return res; + } + + inline bool strStartsWith(const string& str, const string& prefix) + { + //return str.substr(0, prefix.size()) == prefix; + if(prefix.length() > str.length()) + { + return false; + } + return 0 == str.compare(0, prefix.length(), prefix); + } + + inline bool strEndsWith(const string& str, const string& suffix) + { + if(suffix.length() > str.length()) + { + return false; + } + return 0 == str.compare(str.length() - suffix.length(), suffix.length(), suffix); + } + + inline bool isInStr(const string& str, char ch) + { + return str.find(ch) != string::npos; + } + + //inline void extractWords(const string& sentence, vector& words) + //{ + // bool flag = false; + // uint lhs = 0, len = 0; + // for(uint i = 0; i < sentence.size(); i++) + // { + // char x = sentence[i]; + // if((0x0030 <= x && x<= 0x0039) || (0x0041 <= x && x <= 0x005a ) || (0x0061 <= x && x <= 0x007a)) + // { + // if(flag) + // { + // len ++; + // } + // else + // { + // lhs = i; + // len = 1; + // } + // flag = true; + // } + // else + // { + // if(flag) + // { + // words.push_back(string(sentence, lhs, len)); + // } + // flag = false; + // } + // } + // if(flag) + // { + // words.push_back(string(sentence, lhs, len)); + // } + //} + + +} +#endif diff --git a/src/Husky/Limonp/typedefs.h b/src/Husky/Limonp/typedefs.h new file mode 100644 index 0000000..a8da002 --- /dev/null +++ b/src/Husky/Limonp/typedefs.h @@ -0,0 +1,21 @@ +/************************************ + * file enc : utf8 + * author : wuyanyi09@gmail.com +************************************/ +#ifndef LIMONP_TYPEDEFS_H +#define LIMONP_TYPEDEFS_H + +#include +#include +#include +#include +#define HashMap std::tr1::unordered_map + +namespace Limonp +{ + typedef std::vector Unicode; + typedef std::vector::const_iterator UnicodeConstIterator; +} + + +#endif diff --git a/src/Husky/Limonp/vec_functs.hpp b/src/Husky/Limonp/vec_functs.hpp new file mode 100644 index 0000000..ac18548 --- /dev/null +++ b/src/Husky/Limonp/vec_functs.hpp @@ -0,0 +1,142 @@ +/************************************ + * file enc : ascii + * author : wuyanyi09@gmail.com +************************************/ +#ifndef LIMONP_VEC_FUNCTS_H +#define LIMONP_VEC_FUNCTS_H +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define FOR_VECTOR(vec, i) for(size_t i = 0; i < vec.size(); i++) + +#define PRINT_VECTOR(vec) FOR_VECTOR(vec, i)\ +{\ + cout< + bool vecToString(const vector& vec, string& res) + { + if(vec.empty()) + { + res = "[]"; + return false; + } + stringstream ss; + ss<<"[\""< + string vecToString(const vector& vec) + { + string res; + vecToString(vec, res); + return res; + } + + template + bool isInVec(const vector& vec, const T& item) + { + typename vector::const_iterator it = find(vec.begin(), vec.end(), item); + return it != vec.end(); + } + template + void splitVec(const vector& vecSrc, vector< pair > >& outVec, const vector& patterns) + { + vector tmp; + T pattern; + size_t patternSize = patterns.size(); + for(size_t i = 0; i < vecSrc.size(); i++) + { + size_t patternPos = patternSize; + for(size_t j = 0; j < patternSize; j++) + { + if(patterns[j] == vecSrc[i]) + { + patternPos = j; + break; + } + } + if(patternPos != patternSize) + { + if(!tmp.empty()) + { + outVec.push_back(make_pair >(pattern, tmp)); + tmp.clear(); + } + pattern = patterns[patternPos]; + } + else + { + tmp.push_back(vecSrc[i]); + } + } + if(!tmp.empty()) + { + outVec.push_back(make_pair >(pattern, tmp)); + } + } + + template + void splitVec(const vector& vecSrc, vector< vector >& outVec, const vector& patternVec) + { + vector tmp; + for(size_t i = 0; i < vecSrc.size(); i++) + { + bool flag = false; + for(size_t j = 0; j < patternVec.size(); j++) + { + if(patternVec[j] == vecSrc[i]) + { + flag = true; + break; + } + } + if(flag) + { + if(!tmp.empty()) + { + outVec.push_back(tmp); + tmp.clear(); + } + } + else + { + tmp.push_back(vecSrc[i]); + } + } + if(!tmp.empty()) + { + outVec.push_back(tmp); + } + } +} + +#endif diff --git a/src/Husky/ServerFrame.cpp b/src/Husky/ServerFrame.cpp index 5d83eb8..aa3eab9 100644 --- a/src/Husky/ServerFrame.cpp +++ b/src/Husky/ServerFrame.cpp @@ -134,8 +134,8 @@ namespace Husky nRetCode = recv(hClientSock, chRecvBuf, RECV_BUFFER, 0); strRec = chRecvBuf; -#ifdef DEBUG - LogDebug("response[%s]", strRec.c_str()); +#ifdef HUKSY_DEBUG + LogDebug("request[%s]", strRec.c_str()); #endif if(SOCKET_ERROR==nRetCode) @@ -160,15 +160,15 @@ namespace Husky strHttpResp=chHttpHeader; strHttpResp+=strSnd; +#ifdef HUKSY_DEBUG + LogDebug("response'body [%s]", strSnd.c_str()); +#endif if (SOCKET_ERROR==send(hClientSock,strHttpResp.c_str(),strHttpResp.length(),0)) { LogError("error [%s]", strerror(errno)); } -#ifdef DEBUG - LogDebug("send response [%s] ", strHttpResp.c_str()); -#endif closesocket(hClientSock); }