mirror of
https://github.com/yanyiwu/cppjieba.git
synced 2025-07-18 00:00:12 +08:00
upload limonp for Colors.hpp and use ColorPrintln in load_test.cpp
This commit is contained in:
parent
8c23da4332
commit
660cd9d93e
@ -10,31 +10,21 @@
|
|||||||
#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 :
|
||||||
ArgvContext(int argc, const char* const * argv)
|
ArgvContext(int argc, const char* const * argv) {
|
||||||
{
|
|
||||||
|
|
||||||
for(int i = 0; i < argc; i++)
|
for(int i = 0; i < argc; i++) {
|
||||||
{
|
if(startsWith(argv[i], "-")) {
|
||||||
if(startsWith(argv[i], "-"))
|
if(i + 1 < argc && !startsWith(argv[i + 1], "-")) {
|
||||||
{
|
|
||||||
if(i + 1 < argc && !startsWith(argv[i + 1], "-"))
|
|
||||||
{
|
|
||||||
mpss_[argv[i]] = argv[i+1];
|
mpss_[argv[i]] = argv[i+1];
|
||||||
i++;
|
i++;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
sset_.insert(argv[i]);
|
sset_.insert(argv[i]);
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
args_.push_back(argv[i]);
|
args_.push_back(argv[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -42,28 +32,22 @@ namespace Limonp
|
|||||||
~ArgvContext() {};
|
~ArgvContext() {};
|
||||||
public:
|
public:
|
||||||
friend ostream& operator << (ostream& os, const ArgvContext& args);
|
friend ostream& operator << (ostream& os, const ArgvContext& args);
|
||||||
string operator [](size_t i) const
|
string operator [](size_t i) const {
|
||||||
{
|
if(i < args_.size()) {
|
||||||
if(i < args_.size())
|
|
||||||
{
|
|
||||||
return args_[i];
|
return args_[i];
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
string operator [](const string& key) const
|
string operator [](const string& key) const {
|
||||||
{
|
|
||||||
map<string, string>::const_iterator it = mpss_.find(key);
|
map<string, string>::const_iterator it = mpss_.find(key);
|
||||||
if(it != mpss_.end())
|
if(it != mpss_.end()) {
|
||||||
{
|
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
bool hasKey(const string& key) const
|
bool hasKey(const string& key) const {
|
||||||
{
|
if(mpss_.find(key) != mpss_.end() || sset_.find(key) != sset_.end()) {
|
||||||
if(mpss_.find(key) != mpss_.end() || sset_.find(key) != sset_.end())
|
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -75,8 +59,7 @@ namespace Limonp
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline ostream& operator << (ostream& os, const ArgvContext& args)
|
inline ostream& operator << (ostream& os, const ArgvContext& args) {
|
||||||
{
|
|
||||||
return os<<args.args_<<args.mpss_<<args.sset_;
|
return os<<args.args_<<args.mpss_<<args.sset_;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,30 +9,24 @@ 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:
|
||||||
BlockingQueue()
|
BlockingQueue()
|
||||||
: mutex_(), notEmpty_(mutex_), queue_()
|
: mutex_(), notEmpty_(mutex_), queue_() {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void push(const T& x)
|
void push(const T& x) {
|
||||||
{
|
|
||||||
MutexLockGuard lock(mutex_);
|
MutexLockGuard lock(mutex_);
|
||||||
queue_.push(x);
|
queue_.push(x);
|
||||||
notEmpty_.notify(); // wait morphing saves us
|
notEmpty_.notify(); // wait morphing saves us
|
||||||
}
|
}
|
||||||
|
|
||||||
T pop()
|
T pop() {
|
||||||
{
|
|
||||||
MutexLockGuard lock(mutex_);
|
MutexLockGuard lock(mutex_);
|
||||||
// always use a while-loop, due to spurious wakeup
|
// always use a while-loop, due to spurious wakeup
|
||||||
while (queue_.empty())
|
while (queue_.empty()) {
|
||||||
{
|
|
||||||
notEmpty_.wait();
|
notEmpty_.wait();
|
||||||
}
|
}
|
||||||
assert(!queue_.empty());
|
assert(!queue_.empty());
|
||||||
@ -41,13 +35,11 @@ namespace Limonp
|
|||||||
return front;
|
return front;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const
|
size_t size() const {
|
||||||
{
|
|
||||||
MutexLockGuard lock(mutex_);
|
MutexLockGuard lock(mutex_);
|
||||||
return queue_.size();
|
return queue_.size();
|
||||||
}
|
}
|
||||||
bool empty() const
|
bool empty() const {
|
||||||
{
|
|
||||||
return size() == 0;
|
return size() == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,21 +50,18 @@ namespace Limonp
|
|||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class BoundedBlockingQueue : NonCopyable
|
class BoundedBlockingQueue : NonCopyable {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
explicit BoundedBlockingQueue(size_t maxSize)
|
explicit BoundedBlockingQueue(size_t maxSize)
|
||||||
: mutex_(),
|
: mutex_(),
|
||||||
notEmpty_(mutex_),
|
notEmpty_(mutex_),
|
||||||
notFull_(mutex_),
|
notFull_(mutex_),
|
||||||
queue_(maxSize)
|
queue_(maxSize) {
|
||||||
{}
|
}
|
||||||
|
|
||||||
void push(const T& x)
|
void push(const T& x) {
|
||||||
{
|
|
||||||
MutexLockGuard lock(mutex_);
|
MutexLockGuard lock(mutex_);
|
||||||
while (queue_.full())
|
while (queue_.full()) {
|
||||||
{
|
|
||||||
notFull_.wait();
|
notFull_.wait();
|
||||||
}
|
}
|
||||||
assert(!queue_.full());
|
assert(!queue_.full());
|
||||||
@ -80,11 +69,9 @@ namespace Limonp
|
|||||||
notEmpty_.notify();
|
notEmpty_.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
T pop()
|
T pop() {
|
||||||
{
|
|
||||||
MutexLockGuard lock(mutex_);
|
MutexLockGuard lock(mutex_);
|
||||||
while (queue_.empty())
|
while (queue_.empty()) {
|
||||||
{
|
|
||||||
notEmpty_.wait();
|
notEmpty_.wait();
|
||||||
}
|
}
|
||||||
assert(!queue_.empty());
|
assert(!queue_.empty());
|
||||||
@ -93,26 +80,22 @@ namespace Limonp
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool empty() const
|
bool empty() const {
|
||||||
{
|
|
||||||
MutexLockGuard lock(mutex_);
|
MutexLockGuard lock(mutex_);
|
||||||
return queue_.empty();
|
return queue_.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool full() const
|
bool full() const {
|
||||||
{
|
|
||||||
MutexLockGuard lock(mutex_);
|
MutexLockGuard lock(mutex_);
|
||||||
return queue_.full();
|
return queue_.full();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const
|
size_t size() const {
|
||||||
{
|
|
||||||
MutexLockGuard lock(mutex_);
|
MutexLockGuard lock(mutex_);
|
||||||
return queue_.size();
|
return queue_.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t capacity() const
|
size_t capacity() const {
|
||||||
{
|
|
||||||
return queue_.capacity();
|
return queue_.capacity();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,12 +5,10 @@
|
|||||||
#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 {
|
||||||
{
|
|
||||||
private:
|
private:
|
||||||
size_t head_;
|
size_t head_;
|
||||||
size_t tail_;
|
size_t tail_;
|
||||||
@ -18,8 +16,7 @@ namespace Limonp
|
|||||||
const size_t capacity_;
|
const size_t capacity_;
|
||||||
vector<T> circular__buffer;
|
vector<T> circular__buffer;
|
||||||
public:
|
public:
|
||||||
explicit BoundedQueue(size_t capacity): capacity_(capacity), circular__buffer(capacity)
|
explicit BoundedQueue(size_t capacity): capacity_(capacity), circular__buffer(capacity) {
|
||||||
{
|
|
||||||
head_ = 0;
|
head_ = 0;
|
||||||
tail_ = 0;
|
tail_ = 0;
|
||||||
size_ = 0;
|
size_ = 0;
|
||||||
@ -27,39 +24,32 @@ namespace Limonp
|
|||||||
}
|
}
|
||||||
~BoundedQueue() {}
|
~BoundedQueue() {}
|
||||||
public:
|
public:
|
||||||
void clear()
|
void clear() {
|
||||||
{
|
|
||||||
head_ = 0;
|
head_ = 0;
|
||||||
tail_ = 0;
|
tail_ = 0;
|
||||||
size_ = 0;
|
size_ = 0;
|
||||||
}
|
}
|
||||||
bool empty() const
|
bool empty() const {
|
||||||
{
|
|
||||||
return !size_;
|
return !size_;
|
||||||
}
|
}
|
||||||
bool full() const
|
bool full() const {
|
||||||
{
|
|
||||||
return capacity_ == size_;
|
return capacity_ == size_;
|
||||||
}
|
}
|
||||||
size_t size() const
|
size_t size() const {
|
||||||
{
|
|
||||||
return size_;
|
return size_;
|
||||||
}
|
}
|
||||||
size_t capacity() const
|
size_t capacity() const {
|
||||||
{
|
|
||||||
return capacity_;
|
return capacity_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void push(const T& t)
|
void push(const T& t) {
|
||||||
{
|
|
||||||
assert(!full());
|
assert(!full());
|
||||||
circular__buffer[tail_] = t;
|
circular__buffer[tail_] = t;
|
||||||
tail_ = (tail_ + 1) % capacity_;
|
tail_ = (tail_ + 1) % capacity_;
|
||||||
size_ ++;
|
size_ ++;
|
||||||
}
|
}
|
||||||
|
|
||||||
T pop()
|
T pop() {
|
||||||
{
|
|
||||||
assert(!empty());
|
assert(!empty());
|
||||||
size_t oldPos = head_;
|
size_t oldPos = head_;
|
||||||
head_ = (head_ + 1) % capacity_;
|
head_ = (head_ + 1) % capacity_;
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
#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;
|
||||||
static const int exponent_32 = 0x07800000;
|
static const int exponent_32 = 0x07800000;
|
||||||
@ -22,10 +20,8 @@ namespace Limonp
|
|||||||
static const short infinite_16 = (short) 0x7FFF;
|
static const short infinite_16 = (short) 0x7FFF;
|
||||||
static const short infinitesmall_16 = (short) 0x0000;
|
static const short infinitesmall_16 = (short) 0x0000;
|
||||||
|
|
||||||
inline float intBitsToFloat(unsigned int x)
|
inline float intBitsToFloat(unsigned int x) {
|
||||||
{
|
union {
|
||||||
union
|
|
||||||
{
|
|
||||||
float f;
|
float f;
|
||||||
int i;
|
int i;
|
||||||
} u;
|
} u;
|
||||||
@ -33,10 +29,8 @@ namespace Limonp
|
|||||||
return u.f;
|
return u.f;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int floatToIntBits(float f)
|
inline int floatToIntBits(float f) {
|
||||||
{
|
union {
|
||||||
union
|
|
||||||
{
|
|
||||||
float f;
|
float f;
|
||||||
int i ;
|
int i ;
|
||||||
} u;
|
} u;
|
||||||
@ -44,8 +38,7 @@ namespace Limonp
|
|||||||
return u.i;
|
return u.i;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline short floatToShortBits(float f)
|
inline short floatToShortBits(float f) {
|
||||||
{
|
|
||||||
int fi = floatToIntBits(f);
|
int fi = floatToIntBits(f);
|
||||||
|
|
||||||
// 提取关键信息
|
// 提取关键信息
|
||||||
@ -67,8 +60,7 @@ namespace Limonp
|
|||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline float shortBitsToFloat(short s)
|
inline float shortBitsToFloat(short s) {
|
||||||
{
|
|
||||||
/*
|
/*
|
||||||
* 指数空余3位:若符号位为1,补0;若符号位为0,补1。 尾数位在后补0(13个)
|
* 指数空余3位:若符号位为1,补0;若符号位为0,补1。 尾数位在后补0(13个)
|
||||||
*/
|
*/
|
||||||
|
36
src/Limonp/Colors.hpp
Normal file
36
src/Limonp/Colors.hpp
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#ifndef LIMONP_COLOR_PRINT_HPP
|
||||||
|
#define LIMONP_COLOR_PRINT_HPP
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
namespace Limonp {
|
||||||
|
using std::string;
|
||||||
|
|
||||||
|
enum COLOR {
|
||||||
|
BLACK = 30,
|
||||||
|
RED,
|
||||||
|
GREEN,
|
||||||
|
YELLOW,
|
||||||
|
BLUE,
|
||||||
|
PURPLE
|
||||||
|
};
|
||||||
|
|
||||||
|
static void ColorPrintln(enum COLOR color, const char * fmt, ...) {
|
||||||
|
va_list ap;
|
||||||
|
printf("\033[0;%dm", color);
|
||||||
|
va_start(ap, fmt);
|
||||||
|
vprintf(fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
printf("\033[0m\n"); // if not \n , in some situation , the next lines will be set the same color unexpectedly
|
||||||
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
static void ColorPrint(const string& str, enum COLOR color = GREEN) {
|
||||||
|
printf("\033[0;%dm%s\033[0m", color, str.c_str());
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} // namespace Limonp
|
||||||
|
|
||||||
|
#endif
|
@ -7,34 +7,27 @@
|
|||||||
|
|
||||||
#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)
|
||||||
: mutex_(mutex)
|
: mutex_(mutex) {
|
||||||
{
|
|
||||||
LIMONP_CHECK(!pthread_cond_init(&pcond_, NULL));
|
LIMONP_CHECK(!pthread_cond_init(&pcond_, NULL));
|
||||||
}
|
}
|
||||||
|
|
||||||
~Condition()
|
~Condition() {
|
||||||
{
|
|
||||||
LIMONP_CHECK(!pthread_cond_destroy(&pcond_));
|
LIMONP_CHECK(!pthread_cond_destroy(&pcond_));
|
||||||
}
|
}
|
||||||
|
|
||||||
void wait()
|
void wait() {
|
||||||
{
|
|
||||||
LIMONP_CHECK(!pthread_cond_wait(&pcond_, mutex_.getPthreadMutex()));
|
LIMONP_CHECK(!pthread_cond_wait(&pcond_, mutex_.getPthreadMutex()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void notify()
|
void notify() {
|
||||||
{
|
|
||||||
LIMONP_CHECK(!pthread_cond_signal(&pcond_));
|
LIMONP_CHECK(!pthread_cond_signal(&pcond_));
|
||||||
}
|
}
|
||||||
|
|
||||||
void notifyAll()
|
void notifyAll() {
|
||||||
{
|
|
||||||
LIMONP_CHECK(!pthread_cond_broadcast(&pcond_));
|
LIMONP_CHECK(!pthread_cond_broadcast(&pcond_));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,40 +12,32 @@
|
|||||||
#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:
|
||||||
explicit Config(const string& filePath)
|
explicit Config(const string& filePath) {
|
||||||
{
|
|
||||||
loadFile_(filePath);
|
loadFile_(filePath);
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
operator bool ()
|
operator bool () {
|
||||||
{
|
|
||||||
return !map_.empty();
|
return !map_.empty();
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
void loadFile_(const string& filePath)
|
void loadFile_(const string& filePath) {
|
||||||
{
|
|
||||||
ifstream ifs(filePath.c_str());
|
ifstream ifs(filePath.c_str());
|
||||||
assert(ifs);
|
assert(ifs);
|
||||||
string line;
|
string line;
|
||||||
vector<string> vecBuf;
|
vector<string> vecBuf;
|
||||||
size_t lineno = 0;
|
size_t lineno = 0;
|
||||||
while(getline(ifs, line))
|
while(getline(ifs, line)) {
|
||||||
{
|
|
||||||
lineno ++;
|
lineno ++;
|
||||||
trim(line);
|
trim(line);
|
||||||
if(line.empty() || startsWith(line, "#"))
|
if(line.empty() || startsWith(line, "#")) {
|
||||||
{
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
vecBuf.clear();
|
vecBuf.clear();
|
||||||
if(!split(line, vecBuf, "=") || 2 != vecBuf.size())
|
if(!split(line, vecBuf, "=") || 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;
|
||||||
@ -54,8 +46,7 @@ namespace Limonp
|
|||||||
string& value = vecBuf[1];
|
string& value = vecBuf[1];
|
||||||
trim(key);
|
trim(key);
|
||||||
trim(value);
|
trim(value);
|
||||||
if(!map_.insert(make_pair(key, value)).second)
|
if(!map_.insert(make_pair(key, value)).second) {
|
||||||
{
|
|
||||||
fprintf(stderr, "key[%s] already exits.\n", key.c_str());
|
fprintf(stderr, "key[%s] already exits.\n", key.c_str());
|
||||||
assert(false);
|
assert(false);
|
||||||
continue;
|
continue;
|
||||||
@ -64,18 +55,15 @@ namespace Limonp
|
|||||||
ifs.close();
|
ifs.close();
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
bool get(const string& key, string& value) const
|
bool get(const string& key, string& value) const {
|
||||||
{
|
|
||||||
map<string, string>::const_iterator it = map_.find(key);
|
map<string, string>::const_iterator it = map_.find(key);
|
||||||
if(map_.end() != it)
|
if(map_.end() != it) {
|
||||||
{
|
|
||||||
value = it->second;
|
value = it->second;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
bool get(const string& key, int & value) const
|
bool get(const string& key, int & value) const {
|
||||||
{
|
|
||||||
string str;
|
string str;
|
||||||
if(!get(key, str)) {
|
if(!get(key, str)) {
|
||||||
return false;
|
return false;
|
||||||
@ -83,22 +71,18 @@ namespace Limonp
|
|||||||
value = atoi(str.c_str());
|
value = atoi(str.c_str());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
const char* operator [] (const char* key) const
|
const char* operator [] (const char* key) const {
|
||||||
{
|
if(NULL == key) {
|
||||||
if(NULL == key)
|
|
||||||
{
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
map<string, string>::const_iterator it = map_.find(key);
|
map<string, string>::const_iterator it = map_.find(key);
|
||||||
if(map_.end() != it)
|
if(map_.end() != it) {
|
||||||
{
|
|
||||||
return it->second.c_str();
|
return it->second.c_str();
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
string getConfigInfo() const
|
string getConfigInfo() const {
|
||||||
{
|
|
||||||
string res;
|
string res;
|
||||||
res << *this;
|
res << *this;
|
||||||
return res;
|
return res;
|
||||||
@ -109,8 +93,7 @@ namespace Limonp
|
|||||||
friend ostream& operator << (ostream& os, const Config& config);
|
friend ostream& operator << (ostream& os, const Config& config);
|
||||||
};
|
};
|
||||||
|
|
||||||
inline ostream& operator << (ostream& os, const Config& config)
|
inline ostream& operator << (ostream& os, const Config& config) {
|
||||||
{
|
|
||||||
return os << config.map_;
|
return os << config.map_;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,23 @@
|
|||||||
#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) {};
|
||||||
~InitOnOff() {};
|
~InitOnOff() {};
|
||||||
protected:
|
protected:
|
||||||
bool isInited_;
|
bool isInited_;
|
||||||
bool getInitFlag_()const{return isInited_;};
|
bool getInitFlag_()const {
|
||||||
bool setInitFlag_(bool flag){return isInited_ = flag;};
|
return isInited_;
|
||||||
|
};
|
||||||
|
bool setInitFlag_(bool flag) {
|
||||||
|
return isInited_ = flag;
|
||||||
|
};
|
||||||
public:
|
public:
|
||||||
operator bool() const {return getInitFlag_();};
|
operator bool() const {
|
||||||
|
return getInitFlag_();
|
||||||
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -6,8 +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..
|
||||||
@ -15,8 +14,7 @@ namespace Limonp
|
|||||||
*/
|
*/
|
||||||
const size_t LOCAL_VECTOR_BUFFER_SIZE = 16;
|
const size_t LOCAL_VECTOR_BUFFER_SIZE = 16;
|
||||||
template <class T>
|
template <class T>
|
||||||
class LocalVector
|
class LocalVector {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
typedef const T* const_iterator ;
|
typedef const T* const_iterator ;
|
||||||
typedef T value_type;
|
typedef T value_type;
|
||||||
@ -27,51 +25,39 @@ namespace Limonp
|
|||||||
size_t size_;
|
size_t size_;
|
||||||
size_t capacity_;
|
size_t capacity_;
|
||||||
public:
|
public:
|
||||||
LocalVector()
|
LocalVector() {
|
||||||
{
|
|
||||||
init_();
|
init_();
|
||||||
};
|
};
|
||||||
LocalVector(const LocalVector<T>& vec)
|
LocalVector(const LocalVector<T>& vec) {
|
||||||
{
|
|
||||||
init_();
|
init_();
|
||||||
*this = vec;
|
*this = vec;
|
||||||
}
|
}
|
||||||
LocalVector(const_iterator begin, const_iterator end) // TODO: make it faster
|
LocalVector(const_iterator begin, const_iterator end) { // TODO: make it faster
|
||||||
{
|
|
||||||
init_();
|
init_();
|
||||||
while(begin != end)
|
while(begin != end) {
|
||||||
{
|
|
||||||
push_back(*begin++);
|
push_back(*begin++);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
LocalVector(size_t size, const T& t) // TODO: make it faster
|
LocalVector(size_t size, const T& t) { // TODO: make it faster
|
||||||
{
|
|
||||||
init_();
|
init_();
|
||||||
while(size--)
|
while(size--) {
|
||||||
{
|
|
||||||
push_back(t);
|
push_back(t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
~LocalVector()
|
~LocalVector() {
|
||||||
{
|
if(ptr_ != buffer_) {
|
||||||
if(ptr_ != buffer_)
|
|
||||||
{
|
|
||||||
free(ptr_);
|
free(ptr_);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
public:
|
public:
|
||||||
LocalVector<T>& operator = (const LocalVector<T>& vec)
|
LocalVector<T>& operator = (const LocalVector<T>& vec) {
|
||||||
{
|
|
||||||
clear();
|
clear();
|
||||||
size_ = vec.size();
|
size_ = vec.size();
|
||||||
capacity_ = vec.capacity();
|
capacity_ = vec.capacity();
|
||||||
if(vec.buffer_ == vec.ptr_)
|
if(vec.buffer_ == vec.ptr_) {
|
||||||
{
|
|
||||||
memcpy(buffer_, vec.buffer_, sizeof(T) * size_);
|
memcpy(buffer_, vec.buffer_, sizeof(T) * size_);
|
||||||
ptr_ = buffer_;
|
ptr_ = buffer_;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
ptr_ = (T*) malloc(vec.capacity() * sizeof(T));
|
ptr_ = (T*) malloc(vec.capacity() * sizeof(T));
|
||||||
assert(ptr_);
|
assert(ptr_);
|
||||||
memcpy(ptr_, vec.ptr_, vec.size() * sizeof(T));
|
memcpy(ptr_, vec.ptr_, vec.size() * sizeof(T));
|
||||||
@ -79,34 +65,27 @@ namespace Limonp
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
void init_()
|
void init_() {
|
||||||
{
|
|
||||||
ptr_ = buffer_;
|
ptr_ = buffer_;
|
||||||
size_ = 0;
|
size_ = 0;
|
||||||
capacity_ = LOCAL_VECTOR_BUFFER_SIZE;
|
capacity_ = LOCAL_VECTOR_BUFFER_SIZE;
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
T& operator [] (size_t i)
|
T& operator [] (size_t i) {
|
||||||
{
|
|
||||||
return ptr_[i];
|
return ptr_[i];
|
||||||
}
|
}
|
||||||
const T& operator [] (size_t i) const
|
const T& operator [] (size_t i) const {
|
||||||
{
|
|
||||||
return ptr_[i];
|
return ptr_[i];
|
||||||
}
|
}
|
||||||
void push_back(const T& t)
|
void push_back(const T& t) {
|
||||||
{
|
if(size_ == capacity_) {
|
||||||
if(size_ == capacity_)
|
|
||||||
{
|
|
||||||
assert(capacity_);
|
assert(capacity_);
|
||||||
reserve(capacity_ * 2);
|
reserve(capacity_ * 2);
|
||||||
}
|
}
|
||||||
ptr_[size_ ++ ] = t;
|
ptr_[size_ ++ ] = t;
|
||||||
}
|
}
|
||||||
void reserve(size_t size)
|
void reserve(size_t size) {
|
||||||
{
|
if(size <= capacity_) {
|
||||||
if(size <= capacity_)
|
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
T * next = (T*)malloc(sizeof(T) * size);
|
T * next = (T*)malloc(sizeof(T) * size);
|
||||||
@ -115,35 +94,27 @@ namespace Limonp
|
|||||||
ptr_ = next;
|
ptr_ = next;
|
||||||
memcpy(ptr_, old, sizeof(T) * capacity_);
|
memcpy(ptr_, old, sizeof(T) * capacity_);
|
||||||
capacity_ = size;
|
capacity_ = size;
|
||||||
if(old != buffer_)
|
if(old != buffer_) {
|
||||||
{
|
|
||||||
free(old);
|
free(old);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool empty() const
|
bool empty() const {
|
||||||
{
|
|
||||||
return 0 == size();
|
return 0 == size();
|
||||||
}
|
}
|
||||||
size_t size() const
|
size_t size() const {
|
||||||
{
|
|
||||||
return size_;
|
return size_;
|
||||||
}
|
}
|
||||||
size_t capacity() const
|
size_t capacity() const {
|
||||||
{
|
|
||||||
return capacity_;
|
return capacity_;
|
||||||
}
|
}
|
||||||
const_iterator begin() const
|
const_iterator begin() const {
|
||||||
{
|
|
||||||
return ptr_;
|
return ptr_;
|
||||||
}
|
}
|
||||||
const_iterator end() const
|
const_iterator end() const {
|
||||||
{
|
|
||||||
return ptr_ + size_;
|
return ptr_ + size_;
|
||||||
}
|
}
|
||||||
void clear()
|
void clear() {
|
||||||
{
|
if(ptr_ != buffer_) {
|
||||||
if(ptr_ != buffer_)
|
|
||||||
{
|
|
||||||
free(ptr_);
|
free(ptr_);
|
||||||
}
|
}
|
||||||
init_();
|
init_();
|
||||||
@ -151,15 +122,12 @@ namespace Limonp
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
ostream & operator << (ostream& os, const LocalVector<T>& vec)
|
ostream & operator << (ostream& os, const LocalVector<T>& vec) {
|
||||||
{
|
if(vec.empty()) {
|
||||||
if(vec.empty())
|
|
||||||
{
|
|
||||||
return os << "[]";
|
return os << "[]";
|
||||||
}
|
}
|
||||||
os<<"[\""<<vec[0];
|
os<<"[\""<<vec[0];
|
||||||
for(size_t i = 1; i < vec.size(); i++)
|
for(size_t i = 1; i < vec.size(); i++) {
|
||||||
{
|
|
||||||
os<<"\", \""<<vec[i];
|
os<<"\", \""<<vec[i];
|
||||||
}
|
}
|
||||||
os<<"\"]";
|
os<<"\"]";
|
||||||
|
@ -24,19 +24,16 @@
|
|||||||
#define LogError(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_ERROR, 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__)
|
#define LogFatal(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_FATAL, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__)
|
||||||
|
|
||||||
namespace Limonp
|
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"};
|
||||||
static const char * LOG_FORMAT = "%s %s:%d %s %s\n";
|
static const char * LOG_FORMAT = "%s %s:%d %s %s\n";
|
||||||
static const char * LOG_TIME_FORMAT = "%Y-%m-%d %H:%M:%S";
|
static const char * LOG_TIME_FORMAT = "%Y-%m-%d %H:%M:%S";
|
||||||
|
|
||||||
class Logger
|
class Logger {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
static void Logging(size_t level, const string& msg, const char* fileName, int lineno)
|
static void Logging(size_t level, const string& msg, const char* fileName, int lineno) {
|
||||||
{
|
|
||||||
assert(level <= LL_FATAL);
|
assert(level <= LL_FATAL);
|
||||||
char buf[CSTR_BUFFER_SIZE];
|
char buf[CSTR_BUFFER_SIZE];
|
||||||
time_t timeNow;
|
time_t timeNow;
|
||||||
@ -44,8 +41,7 @@ namespace Limonp
|
|||||||
strftime(buf, sizeof(buf), LOG_TIME_FORMAT, localtime(&timeNow));
|
strftime(buf, sizeof(buf), LOG_TIME_FORMAT, localtime(&timeNow));
|
||||||
fprintf(stderr, LOG_FORMAT, buf, fileName, lineno,LOG_LEVEL_ARRAY[level], msg.c_str());
|
fprintf(stderr, LOG_FORMAT, buf, fileName, lineno,LOG_LEVEL_ARRAY[level], msg.c_str());
|
||||||
}
|
}
|
||||||
static void LoggingF(size_t level, const char* fileName, int lineno, const char* const fmt, ...)
|
static void LoggingF(size_t level, const char* fileName, int lineno, const char* const fmt, ...) {
|
||||||
{
|
|
||||||
#ifdef LOGGER_LEVEL
|
#ifdef LOGGER_LEVEL
|
||||||
if(level < LOGGER_LEVEL) return;
|
if(level < LOGGER_LEVEL) return;
|
||||||
#endif
|
#endif
|
||||||
|
@ -31,8 +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.
|
||||||
@ -106,8 +105,7 @@ static unsigned char PADDING[64] = {
|
|||||||
};
|
};
|
||||||
// convenient object that wraps
|
// convenient object that wraps
|
||||||
// the C-functions for use in C++ only
|
// the C-functions for use in C++ only
|
||||||
class MD5
|
class MD5 {
|
||||||
{
|
|
||||||
private:
|
private:
|
||||||
struct __context_t {
|
struct __context_t {
|
||||||
UINT4 state[4]; /* state (ABCD) */
|
UINT4 state[4]; /* state (ABCD) */
|
||||||
@ -118,8 +116,7 @@ private:
|
|||||||
//#pragma region static helper functions
|
//#pragma region static helper functions
|
||||||
// The core of the MD5 algorithm is here.
|
// The core of the MD5 algorithm is here.
|
||||||
// MD5 basic transformation. Transforms state based on block.
|
// MD5 basic transformation. Transforms state based on block.
|
||||||
static void MD5Transform( UINT4 state[4], unsigned char block[64] )
|
static void MD5Transform( UINT4 state[4], unsigned char block[64] ) {
|
||||||
{
|
|
||||||
UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
|
UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
|
||||||
|
|
||||||
Decode (x, block, 64);
|
Decode (x, block, 64);
|
||||||
@ -207,8 +204,7 @@ private:
|
|||||||
|
|
||||||
// Encodes input (UINT4) into output (unsigned char). Assumes len is
|
// Encodes input (UINT4) into output (unsigned char). Assumes len is
|
||||||
// a multiple of 4.
|
// a multiple of 4.
|
||||||
static void Encode( unsigned char *output, UINT4 *input, unsigned int len )
|
static void Encode( unsigned char *output, UINT4 *input, unsigned int len ) {
|
||||||
{
|
|
||||||
unsigned int i, j;
|
unsigned int i, j;
|
||||||
|
|
||||||
for (i = 0, j = 0; j < len; i++, j += 4) {
|
for (i = 0, j = 0; j < len; i++, j += 4) {
|
||||||
@ -221,8 +217,7 @@ private:
|
|||||||
|
|
||||||
// Decodes input (unsigned char) into output (UINT4). Assumes len is
|
// Decodes input (unsigned char) into output (UINT4). Assumes len is
|
||||||
// a multiple of 4.
|
// a multiple of 4.
|
||||||
static void Decode( UINT4 *output, unsigned char *input, unsigned int len )
|
static void Decode( UINT4 *output, unsigned char *input, unsigned int len ) {
|
||||||
{
|
|
||||||
unsigned int i, j;
|
unsigned int i, j;
|
||||||
|
|
||||||
for (i = 0, j = 0; j < len; i++, j += 4)
|
for (i = 0, j = 0; j < len; i++, j += 4)
|
||||||
@ -234,14 +229,12 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// MAIN FUNCTIONS
|
// MAIN FUNCTIONS
|
||||||
MD5()
|
MD5() {
|
||||||
{
|
|
||||||
Init() ;
|
Init() ;
|
||||||
}
|
}
|
||||||
|
|
||||||
// MD5 initialization. Begins an MD5 operation, writing a new context.
|
// MD5 initialization. Begins an MD5 operation, writing a new context.
|
||||||
void Init()
|
void Init() {
|
||||||
{
|
|
||||||
context.count[0] = context.count[1] = 0;
|
context.count[0] = context.count[1] = 0;
|
||||||
|
|
||||||
// Load magic initialization constants.
|
// Load magic initialization constants.
|
||||||
@ -256,8 +249,7 @@ public:
|
|||||||
// context.
|
// context.
|
||||||
void Update(
|
void Update(
|
||||||
unsigned char *input, // input block
|
unsigned char *input, // input block
|
||||||
unsigned int inputLen ) // length of input block
|
unsigned int inputLen ) { // length of input block
|
||||||
{
|
|
||||||
unsigned int i, index, partLen;
|
unsigned int i, index, partLen;
|
||||||
|
|
||||||
// Compute number of bytes mod 64
|
// Compute number of bytes mod 64
|
||||||
@ -280,8 +272,7 @@ public:
|
|||||||
MD5Transform (context.state, &input[i]);
|
MD5Transform (context.state, &input[i]);
|
||||||
|
|
||||||
index = 0;
|
index = 0;
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
/* Buffer remaining input */
|
/* Buffer remaining input */
|
||||||
@ -291,8 +282,7 @@ public:
|
|||||||
// MD5 finalization. Ends an MD5 message-digest operation, writing the
|
// MD5 finalization. Ends an MD5 message-digest operation, writing the
|
||||||
// the message digest and zeroizing the context.
|
// the message digest and zeroizing the context.
|
||||||
// Writes to digestRaw
|
// Writes to digestRaw
|
||||||
void Final()
|
void Final() {
|
||||||
{
|
|
||||||
unsigned char bits[8];
|
unsigned char bits[8];
|
||||||
unsigned int index, padLen;
|
unsigned int index, padLen;
|
||||||
|
|
||||||
@ -317,8 +307,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Buffer must be 32+1 (nul) = 33 chars long at least
|
/// Buffer must be 32+1 (nul) = 33 chars long at least
|
||||||
void writeToString()
|
void writeToString() {
|
||||||
{
|
|
||||||
int pos ;
|
int pos ;
|
||||||
|
|
||||||
for( pos = 0 ; pos < 16 ; pos++ )
|
for( pos = 0 ; pos < 16 ; pos++ )
|
||||||
@ -336,8 +325,7 @@ public:
|
|||||||
|
|
||||||
/// Load a file from disk and digest it
|
/// Load a file from disk and digest it
|
||||||
// Digests a file and returns the result.
|
// Digests a file and returns the result.
|
||||||
const char* digestFile( const char *filename )
|
const char* digestFile( const char *filename ) {
|
||||||
{
|
|
||||||
if (NULL == filename || strcmp(filename, "") == 0)
|
if (NULL == filename || strcmp(filename, "") == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@ -347,8 +335,7 @@ public:
|
|||||||
|
|
||||||
unsigned char buffer[1024] ;
|
unsigned char buffer[1024] ;
|
||||||
|
|
||||||
if((file = fopen (filename, "rb")) == NULL)
|
if((file = fopen (filename, "rb")) == NULL) {
|
||||||
{
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
int len;
|
int len;
|
||||||
@ -362,8 +349,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Digests a byte-array already in memory
|
/// Digests a byte-array already in memory
|
||||||
const char* digestMemory( BYTE *memchunk, int len )
|
const char* digestMemory( BYTE *memchunk, int len ) {
|
||||||
{
|
|
||||||
if (NULL == memchunk)
|
if (NULL == memchunk)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@ -375,8 +361,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Digests a string and prints the result.
|
// Digests a string and prints the result.
|
||||||
const char* digestString(const char *string )
|
const char* digestString(const char *string ) {
|
||||||
{
|
|
||||||
if (string == NULL)
|
if (string == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@ -388,18 +373,15 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
inline bool md5String(const char* str, std::string& res)
|
inline bool md5String(const char* str, std::string& res) {
|
||||||
{
|
if (NULL == str) {
|
||||||
if (NULL == str)
|
|
||||||
{
|
|
||||||
res = "";
|
res = "";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
MD5 md5;
|
MD5 md5;
|
||||||
const char *pRes = md5.digestString(str);
|
const char *pRes = md5.digestString(str);
|
||||||
if (NULL == pRes)
|
if (NULL == pRes) {
|
||||||
{
|
|
||||||
res = "";
|
res = "";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -408,10 +390,8 @@ inline bool md5String(const char* str, std::string& res)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool md5File(const char* filepath, std::string& res)
|
inline bool md5File(const char* filepath, std::string& res) {
|
||||||
{
|
if (NULL == filepath || strcmp(filepath, "") == 0) {
|
||||||
if (NULL == filepath || strcmp(filepath, "") == 0)
|
|
||||||
{
|
|
||||||
res = "";
|
res = "";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -419,8 +399,7 @@ inline bool md5File(const char* filepath, std::string& res)
|
|||||||
MD5 md5;
|
MD5 md5;
|
||||||
const char *pRes = md5.digestFile(filepath);
|
const char *pRes = md5.digestFile(filepath);
|
||||||
|
|
||||||
if (NULL == pRes)
|
if (NULL == pRes) {
|
||||||
{
|
|
||||||
res = "";
|
res = "";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -5,47 +5,37 @@
|
|||||||
#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_;
|
||||||
public:
|
public:
|
||||||
pthread_mutex_t* getPthreadMutex()
|
pthread_mutex_t* getPthreadMutex() {
|
||||||
{
|
|
||||||
return &mutex_;
|
return &mutex_;
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
MutexLock()
|
MutexLock() {
|
||||||
{
|
|
||||||
LIMONP_CHECK(!pthread_mutex_init(&mutex_, NULL));
|
LIMONP_CHECK(!pthread_mutex_init(&mutex_, NULL));
|
||||||
}
|
}
|
||||||
~MutexLock()
|
~MutexLock() {
|
||||||
{
|
|
||||||
LIMONP_CHECK(!pthread_mutex_destroy(&mutex_));
|
LIMONP_CHECK(!pthread_mutex_destroy(&mutex_));
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
void lock()
|
void lock() {
|
||||||
{
|
|
||||||
LIMONP_CHECK(!pthread_mutex_lock(&mutex_));
|
LIMONP_CHECK(!pthread_mutex_lock(&mutex_));
|
||||||
}
|
}
|
||||||
void unlock()
|
void unlock() {
|
||||||
{
|
|
||||||
LIMONP_CHECK(!pthread_mutex_unlock(&mutex_));
|
LIMONP_CHECK(!pthread_mutex_unlock(&mutex_));
|
||||||
}
|
}
|
||||||
friend class MutexLockGuard;
|
friend class MutexLockGuard;
|
||||||
};
|
};
|
||||||
class MutexLockGuard: NonCopyable
|
class MutexLockGuard: NonCopyable {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
explicit MutexLockGuard(MutexLock & mutex)
|
explicit MutexLockGuard(MutexLock & mutex)
|
||||||
: mutex_(mutex)
|
: mutex_(mutex) {
|
||||||
{
|
|
||||||
mutex_.lock();
|
mutex_.lock();
|
||||||
}
|
}
|
||||||
~MutexLockGuard()
|
~MutexLockGuard() {
|
||||||
{
|
|
||||||
mutex_.unlock();
|
mutex_.unlock();
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
|
@ -8,11 +8,9 @@
|
|||||||
#include "Logger.hpp"
|
#include "Logger.hpp"
|
||||||
#include "InitOnOff.hpp"
|
#include "InitOnOff.hpp"
|
||||||
|
|
||||||
namespace Limonp
|
namespace Limonp {
|
||||||
{
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
class MysqlClient: public InitOnOff
|
class MysqlClient: public InitOnOff {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
typedef vector< vector<string> > RowsType;
|
typedef vector< vector<string> > RowsType;
|
||||||
private:
|
private:
|
||||||
@ -23,37 +21,30 @@ namespace Limonp
|
|||||||
const string db_;
|
const string db_;
|
||||||
const string charset_;
|
const string charset_;
|
||||||
public:
|
public:
|
||||||
MysqlClient(const string& host, size_t port, const string& user, const string& passwd, const string& db, const string& charset = "utf8"): host_(host), port_(port), user_(user), passwd_(passwd), db_(db), charset_(charset), conn_(NULL)
|
MysqlClient(const string& host, size_t port, const string& user, const string& passwd, const string& db, const string& charset = "utf8"): host_(host), port_(port), user_(user), passwd_(passwd), db_(db), charset_(charset), conn_(NULL) {
|
||||||
{
|
|
||||||
setInitFlag_(init_());
|
setInitFlag_(init_());
|
||||||
}
|
}
|
||||||
~MysqlClient()
|
~MysqlClient() {
|
||||||
{
|
if(conn_) {
|
||||||
if(conn_)
|
|
||||||
{
|
|
||||||
mysql_close(conn_);
|
mysql_close(conn_);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
private:
|
private:
|
||||||
bool init_()
|
bool init_() {
|
||||||
{
|
|
||||||
//cout<<mysql_get_client_info()<<endl;
|
//cout<<mysql_get_client_info()<<endl;
|
||||||
if(NULL == (conn_ = mysql_init(NULL)))
|
if(NULL == (conn_ = mysql_init(NULL))) {
|
||||||
{
|
|
||||||
LogError("mysql_init faield. %s", mysql_error(conn_));
|
LogError("mysql_init faield. %s", mysql_error(conn_));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mysql_real_connect(conn_, host_.c_str(), user_.c_str(), passwd_.c_str(), db_.c_str(), port_, NULL, 0) == NULL)
|
if (mysql_real_connect(conn_, host_.c_str(), user_.c_str(), passwd_.c_str(), db_.c_str(), port_, NULL, 0) == NULL) {
|
||||||
{
|
|
||||||
LogError("mysql_real_connect failed. %s", mysql_error(conn_));
|
LogError("mysql_real_connect failed. %s", mysql_error(conn_));
|
||||||
mysql_close(conn_);
|
mysql_close(conn_);
|
||||||
conn_ = NULL;
|
conn_ = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mysql_set_character_set(conn_, charset_.c_str()))
|
if(mysql_set_character_set(conn_, charset_.c_str())) {
|
||||||
{
|
|
||||||
LogError("mysql_set_character_set [%s] failed.", charset_.c_str());
|
LogError("mysql_set_character_set [%s] failed.", charset_.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -66,48 +57,39 @@ namespace Limonp
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
bool executeSql(const string& sql)
|
bool executeSql(const string& sql) {
|
||||||
{
|
|
||||||
assert(getInitFlag_());
|
assert(getInitFlag_());
|
||||||
if(mysql_query(conn_, sql.c_str()))
|
if(mysql_query(conn_, sql.c_str())) {
|
||||||
{
|
|
||||||
LogError("mysql_query failed. %s", mysql_error(conn_));
|
LogError("mysql_query failed. %s", mysql_error(conn_));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
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;
|
string sql;
|
||||||
for(size_t i = 0; i < vals.size(); i ++)
|
for(size_t i = 0; i < vals.size(); i ++) {
|
||||||
{
|
|
||||||
sql.clear();
|
sql.clear();
|
||||||
string_format(sql, "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;
|
||||||
}
|
}
|
||||||
bool select(const string& sql, RowsType& rows)
|
bool select(const string& sql, RowsType& rows) {
|
||||||
{
|
if(!executeSql(sql)) {
|
||||||
if(!executeSql(sql))
|
|
||||||
{
|
|
||||||
LogError("executeSql failed. [%s]", sql.c_str());
|
LogError("executeSql failed. [%s]", sql.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
MYSQL_RES * result = mysql_store_result(conn_);
|
MYSQL_RES * result = mysql_store_result(conn_);
|
||||||
if(!result)
|
if(!result) {
|
||||||
{
|
|
||||||
LogError("mysql_store_result failed.[%d]", mysql_error(conn_));
|
LogError("mysql_store_result failed.[%d]", mysql_error(conn_));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
size_t num_fields = mysql_num_fields(result);
|
size_t num_fields = mysql_num_fields(result);
|
||||||
MYSQL_ROW row;
|
MYSQL_ROW row;
|
||||||
while((row = mysql_fetch_row(result)))
|
while((row = mysql_fetch_row(result))) {
|
||||||
{
|
|
||||||
vector<string> vec;
|
vector<string> vec;
|
||||||
for(size_t i = 0; i < num_fields; i ++)
|
for(size_t i = 0; i < num_fields; i ++) {
|
||||||
{
|
|
||||||
row[i] ? vec.push_back(row[i]) : vec.push_back("NULL");
|
row[i] ? vec.push_back(row[i]) : vec.push_back("NULL");
|
||||||
}
|
}
|
||||||
rows.push_back(vec);
|
rows.push_back(vec);
|
||||||
|
@ -6,10 +6,8 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace Limonp
|
namespace Limonp {
|
||||||
{
|
class NonCopyable {
|
||||||
class NonCopyable
|
|
||||||
{
|
|
||||||
protected:
|
protected:
|
||||||
NonCopyable() {};
|
NonCopyable() {};
|
||||||
~NonCopyable() {};
|
~NonCopyable() {};
|
||||||
|
@ -9,8 +9,7 @@
|
|||||||
#else
|
#else
|
||||||
#include <tr1/unordered_map>
|
#include <tr1/unordered_map>
|
||||||
#include <tr1/unordered_set>
|
#include <tr1/unordered_set>
|
||||||
namespace std
|
namespace std {
|
||||||
{
|
|
||||||
using std::tr1::unordered_map;
|
using std::tr1::unordered_map;
|
||||||
using std::tr1::unordered_set;
|
using std::tr1::unordered_set;
|
||||||
}
|
}
|
||||||
@ -18,49 +17,59 @@ namespace std
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <set>
|
#include <set>
|
||||||
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <deque>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
namespace std
|
namespace std {
|
||||||
{
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ostream& operator << (ostream& os, const vector<T>& vec)
|
ostream& operator << (ostream& os, const vector<T>& v) {
|
||||||
{
|
if(v.empty()) {
|
||||||
if(vec.empty())
|
|
||||||
{
|
|
||||||
return os << "[]";
|
return os << "[]";
|
||||||
}
|
}
|
||||||
os<<"[\""<<vec[0];
|
os<<"[\""<<v[0];
|
||||||
for(size_t i = 1; i < vec.size(); i++)
|
for(size_t i = 1; i < v.size(); i++) {
|
||||||
{
|
os<<"\", \""<<v[i];
|
||||||
os<<"\", \""<<vec[i];
|
|
||||||
}
|
}
|
||||||
os<<"\"]";
|
os<<"\"]";
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
ostream& operator << (ostream& os, const deque<T>& dq) {
|
||||||
|
if(dq.empty()) {
|
||||||
|
return os << "[]";
|
||||||
|
}
|
||||||
|
os<<"[\""<<dq[0];
|
||||||
|
for(size_t i = 1; i < dq.size(); i++) {
|
||||||
|
os<<"\", \""<<dq[i];
|
||||||
|
}
|
||||||
|
os<<"\"]";
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class T1, class T2>
|
template<class T1, class T2>
|
||||||
ostream& operator << (ostream& os, const pair<T1, T2>& pr)
|
ostream& operator << (ostream& os, const pair<T1, T2>& pr) {
|
||||||
{
|
|
||||||
os << pr.first << ":" << pr.second ;
|
os << pr.first << ":" << pr.second ;
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
string& operator << (string& str, const T& obj)
|
string& operator << (string& str, const T& obj) {
|
||||||
{
|
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
ss << obj; // call ostream& operator << (ostream& os,
|
ss << obj; // call ostream& operator << (ostream& os,
|
||||||
return str = ss.str();
|
return str = ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T1, class T2>
|
template<class T1, class T2>
|
||||||
ostream& operator << (ostream& os, const map<T1, T2>& mp)
|
ostream& operator << (ostream& os, const map<T1, T2>& mp) {
|
||||||
{
|
if(mp.empty()) {
|
||||||
if(mp.empty())
|
|
||||||
{
|
|
||||||
os<<"{}";
|
os<<"{}";
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
@ -68,8 +77,7 @@ namespace std
|
|||||||
typename map<T1, T2>::const_iterator it = mp.begin();
|
typename map<T1, T2>::const_iterator it = mp.begin();
|
||||||
os<<*it;
|
os<<*it;
|
||||||
it++;
|
it++;
|
||||||
while(it != mp.end())
|
while(it != mp.end()) {
|
||||||
{
|
|
||||||
os<<", "<<*it;
|
os<<", "<<*it;
|
||||||
it++;
|
it++;
|
||||||
}
|
}
|
||||||
@ -77,28 +85,23 @@ namespace std
|
|||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
template<class T1, class T2>
|
template<class T1, class T2>
|
||||||
ostream& operator << (ostream& os, const std::unordered_map<T1, T2>& mp)
|
ostream& operator << (ostream& os, const std::unordered_map<T1, T2>& mp) {
|
||||||
{
|
if(mp.empty()) {
|
||||||
if(mp.empty())
|
|
||||||
{
|
|
||||||
return os << "{}";
|
return os << "{}";
|
||||||
}
|
}
|
||||||
os<<'{';
|
os<<'{';
|
||||||
typename std::unordered_map<T1, T2>::const_iterator it = mp.begin();
|
typename std::unordered_map<T1, T2>::const_iterator it = mp.begin();
|
||||||
os<<*it;
|
os<<*it;
|
||||||
it++;
|
it++;
|
||||||
while(it != mp.end())
|
while(it != mp.end()) {
|
||||||
{
|
|
||||||
os<<", "<<*it++;
|
os<<", "<<*it++;
|
||||||
}
|
}
|
||||||
return os<<'}';
|
return os<<'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
ostream& operator << (ostream& os, const set<T>& st)
|
ostream& operator << (ostream& os, const set<T>& st) {
|
||||||
{
|
if(st.empty()) {
|
||||||
if(st.empty())
|
|
||||||
{
|
|
||||||
os << "{}";
|
os << "{}";
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
@ -106,8 +109,7 @@ namespace std
|
|||||||
typename set<T>::const_iterator it = st.begin();
|
typename set<T>::const_iterator it = st.begin();
|
||||||
os<<*it;
|
os<<*it;
|
||||||
it++;
|
it++;
|
||||||
while(it != st.end())
|
while(it != st.end()) {
|
||||||
{
|
|
||||||
os<<", "<<*it;
|
os<<", "<<*it;
|
||||||
it++;
|
it++;
|
||||||
}
|
}
|
||||||
@ -116,24 +118,22 @@ namespace std
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<class KeyType, class ContainType>
|
template<class KeyType, class ContainType>
|
||||||
bool isIn(const ContainType& contain, const KeyType& key)
|
bool isIn(const ContainType& contain, const KeyType& key) {
|
||||||
{
|
|
||||||
return contain.end() != contain.find(key);
|
return contain.end() != contain.find(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
basic_string<T> & operator << (basic_string<T> & s, ifstream & ifs)
|
basic_string<T> & operator << (basic_string<T> & s, ifstream & ifs) {
|
||||||
{
|
|
||||||
return s.assign((istreambuf_iterator<T>(ifs)), istreambuf_iterator<T>());
|
return s.assign((istreambuf_iterator<T>(ifs)), istreambuf_iterator<T>());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
ofstream & operator << (ofstream & ofs, const basic_string<T>& s)
|
ofstream & operator << (ofstream & ofs, const basic_string<T>& s) {
|
||||||
{
|
|
||||||
ostreambuf_iterator<T> itr (ofs);
|
ostreambuf_iterator<T> itr (ofs);
|
||||||
copy(s.begin(), s.end(), itr);
|
copy(s.begin(), s.end(), itr);
|
||||||
return ofs;
|
return ofs;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
} // namespace std
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -23,11 +23,9 @@
|
|||||||
#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;
|
||||||
std::string str;
|
std::string str;
|
||||||
va_list ap;
|
va_list ap;
|
||||||
@ -49,17 +47,14 @@ namespace Limonp
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
void join(T begin, T end, string& res, const string& connector)
|
void join(T begin, T end, string& res, const string& connector) {
|
||||||
{
|
if(begin == end) {
|
||||||
if(begin == end)
|
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
ss<<*begin;
|
ss<<*begin;
|
||||||
begin++;
|
begin++;
|
||||||
while(begin != end)
|
while(begin != end) {
|
||||||
{
|
|
||||||
ss << connector << *begin;
|
ss << connector << *begin;
|
||||||
begin ++;
|
begin ++;
|
||||||
}
|
}
|
||||||
@ -67,8 +62,7 @@ namespace Limonp
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
string join(T begin, T end, const string& connector)
|
string join(T begin, T end, const string& connector) {
|
||||||
{
|
|
||||||
string res;
|
string res;
|
||||||
join(begin ,end, res, connector);
|
join(begin ,end, res, connector);
|
||||||
return res;
|
return res;
|
||||||
@ -76,10 +70,8 @@ namespace Limonp
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
inline bool split(const string& src, vector<string>& res, const string& pattern, size_t offset = 0, size_t len = string::npos)
|
inline bool split(const string& src, vector<string>& res, const string& pattern, size_t offset = 0, size_t len = string::npos) {
|
||||||
{
|
if(src.empty()) {
|
||||||
if(src.empty())
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
res.clear();
|
res.clear();
|
||||||
@ -87,13 +79,10 @@ namespace Limonp
|
|||||||
size_t start = 0;
|
size_t start = 0;
|
||||||
size_t end = 0;
|
size_t end = 0;
|
||||||
size_t cnt = 0;
|
size_t cnt = 0;
|
||||||
while(start < src.size() && res.size() < len)
|
while(start < src.size() && res.size() < len) {
|
||||||
{
|
|
||||||
end = src.find_first_of(pattern, start);
|
end = src.find_first_of(pattern, start);
|
||||||
if(string::npos == end)
|
if(string::npos == end) {
|
||||||
{
|
if(cnt >= offset) {
|
||||||
if(cnt >= offset)
|
|
||||||
{
|
|
||||||
res.push_back(src.substr(start));
|
res.push_back(src.substr(start));
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -103,8 +92,7 @@ namespace Limonp
|
|||||||
// res.push_back("");
|
// res.push_back("");
|
||||||
// return true;
|
// return true;
|
||||||
//}
|
//}
|
||||||
if(cnt >= offset)
|
if(cnt >= offset) {
|
||||||
{
|
|
||||||
res.push_back(src.substr(start, end - start));
|
res.push_back(src.substr(start, end - start));
|
||||||
}
|
}
|
||||||
cnt ++;
|
cnt ++;
|
||||||
@ -113,149 +101,116 @@ namespace Limonp
|
|||||||
return true;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline string& lower(string& str)
|
inline string& lower(string& str) {
|
||||||
{
|
|
||||||
transform(str.begin(), str.end(), str.begin(), (int (*)(int))tolower);
|
transform(str.begin(), str.end(), str.begin(), (int (*)(int))tolower);
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::string <rim(std::string &s)
|
inline std::string <rim(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 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;
|
||||||
}
|
}
|
||||||
return 0 == str.compare(0, prefix.length(), prefix);
|
return 0 == str.compare(0, prefix.length(), prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool endsWith(const string& str, const string& suffix)
|
inline bool endsWith(const string& str, const string& suffix) {
|
||||||
{
|
if(suffix.length() > str.length()) {
|
||||||
if(suffix.length() > str.length())
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return 0 == str.compare(str.length() - suffix.length(), suffix.length(), suffix);
|
return 0 == str.compare(str.length() - suffix.length(), suffix.length(), suffix);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool isInStr(const string& str, char ch)
|
inline bool isInStr(const string& str, char ch) {
|
||||||
{
|
|
||||||
return str.find(ch) != string::npos;
|
return str.find(ch) != string::npos;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline uint16_t twocharToUint16(char high, char low)
|
inline uint16_t twocharToUint16(char high, char low) {
|
||||||
{
|
|
||||||
return (((uint16_t(high) & 0x00ff ) << 8) | (uint16_t(low) & 0x00ff));
|
return (((uint16_t(high) & 0x00ff ) << 8) | (uint16_t(low) & 0x00ff));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Uint16Container>
|
template <class Uint16Container>
|
||||||
bool utf8ToUnicode(const char * const str, size_t len, Uint16Container& vec)
|
bool utf8ToUnicode(const char * const str, size_t len, Uint16Container& vec) {
|
||||||
{
|
if(!str) {
|
||||||
if(!str)
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
char ch1, ch2;
|
char ch1, ch2;
|
||||||
uint16_t tmp;
|
uint16_t tmp;
|
||||||
vec.clear();
|
vec.clear();
|
||||||
for(size_t i = 0;i < len;)
|
for(size_t i = 0; i < len;) {
|
||||||
{
|
if(!(str[i] & 0x80)) { // 0xxxxxxx
|
||||||
if(!(str[i] & 0x80)) // 0xxxxxxx
|
|
||||||
{
|
|
||||||
vec.push_back(str[i]);
|
vec.push_back(str[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
} else if ((uint8_t)str[i] <= 0xdf && i + 1 < len) { // 110xxxxxx
|
||||||
else if ((uint8_t)str[i] <= 0xdf && i + 1 < len) // 110xxxxxx
|
|
||||||
{
|
|
||||||
ch1 = (str[i] >> 2) & 0x07;
|
ch1 = (str[i] >> 2) & 0x07;
|
||||||
ch2 = (str[i+1] & 0x3f) | ((str[i] & 0x03) << 6 );
|
ch2 = (str[i+1] & 0x3f) | ((str[i] & 0x03) << 6 );
|
||||||
tmp = (((uint16_t(ch1) & 0x00ff ) << 8) | (uint16_t(ch2) & 0x00ff));
|
tmp = (((uint16_t(ch1) & 0x00ff ) << 8) | (uint16_t(ch2) & 0x00ff));
|
||||||
vec.push_back(tmp);
|
vec.push_back(tmp);
|
||||||
i += 2;
|
i += 2;
|
||||||
}
|
} else if((uint8_t)str[i] <= 0xef && i + 2 < len) {
|
||||||
else if((uint8_t)str[i] <= 0xef && i + 2 < len)
|
|
||||||
{
|
|
||||||
ch1 = ((uint8_t)str[i] << 4) | ((str[i+1] >> 2) & 0x0f );
|
ch1 = ((uint8_t)str[i] << 4) | ((str[i+1] >> 2) & 0x0f );
|
||||||
ch2 = (((uint8_t)str[i+1]<<6) & 0xc0) | (str[i+2] & 0x3f);
|
ch2 = (((uint8_t)str[i+1]<<6) & 0xc0) | (str[i+2] & 0x3f);
|
||||||
tmp = (((uint16_t(ch1) & 0x00ff ) << 8) | (uint16_t(ch2) & 0x00ff));
|
tmp = (((uint16_t(ch1) & 0x00ff ) << 8) | (uint16_t(ch2) & 0x00ff));
|
||||||
vec.push_back(tmp);
|
vec.push_back(tmp);
|
||||||
i += 3;
|
i += 3;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
template <class Uint16Container>
|
template <class Uint16Container>
|
||||||
bool utf8ToUnicode(const string& str, Uint16Container& vec)
|
bool utf8ToUnicode(const string& str, Uint16Container& vec) {
|
||||||
{
|
|
||||||
return utf8ToUnicode(str.c_str(), str.size(), vec);
|
return utf8ToUnicode(str.c_str(), str.size(), vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Uint16ContainerConIter>
|
template <class Uint16ContainerConIter>
|
||||||
bool unicodeToUtf8(Uint16ContainerConIter begin, Uint16ContainerConIter end, string& res)
|
bool unicodeToUtf8(Uint16ContainerConIter begin, Uint16ContainerConIter end, string& res) {
|
||||||
{
|
if(begin >= end) {
|
||||||
if(begin >= end)
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
res.clear();
|
res.clear();
|
||||||
uint16_t ui;
|
uint16_t ui;
|
||||||
while(begin != end)
|
while(begin != end) {
|
||||||
{
|
|
||||||
ui = *begin;
|
ui = *begin;
|
||||||
if(ui <= 0x7f)
|
if(ui <= 0x7f) {
|
||||||
{
|
|
||||||
res += char(ui);
|
res += char(ui);
|
||||||
}
|
} else if(ui <= 0x7ff) {
|
||||||
else if(ui <= 0x7ff)
|
|
||||||
{
|
|
||||||
res += char(((ui>>6) & 0x1f) | 0xc0);
|
res += char(((ui>>6) & 0x1f) | 0xc0);
|
||||||
res += char((ui & 0x3f) | 0x80);
|
res += char((ui & 0x3f) | 0x80);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
res += char(((ui >> 12) & 0x0f )| 0xe0);
|
res += char(((ui >> 12) & 0x0f )| 0xe0);
|
||||||
res += char(((ui>>6) & 0x3f )| 0x80 );
|
res += char(((ui>>6) & 0x3f )| 0x80 );
|
||||||
res += char((ui & 0x3f) | 0x80);
|
res += char((ui & 0x3f) | 0x80);
|
||||||
@ -267,31 +222,22 @@ namespace Limonp
|
|||||||
|
|
||||||
|
|
||||||
template <class Uint16Container>
|
template <class Uint16Container>
|
||||||
bool gbkTrans(const char* const str, size_t len, Uint16Container& vec)
|
bool gbkTrans(const char* const str, size_t len, Uint16Container& vec) {
|
||||||
{
|
|
||||||
vec.clear();
|
vec.clear();
|
||||||
if(!str)
|
if(!str) {
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
while(i < len)
|
while(i < len) {
|
||||||
{
|
if(0 == (str[i] & 0x80)) {
|
||||||
if(0 == (str[i] & 0x80))
|
|
||||||
{
|
|
||||||
vec.push_back(uint16_t(str[i]));
|
vec.push_back(uint16_t(str[i]));
|
||||||
i++;
|
i++;
|
||||||
}
|
} else {
|
||||||
else
|
if(i + 1 < len) { //&& (str[i+1] & 0x80))
|
||||||
{
|
|
||||||
if(i + 1 < len) //&& (str[i+1] & 0x80))
|
|
||||||
{
|
|
||||||
uint16_t tmp = (((uint16_t(str[i]) & 0x00ff ) << 8) | (uint16_t(str[i+1]) & 0x00ff));
|
uint16_t tmp = (((uint16_t(str[i]) & 0x00ff ) << 8) | (uint16_t(str[i+1]) & 0x00ff));
|
||||||
vec.push_back(tmp);
|
vec.push_back(tmp);
|
||||||
i += 2;
|
i += 2;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -300,33 +246,26 @@ namespace Limonp
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class Uint16Container>
|
template <class Uint16Container>
|
||||||
bool gbkTrans(const string& str, Uint16Container& vec)
|
bool gbkTrans(const string& str, Uint16Container& vec) {
|
||||||
{
|
|
||||||
return gbkTrans(str.c_str(), str.size(), vec);
|
return gbkTrans(str.c_str(), str.size(), vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Uint16ContainerConIter>
|
template <class Uint16ContainerConIter>
|
||||||
bool gbkTrans(Uint16ContainerConIter begin, Uint16ContainerConIter end, string& res)
|
bool gbkTrans(Uint16ContainerConIter begin, Uint16ContainerConIter end, string& res) {
|
||||||
{
|
if(begin >= end) {
|
||||||
if(begin >= end)
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
res.clear();
|
res.clear();
|
||||||
//pair<char, char> pa;
|
//pair<char, char> pa;
|
||||||
char first, second;
|
char first, second;
|
||||||
while(begin != end)
|
while(begin != end) {
|
||||||
{
|
|
||||||
//pa = uint16ToChar2(*begin);
|
//pa = uint16ToChar2(*begin);
|
||||||
first = ((*begin)>>8) & 0x00ff;
|
first = ((*begin)>>8) & 0x00ff;
|
||||||
second = (*begin) & 0x00ff;
|
second = (*begin) & 0x00ff;
|
||||||
if(first & 0x80)
|
if(first & 0x80) {
|
||||||
{
|
|
||||||
res += first;
|
res += first;
|
||||||
res += second;
|
res += second;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
res += second;
|
res += second;
|
||||||
}
|
}
|
||||||
begin++;
|
begin++;
|
||||||
@ -337,13 +276,20 @@ namespace Limonp
|
|||||||
/*
|
/*
|
||||||
* format example: "%Y-%m-%d %H:%M:%S"
|
* format example: "%Y-%m-%d %H:%M:%S"
|
||||||
*/
|
*/
|
||||||
inline void getTime(const string& format, string& timeStr)
|
inline void getTime(const string& format, string& timeStr) {
|
||||||
{
|
|
||||||
time_t timeNow;
|
time_t timeNow;
|
||||||
time(&timeNow);
|
time(&timeNow);
|
||||||
timeStr.resize(64);
|
timeStr.resize(64);
|
||||||
size_t len = strftime((char*)timeStr.c_str(), timeStr.size(), format.c_str(), localtime(&timeNow));
|
size_t len = strftime((char*)timeStr.c_str(), timeStr.size(), format.c_str(), localtime(&timeNow));
|
||||||
timeStr.resize(len);
|
timeStr.resize(len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline string pathJoin(const string& path1, const string& path2) {
|
||||||
|
if(endsWith(path1, "/")) {
|
||||||
|
return path1 + path2;
|
||||||
|
}
|
||||||
|
return path1 + "/" + path2;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -4,42 +4,34 @@
|
|||||||
#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_;
|
||||||
bool isStarted;
|
bool isStarted;
|
||||||
bool isJoined;
|
bool isJoined;
|
||||||
public:
|
public:
|
||||||
IThread(): isStarted(false), isJoined(false)
|
IThread(): isStarted(false), isJoined(false) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
virtual ~IThread()
|
virtual ~IThread() {
|
||||||
{
|
if(isStarted && !isJoined) {
|
||||||
if(isStarted && !isJoined)
|
|
||||||
{
|
|
||||||
LIMONP_CHECK(!pthread_detach(thread_));
|
LIMONP_CHECK(!pthread_detach(thread_));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
public:
|
public:
|
||||||
virtual void run() = 0;
|
virtual void run() = 0;
|
||||||
void start()
|
void start() {
|
||||||
{
|
LIMONP_CHECK(!isStarted);
|
||||||
assert(!isStarted);
|
|
||||||
LIMONP_CHECK(!pthread_create(&thread_, NULL, worker_, this));
|
LIMONP_CHECK(!pthread_create(&thread_, NULL, worker_, this));
|
||||||
isStarted = true;
|
isStarted = true;
|
||||||
}
|
}
|
||||||
void join()
|
void join() {
|
||||||
{
|
LIMONP_CHECK(!isJoined);
|
||||||
assert(!isJoined);
|
|
||||||
LIMONP_CHECK(!pthread_join(thread_, NULL));
|
LIMONP_CHECK(!pthread_join(thread_, NULL));
|
||||||
isJoined = true;
|
isJoined = true;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
static void * worker_(void * data)
|
static void * worker_(void * data) {
|
||||||
{
|
|
||||||
IThread * ptr = (IThread* ) data;
|
IThread * ptr = (IThread* ) data;
|
||||||
ptr->run();
|
ptr->run();
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -4,50 +4,43 @@
|
|||||||
#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, class ArgType>
|
template <class TaskType, class ArgType>
|
||||||
ITask* CreateTask(ArgType arg)
|
ITask* CreateTask(ArgType arg) {
|
||||||
{
|
|
||||||
return new TaskType(arg);
|
return new TaskType(arg);
|
||||||
}
|
}
|
||||||
template <class TaskType, class ArgType0, class ArgType1>
|
template <class TaskType, class ArgType0, class ArgType1>
|
||||||
ITask* CreateTask(ArgType0 arg0, ArgType1 arg1)
|
ITask* CreateTask(ArgType0 arg0, ArgType1 arg1) {
|
||||||
{
|
|
||||||
return new TaskType(arg0, arg1);
|
return new TaskType(arg0, arg1);
|
||||||
}
|
}
|
||||||
|
template <class TaskType, class ArgType0, class ArgType1, class ArgType2>
|
||||||
|
ITask* CreateTask(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2) {
|
||||||
|
return new TaskType(arg0, arg1, arg2);
|
||||||
|
}
|
||||||
|
|
||||||
//class ThreadPool;
|
//class ThreadPool;
|
||||||
class ThreadPool: NonCopyable
|
class ThreadPool: NonCopyable {
|
||||||
{
|
|
||||||
private:
|
private:
|
||||||
class Worker: public IThread
|
class Worker: public IThread {
|
||||||
{
|
|
||||||
private:
|
private:
|
||||||
ThreadPool * ptThreadPool_;
|
ThreadPool * ptThreadPool_;
|
||||||
public:
|
public:
|
||||||
Worker(ThreadPool* pool): ptThreadPool_(pool)
|
Worker(ThreadPool* pool): ptThreadPool_(pool) {
|
||||||
{
|
|
||||||
assert(ptThreadPool_);
|
assert(ptThreadPool_);
|
||||||
}
|
}
|
||||||
virtual ~Worker()
|
virtual ~Worker() {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
virtual void run()
|
virtual void run() {
|
||||||
{
|
while(true) {
|
||||||
while(true)
|
|
||||||
{
|
|
||||||
ITask * task = ptThreadPool_->queue_.pop();
|
ITask * task = ptThreadPool_->queue_.pop();
|
||||||
if(task == NULL)
|
if(task == NULL) {
|
||||||
{
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
task->run();
|
task->run();
|
||||||
@ -63,39 +56,31 @@ namespace Limonp
|
|||||||
//mutable MutexLock mutex_;
|
//mutable MutexLock mutex_;
|
||||||
//Condition isEmpty__;
|
//Condition isEmpty__;
|
||||||
public:
|
public:
|
||||||
ThreadPool(size_t threadNum, size_t queueMaxSize): threads_(threadNum), queue_(queueMaxSize)//, mutex_(), isEmpty__(mutex_)
|
ThreadPool(size_t threadNum, size_t queueMaxSize): threads_(threadNum), queue_(queueMaxSize) { //, mutex_(), isEmpty__(mutex_)
|
||||||
{
|
|
||||||
assert(threadNum);
|
assert(threadNum);
|
||||||
assert(queueMaxSize);
|
assert(queueMaxSize);
|
||||||
for(size_t i = 0; i < threads_.size(); i ++)
|
for(size_t i = 0; i < threads_.size(); i ++) {
|
||||||
{
|
|
||||||
threads_[i] = new Worker(this);
|
threads_[i] = new Worker(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
~ThreadPool()
|
~ThreadPool() {
|
||||||
{
|
for(size_t i = 0; i < threads_.size(); i ++) {
|
||||||
for(size_t i = 0; i < threads_.size(); i ++)
|
|
||||||
{
|
|
||||||
queue_.push(NULL);
|
queue_.push(NULL);
|
||||||
}
|
}
|
||||||
for(size_t i = 0; i < threads_.size(); i ++)
|
for(size_t i = 0; i < threads_.size(); i ++) {
|
||||||
{
|
|
||||||
threads_[i]->join();
|
threads_[i]->join();
|
||||||
delete threads_[i];
|
delete threads_[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void start()
|
void start() {
|
||||||
{
|
for(size_t i = 0; i < threads_.size(); i++) {
|
||||||
for(size_t i = 0; i < threads_.size(); i++)
|
|
||||||
{
|
|
||||||
threads_[i]->start();
|
threads_[i]->start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void add(ITask* task)
|
void add(ITask* task) {
|
||||||
{
|
|
||||||
assert(task);
|
assert(task);
|
||||||
queue_.push(task);
|
queue_.push(task);
|
||||||
}
|
}
|
||||||
|
@ -5,6 +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"
|
||||||
|
|
||||||
using namespace CppJieba;
|
using namespace CppJieba;
|
||||||
|
|
||||||
@ -24,8 +25,9 @@ void cut(size_t times = 50)
|
|||||||
res.clear();
|
res.clear();
|
||||||
seg.cut(doc, res);
|
seg.cut(doc, res);
|
||||||
}
|
}
|
||||||
|
printf("\n");
|
||||||
long endTime = clock();
|
long endTime = clock();
|
||||||
printf("\ncut: [%.3lf seconds]time consumed.\n", double(endTime - beginTime)/CLOCKS_PER_SEC);
|
ColorPrintln(GREEN, "cut: [%.3lf seconds]time consumed.", double(endTime - beginTime)/CLOCKS_PER_SEC);
|
||||||
}
|
}
|
||||||
|
|
||||||
void extract(size_t times = 400)
|
void extract(size_t times = 400)
|
||||||
@ -44,8 +46,9 @@ void extract(size_t times = 400)
|
|||||||
words.clear();
|
words.clear();
|
||||||
extractor.extract(doc, words, 5);
|
extractor.extract(doc, words, 5);
|
||||||
}
|
}
|
||||||
|
printf("\n");
|
||||||
long endTime = clock();
|
long endTime = clock();
|
||||||
printf("\nextract: [%.3lf seconds]time consumed.\n", double(endTime - beginTime)/CLOCKS_PER_SEC);
|
ColorPrintln(GREEN, "extract: [%.3lf seconds]time consumed.", double(endTime - beginTime)/CLOCKS_PER_SEC);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char ** argv)
|
int main(int argc, char ** argv)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user