namespace husky; namespace limonp;

This commit is contained in:
yanyiwu 2015-08-08 12:30:14 +08:00
parent 8a3ced2b27
commit efd029c20b
38 changed files with 179 additions and 152 deletions

View File

@ -1,16 +0,0 @@
#ifndef HUSKY_IREQUESTHANDLER_HPP
#define HUSKY_IREQUESTHANDLER_HPP
#include "HttpReqInfo.hpp"
namespace Husky {
class IRequestHandler {
public:
virtual ~IRequestHandler() {};
public:
virtual bool do_GET(const HttpReqInfo& httpReq, string& res) const = 0;
virtual bool do_POST(const HttpReqInfo& httpReq, string& res) const = 0;
};
}
#endif

View File

@ -3,11 +3,11 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include "Limonp/Logger.hpp" #include "limonp/Logger.hpp"
#include "Limonp/StringUtil.hpp" #include "limonp/StringUtil.hpp"
namespace Husky { namespace husky {
using namespace Limonp; using namespace limonp;
using namespace std; using namespace std;
static const char* const KEY_METHOD = "METHOD"; static const char* const KEY_METHOD = "METHOD";
@ -62,7 +62,7 @@ class HttpReqInfo {
_isBodyFinished = false; _isBodyFinished = false;
_contentLength = 0; _contentLength = 0;
} }
public:
bool parseHeader(const string& buffer) { bool parseHeader(const string& buffer) {
return parseHeader(buffer.c_str(), buffer.size()); return parseHeader(buffer.c_str(), buffer.size());
} }
@ -77,7 +77,8 @@ class HttpReqInfo {
} }
string firstline(headerStr, lpos, rpos - lpos); string firstline(headerStr, lpos, rpos - lpos);
trim(firstline); trim(firstline);
if(!split(firstline, buf, " ") || 3 != buf.size()) { split(firstline, buf, " ");
if (3 != buf.size()) {
LogError("parse header firstline[%s] failed.", firstline.c_str()); LogError("parse header firstline[%s] failed.", firstline.c_str());
return false; return false;
} }
@ -141,7 +142,7 @@ class HttpReqInfo {
bool isBodyFinished() const { bool isBodyFinished() const {
return _isBodyFinished; return _isBodyFinished;
} }
public:
const string& set(const string& key, const string& value) { const string& set(const string& key, const string& value) {
return _headerMap[key] = value; return _headerMap[key] = value;
} }
@ -149,8 +150,30 @@ class HttpReqInfo {
return _find(_headerMap, key, res); return _find(_headerMap, key, res);
} }
bool GET(const string& argKey, string& res)const { bool GET(const string& argKey, string& res)const {
return _find(_methodGetMap, argKey, res); string tmp;
if (!_find(_methodGetMap, argKey, tmp)) {
return false;
}
URLDecode(tmp, res);
return true;
} }
bool GET(const string& argKey, int& res) const {
string tmp;
if (!GET(argKey, tmp)) {
return false;
}
res = atoi(tmp.c_str());
return true;
}
bool GET(const string& argKey, size_t& res) const {
int tmp = 0;
if (!GET(argKey, tmp) || tmp < 0) {
return false;
}
res = tmp;
return true;
}
//const string& getMethod() const //const string& getMethod() const
//{ //{
// return _headerMap.find(KEY_METHOD)->second; // return _headerMap.find(KEY_METHOD)->second;
@ -190,7 +213,7 @@ class HttpReqInfo {
string _path; string _path;
string _body; string _body;
friend ostream& operator<<(ostream& os, const HttpReqInfo& obj); friend ostream& operator<<(ostream& os, const HttpReqInfo& obj);
private:
bool _find(const std::unordered_map<string, string>& mp, const string& key, string& res)const { bool _find(const std::unordered_map<string, string>& mp, const string& key, string& res)const {
std::unordered_map<string, string>::const_iterator it = mp.find(key); std::unordered_map<string, string>::const_iterator it = mp.find(key);
if(it == mp.end()) { if(it == mp.end()) {
@ -199,7 +222,7 @@ class HttpReqInfo {
res = it->second; res = it->second;
return true; return true;
} }
private:
void _parseUri(const string& uri, string& path, std::unordered_map<string, string>& mp) { void _parseUri(const string& uri, string& path, std::unordered_map<string, string>& mp) {
if(uri.empty()) { if(uri.empty()) {
return; return;
@ -235,7 +258,7 @@ class HttpReqInfo {
} }
}; };
inline std::ostream& operator << (std::ostream& os, const Husky::HttpReqInfo& obj) { inline std::ostream& operator << (std::ostream& os, const husky::HttpReqInfo& obj) {
return os << obj._headerMap << obj._methodGetMap/* << obj._methodPostMap*/ << obj._path << obj._body ; return os << obj._headerMap << obj._methodGetMap/* << obj._methodPostMap*/ << obj._path << obj._body ;
} }

View File

@ -0,0 +1,17 @@
#ifndef HUSKY_IREQUESTHANDLER_HPP
#define HUSKY_IREQUESTHANDLER_HPP
#include "HttpReqInfo.hpp"
namespace husky {
class IRequestHandler {
public:
virtual ~IRequestHandler() {
}
virtual bool doGET(const HttpReqInfo& httpReq, string& res) = 0;
virtual bool doPOST(const HttpReqInfo& httpReq, string& res) = 0;
};
}
#endif

View File

@ -15,14 +15,14 @@
#include <unistd.h> #include <unistd.h>
#include <vector> #include <vector>
#include "Limonp/StdExtension.hpp" #include "limonp/StdExtension.hpp"
#include "Limonp/Logger.hpp" #include "limonp/Logger.hpp"
namespace Husky { namespace husky {
static const size_t LISTEN_QUEUE_LEN = 1024; static const size_t LISTEN_QUEUE_LEN = 1024;
typedef int SocketFd; typedef int SocketFd;
SocketFd CreateAndListenSocket(int port) { inline SocketFd CreateAndListenSocket(int port) {
SocketFd sock; SocketFd sock;
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) { if (sock == -1) {
@ -50,7 +50,7 @@ SocketFd CreateAndListenSocket(int port) {
const char* const HTTP_FORMAT = "HTTP/1.1 200 OK\r\nConnection: close\r\nServer: HuskyServer/1.0.0\r\nContent-Type: text/json; charset=%s\r\nContent-Length: %d\r\n\r\n%s"; const char* const HTTP_FORMAT = "HTTP/1.1 200 OK\r\nConnection: close\r\nServer: HuskyServer/1.0.0\r\nContent-Type: text/json; charset=%s\r\nContent-Length: %d\r\n\r\n%s";
const char* const CHARSET_UTF8 = "UTF-8"; const char* const CHARSET_UTF8 = "UTF-8";
} // namespace Husky } // namespace husky
#endif #endif

View File

@ -4,21 +4,17 @@
#include "NetUtils.hpp" #include "NetUtils.hpp"
#include "WorkerThread.hpp" #include "WorkerThread.hpp"
namespace Husky { namespace husky {
using namespace Limonp; using namespace limonp;
class ThreadPoolServer { class ThreadPoolServer {
private:
ThreadPool _pool;
const IRequestHandler & _reqHandler;
int _host_socket;
public: public:
ThreadPoolServer(size_t thread_number, size_t queue_max_size, size_t port, const IRequestHandler & handler): ThreadPoolServer(size_t thread_number, size_t queue_max_size, size_t port, IRequestHandler & handler):
_pool(thread_number, queue_max_size), _reqHandler(handler), _host_socket(-1) { _pool(thread_number, queue_max_size), _reqHandler(handler), _host_socket(-1) {
_host_socket = CreateAndListenSocket(port); _host_socket = CreateAndListenSocket(port);
} }
~ThreadPoolServer() {}; ~ThreadPoolServer() {};
public:
bool start() { bool start() {
_pool.start(); _pool.start();
sockaddr_in clientaddr; sockaddr_in clientaddr;
@ -30,10 +26,16 @@ class ThreadPoolServer {
LogError(strerror(errno)); LogError(strerror(errno));
break; break;
} }
_pool.add(CreateTask<WorkerThread,int, const IRequestHandler&>(clientSock, _reqHandler)); _pool.add(CreateTask<WorkerThread,int, IRequestHandler&>(clientSock, _reqHandler));
} }
return true; return true;
} }
};
} private:
ThreadPool _pool;
IRequestHandler & _reqHandler;
int _host_socket;
}; // class ThreadPoolServer
} // namespace husky
#endif #endif

View File

@ -1,11 +1,11 @@
#ifndef HUSKY_WORKER_HPP #ifndef HUSKY_WORKER_HPP
#define HUSKY_WORKER_HPP #define HUSKY_WORKER_HPP
#include "Limonp/ThreadPool.hpp" #include "limonp/ThreadPool.hpp"
#include "IRequestHandler.hpp" #include "IRequestHandler.hpp"
#include "NetUtils.hpp" #include "NetUtils.hpp"
namespace Husky { namespace husky {
const char* const CLIENT_IP_K = "CLIENT_IP"; const char* const CLIENT_IP_K = "CLIENT_IP";
const size_t RECV_BUFFER_SIZE = 16 * 1024; const size_t RECV_BUFFER_SIZE = 16 * 1024;
@ -14,14 +14,11 @@ const struct timeval SOCKET_TIMEOUT = {16, 0};
class WorkerThread: public ITask { class WorkerThread: public ITask {
public: public:
WorkerThread(int sockfs, const IRequestHandler& reqHandler): WorkerThread(int sockfs, IRequestHandler& reqHandler):
_sockfd(sockfs), _reqHandler(reqHandler) { _sockfd(sockfs), _reqHandler(reqHandler) {
} }
virtual ~WorkerThread() { virtual ~WorkerThread() {
} }
private:
int _sockfd;
const IRequestHandler& _reqHandler;
public: public:
void run() { void run() {
@ -37,12 +34,12 @@ class WorkerThread: public ITask {
break; break;
} }
if(httpReq.isGET() && !_reqHandler.do_GET(httpReq, strRetByHandler)) { if(httpReq.isGET() && !_reqHandler.doGET(httpReq, strRetByHandler)) {
LogError("do_GET failed."); LogError("doGET failed.");
break; break;
} }
if(httpReq.isPOST() && !_reqHandler.do_POST(httpReq, strRetByHandler)) { if(httpReq.isPOST() && !_reqHandler.doPOST(httpReq, strRetByHandler)) {
LogError("do_POST failed."); LogError("doPOST failed.");
break; break;
} }
strSnd = string_format(HTTP_FORMAT, CHARSET_UTF8, strRetByHandler.length(), strRetByHandler.c_str()); strSnd = string_format(HTTP_FORMAT, CHARSET_UTF8, strRetByHandler.length(), strRetByHandler.c_str());
@ -101,7 +98,9 @@ class WorkerThread: public ITask {
} }
return true; return true;
} }
private:
int _sockfd;
IRequestHandler& _reqHandler;
}; };
} }

View File

@ -3,11 +3,11 @@
#include <string> #include <string>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include "Limonp/Config.hpp" #include "limonp/Config.hpp"
#include "Husky/ThreadPoolServer.hpp" #include "husky/ThreadPoolServer.hpp"
#include "Application.hpp" #include "Application.hpp"
using namespace Husky; using namespace husky;
using namespace CppJieba; using namespace CppJieba;
class ReqHandler: public IRequestHandler { class ReqHandler: public IRequestHandler {
@ -17,7 +17,7 @@ class ReqHandler: public IRequestHandler {
virtual ~ReqHandler() { virtual ~ReqHandler() {
} }
virtual bool do_GET(const HttpReqInfo& httpReq, string& strSnd) const { virtual bool doGET(const HttpReqInfo& httpReq, string& strSnd) {
string sentence, method, format; string sentence, method, format;
string tmp; string tmp;
vector<string> words; vector<string> words;
@ -30,7 +30,7 @@ class ReqHandler: public IRequestHandler {
return true; return true;
} }
virtual bool do_POST(const HttpReqInfo& httpReq, string& strSnd) const { virtual bool doPOST(const HttpReqInfo& httpReq, string& strSnd) {
vector<string> words; vector<string> words;
run(httpReq.getBody(), "MIX", "simple", strSnd); run(httpReq.getBody(), "MIX", "simple", strSnd);
return true; return true;

View File

@ -8,13 +8,13 @@
#include <stdint.h> #include <stdint.h>
#include <cmath> #include <cmath>
#include <limits> #include <limits>
#include "Limonp/StringUtil.hpp" #include "limonp/StringUtil.hpp"
#include "Limonp/Logger.hpp" #include "limonp/Logger.hpp"
#include "TransCode.hpp" #include "TransCode.hpp"
#include "Trie.hpp" #include "Trie.hpp"
namespace CppJieba { namespace CppJieba {
using namespace Limonp; using namespace limonp;
const double MIN_DOUBLE = -3.14e+100; const double MIN_DOUBLE = -3.14e+100;
const double MAX_DOUBLE = 3.14e+100; const double MAX_DOUBLE = 3.14e+100;
const size_t DICT_COLUMN_NUM = 3; const size_t DICT_COLUMN_NUM = 3;

View File

@ -4,7 +4,7 @@
#include <algorithm> #include <algorithm>
#include <set> #include <set>
#include <cassert> #include <cassert>
#include "Limonp/Logger.hpp" #include "limonp/Logger.hpp"
#include "DictTrie.hpp" #include "DictTrie.hpp"
#include "ISegment.hpp" #include "ISegment.hpp"
#include "SegmentBase.hpp" #include "SegmentBase.hpp"

View File

@ -1,11 +1,11 @@
#ifndef CPPJIEBA_HMMMODEL_H #ifndef CPPJIEBA_HMMMODEL_H
#define CPPJIEBA_HMMMODEL_H #define CPPJIEBA_HMMMODEL_H
#include "Limonp/StringUtil.hpp" #include "limonp/StringUtil.hpp"
namespace CppJieba { namespace CppJieba {
using namespace Limonp; using namespace limonp;
typedef unordered_map<uint16_t, double> EmitProbMap; typedef unordered_map<uint16_t, double> EmitProbMap;
struct HMMModel { struct HMMModel {

View File

@ -6,7 +6,7 @@
#include <set> #include <set>
namespace CppJieba { namespace CppJieba {
using namespace Limonp; using namespace limonp;
/*utf8*/ /*utf8*/
class KeywordExtractor { class KeywordExtractor {
@ -96,7 +96,8 @@ class KeywordExtractor {
LogError("line[%d] empty. skipped.", lineno); LogError("line[%d] empty. skipped.", lineno);
continue; continue;
} }
if(!split(line, buf, " ") || buf.size() != 2) { split(line, buf, " ");
if (buf.size() != 2) {
LogError("line %d [%s] illegal. skipped.", lineno, line.c_str()); LogError("line %d [%s] illegal. skipped.", lineno, line.c_str());
continue; continue;
} }

View File

@ -4,7 +4,7 @@
#include <algorithm> #include <algorithm>
#include <set> #include <set>
#include <cassert> #include <cassert>
#include "Limonp/Logger.hpp" #include "limonp/Logger.hpp"
#include "DictTrie.hpp" #include "DictTrie.hpp"
#include "ISegment.hpp" #include "ISegment.hpp"
#include "SegmentBase.hpp" #include "SegmentBase.hpp"

View File

@ -4,7 +4,7 @@
#include <cassert> #include <cassert>
#include "MPSegment.hpp" #include "MPSegment.hpp"
#include "HMMSegment.hpp" #include "HMMSegment.hpp"
#include "Limonp/StringUtil.hpp" #include "limonp/StringUtil.hpp"
namespace CppJieba { namespace CppJieba {
class MixSegment: public SegmentBase { class MixSegment: public SegmentBase {

View File

@ -2,11 +2,11 @@
#define CPPJIEBA_POS_TAGGING_H #define CPPJIEBA_POS_TAGGING_H
#include "MixSegment.hpp" #include "MixSegment.hpp"
#include "Limonp/StringUtil.hpp" #include "limonp/StringUtil.hpp"
#include "DictTrie.hpp" #include "DictTrie.hpp"
namespace CppJieba { namespace CppJieba {
using namespace Limonp; using namespace limonp;
static const char* const POS_M = "m"; static const char* const POS_M = "m";
static const char* const POS_ENG = "eng"; static const char* const POS_ENG = "eng";

View File

@ -4,7 +4,7 @@
#include <algorithm> #include <algorithm>
#include <set> #include <set>
#include <cassert> #include <cassert>
#include "Limonp/Logger.hpp" #include "limonp/Logger.hpp"
#include "DictTrie.hpp" #include "DictTrie.hpp"
#include "ISegment.hpp" #include "ISegment.hpp"
#include "SegmentBase.hpp" #include "SegmentBase.hpp"

View File

@ -2,15 +2,15 @@
#define CPPJIEBA_SEGMENTBASE_H #define CPPJIEBA_SEGMENTBASE_H
#include "TransCode.hpp" #include "TransCode.hpp"
#include "Limonp/Logger.hpp" #include "limonp/Logger.hpp"
#include "Limonp/NonCopyable.hpp" #include "limonp/NonCopyable.hpp"
#include "Limonp/HandyMacro.hpp" #include "limonp/HandyMacro.hpp"
#include "ISegment.hpp" #include "ISegment.hpp"
#include <cassert> #include <cassert>
namespace CppJieba { namespace CppJieba {
using namespace Limonp; using namespace limonp;
//const char* const SPECIAL_CHARS = " \t\n"; //const char* const SPECIAL_CHARS = " \t\n";
#ifndef CPPJIEBA_GBK #ifndef CPPJIEBA_GBK

View File

@ -6,15 +6,15 @@
#define CPPJIEBA_TRANSCODE_H #define CPPJIEBA_TRANSCODE_H
#include "Limonp/StringUtil.hpp" #include "limonp/StringUtil.hpp"
#include "Limonp/LocalVector.hpp" #include "limonp/LocalVector.hpp"
namespace CppJieba { namespace CppJieba {
using namespace Limonp; using namespace limonp;
typedef uint16_t Rune; typedef uint16_t Rune;
typedef Limonp::LocalVector<Rune> Unicode; typedef limonp::LocalVector<Rune> Unicode;
namespace TransCode { namespace TransCode {
inline bool decode(const string& str, Unicode& res) { inline bool decode(const string& str, Unicode& res) {

View File

@ -1,7 +1,7 @@
#ifndef CPPJIEBA_TRIE_HPP #ifndef CPPJIEBA_TRIE_HPP
#define CPPJIEBA_TRIE_HPP #define CPPJIEBA_TRIE_HPP
#include "Limonp/StdExtension.hpp" #include "limonp/StdExtension.hpp"
#include <vector> #include <vector>
#include <queue> #include <queue>

View File

@ -10,7 +10,7 @@
#include <sstream> #include <sstream>
#include "StringUtil.hpp" #include "StringUtil.hpp"
namespace Limonp { namespace limonp {
using namespace std; using namespace std;
class ArgvContext { class ArgvContext {
public : public :

View File

@ -9,7 +9,7 @@ https://github.com/chenshuo/muduo/blob/master/muduo/base/BlockingQueue.h
#include "BoundedQueue.hpp" #include "BoundedQueue.hpp"
#include "Condition.hpp" #include "Condition.hpp"
namespace Limonp { namespace limonp {
template<class T> template<class T>
class BlockingQueue: NonCopyable { class BlockingQueue: NonCopyable {
public: public:

View File

@ -5,7 +5,7 @@
#include <fstream> #include <fstream>
#include <cassert> #include <cassert>
namespace Limonp { namespace limonp {
using namespace std; using namespace std;
template<class T> template<class T>
class BoundedQueue { class BoundedQueue {

View File

@ -1,7 +1,7 @@
#ifndef LIMONP_CAST_FUNCTS_H #ifndef LIMONP_CAST_FUNCTS_H
#define LIMONP_CAST_FUNCTS_H #define LIMONP_CAST_FUNCTS_H
namespace Limonp { namespace limonp {
namespace CastFloat { namespace CastFloat {
//logical and or //logical and or
static const int sign_32 = 0xC0000000; static const int sign_32 = 0xC0000000;

View File

@ -4,7 +4,7 @@
#include <string> #include <string>
#include <stdarg.h> #include <stdarg.h>
namespace Limonp { namespace limonp {
using std::string; using std::string;
enum COLOR { enum COLOR {
@ -31,6 +31,6 @@ static void ColorPrint(const string& str, enum COLOR color = GREEN) {
} }
#endif #endif
} // namespace Limonp } // namespace limonp
#endif #endif

View File

@ -7,7 +7,7 @@
#include "MutexLock.hpp" #include "MutexLock.hpp"
namespace Limonp { namespace limonp {
class Condition : NonCopyable { class Condition : NonCopyable {
public: public:
explicit Condition(MutexLock& mutex) explicit Condition(MutexLock& mutex)

View File

@ -12,7 +12,7 @@
#include <assert.h> #include <assert.h>
#include "StringUtil.hpp" #include "StringUtil.hpp"
namespace Limonp { namespace limonp {
using namespace std; using namespace std;
class Config { class Config {
public: public:
@ -37,7 +37,8 @@ class Config {
continue; continue;
} }
vecBuf.clear(); vecBuf.clear();
if(!split(line, vecBuf, "=") || 2 != vecBuf.size()) { split(line, vecBuf, "=");
if(2 != vecBuf.size()) {
fprintf(stderr, "line[%s] illegal.\n", line.c_str()); fprintf(stderr, "line[%s] illegal.\n", line.c_str());
assert(false); assert(false);
continue; continue;

View File

@ -7,7 +7,7 @@
#define LIMONP_CHECK(exp) \ #define LIMONP_CHECK(exp) \
if(!(exp)){fprintf(stderr, "File:%s, Line:%d Exp:[" #exp "] is true, abort.\n", __FILE__, __LINE__); abort();} if(!(exp)){fprintf(stderr, "File:%s, Line:%d Exp:[" #exp "] is true, abort.\n", __FILE__, __LINE__); abort();}
#define print(x) cout<< #x": " << x <<endl #define print(x) cout << x << endl
/* /*
#define XX_GET_SET(varType, varName, funName)\ #define XX_GET_SET(varType, varName, funName)\
private: varType varName;\ private: varType varName;\

View File

@ -1,7 +1,7 @@
#ifndef LIMONP_INITONOFF_H #ifndef LIMONP_INITONOFF_H
#define LIMONP_INITONOFF_H #define LIMONP_INITONOFF_H
namespace Limonp { namespace limonp {
class InitOnOff { class InitOnOff {
public: public:
InitOnOff():isInited_(false) {}; InitOnOff():isInited_(false) {};

View File

@ -6,7 +6,7 @@
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
namespace Limonp { namespace limonp {
using namespace std; using namespace std;
/* /*
* LocalVector<T> : T must be primitive type (char , int, size_t), if T is struct or class, LocalVector<T> may be dangerous.. * LocalVector<T> : T must be primitive type (char , int, size_t), if T is struct or class, LocalVector<T> may be dangerous..

View File

@ -16,15 +16,13 @@
#include <time.h> #include <time.h>
#include <cassert> #include <cassert>
#define FILE_BASENAME strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__ #define LogDebug(fmt, ...) limonp::Logger::LoggingF(limonp::LL_DEBUG, __FILE__, __LINE__, fmt, ## __VA_ARGS__)
#define LogInfo(fmt, ...) limonp::Logger::LoggingF(limonp::LL_INFO, __FILE__, __LINE__, fmt, ## __VA_ARGS__)
#define LogWarn(fmt, ...) limonp::Logger::LoggingF(limonp::LL_WARN, __FILE__, __LINE__, fmt, ## __VA_ARGS__)
#define LogError(fmt, ...) limonp::Logger::LoggingF(limonp::LL_ERROR, __FILE__, __LINE__, fmt, ## __VA_ARGS__)
#define LogFatal(fmt, ...) {limonp::Logger::LoggingF(limonp::LL_FATAL, __FILE__, __LINE__, fmt, ## __VA_ARGS__); abort();}
#define LogDebug(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_DEBUG, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__) namespace limonp {
#define LogInfo(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_INFO, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__)
#define LogWarn(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_WARN, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__)
#define LogError(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_ERROR, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__)
#define LogFatal(fmt, ...) {Limonp::Logger::LoggingF(Limonp::LL_FATAL, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__); abort();}
namespace Limonp {
using namespace std; 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 = 32}; enum {LL_DEBUG = 0, LL_INFO = 1, LL_WARN = 2, LL_ERROR = 3, LL_FATAL = 4, LEVEL_ARRAY_SIZE = 5, CSTR_BUFFER_SIZE = 32};
static const char * LOG_LEVEL_ARRAY[LEVEL_ARRAY_SIZE]= {"DEBUG","INFO","WARN","ERROR","FATAL"}; static const char * LOG_LEVEL_ARRAY[LEVEL_ARRAY_SIZE]= {"DEBUG","INFO","WARN","ERROR","FATAL"};

View File

@ -31,7 +31,7 @@
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
namespace Limonp { namespace limonp {
//#pragma region MD5 defines //#pragma region MD5 defines
// Constants for MD5Transform routine. // Constants for MD5Transform routine.

View File

@ -5,7 +5,7 @@
#include "NonCopyable.hpp" #include "NonCopyable.hpp"
#include "HandyMacro.hpp" #include "HandyMacro.hpp"
namespace Limonp { namespace limonp {
class MutexLock: NonCopyable { class MutexLock: NonCopyable {
private: private:
pthread_mutex_t mutex_; pthread_mutex_t mutex_;

View File

@ -7,8 +7,9 @@
#include <string> #include <string>
#include "Logger.hpp" #include "Logger.hpp"
#include "InitOnOff.hpp" #include "InitOnOff.hpp"
#include "StringUtil.hpp"
namespace Limonp { namespace limonp {
using namespace std; using namespace std;
class MysqlClient: public InitOnOff { class MysqlClient: public InitOnOff {
public: public:
@ -67,10 +68,8 @@ class MysqlClient: public InitOnOff {
} }
size_t insert(const string& tableName, const string& keys, const vector<string>& vals) { size_t insert(const string& tableName, const string& keys, const vector<string>& vals) {
size_t retn = 0; size_t retn = 0;
string sql;
for(size_t i = 0; i < vals.size(); i ++) { for(size_t i = 0; i < vals.size(); i ++) {
sql.clear(); string sql = string_format("insert into %s (%s) values %s", tableName.c_str(), keys.c_str(), vals[i].c_str());
string_format(sql, "insert into %s (%s) values %s", tableName.c_str(), keys.c_str(), vals[i].c_str());
retn += executeSql(sql.c_str()); retn += executeSql(sql.c_str());
} }
return retn; return retn;

View File

@ -6,7 +6,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
namespace Limonp { namespace limonp {
class NonCopyable { class NonCopyable {
protected: protected:
NonCopyable() {}; NonCopyable() {};

View File

@ -23,7 +23,7 @@
#include <algorithm> #include <algorithm>
#include "StdExtension.hpp" #include "StdExtension.hpp"
namespace Limonp { namespace limonp {
using namespace std; using namespace std;
inline string string_format(const char* fmt, ...) { inline string string_format(const char* fmt, ...) {
int size = 256; int size = 256;
@ -68,39 +68,6 @@ string join(T begin, T end, const string& connector) {
return res; return res;
} }
inline bool split(const string& src, vector<string>& res, const string& pattern, size_t offset = 0, size_t len = string::npos) {
if(src.empty()) {
return false;
}
res.clear();
size_t start = 0;
size_t end = 0;
size_t cnt = 0;
while(start < src.size() && res.size() < len) {
end = src.find_first_of(pattern, start);
if(string::npos == end) {
if(cnt >= offset) {
res.push_back(src.substr(start));
}
return true;
}
//if(end == src.size() - 1)
//{
// res.push_back("");
// return true;
//}
if(cnt >= offset) {
res.push_back(src.substr(start, end - start));
}
cnt ++;
start = end + 1;
}
return true;
}
inline string& upper(string& str) { inline string& upper(string& str) {
transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper); transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper);
return str; return str;
@ -111,34 +78,56 @@ inline string& lower(string& str) {
return str; return str;
} }
inline std::string &ltrim(std::string &s) { 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)))); s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s; return s;
} }
inline std::string &rtrim(std::string &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()); s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s; return s;
} }
inline std::string &trim(std::string &s) { inline std::string& trim(std::string &s) {
return ltrim(rtrim(s)); return ltrim(rtrim(s));
} }
inline std::string & ltrim(std::string & s, char x) { inline std::string& ltrim(std::string & s, char x) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::bind2nd(std::equal_to<char>(), x)))); s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::bind2nd(std::equal_to<char>(), x))));
return s; return s;
} }
inline std::string & rtrim(std::string & s, char x) { inline std::string& rtrim(std::string & s, char x) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::bind2nd(std::equal_to<char>(), x))).base(), s.end()); s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::bind2nd(std::equal_to<char>(), x))).base(), s.end());
return s; return s;
} }
inline std::string &trim(std::string &s, char x) { inline std::string& trim(std::string &s, char x) {
return ltrim(rtrim(s, x), x); return ltrim(rtrim(s, x), x);
} }
inline void split(const string& src, vector<string>& res, const string& pattern, size_t maxsplit = string::npos) {
res.clear();
size_t start = 0;
size_t end = 0;
string sub;
while(start < src.size()) {
end = src.find_first_of(pattern, start);
if(string::npos == end || res.size() >= maxsplit) {
sub = src.substr(start);
trim(sub);
res.push_back(sub);
return;
}
sub = src.substr(start, end - start);
trim(sub);
res.push_back(sub);
start = end + 1;
}
return;
}
inline bool startsWith(const string& str, const string& prefix) { inline bool startsWith(const string& str, const string& prefix) {
if(prefix.length() > str.length()) { if(prefix.length() > str.length()) {
return false; return false;

View File

@ -4,7 +4,7 @@
#include "HandyMacro.hpp" #include "HandyMacro.hpp"
#include "NonCopyable.hpp" #include "NonCopyable.hpp"
namespace Limonp { namespace limonp {
class IThread: NonCopyable { class IThread: NonCopyable {
private: private:
pthread_t thread_; pthread_t thread_;

View File

@ -4,13 +4,17 @@
#include "Thread.hpp" #include "Thread.hpp"
#include "BlockingQueue.hpp" #include "BlockingQueue.hpp"
namespace Limonp { namespace limonp {
class ITask { class ITask {
public: public:
virtual void run() = 0; virtual void run() = 0;
virtual ~ITask() {} virtual ~ITask() {}
}; };
template <class TaskType>
ITask* CreateTask() {
return new TaskType();
}
template <class TaskType, class ArgType> template <class TaskType, class ArgType>
ITask* CreateTask(ArgType arg) { ITask* CreateTask(ArgType arg) {
return new TaskType(arg); return new TaskType(arg);
@ -43,7 +47,17 @@ class ThreadPool: NonCopyable {
if(task == NULL) { if(task == NULL) {
break; break;
} }
task->run(); try {
task->run();
} catch(std::exception& e) {
cerr << "file:" << __FILE__
<< ", line:" << __LINE__
<< ", " << e.what() << endl;
} catch(...) {
cerr << "file:" << __FILE__
<< ", line:" << __LINE__
<< ", unknown exception." << endl;
}
delete task; delete task;
} }
} }

View File

@ -5,7 +5,7 @@
#include "../src/HMMSegment.hpp" #include "../src/HMMSegment.hpp"
#include "../src/MixSegment.hpp" #include "../src/MixSegment.hpp"
#include "../src/KeywordExtractor.hpp" #include "../src/KeywordExtractor.hpp"
#include "../src/Limonp/Colors.hpp" #include "../src/limonp/Colors.hpp"
using namespace CppJieba; using namespace CppJieba;