diff --git a/server/Husky/IRequestHandler.hpp b/server/Husky/IRequestHandler.hpp deleted file mode 100644 index 6e7522f..0000000 --- a/server/Husky/IRequestHandler.hpp +++ /dev/null @@ -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 diff --git a/server/Husky/HttpReqInfo.hpp b/server/husky/HttpReqInfo.hpp similarity index 89% rename from server/Husky/HttpReqInfo.hpp rename to server/husky/HttpReqInfo.hpp index 4f5a811..2dc8e76 100644 --- a/server/Husky/HttpReqInfo.hpp +++ b/server/husky/HttpReqInfo.hpp @@ -3,11 +3,11 @@ #include #include -#include "Limonp/Logger.hpp" -#include "Limonp/StringUtil.hpp" +#include "limonp/Logger.hpp" +#include "limonp/StringUtil.hpp" -namespace Husky { -using namespace Limonp; +namespace husky { +using namespace limonp; using namespace std; static const char* const KEY_METHOD = "METHOD"; @@ -62,7 +62,7 @@ class HttpReqInfo { _isBodyFinished = false; _contentLength = 0; } - public: + bool parseHeader(const string& buffer) { return parseHeader(buffer.c_str(), buffer.size()); } @@ -77,7 +77,8 @@ class HttpReqInfo { } string firstline(headerStr, lpos, rpos - lpos); 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()); return false; } @@ -141,7 +142,7 @@ class HttpReqInfo { bool isBodyFinished() const { return _isBodyFinished; } - public: + const string& set(const string& key, const string& value) { return _headerMap[key] = value; } @@ -149,8 +150,30 @@ class HttpReqInfo { return _find(_headerMap, key, res); } 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 //{ // return _headerMap.find(KEY_METHOD)->second; @@ -190,7 +213,7 @@ class HttpReqInfo { string _path; string _body; friend ostream& operator<<(ostream& os, const HttpReqInfo& obj); - private: + bool _find(const std::unordered_map& mp, const string& key, string& res)const { std::unordered_map::const_iterator it = mp.find(key); if(it == mp.end()) { @@ -199,7 +222,7 @@ class HttpReqInfo { res = it->second; return true; } - private: + void _parseUri(const string& uri, string& path, std::unordered_map& mp) { if(uri.empty()) { 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 ; } diff --git a/server/husky/IRequestHandler.hpp b/server/husky/IRequestHandler.hpp new file mode 100644 index 0000000..d5bee48 --- /dev/null +++ b/server/husky/IRequestHandler.hpp @@ -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 diff --git a/server/Husky/NetUtils.hpp b/server/husky/NetUtils.hpp similarity index 88% rename from server/Husky/NetUtils.hpp rename to server/husky/NetUtils.hpp index deec0db..b436cea 100644 --- a/server/Husky/NetUtils.hpp +++ b/server/husky/NetUtils.hpp @@ -15,14 +15,14 @@ #include #include -#include "Limonp/StdExtension.hpp" -#include "Limonp/Logger.hpp" +#include "limonp/StdExtension.hpp" +#include "limonp/Logger.hpp" -namespace Husky { +namespace husky { static const size_t LISTEN_QUEUE_LEN = 1024; typedef int SocketFd; -SocketFd CreateAndListenSocket(int port) { +inline SocketFd CreateAndListenSocket(int port) { SocketFd sock; sock = socket(AF_INET, SOCK_STREAM, 0); 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 CHARSET_UTF8 = "UTF-8"; -} // namespace Husky +} // namespace husky #endif diff --git a/server/Husky/ThreadPoolServer.hpp b/server/husky/ThreadPoolServer.hpp similarity index 74% rename from server/Husky/ThreadPoolServer.hpp rename to server/husky/ThreadPoolServer.hpp index 02a16d2..9d69b25 100644 --- a/server/Husky/ThreadPoolServer.hpp +++ b/server/husky/ThreadPoolServer.hpp @@ -4,21 +4,17 @@ #include "NetUtils.hpp" #include "WorkerThread.hpp" -namespace Husky { -using namespace Limonp; +namespace husky { +using namespace limonp; class ThreadPoolServer { - private: - ThreadPool _pool; - const IRequestHandler & _reqHandler; - int _host_socket; 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) { _host_socket = CreateAndListenSocket(port); } ~ThreadPoolServer() {}; - public: + bool start() { _pool.start(); sockaddr_in clientaddr; @@ -30,10 +26,16 @@ class ThreadPoolServer { LogError(strerror(errno)); break; } - _pool.add(CreateTask(clientSock, _reqHandler)); + _pool.add(CreateTask(clientSock, _reqHandler)); } return true; } -}; -} + + private: + ThreadPool _pool; + IRequestHandler & _reqHandler; + int _host_socket; +}; // class ThreadPoolServer +} // namespace husky + #endif diff --git a/server/Husky/WorkerThread.hpp b/server/husky/WorkerThread.hpp similarity index 86% rename from server/Husky/WorkerThread.hpp rename to server/husky/WorkerThread.hpp index 08937f2..0f8da79 100644 --- a/server/Husky/WorkerThread.hpp +++ b/server/husky/WorkerThread.hpp @@ -1,11 +1,11 @@ #ifndef HUSKY_WORKER_HPP #define HUSKY_WORKER_HPP -#include "Limonp/ThreadPool.hpp" +#include "limonp/ThreadPool.hpp" #include "IRequestHandler.hpp" #include "NetUtils.hpp" -namespace Husky { +namespace husky { const char* const CLIENT_IP_K = "CLIENT_IP"; const size_t RECV_BUFFER_SIZE = 16 * 1024; @@ -14,14 +14,11 @@ const struct timeval SOCKET_TIMEOUT = {16, 0}; class WorkerThread: public ITask { public: - WorkerThread(int sockfs, const IRequestHandler& reqHandler): + WorkerThread(int sockfs, IRequestHandler& reqHandler): _sockfd(sockfs), _reqHandler(reqHandler) { } virtual ~WorkerThread() { } - private: - int _sockfd; - const IRequestHandler& _reqHandler; public: void run() { @@ -37,12 +34,12 @@ class WorkerThread: public ITask { break; } - if(httpReq.isGET() && !_reqHandler.do_GET(httpReq, strRetByHandler)) { - LogError("do_GET failed."); + if(httpReq.isGET() && !_reqHandler.doGET(httpReq, strRetByHandler)) { + LogError("doGET failed."); break; } - if(httpReq.isPOST() && !_reqHandler.do_POST(httpReq, strRetByHandler)) { - LogError("do_POST failed."); + if(httpReq.isPOST() && !_reqHandler.doPOST(httpReq, strRetByHandler)) { + LogError("doPOST failed."); break; } strSnd = string_format(HTTP_FORMAT, CHARSET_UTF8, strRetByHandler.length(), strRetByHandler.c_str()); @@ -101,7 +98,9 @@ class WorkerThread: public ITask { } return true; } - + private: + int _sockfd; + IRequestHandler& _reqHandler; }; } diff --git a/server/server.cpp b/server/server.cpp index f56e7c5..2f4fabd 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -3,11 +3,11 @@ #include #include #include -#include "Limonp/Config.hpp" -#include "Husky/ThreadPoolServer.hpp" +#include "limonp/Config.hpp" +#include "husky/ThreadPoolServer.hpp" #include "Application.hpp" -using namespace Husky; +using namespace husky; using namespace CppJieba; class ReqHandler: public IRequestHandler { @@ -17,7 +17,7 @@ class ReqHandler: public IRequestHandler { 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 tmp; vector words; @@ -30,7 +30,7 @@ class ReqHandler: public IRequestHandler { return true; } - virtual bool do_POST(const HttpReqInfo& httpReq, string& strSnd) const { + virtual bool doPOST(const HttpReqInfo& httpReq, string& strSnd) { vector words; run(httpReq.getBody(), "MIX", "simple", strSnd); return true; diff --git a/src/DictTrie.hpp b/src/DictTrie.hpp index 9d925f8..231aa2a 100644 --- a/src/DictTrie.hpp +++ b/src/DictTrie.hpp @@ -8,13 +8,13 @@ #include #include #include -#include "Limonp/StringUtil.hpp" -#include "Limonp/Logger.hpp" +#include "limonp/StringUtil.hpp" +#include "limonp/Logger.hpp" #include "TransCode.hpp" #include "Trie.hpp" namespace CppJieba { -using namespace Limonp; +using namespace limonp; const double MIN_DOUBLE = -3.14e+100; const double MAX_DOUBLE = 3.14e+100; const size_t DICT_COLUMN_NUM = 3; diff --git a/src/FullSegment.hpp b/src/FullSegment.hpp index 351198b..a8edb40 100644 --- a/src/FullSegment.hpp +++ b/src/FullSegment.hpp @@ -4,7 +4,7 @@ #include #include #include -#include "Limonp/Logger.hpp" +#include "limonp/Logger.hpp" #include "DictTrie.hpp" #include "ISegment.hpp" #include "SegmentBase.hpp" diff --git a/src/HMMModel.hpp b/src/HMMModel.hpp index a79e720..71cfe2f 100644 --- a/src/HMMModel.hpp +++ b/src/HMMModel.hpp @@ -1,11 +1,11 @@ #ifndef CPPJIEBA_HMMMODEL_H #define CPPJIEBA_HMMMODEL_H -#include "Limonp/StringUtil.hpp" +#include "limonp/StringUtil.hpp" namespace CppJieba { -using namespace Limonp; +using namespace limonp; typedef unordered_map EmitProbMap; struct HMMModel { diff --git a/src/KeywordExtractor.hpp b/src/KeywordExtractor.hpp index 11fcea8..34c0096 100644 --- a/src/KeywordExtractor.hpp +++ b/src/KeywordExtractor.hpp @@ -6,7 +6,7 @@ #include namespace CppJieba { -using namespace Limonp; +using namespace limonp; /*utf8*/ class KeywordExtractor { @@ -96,7 +96,8 @@ class KeywordExtractor { LogError("line[%d] empty. skipped.", lineno); 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()); continue; } diff --git a/src/MPSegment.hpp b/src/MPSegment.hpp index 1298d67..c0fd036 100644 --- a/src/MPSegment.hpp +++ b/src/MPSegment.hpp @@ -4,7 +4,7 @@ #include #include #include -#include "Limonp/Logger.hpp" +#include "limonp/Logger.hpp" #include "DictTrie.hpp" #include "ISegment.hpp" #include "SegmentBase.hpp" diff --git a/src/MixSegment.hpp b/src/MixSegment.hpp index c9d0880..3c91899 100644 --- a/src/MixSegment.hpp +++ b/src/MixSegment.hpp @@ -4,7 +4,7 @@ #include #include "MPSegment.hpp" #include "HMMSegment.hpp" -#include "Limonp/StringUtil.hpp" +#include "limonp/StringUtil.hpp" namespace CppJieba { class MixSegment: public SegmentBase { diff --git a/src/PosTagger.hpp b/src/PosTagger.hpp index 2bb9e76..39b4709 100644 --- a/src/PosTagger.hpp +++ b/src/PosTagger.hpp @@ -2,11 +2,11 @@ #define CPPJIEBA_POS_TAGGING_H #include "MixSegment.hpp" -#include "Limonp/StringUtil.hpp" +#include "limonp/StringUtil.hpp" #include "DictTrie.hpp" namespace CppJieba { -using namespace Limonp; +using namespace limonp; static const char* const POS_M = "m"; static const char* const POS_ENG = "eng"; diff --git a/src/QuerySegment.hpp b/src/QuerySegment.hpp index fb95d1d..9bb2801 100644 --- a/src/QuerySegment.hpp +++ b/src/QuerySegment.hpp @@ -4,7 +4,7 @@ #include #include #include -#include "Limonp/Logger.hpp" +#include "limonp/Logger.hpp" #include "DictTrie.hpp" #include "ISegment.hpp" #include "SegmentBase.hpp" diff --git a/src/SegmentBase.hpp b/src/SegmentBase.hpp index 4989771..531e0dd 100644 --- a/src/SegmentBase.hpp +++ b/src/SegmentBase.hpp @@ -2,15 +2,15 @@ #define CPPJIEBA_SEGMENTBASE_H #include "TransCode.hpp" -#include "Limonp/Logger.hpp" -#include "Limonp/NonCopyable.hpp" -#include "Limonp/HandyMacro.hpp" +#include "limonp/Logger.hpp" +#include "limonp/NonCopyable.hpp" +#include "limonp/HandyMacro.hpp" #include "ISegment.hpp" #include namespace CppJieba { -using namespace Limonp; +using namespace limonp; //const char* const SPECIAL_CHARS = " \t\n"; #ifndef CPPJIEBA_GBK diff --git a/src/TransCode.hpp b/src/TransCode.hpp index d7fa162..52e5ac7 100644 --- a/src/TransCode.hpp +++ b/src/TransCode.hpp @@ -6,15 +6,15 @@ #define CPPJIEBA_TRANSCODE_H -#include "Limonp/StringUtil.hpp" -#include "Limonp/LocalVector.hpp" +#include "limonp/StringUtil.hpp" +#include "limonp/LocalVector.hpp" namespace CppJieba { -using namespace Limonp; +using namespace limonp; typedef uint16_t Rune; -typedef Limonp::LocalVector Unicode; +typedef limonp::LocalVector Unicode; namespace TransCode { inline bool decode(const string& str, Unicode& res) { diff --git a/src/Trie.hpp b/src/Trie.hpp index ac2659c..80f330a 100644 --- a/src/Trie.hpp +++ b/src/Trie.hpp @@ -1,7 +1,7 @@ #ifndef CPPJIEBA_TRIE_HPP #define CPPJIEBA_TRIE_HPP -#include "Limonp/StdExtension.hpp" +#include "limonp/StdExtension.hpp" #include #include diff --git a/src/Limonp/ArgvContext.hpp b/src/limonp/ArgvContext.hpp similarity index 98% rename from src/Limonp/ArgvContext.hpp rename to src/limonp/ArgvContext.hpp index 0db265b..1ba1311 100644 --- a/src/Limonp/ArgvContext.hpp +++ b/src/limonp/ArgvContext.hpp @@ -10,7 +10,7 @@ #include #include "StringUtil.hpp" -namespace Limonp { +namespace limonp { using namespace std; class ArgvContext { public : diff --git a/src/Limonp/BlockingQueue.hpp b/src/limonp/BlockingQueue.hpp similarity index 99% rename from src/Limonp/BlockingQueue.hpp rename to src/limonp/BlockingQueue.hpp index 1806bd1..5b94a19 100644 --- a/src/Limonp/BlockingQueue.hpp +++ b/src/limonp/BlockingQueue.hpp @@ -9,7 +9,7 @@ https://github.com/chenshuo/muduo/blob/master/muduo/base/BlockingQueue.h #include "BoundedQueue.hpp" #include "Condition.hpp" -namespace Limonp { +namespace limonp { template class BlockingQueue: NonCopyable { public: diff --git a/src/Limonp/BoundedQueue.hpp b/src/limonp/BoundedQueue.hpp similarity index 98% rename from src/Limonp/BoundedQueue.hpp rename to src/limonp/BoundedQueue.hpp index 3f9eef4..ba32c27 100644 --- a/src/Limonp/BoundedQueue.hpp +++ b/src/limonp/BoundedQueue.hpp @@ -5,7 +5,7 @@ #include #include -namespace Limonp { +namespace limonp { using namespace std; template class BoundedQueue { diff --git a/src/Limonp/CastFloat.hpp b/src/limonp/CastFloat.hpp similarity index 99% rename from src/Limonp/CastFloat.hpp rename to src/limonp/CastFloat.hpp index 9af0069..73426bc 100644 --- a/src/Limonp/CastFloat.hpp +++ b/src/limonp/CastFloat.hpp @@ -1,7 +1,7 @@ #ifndef LIMONP_CAST_FUNCTS_H #define LIMONP_CAST_FUNCTS_H -namespace Limonp { +namespace limonp { namespace CastFloat { //logical and or static const int sign_32 = 0xC0000000; diff --git a/src/Limonp/Colors.hpp b/src/limonp/Colors.hpp similarity index 93% rename from src/Limonp/Colors.hpp rename to src/limonp/Colors.hpp index 591037c..d9bf14a 100644 --- a/src/Limonp/Colors.hpp +++ b/src/limonp/Colors.hpp @@ -4,7 +4,7 @@ #include #include -namespace Limonp { +namespace limonp { using std::string; enum COLOR { @@ -31,6 +31,6 @@ static void ColorPrint(const string& str, enum COLOR color = GREEN) { } #endif -} // namespace Limonp +} // namespace limonp #endif diff --git a/src/Limonp/Condition.hpp b/src/limonp/Condition.hpp similarity index 97% rename from src/Limonp/Condition.hpp rename to src/limonp/Condition.hpp index 9687e37..7a7bfbd 100644 --- a/src/Limonp/Condition.hpp +++ b/src/limonp/Condition.hpp @@ -7,7 +7,7 @@ #include "MutexLock.hpp" -namespace Limonp { +namespace limonp { class Condition : NonCopyable { public: explicit Condition(MutexLock& mutex) diff --git a/src/Limonp/Config.hpp b/src/limonp/Config.hpp similarity index 96% rename from src/Limonp/Config.hpp rename to src/limonp/Config.hpp index c9e2088..ebdafcd 100644 --- a/src/Limonp/Config.hpp +++ b/src/limonp/Config.hpp @@ -12,7 +12,7 @@ #include #include "StringUtil.hpp" -namespace Limonp { +namespace limonp { using namespace std; class Config { public: @@ -37,7 +37,8 @@ class Config { continue; } 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()); assert(false); continue; diff --git a/src/Limonp/HandyMacro.hpp b/src/limonp/HandyMacro.hpp similarity index 95% rename from src/Limonp/HandyMacro.hpp rename to src/limonp/HandyMacro.hpp index 0f9102c..e461b3d 100644 --- a/src/Limonp/HandyMacro.hpp +++ b/src/limonp/HandyMacro.hpp @@ -7,7 +7,7 @@ #define LIMONP_CHECK(exp) \ if(!(exp)){fprintf(stderr, "File:%s, Line:%d Exp:[" #exp "] is true, abort.\n", __FILE__, __LINE__); abort();} -#define print(x) cout<< #x": " << x < #include -namespace Limonp { +namespace limonp { using namespace std; /* * LocalVector : T must be primitive type (char , int, size_t), if T is struct or class, LocalVector may be dangerous.. diff --git a/src/Limonp/Logger.hpp b/src/limonp/Logger.hpp similarity index 70% rename from src/Limonp/Logger.hpp rename to src/limonp/Logger.hpp index d46cb9a..cf79a06 100644 --- a/src/Limonp/Logger.hpp +++ b/src/limonp/Logger.hpp @@ -16,15 +16,13 @@ #include #include -#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__) -#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 { +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 = 32}; static const char * LOG_LEVEL_ARRAY[LEVEL_ARRAY_SIZE]= {"DEBUG","INFO","WARN","ERROR","FATAL"}; diff --git a/src/Limonp/Md5.hpp b/src/limonp/Md5.hpp similarity index 96% rename from src/Limonp/Md5.hpp rename to src/limonp/Md5.hpp index 2b82bbd..d30f3b5 100644 --- a/src/Limonp/Md5.hpp +++ b/src/limonp/Md5.hpp @@ -31,7 +31,7 @@ #include #include -namespace Limonp { +namespace limonp { //#pragma region MD5 defines // Constants for MD5Transform routine. diff --git a/src/Limonp/MutexLock.hpp b/src/limonp/MutexLock.hpp similarity index 97% rename from src/Limonp/MutexLock.hpp rename to src/limonp/MutexLock.hpp index 5f9ecbd..cdcce18 100644 --- a/src/Limonp/MutexLock.hpp +++ b/src/limonp/MutexLock.hpp @@ -5,7 +5,7 @@ #include "NonCopyable.hpp" #include "HandyMacro.hpp" -namespace Limonp { +namespace limonp { class MutexLock: NonCopyable { private: pthread_mutex_t mutex_; diff --git a/src/Limonp/MysqlClient.hpp b/src/limonp/MysqlClient.hpp similarity index 94% rename from src/Limonp/MysqlClient.hpp rename to src/limonp/MysqlClient.hpp index 7395fa9..ecf3076 100644 --- a/src/Limonp/MysqlClient.hpp +++ b/src/limonp/MysqlClient.hpp @@ -7,8 +7,9 @@ #include #include "Logger.hpp" #include "InitOnOff.hpp" +#include "StringUtil.hpp" -namespace Limonp { +namespace limonp { using namespace std; class MysqlClient: public InitOnOff { public: @@ -67,10 +68,8 @@ class MysqlClient: public InitOnOff { } size_t insert(const string& tableName, const string& keys, const vector& vals) { size_t retn = 0; - string sql; for(size_t i = 0; i < vals.size(); i ++) { - sql.clear(); - string_format(sql, "insert into %s (%s) values %s", tableName.c_str(), keys.c_str(), vals[i].c_str()); + string sql = string_format("insert into %s (%s) values %s", tableName.c_str(), keys.c_str(), vals[i].c_str()); retn += executeSql(sql.c_str()); } return retn; diff --git a/src/Limonp/NonCopyable.hpp b/src/limonp/NonCopyable.hpp similarity index 94% rename from src/Limonp/NonCopyable.hpp rename to src/limonp/NonCopyable.hpp index c47e66b..1e58027 100644 --- a/src/Limonp/NonCopyable.hpp +++ b/src/limonp/NonCopyable.hpp @@ -6,7 +6,7 @@ #include #include -namespace Limonp { +namespace limonp { class NonCopyable { protected: NonCopyable() {}; diff --git a/src/Limonp/StdExtension.hpp b/src/limonp/StdExtension.hpp similarity index 100% rename from src/Limonp/StdExtension.hpp rename to src/limonp/StdExtension.hpp diff --git a/src/Limonp/StringUtil.hpp b/src/limonp/StringUtil.hpp similarity index 87% rename from src/Limonp/StringUtil.hpp rename to src/limonp/StringUtil.hpp index 0efab05..e57482c 100644 --- a/src/Limonp/StringUtil.hpp +++ b/src/limonp/StringUtil.hpp @@ -23,7 +23,7 @@ #include #include "StdExtension.hpp" -namespace Limonp { +namespace limonp { using namespace std; inline string string_format(const char* fmt, ...) { int size = 256; @@ -68,39 +68,6 @@ string join(T begin, T end, const string& connector) { return res; } - - -inline bool split(const string& src, vector& 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) { transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper); return str; @@ -111,34 +78,56 @@ inline string& lower(string& str) { return str; } -inline std::string <rim(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(std::isspace)))); 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(std::isspace))).base(), s.end()); return s; } -inline std::string &trim(std::string &s) { +inline std::string& trim(std::string &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(), x)))); 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(), x))).base(), s.end()); 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); } +inline void split(const string& src, vector& 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) { if(prefix.length() > str.length()) { return false; diff --git a/src/Limonp/Thread.hpp b/src/limonp/Thread.hpp similarity index 97% rename from src/Limonp/Thread.hpp rename to src/limonp/Thread.hpp index 563ec60..44d1758 100644 --- a/src/Limonp/Thread.hpp +++ b/src/limonp/Thread.hpp @@ -4,7 +4,7 @@ #include "HandyMacro.hpp" #include "NonCopyable.hpp" -namespace Limonp { +namespace limonp { class IThread: NonCopyable { private: pthread_t thread_; diff --git a/src/Limonp/ThreadPool.hpp b/src/limonp/ThreadPool.hpp similarity index 81% rename from src/Limonp/ThreadPool.hpp rename to src/limonp/ThreadPool.hpp index cc68516..db71e58 100644 --- a/src/Limonp/ThreadPool.hpp +++ b/src/limonp/ThreadPool.hpp @@ -4,13 +4,17 @@ #include "Thread.hpp" #include "BlockingQueue.hpp" -namespace Limonp { +namespace limonp { class ITask { public: virtual void run() = 0; virtual ~ITask() {} }; +template +ITask* CreateTask() { + return new TaskType(); +} template ITask* CreateTask(ArgType arg) { return new TaskType(arg); @@ -43,7 +47,17 @@ class ThreadPool: NonCopyable { if(task == NULL) { 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; } } diff --git a/test/load_test.cpp b/test/load_test.cpp index f6f9116..f7b3619 100644 --- a/test/load_test.cpp +++ b/test/load_test.cpp @@ -5,7 +5,7 @@ #include "../src/HMMSegment.hpp" #include "../src/MixSegment.hpp" #include "../src/KeywordExtractor.hpp" -#include "../src/Limonp/Colors.hpp" +#include "../src/limonp/Colors.hpp" using namespace CppJieba;