add Husky

This commit is contained in:
wyy 2013-11-03 22:02:43 -08:00
parent 44c7d4dcb3
commit 01b225dec8
17 changed files with 1239 additions and 27 deletions

View File

@ -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)

View File

@ -8,7 +8,7 @@
#include <sys/wait.h>
#include <sys/stat.h>
#include <signal.h>
#include <logger.hpp>
#include "Limonp/logger.hpp"
#include "ServerFrame.h"
namespace Husky

View File

@ -3,14 +3,14 @@
#include <iostream>
#include <string>
#include "Limonp/logger.hpp"
#include "Limonp/str_functs.hpp"
#include "globals.h"
#include <str_functs.hpp>
#include <logger.hpp>
#include <map_functs.hpp>
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<string, string> _headerMap;
HashMap<string, string> _methodGetMap;
HashMap<string, string> _methodPostMap;
//public:
friend ostream& operator<<(ostream& os, const HttpReqInfo& obj);
private:
bool _find(const HashMap<string, string>& 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<string, string>& 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

View File

@ -0,0 +1,90 @@
/************************************
* file enc : ascii
* author : wuyanyi09@gmail.com
************************************/
#ifndef LIMONP_ARGV_FUNCTS_H
#define LIMONP_ARGV_FUNCTS_H
#include <set>
#include <sstream>
#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<string, string>::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<string> _args;
map<string, string> _mpss;
set<string> _sset;
};
inline ostream& operator << (ostream& os, const ArgvContext& args)
{
return os<<args._args<<args._mpss<<args._sset;
}
//string toString()
//{
// stringstream ss;
// return ss.str();
//}
}
#endif

View File

@ -0,0 +1,2 @@
FILE(GLOB HEAD_HPP_LIST "*.hpp")
INSTALL(FILES ${HEAD_HPP_LIST} DESTINATION include/CppJieba/Husky/Limonp)

View File

@ -0,0 +1,114 @@
#ifndef LIMONP_MYSQLCLIENT_H
#define LIMONP_MYSQLCLIENT_H
#include <mysql/mysql.h>
#include <iostream>
#include <vector>
#include <string>
#include "logger.hpp"
namespace Limonp
{
using namespace std;
class MysqlClient
{
public:
typedef vector< vector<string> > 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<<mysql_get_client_info()<<endl;
if(NULL == (_conn = mysql_init(NULL)))
{
LogError("mysql_init faield. %s", mysql_error(_conn));
return false;
}
if (mysql_real_connect(_conn, HOST, USER, PASSWD, DB, PORT, NULL, 0) == NULL)
{
LogError("mysql_real_connect failed. %s", mysql_error(_conn));
mysql_close(_conn);
_conn = NULL;
return false;
}
if(mysql_set_character_set(_conn, CHARSET))
{
LogError("mysql_set_character_set [%s] failed.", CHARSET);
return false;
}
//set reconenct
char value = 1;
mysql_options(_conn, MYSQL_OPT_RECONNECT, &value);
LogInfo("MysqlClient {host: %s, port:%d, database:%s, charset:%s}", HOST, PORT, DB, CHARSET);
return true;
}
bool dispose()
{
if(NULL != _conn)
{
mysql_close(_conn);
}
_conn = NULL;
return true;
}
bool executeSql(const char* sql)
{
if(NULL == _conn)
{
LogError("_conn is NULL");
return false;
}
if(mysql_query(_conn, sql))
{
LogError("mysql_query failed. %s", mysql_error(_conn));
return false;
}
return true;
}
bool select(const char* sql, RowsType& rows)
{
if(!executeSql(sql))
{
LogError("executeSql failed. [%s]", sql);
return false;
}
MYSQL_RES * result = mysql_store_result(_conn);
if(NULL == result)
{
LogError("mysql_store_result failed.[%d]", mysql_error(_conn));
}
uint num_fields = mysql_num_fields(result);
MYSQL_ROW row;
while((row = mysql_fetch_row(result)))
{
vector<string> 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

View File

@ -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)
{
/*
* 31001 0(13)
*/
int sign = ((int) (s & sign_16)) << 16;
int exponent = ((int) (s & exponent_16)) << 13;
// 指数符号位为0234位补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

107
src/Husky/Limonp/config.hpp Normal file
View File

@ -0,0 +1,107 @@
/************************************
* file enc : utf8
* author : wuyanyi09@gmail.com
************************************/
#ifndef LIMONP_CONFIG_H
#define LIMONP_CONFIG_H
#include <map>
#include <fstream>
#include <iostream>
#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<string> 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<string, string>::iterator it = _map.begin(); it != _map.end(); it++)
{
cout<<"("<<it->first<<","<<it->second<<")"<<endl;
}
}
bool get(const string& key, string& value)
{
if(_map.end() != _map.find(key))
{
value = _map[key];
return true;
}
return false;
}
private:
//bool _stripComment(const string& line, string& res)
//{
// res = line;
// string::size_type pos = res.find('#');
// if(string::npos != pos)
// {
// res = res.substr(0, pos);
// }
// trim(res);
//}
private:
map<string, string> _map;
bool _isInit;
};
}
namespace Limonp
{
extern Config gConfig;
}
#endif

View File

@ -0,0 +1,82 @@
/************************************
* file enc : utf8
* author : wuyanyi09@gmail.com
************************************/
#ifndef LIMONP_IO_FUNCTS_H
#define LIMONP_IO_FUNCTS_H
#include <fstream>
#include <iostream>
#include <stdlib.h>
namespace Limonp
{
using namespace std;
inline string loadFile2Str(const char * const filepath)
{
ifstream in(filepath);
if(!in)
{
return "";
}
istreambuf_iterator<char> 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<char> 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

View File

@ -0,0 +1,78 @@
/************************************
* file enc : utf8
* author : wuyanyi09@gmail.com
************************************/
#ifndef LIMONP_LOGGER_H
#define LIMONP_LOGGER_H
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdarg.h>
#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"<<endl;
return false;
}
char buf[CSTR_BUFFER_SIZE];
time_t timeNow;
time(&timeNow);
size_t ret = strftime(buf, sizeof(buf), LOG_TIME_FORMAT, localtime(&timeNow));
if(0 == ret)
{
fprintf(stderr, "stftime failed.\n");
return false;
}
fprintf(stderr, LOG_FORMAT, buf, fileName, lineNo,LOG_LEVEL_ARRAY[level], msg.c_str());
return true;
}
static bool LoggingF(uint level, const char* fileName, int lineNo, const string& fmt, ...)
{
int size = 256;
string msg;
va_list ap;
while (1) {
msg.resize(size);
va_start(ap, fmt);
int n = vsnprintf((char *)msg.c_str(), size, fmt.c_str(), ap);
va_end(ap);
if (n > -1 && n < size) {
msg.resize(n);
break;
}
if (n > -1)
size = n + 1;
else
size *= 2;
}
return Logging(level, msg, fileName, lineNo);
}
};
}
#endif

View File

@ -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

View File

@ -0,0 +1,116 @@
/************************************
* file enc : ascii
* author : wuyanyi09@gmail.com
************************************/
#ifndef LIMONP_MAP_FUNCTS_H
#define LIMONP_MAP_FUNCTS_H
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <tr1/unordered_map>
#define HashMap std::tr1::unordered_map
namespace Limonp
{
using namespace std;
//template <typename T>
// string setToString(const set<T>& st)
// {
// if(st.empty())
// {
// return "{}";
// }
// stringstream ss;
// ss<<'{';
// typename set<T>::const_iterator it = st.begin();
// ss<<*it;
// it++;
// while(it != st.end())
// {
// ss<<", "<<*it;
// it++;
// }
// ss<<'}';
// return ss.str();
// }
//template<typename T1, typename T2>
// string mapToString(const map<T1, T2>& mp)
// {
// if(mp.empty())
// {
// return "{}";
// }
// stringstream ss;
// ss<<'{';
// typename map<T1, T2>::const_iterator it = mp.begin();
// ss<<it->first<<": "<<it->second;
// it++;
// while(it != mp.end())
// {
// ss<<", "<<it->first<<": "<<it->second;
// it++;
// }
// ss<<'}';
// return ss.str();
// }
//template<typename T1, typename T2>
// string HashMapToString(const HashMap<T1, T2>& mp)
// {
// if(mp.empty())
// {
// return "{}";
// }
// stringstream ss;
// ss<<'{';
// typename HashMap<T1, T2>::const_iterator it = mp.begin();
// ss<<it->first<<": "<<it->second;
// it++;
// while(it != mp.end())
// {
// ss<<", "<<it->first<<": "<<it->second;
// it++;
// }
// ss<<'}';
// return ss.str();
// }
//template<typename T1, typename T2>
// string pairToString(const pair<T1, T2>& p)
// {
// stringstream ss;
// ss<<p.first<<":"<<p.second;
// return ss.str();
// }
template<class kT, class vT>
vT getMap(const map<kT, vT>& mp, const kT & key, const vT & defaultVal)
{
typename map<kT, vT>::const_iterator it;
it = mp.find(key);
if(mp.end() == it)
{
return defaultVal;
}
return it->second;
}
template<class kT, class vT>
void map2Vec(const map<kT, vT>& mp, vector<pair<kT, vT> > & res)
{
typename map<kT, vT>::const_iterator it = mp.begin();
for(; it != mp.end(); it++)
{
res.push_back(*it);
}
}
}
#endif

View File

@ -0,0 +1,101 @@
#ifndef LIMONP_STD_OUTBOUND_H
#define LIMONP_STD_OUTBOUND_H
#include <tr1/unordered_map>
#include <map>
#include <set>
namespace std
{
template<typename T>
ostream& operator << (ostream& os, const vector<T>& vec)
{
if(vec.empty())
{
return os << "[]";
}
os<<"[\""<<vec[0];
for(uint i = 1; i < vec.size(); i++)
{
os<<"\", \""<<vec[i];
}
os<<"\"]";
return os;
}
template<class T1, class T2>
ostream& operator << (ostream& os, const pair<T1, T2>& pr)
{
os << pr.first << ":" << pr.second ;
return os;
}
template<class T>
string& operator << (string& str, const T& obj)
{
stringstream ss;
ss << obj; // call ostream& operator << (ostream& os,
return str = ss.str();
}
template<class T1, class T2>
ostream& operator << (ostream& os, const map<T1, T2>& mp)
{
if(mp.empty())
{
os<<"{}";
return os;
}
os<<'{';
typename map<T1, T2>::const_iterator it = mp.begin();
os<<*it;
it++;
while(it != mp.end())
{
os<<", "<<*it;
it++;
}
os<<'}';
return os;
}
template<class T1, class T2>
ostream& operator << (ostream& os, const std::tr1::unordered_map<T1, T2>& mp)
{
if(mp.empty())
{
return os << "{}";
}
os<<'{';
typename std::tr1::unordered_map<T1, T2>::const_iterator it = mp.begin();
os<<*it;
it++;
while(it != mp.end())
{
os<<", "<<*it++;
}
return os<<'}';
}
template<class T>
ostream& operator << (ostream& os, const set<T>& st)
{
if(st.empty())
{
os << "{}";
return os;
}
os<<'{';
typename set<T>::const_iterator it = st.begin();
os<<*it;
it++;
while(it != st.end())
{
os<<", "<<*it;
it++;
}
os<<'}';
return os;
}
}
#endif

View File

@ -0,0 +1,257 @@
/************************************
* file enc : ascii
* author : wuyanyi09@gmail.com
************************************/
#ifndef LIMONP_STR_FUNCTS_H
#define LIMONP_STR_FUNCTS_H
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
#include <map>
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
#include <memory.h>
#include <functional>
#include <locale>
#include <sstream>
#include <sys/types.h>
#include <iterator>
#include <algorithm>
#include "std_outbound.hpp"
#include "map_functs.hpp"
#define print(x) cout<<(x)<<endl
namespace Limonp
{
using namespace std;
inline string string_format(const char* fmt, ...)
{
int size = 256;
std::string str;
va_list ap;
while (1) {
str.resize(size);
va_start(ap, fmt);
int n = vsnprintf((char *)str.c_str(), size, fmt, ap);
va_end(ap);
if (n > -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<string>& 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<string>& source, const string& connector)
//{
// string res;
// joinStr(source, res, connector);
// return res;
//}
template<class T>
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<class T>
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<string>& 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 &ltrim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(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<int, int>(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<char, char> uint16ToChar2(uint16_t in)
{
pair<char, char> 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<string>& 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

View File

@ -0,0 +1,21 @@
/************************************
* file enc : utf8
* author : wuyanyi09@gmail.com
************************************/
#ifndef LIMONP_TYPEDEFS_H
#define LIMONP_TYPEDEFS_H
#include <stdint.h>
#include <vector>
#include <sys/types.h>
#include <tr1/unordered_map>
#define HashMap std::tr1::unordered_map
namespace Limonp
{
typedef std::vector<uint16_t> Unicode;
typedef std::vector<uint16_t>::const_iterator UnicodeConstIterator;
}
#endif

View File

@ -0,0 +1,142 @@
/************************************
* file enc : ascii
* author : wuyanyi09@gmail.com
************************************/
#ifndef LIMONP_VEC_FUNCTS_H
#define LIMONP_VEC_FUNCTS_H
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <string>
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <sstream>
#define FOR_VECTOR(vec, i) for(size_t i = 0; i < vec.size(); i++)
#define PRINT_VECTOR(vec) FOR_VECTOR(vec, i)\
{\
cout<<vec[i]<<endl;\
}
#define PRINT_MATRIX(mat) FOR_VECTOR(mat, i) \
{\
FOR_VECTOR(mat[i], j)\
{\
cout<<"["<<i<<","<<j<<"]:"<<mat[i][j]<<endl;\
}\
}
namespace Limonp
{
using namespace std;
template <typename T>
bool vecToString(const vector<T>& vec, string& res)
{
if(vec.empty())
{
res = "[]";
return false;
}
stringstream ss;
ss<<"[\""<<vec[0];
for(uint i = 1; i < vec.size(); i++)
{
ss<<"\", \""<<vec[i];
}
ss<<"\"]";
res = ss.str();
return true;
}
template <typename T>
string vecToString(const vector<T>& vec)
{
string res;
vecToString(vec, res);
return res;
}
template<typename T>
bool isInVec(const vector<T>& vec, const T& item)
{
typename vector<T>::const_iterator it = find(vec.begin(), vec.end(), item);
return it != vec.end();
}
template<typename T>
void splitVec(const vector<T>& vecSrc, vector< pair<T, vector<T> > >& outVec, const vector<T>& patterns)
{
vector<T> 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<T, vector<T> >(pattern, tmp));
tmp.clear();
}
pattern = patterns[patternPos];
}
else
{
tmp.push_back(vecSrc[i]);
}
}
if(!tmp.empty())
{
outVec.push_back(make_pair<T, vector<T> >(pattern, tmp));
}
}
template<typename T>
void splitVec(const vector<T>& vecSrc, vector< vector<T> >& outVec, const vector<T>& patternVec)
{
vector<T> 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

View File

@ -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);
}