mirror of
https://github.com/yanyiwu/cppjieba.git
synced 2025-07-18 00:00:12 +08:00
namespace husky; namespace limonp;
This commit is contained in:
parent
8a3ced2b27
commit
efd029c20b
@ -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
|
@ -3,11 +3,11 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#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<string, string>& mp, const string& key, string& res)const {
|
||||
std::unordered_map<string, string>::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<string, string>& 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 ;
|
||||
}
|
||||
|
17
server/husky/IRequestHandler.hpp
Normal file
17
server/husky/IRequestHandler.hpp
Normal 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
|
@ -15,14 +15,14 @@
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
#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
|
@ -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<WorkerThread,int, const IRequestHandler&>(clientSock, _reqHandler));
|
||||
_pool.add(CreateTask<WorkerThread,int, IRequestHandler&>(clientSock, _reqHandler));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private:
|
||||
ThreadPool _pool;
|
||||
IRequestHandler & _reqHandler;
|
||||
int _host_socket;
|
||||
}; // class ThreadPoolServer
|
||||
} // namespace husky
|
||||
|
||||
#endif
|
@ -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;
|
||||
};
|
||||
}
|
||||
|
@ -3,11 +3,11 @@
|
||||
#include <string>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#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<string> 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<string> words;
|
||||
run(httpReq.getBody(), "MIX", "simple", strSnd);
|
||||
return true;
|
||||
|
@ -8,13 +8,13 @@
|
||||
#include <stdint.h>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#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;
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
#include <cassert>
|
||||
#include "Limonp/Logger.hpp"
|
||||
#include "limonp/Logger.hpp"
|
||||
#include "DictTrie.hpp"
|
||||
#include "ISegment.hpp"
|
||||
#include "SegmentBase.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<uint16_t, double> EmitProbMap;
|
||||
|
||||
struct HMMModel {
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <set>
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
#include <cassert>
|
||||
#include "Limonp/Logger.hpp"
|
||||
#include "limonp/Logger.hpp"
|
||||
#include "DictTrie.hpp"
|
||||
#include "ISegment.hpp"
|
||||
#include "SegmentBase.hpp"
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include <cassert>
|
||||
#include "MPSegment.hpp"
|
||||
#include "HMMSegment.hpp"
|
||||
#include "Limonp/StringUtil.hpp"
|
||||
#include "limonp/StringUtil.hpp"
|
||||
|
||||
namespace CppJieba {
|
||||
class MixSegment: public SegmentBase {
|
||||
|
@ -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";
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
#include <cassert>
|
||||
#include "Limonp/Logger.hpp"
|
||||
#include "limonp/Logger.hpp"
|
||||
#include "DictTrie.hpp"
|
||||
#include "ISegment.hpp"
|
||||
#include "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 <cassert>
|
||||
|
||||
|
||||
namespace CppJieba {
|
||||
using namespace Limonp;
|
||||
using namespace limonp;
|
||||
|
||||
//const char* const SPECIAL_CHARS = " \t\n";
|
||||
#ifndef CPPJIEBA_GBK
|
||||
|
@ -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<Rune> Unicode;
|
||||
typedef limonp::LocalVector<Rune> Unicode;
|
||||
|
||||
namespace TransCode {
|
||||
inline bool decode(const string& str, Unicode& res) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
#ifndef CPPJIEBA_TRIE_HPP
|
||||
#define CPPJIEBA_TRIE_HPP
|
||||
|
||||
#include "Limonp/StdExtension.hpp"
|
||||
#include "limonp/StdExtension.hpp"
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
|
||||
|
@ -10,7 +10,7 @@
|
||||
#include <sstream>
|
||||
#include "StringUtil.hpp"
|
||||
|
||||
namespace Limonp {
|
||||
namespace limonp {
|
||||
using namespace std;
|
||||
class ArgvContext {
|
||||
public :
|
@ -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 T>
|
||||
class BlockingQueue: NonCopyable {
|
||||
public:
|
@ -5,7 +5,7 @@
|
||||
#include <fstream>
|
||||
#include <cassert>
|
||||
|
||||
namespace Limonp {
|
||||
namespace limonp {
|
||||
using namespace std;
|
||||
template<class T>
|
||||
class BoundedQueue {
|
@ -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;
|
@ -4,7 +4,7 @@
|
||||
#include <string>
|
||||
#include <stdarg.h>
|
||||
|
||||
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
|
@ -7,7 +7,7 @@
|
||||
|
||||
#include "MutexLock.hpp"
|
||||
|
||||
namespace Limonp {
|
||||
namespace limonp {
|
||||
class Condition : NonCopyable {
|
||||
public:
|
||||
explicit Condition(MutexLock& mutex)
|
@ -12,7 +12,7 @@
|
||||
#include <assert.h>
|
||||
#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;
|
@ -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 <<endl
|
||||
#define print(x) cout << x << endl
|
||||
/*
|
||||
#define XX_GET_SET(varType, varName, funName)\
|
||||
private: varType varName;\
|
@ -1,7 +1,7 @@
|
||||
#ifndef LIMONP_INITONOFF_H
|
||||
#define LIMONP_INITONOFF_H
|
||||
|
||||
namespace Limonp {
|
||||
namespace limonp {
|
||||
class InitOnOff {
|
||||
public:
|
||||
InitOnOff():isInited_(false) {};
|
@ -6,7 +6,7 @@
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace Limonp {
|
||||
namespace limonp {
|
||||
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..
|
@ -16,15 +16,13 @@
|
||||
#include <time.h>
|
||||
#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__)
|
||||
#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"};
|
@ -31,7 +31,7 @@
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
namespace Limonp {
|
||||
namespace limonp {
|
||||
|
||||
//#pragma region MD5 defines
|
||||
// Constants for MD5Transform routine.
|
@ -5,7 +5,7 @@
|
||||
#include "NonCopyable.hpp"
|
||||
#include "HandyMacro.hpp"
|
||||
|
||||
namespace Limonp {
|
||||
namespace limonp {
|
||||
class MutexLock: NonCopyable {
|
||||
private:
|
||||
pthread_mutex_t mutex_;
|
@ -7,8 +7,9 @@
|
||||
#include <string>
|
||||
#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<string>& 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;
|
@ -6,7 +6,7 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace Limonp {
|
||||
namespace limonp {
|
||||
class NonCopyable {
|
||||
protected:
|
||||
NonCopyable() {};
|
@ -23,7 +23,7 @@
|
||||
#include <algorithm>
|
||||
#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<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) {
|
||||
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<int, int>(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<int, int>(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<char>(), 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<char>(), 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<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) {
|
||||
if(prefix.length() > str.length()) {
|
||||
return false;
|
@ -4,7 +4,7 @@
|
||||
#include "HandyMacro.hpp"
|
||||
#include "NonCopyable.hpp"
|
||||
|
||||
namespace Limonp {
|
||||
namespace limonp {
|
||||
class IThread: NonCopyable {
|
||||
private:
|
||||
pthread_t thread_;
|
@ -4,13 +4,17 @@
|
||||
#include "Thread.hpp"
|
||||
#include "BlockingQueue.hpp"
|
||||
|
||||
namespace Limonp {
|
||||
namespace limonp {
|
||||
class ITask {
|
||||
public:
|
||||
virtual void run() = 0;
|
||||
virtual ~ITask() {}
|
||||
};
|
||||
|
||||
template <class TaskType>
|
||||
ITask* CreateTask() {
|
||||
return new TaskType();
|
||||
}
|
||||
template <class TaskType, class ArgType>
|
||||
ITask* CreateTask(ArgType arg) {
|
||||
return new TaskType(arg);
|
||||
@ -43,7 +47,17 @@ class ThreadPool: NonCopyable {
|
||||
if(task == NULL) {
|
||||
break;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user