From 64f2b8812d20e7ae888e22bff566e82026d45609 Mon Sep 17 00:00:00 2001 From: wyy Date: Sat, 26 Oct 2013 08:52:07 -0700 Subject: [PATCH] update cppcommon -> limonp --- cppcommon/Makefile | 46 ---- cppcommon/argv_functs.cpp | 110 -------- cppcommon/argv_functs.h | 38 --- cppcommon/config.cpp | 116 -------- cppcommon/config.h | 42 --- cppcommon/encoding.cpp | 117 -------- cppcommon/encoding.h | 37 --- cppcommon/file_functs.cpp | 48 ---- cppcommon/file_functs.h | 21 -- cppcommon/headers.h | 15 - cppcommon/io_functs.cpp | 29 -- cppcommon/io_functs.h | 14 - cppcommon/logger.cpp | 97 ------- cppcommon/logger.h | 61 ----- cppcommon/str_functs.cpp | 259 ------------------ cppcommon/str_functs.h | 85 ------ cppcommon/testdata/dict.gbk | 101 ------- cppcommon/testdata/dict.utf8 | 102 ------- cppcommon/vec_functs.cpp | 34 --- cppcommon/vec_functs.h | 35 --- limonp/ArgvContext.hpp | 86 ++++++ limonp/MysqlClient.hpp | 107 ++++++++ limonp/cast_functs.hpp | 87 ++++++ limonp/config.hpp | 107 ++++++++ limonp/io_functs.hpp | 82 ++++++ limonp/logger.hpp | 79 ++++++ limonp/macro_def.hpp | 22 ++ .../map_functs.h => limonp/map_functs.hpp | 37 ++- limonp/str_functs.hpp | 223 +++++++++++++++ {cppcommon => limonp}/typedefs.h | 8 +- .../vec_functs.tcc => limonp/vec_functs.hpp | 79 ++++-- 31 files changed, 886 insertions(+), 1438 deletions(-) delete mode 100644 cppcommon/Makefile delete mode 100644 cppcommon/argv_functs.cpp delete mode 100644 cppcommon/argv_functs.h delete mode 100644 cppcommon/config.cpp delete mode 100644 cppcommon/config.h delete mode 100644 cppcommon/encoding.cpp delete mode 100644 cppcommon/encoding.h delete mode 100644 cppcommon/file_functs.cpp delete mode 100644 cppcommon/file_functs.h delete mode 100644 cppcommon/headers.h delete mode 100644 cppcommon/io_functs.cpp delete mode 100644 cppcommon/io_functs.h delete mode 100644 cppcommon/logger.cpp delete mode 100644 cppcommon/logger.h delete mode 100644 cppcommon/str_functs.cpp delete mode 100644 cppcommon/str_functs.h delete mode 100644 cppcommon/testdata/dict.gbk delete mode 100644 cppcommon/testdata/dict.utf8 delete mode 100644 cppcommon/vec_functs.cpp delete mode 100644 cppcommon/vec_functs.h create mode 100644 limonp/ArgvContext.hpp create mode 100644 limonp/MysqlClient.hpp create mode 100644 limonp/cast_functs.hpp create mode 100644 limonp/config.hpp create mode 100644 limonp/io_functs.hpp create mode 100644 limonp/logger.hpp create mode 100644 limonp/macro_def.hpp rename cppcommon/map_functs.h => limonp/map_functs.hpp (68%) create mode 100644 limonp/str_functs.hpp rename {cppcommon => limonp}/typedefs.h (69%) rename cppcommon/vec_functs.tcc => limonp/vec_functs.hpp (60%) diff --git a/cppcommon/Makefile b/cppcommon/Makefile deleted file mode 100644 index 88720e4..0000000 --- a/cppcommon/Makefile +++ /dev/null @@ -1,46 +0,0 @@ -CC = g++ -CCOPT = -Wall -c -LINK = g++ -LINKOPT = -PACK = ar -PACKOPT = rc -SOURCES := $(wildcard *.cpp) -OBJS := $(patsubst %.cpp,%.o,$(SOURCES)) - -DOPACK = $(PACK) $(PACKOPT) $@ $^ -DOLINK = $(LINK) $(LINKOPT) -o $@ $^ - -CMLIB = libcm.a - -%.o: %.cpp - $(CC) $(CCOPT) $< - - -all: $(CMLIB) - - -$(CMLIB): $(OBJS) - $(DOPACK) - - -file_functs.ut: file_functs.cpp file_functs.h - g++ -o $@ $< -DTEST_FILE_FUNCTS -io_functs.test: io_functs.cpp io_functs.h - g++ -o $@ $< -DTEST_IO_FUNCTS -str_functs.ut: str_functs.cpp str_functs.h - g++ -o $@ $< -DTEST_STR_FUNCTS -argv_functs.ut: argv_functs.cpp str_functs.cpp argv_functs.h - g++ -o $@ argv_functs.cpp str_functs.cpp -DARGV_FUNCTS_UT -encoding.ut: encoding.cpp str_functs.cpp str_functs.h encoding.h - g++ -o $@ encoding.cpp str_functs.cpp -DENCODING_UT -vec_functs.test: vec_functs.cpp vec_functs.h vec_functs.tcc - g++ -o $@ $< -DTEST_VEC_FUNCTS - -logger.ut: logger.cpp logger.h file_functs.cpp file_functs.h str_functs.cpp str_functs.h - g++ -o $@ $< file_functs.cpp str_functs.cpp -DLOGGER_UT -config.ut: config.cpp config.h - g++ -o $@ $< -DCONFIG_UT $(CMLIB) - -clean: - rm -f *.test *.ut *.o $(CMLIB) - diff --git a/cppcommon/argv_functs.cpp b/cppcommon/argv_functs.cpp deleted file mode 100644 index 41e6fed..0000000 --- a/cppcommon/argv_functs.cpp +++ /dev/null @@ -1,110 +0,0 @@ -#include "argv_functs.h" - - -namespace CPPCOMMON -{ - bool getArgvMap(int argc, const char* const * argv, map& mpss) - { - mpss.clear(); - for(int i = 0; i < argc; i++) - { - if(strStartsWith(argv[i], "--")) - { - if(i + 1 < argc && !strStartsWith(argv[i+1], "--")) - { - mpss[argv[i]] = argv[i+1]; - i++; - } - else - { - return false; - } - } - } - return true; - } - - ArgvContext::ArgvContext(int argc, const char* const * argv) - { - for(int i = 0; i < argc; i++) - { - if(strStartsWith(argv[i], "-")) - { - if(i + 1 < argc && !strStartsWith(argv[i + 1], "-")) - { - _mpss[argv[i]] = argv[i+1]; - i++; - } - else - { - _sset.insert(argv[i]); - } - } - else - { - _args.push_back(argv[i]); - } - } - } - - ArgvContext::~ArgvContext() - { - } - - string ArgvContext::toString() - { - stringstream ss; - ss<(_args)<(_mpss)<(_sset); - return ss.str(); - } - - string ArgvContext::operator [](uint i) - { - if(i < _args.size()) - { - return _args[i]; - } - return ""; - } - - string ArgvContext::operator [](const string& key) - { - map::const_iterator it = _mpss.find(key); - if(it != _mpss.end()) - { - return it->second; - } - return ""; - } - - bool ArgvContext::isKeyExist(const string& key) - { - if(_mpss.find(key) != _mpss.end() || _sset.find(key) != _sset.end()) - { - return true; - } - return false; - } -} - - -#ifdef ARGV_FUNCTS_UT - -using namespace CPPCOMMON; - -int main(int argc, char** argv) -{ - //map argvMap; - //getArgvMap(argc, argv, argvMap); - //PRINT_MAPSS(argvMap); - ArgvContext arg(argc, argv); - cout<(pair(1,1.2))<>>>>>> e81a664dae9397a6bf937b6dd6482832f67a6cf6 -} - -#endif diff --git a/cppcommon/argv_functs.h b/cppcommon/argv_functs.h deleted file mode 100644 index 15bbc82..0000000 --- a/cppcommon/argv_functs.h +++ /dev/null @@ -1,38 +0,0 @@ -/************************************ - * file enc : ascii - * author : wuyanyi09@gmail.com -************************************/ - -#ifndef CPPCOMMON_ARGV_FUNCTS_H -#define CPPCOMMON_ARGV_FUNCTS_H - -#include -#include -#include "str_functs.h" -#include "map_functs.h" -#include "vec_functs.h" - -namespace CPPCOMMON -{ - using namespace std; - bool getArgvMap(int argc, const char* const* argv, map& mpss); - class ArgvContext - { - public : - ArgvContext(int argc, const char* const * argv); - ~ArgvContext(); - public: - string toString(); - string operator [](uint i); - string operator [](const string& key); - public: - bool isKeyExist(const string& key); - private: - vector _args; - map _mpss; - set _sset; - - }; -} - -#endif diff --git a/cppcommon/config.cpp b/cppcommon/config.cpp deleted file mode 100644 index 9b8c9aa..0000000 --- a/cppcommon/config.cpp +++ /dev/null @@ -1,116 +0,0 @@ -/************************************ - * file enc : utf8 - * author : wuyanyi09@gmail.com -************************************/ -#include "config.h" -#include "str_functs.h" - -namespace CPPCOMMON -{ - Config::Config() - { - _isInit = false; - } - - Config::~Config() - { - } - - bool Config::init(const string& configFile) - { - char msgBuf[1024]; - if(_isInit) - { - LogFatal("already have been initialized. "); - return false; - } - ifstream ifile(configFile.c_str()); - if(!ifile) - { - sprintf(msgBuf, "open configFile[%s] failed.", configFile.c_str()); - LogFatal(msgBuf); - return false; - } - string line, key, value; - vector vecBuf; - while(getline(ifile, line)) - { - line = _stripComment(line); - if(line.empty()) - { - continue; - } - vecBuf = splitStr(line, "="); - if(2 != vecBuf.size()) - { - sprintf(msgBuf, "line[%s] is illegal.", line.c_str()); - LogFatal(msgBuf); - return false; - } - key = vecBuf[0]; - value = vecBuf[1]; - if(_map.end() != _map.find(key)) - { - sprintf(msgBuf, "key[%s] already exists.", key.c_str()); - LogFatal(msgBuf); - return false; - } - _map[key] = value; - } - ifile.close(); - _isInit = true; - return true; - } - - void Config::display() - { - for(map::iterator it = _map.begin(); it != _map.end(); it++) - { - cout<<"("<first<<","<second<<")"< -#include -#include -#include "logger.h" - -namespace CPPCOMMON -{ - using std::map; - using std::string; - using std::cout; - using std::endl; - using std::ifstream; - class Config - { - public: - Config(); - ~Config(); - bool init(const string& configFile); - void display(); - string getByKey(const string& key); - private: - string _stripComment(const string& line); - map _map; - bool _isInit; - - }; -} - -namespace CPPCOMMON -{ - extern Config gConfig; -} - -#endif diff --git a/cppcommon/encoding.cpp b/cppcommon/encoding.cpp deleted file mode 100644 index 70ea629..0000000 --- a/cppcommon/encoding.cpp +++ /dev/null @@ -1,117 +0,0 @@ -/************************************ - * file enc : utf8 - * author : wuyanyi09@gmail.com -************************************/ -#include "encoding.h" - -namespace CPPCOMMON -{ - - //UnicodeEncoding::UnicodeEncoding(const string& enc) - //{ - // - // _encVec.push_back(UTF8ENC); - // _encVec.push_back(GBKENC); - // - // if(!isInVec(_encVec, enc)) - // { - // //default - // _encoding = UTF8ENC; - // } - // else - // { - // _encoding = enc; - // } - //} - - //UnicodeEncoding::~UnicodeEncoding() - //{ - //} - // - //bool UnicodeEncoding::setEncoding(const string& enc) - //{ - // if(!isInVec(_encVec, enc)) - // { - // return false; - // } - // _encoding = enc; - // return true; - //} - - //string UnicodeEncoding::encode(UnicodeConstIterator begin, UnicodeConstIterator end) - //{ - // if(begin >= end) - // { - // return ""; - // } - // Unicode unicode(begin, end); - // return encode(unicode); - //} - - //string UnicodeEncoding::encode(const Unicode& unicode) - //{ - // if(unicode.empty()) - // { - // return ""; - // } - // if(UTF8ENC == _encoding) - // { - // return unicodeToUtf8(unicode); - // } - // else if(GBKENC == _encoding) - // { - // return utf8ToGbk(unicodeToUtf8(unicode)); - // } - // return ""; - //} - - //bool UnicodeEncoding::decode(const string& str, Unicode& unicode) - //{ - // if(str.empty()) - // { - // return false; - // } - // if(UTF8ENC == _encoding) - // { - // return utf8ToUnicode(str, unicode); - // } - // else if(GBKENC == _encoding) - // { - // return utf8ToUnicode(gbkToUtf8(str), unicode); - // } - // return false; - //} - - //size_t UnicodeEncoding::getWordLength(const string& str) - //{ - // Unicode unicode; - // decode(str, unicode); - // return unicode.size(); - //} - -} - -#ifdef ENCODING_UT -using namespace CPPCOMMON; -int main() -{ - UnicodeEncoding enc(GBKENC); - ifstream ifile("testdata/dict.gbk"); - Unicode unicode; - string line; - while(getline(ifile, line)) - { - - cout< _encVec; - // public: - // UnicodeEncoding(const string& enc); - // ~UnicodeEncoding(); - // public: - // bool setEncoding(const string& enc); - // string encode(const Unicode& unicode); - // string encode(UnicodeConstIterator begin, UnicodeConstIterator end); - // bool decode(const string& str, Unicode& unicode); - // public: - // size_t getWordLength(const string& str); - //}; -} - -#endif diff --git a/cppcommon/file_functs.cpp b/cppcommon/file_functs.cpp deleted file mode 100644 index a7af09c..0000000 --- a/cppcommon/file_functs.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/************************************ - * file enc : utf8 - * author : wuyanyi09@gmail.com -************************************/ -#include "file_functs.h" - -namespace CPPCOMMON -{ - bool checkFileExist(const string& filePath) - { - fstream _file; - _file.open(filePath.c_str(), ios::in); - if(_file) - return true; - return false; - } - bool createDir(const string& dirPath, bool p) - { - string dir_str(dirPath); - string cmd = "mkdir"; - if(p) - { - cmd += " -p"; - } - cmd += " " + dir_str; - int res = system(cmd.c_str()); - return res; - } - bool checkDirExist(const string& dirPath) - { - return checkFileExist(dirPath); - } -} - -#ifdef TEST_FILE_FUNCTS -#include -using namespace CPPCOMMON; -using namespace std; -int main() -{ - char filename[] = "1/2/3"; - if(!checkFileExist(filename)) - { - createDir(filename); - } - return 0; -} -#endif diff --git a/cppcommon/file_functs.h b/cppcommon/file_functs.h deleted file mode 100644 index 2377126..0000000 --- a/cppcommon/file_functs.h +++ /dev/null @@ -1,21 +0,0 @@ -/************************************ - * file enc : utf8 - * author : wuyanyi09@gmail.com -************************************/ -#ifndef CPPCOMMON_FILE_FUNCTS_H -#define CPPCOMMON_FILE_FUNCTS_H - -#include -#include -#include -#include -namespace CPPCOMMON -{ - using namespace std; - bool checkFileExist(const string& filePath); - bool createDir(const string& dirPath, bool p = true); - bool checkDirExist(const string& dirPath); - -} - -#endif diff --git a/cppcommon/headers.h b/cppcommon/headers.h deleted file mode 100644 index 81abbae..0000000 --- a/cppcommon/headers.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef CPPCOMMON_HEADERS_H -#define CPPCOMMON_HEADERS_H - -#include "logger.h" -#include "file_functs.h" -#include "vec_functs.h" -#include "io_functs.h" -#include "encoding.h" -#include "config.h" -#include "typedefs.h" -#include "str_functs.h" -#include "map_functs.h" -#include "argv_functs.h" - -#endif diff --git a/cppcommon/io_functs.cpp b/cppcommon/io_functs.cpp deleted file mode 100644 index 768b6d8..0000000 --- a/cppcommon/io_functs.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/************************************ - * file enc : utf8 - * author : wuyanyi09@gmail.com -************************************/ -#include "io_functs.h" - -namespace CPPCOMMON -{ - string loadFile2Str(const char * const filepath) - { - ifstream in(filepath); - istreambuf_iterator beg(in), end; - string str(beg, end); - in.close(); - return str; - } -} - -#ifdef TEST_IO_FUNCTS -#include -using namespace CPPCOMMON; -using namespace std; -int main() -{ - char filename[] = "1/2/3"; - cout< -#include -namespace CPPCOMMON -{ - using namespace std; - string loadFile2Str(const char * const filepath); -} -#endif diff --git a/cppcommon/logger.cpp b/cppcommon/logger.cpp deleted file mode 100644 index 93792bf..0000000 --- a/cppcommon/logger.cpp +++ /dev/null @@ -1,97 +0,0 @@ -/************************************ - * file enc : utf8 - * author : wuyanyi09@gmail.com -************************************/ -#include "logger.h" -namespace CPPCOMMON -{ - char Logger::_cStrBuf[CSTR_BUFFER_SIZE]; - const char * Logger::_logLevel[LEVEL_ARRAY_SIZE] = { - "DEBUG","INFO","WARN","ERROR","FATAL" - }; - - const char * Logger::_logFormat = "%s [File:%s] [Line:%d] [%s] Msg:%s\n"; - const char * Logger::_timeFormat = "%Y-%m-%d %H:%M:%S"; - time_t Logger::_timeNow; - Logger::Logger() - { - } - - Logger::~Logger() - { - } - - bool Logger::LoggingF(uint level, const char* fileName, int lineNo, const string& fmt, ...) - { - int size = 256; - string msg; - va_list ap; - while (1) { - msg.resize(size); - va_start(ap, fmt); - int n = vsnprintf((char *)msg.c_str(), size, fmt.c_str(), ap); - va_end(ap); - if (n > -1 && n < size) { - msg.resize(n); - break; - } - if (n > -1) - size = n + 1; - else - size *= 2; - } - return Logging(level, msg, fileName, lineNo); - } - - bool Logger::Logging(uint level, const string& msg, const char * fileName, int lineNo) - { - return Logging(level, msg.c_str(), fileName, lineNo); - } - - - bool Logger::Logging(uint level, const char * msg, const char* fileName, int lineNo) - { - if(level > LL_FATAL) - { - cerr<<"level's value is out of range"<= LL_WARN) - { - fprintf(stderr, _logFormat, _cStrBuf, fileName, lineNo, _logLevel[level], msg); - } - else - { - fprintf(stdout, _logFormat, _cStrBuf, fileName, lineNo, _logLevel[level], msg); - fflush(stdout); - } - return true; - } -} - - -#ifdef LOGGER_UT -using namespace CPPCOMMON; -int main() -{ - LogDebug("debug log!"); - LogInfo("test info log"); - LogWarn("warning log"); - LogInfo("str[%s] int[%d]", "str1"); - LogInfo("str[%s] int[%d]", "str1",15); - LogError("error log"); - LogFatal("fatal !!!!"); - LogFatal("str[%s] int[%d]", "str1"); - LogFatal("str[%s] int[%d]", "str1", 17,16); - LogFatal("str"); - return 0; -} -#endif - diff --git a/cppcommon/logger.h b/cppcommon/logger.h deleted file mode 100644 index 049a018..0000000 --- a/cppcommon/logger.h +++ /dev/null @@ -1,61 +0,0 @@ -/************************************ - * file enc : utf8 - * author : wuyanyi09@gmail.com -************************************/ -#ifndef CPPCOMMON_LOGGER_H -#define CPPCOMMON_LOGGER_H - -#include -#include -#include -#include -#include -#include -#include "file_functs.h" -#include "str_functs.h" -#include "typedefs.h" - -#define LL_DEBUG 0 -#define LL_INFO 1 -#define LL_WARN 2 -#define LL_ERROR 3 -#define LL_FATAL 4 -#define LEVEL_ARRAY_SIZE 5 -#define CSTR_BUFFER_SIZE 1024 - - -//#define LogDebug(msg) Logger::Logging(LL_DEBUG, msg, __FILE__, __LINE__) -//#define LogInfo(msg) Logger::Logging(LL_INFO, msg, __FILE__, __LINE__) -//#define LogWarn(msg) Logger::Logging(LL_WARN, msg, __FILE__, __LINE__) -//#define LogError(msg) Logger::Logging(LL_ERROR, msg, __FILE__, __LINE__) -//#define LogFatal(msg) Logger::Logging(LL_FATAL, msg, __FILE__, __LINE__) - -#define LogDebug(fmt, ...) Logger::LoggingF(LL_DEBUG, __FILE__, __LINE__, fmt, ## __VA_ARGS__) -#define LogInfo(fmt, ...) Logger::LoggingF(LL_INFO, __FILE__, __LINE__, fmt, ## __VA_ARGS__) -#define LogWarn(fmt, ...) Logger::LoggingF(LL_WARN, __FILE__, __LINE__, fmt, ## __VA_ARGS__) -#define LogError(fmt, ...) Logger::LoggingF(LL_ERROR, __FILE__, __LINE__, fmt, ## __VA_ARGS__) -#define LogFatal(fmt, ...) Logger::LoggingF(LL_FATAL, __FILE__, __LINE__, fmt, ## __VA_ARGS__) - - -namespace CPPCOMMON -{ - using namespace std; - class Logger - { - public: - Logger(); - ~Logger(); - public: - static bool Logging(uint level, const string& msg, const char* fileName, int lineNo); - static bool Logging(uint level, const char * msg, const char* fileName, int lineNo); - static bool LoggingF(uint level, const char* fileName, int lineNo, const string& fmt, ...); - private: - static char _cStrBuf[CSTR_BUFFER_SIZE]; - static const char * _logLevel[LEVEL_ARRAY_SIZE]; - static const char * _logFormat; - static const char * _timeFormat; - static time_t _timeNow; - }; -} - -#endif diff --git a/cppcommon/str_functs.cpp b/cppcommon/str_functs.cpp deleted file mode 100644 index f23afb6..0000000 --- a/cppcommon/str_functs.cpp +++ /dev/null @@ -1,259 +0,0 @@ -/************************************ - * file enc : utf8 - * author : wuyanyi09@gmail.com - ************************************/ -#include "str_functs.h" - -namespace CPPCOMMON -{ - //http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf - string string_format(const char* fmt, ...) - { - int size = 256; - std::string str; - va_list ap; - while (1) { - str.resize(size); - va_start(ap, fmt); - int n = vsnprintf((char *)str.c_str(), size, fmt, ap); - va_end(ap); - if (n > -1 && n < size) { - str.resize(n); - return str; - } - if (n > -1) - size = n + 1; - else - size *= 2; - } - return str; - } - - string joinStr(const vector& src, const string& connectorStr) - { - string res = ""; - int len = src.size(); - if(0 == len) - { - return ""; - } - for(int i = 0; i < len - 1; i++) - { - res += stripStr(src[i]); - res += connectorStr; - } - res += stripStr(src[len-1]); - return res; - } - - vector splitStr(const string& source, const string& pattern) - { - vector res; - splitStr(source, res, pattern); - return res; - } - - bool splitStr(const string& source, vector& res, const string& pattern) - { - if(source.empty()) - { - return false; - } - res.clear(); - - size_t start = source.find_first_not_of(pattern); - size_t end; - if(string::npos == start) - { - return false; - } - while(string::npos != start) - { - end = source.find_first_of(pattern, start); - if(string::npos == end) - { - res.push_back(source.substr(start)); - return true; - } - res.push_back(source.substr(start, end - start)); - start = source.find_first_not_of(pattern, end); - } - return true; - } - - string stripStr(const string& str, const string& patternStr) - { - if(str.empty()) - { - return str; - } - string::size_type posL = str.find_first_not_of(patternStr); - if(string::npos == posL) - { - return str; - } - string::size_type posR = str.find_last_not_of(patternStr); - return str.substr(posL, posR - posL + 1); - - } - - - //http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring - // trim from start - std::string <rim(std::string &s) - { - s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(std::isspace)))); - return s; - } - - // trim from end - std::string &rtrim(std::string &s) - { - s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun(std::isspace))).base(), s.end()); - return s; - } - - // trim from both ends - std::string &trim(std::string &s) - { - return ltrim(rtrim(s)); - } - - bool splitStrMultiPatterns( - const string& strSrc, - vector& outVec, - const vector& patterns - ) - { - char transChar = '#'; - uint transLenThreshold = 10; - string transStr; - transStr += transChar; - while(strSrc.find(transStr) != string::npos) - { - transStr += transChar; - if(transStr.size() > transLenThreshold) - return false; - } - string strSrcMutable = strSrc; - for(uint i = 0; i < patterns.size(); i++) - { - strSrcMutable = replaceStr(strSrcMutable, patterns[i], transStr); - } - splitStr(strSrcMutable, outVec, transStr); - return true; - } - - string upperStr(const string& strIn) - { - string str = strIn; - transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper); - return str; - } - - string lowerStr(const string& strIn) - { - string str = strIn; - transform(str.begin(), str.end(), str.begin(), (int (*)(int))tolower); - return str; - } - - string replaceStr(const string& strSrc, const string& oldStr, const string& newStr, int count) - { - string strRet = strSrc; - size_t pos = 0; - int l_count = 0; - if(-1 == count) - count = strRet.size(); - while((pos = strRet.find(oldStr, pos)) != string::npos) - { - strRet.replace(pos, oldStr.size(), newStr); - if(++l_count >= count) - break; - pos += newStr.size(); - } - return strRet; - } - - unsigned int countStrDistance(const string& A, const string& B) - { - unsigned int lenA = A.size(); - unsigned int lenB = B.size(); - unsigned int len = (lenA < lenB ? lenA : lenB); - unsigned int res = lenA + lenB - 2 * len; - for(size_t i = 0; i < len; i++) - { - if(A[i] != B[i]) - res++; - } - return res; - } - - unsigned int countStrSimilarity(const string& A, const string& B) - { - unsigned int lenA = A.size(); - unsigned int lenB = B.size(); - unsigned int len = (lenA < lenB ? lenA : lenB); - unsigned int res = 0; - for(size_t i = 0; i < len; i++) - { - if(A[i] == B[i]) - res++; - } - return res; - } - - //unicode str to vec - bool uniStrToVec(const string& str, Unicode& vec) - { - vec.clear(); - if(str.empty() || str.size() % 2) - { - return false; - } - for(uint i = 0; i < str.size(); i+=2) - { - vec.push_back(twocharToUint16(str[i], str[i + 1])); - } - - return true; - } - - //unicode vec to str - string uniVecToStr(const Unicode& vec) - { - string res(""); - for(uint i = 0; i < vec.size(); i++) - { - pair pa = uint16ToChar2(vec[i]); - res += pa.first; - res += pa.second; - } - return res; - } - -} - -#ifdef TEST_STR_FUNCTS -#include -using namespace CPPCOMMON; -using namespace std; -int main() -{ - vector vec; - splitStr("1 3 4", vec); - for(uint i =0;i < vec.size(); i++) - { - cout< -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "typedefs.h" -#include -#include -namespace CPPCOMMON -{ - using namespace std; - string string_format(const char*, ...) ; - string joinStr(const vector& source, const string& connector); - vector splitStr(const string& source, const string& pattern = " \t\n"); - bool splitStr(const string& source, vector& res, const string& pattern = " \t\n"); - bool splitStrMultiPatterns( - const string& strSrc, - vector& outVec, - const vector& patterns - ); - string upperStr(const string& str); - string lowerStr(const string& str); - string replaceStr(const string& strSrc, const string& oldStr, const string& newStr, int count = -1); - string stripStr(const string& str, const string& patternstr = " \n\t"); - std::string <rim(std::string &s) ; - std::string &rtrim(std::string &s) ; - std::string &trim(std::string &s) ; - unsigned int countStrDistance(const string& A, const string& B); - unsigned int countStrSimilarity(const string& A, const string& B); - - - bool uniStrToVec(const string& str, Unicode& vec); - string uniVecToStr(const Unicode& vec); - - inline uint16_t twocharToUint16(char high, char low) - { - return (((uint16_t(high) & 0x00ff ) << 8) | (uint16_t(low) & 0x00ff)); - } - - inline pair uint16ToChar2(uint16_t in) - { - pair res; - res.first = (in>>8) & 0x00ff; //high - res.second = (in) & 0x00ff; //low - return res; - } - - inline void printUnicode(const Unicode& unicode) - { - cout< str.length()) - { - return false; - } - return 0 == str.compare(0, prefix.length(), prefix); - } - - inline bool strEndsWith(const string& str, const string& suffix) - { - if(suffix.length() > str.length()) - { - return false; - } - return 0 == str.compare(str.length() - suffix.length(), suffix.length(), suffix); - } - -} -#endif diff --git a/cppcommon/testdata/dict.gbk b/cppcommon/testdata/dict.gbk deleted file mode 100644 index 0fde7e6..0000000 --- a/cppcommon/testdata/dict.gbk +++ /dev/null @@ -1,101 +0,0 @@ -AT&T 3 nz -B超 3 n -c# 3 nz -C# 3 nz - -c++ 3 nz -C++ 3 nz -T恤 4 n -一 217830 m -一一 1670 m -一一二 11 m -一一例 3 m -一一分 8 m -一一列举 34 i -一一对 9 m -一一对应 43 l -一一记 2 m -一一道来 4 l -一丁 18 d -一丁不识 3 i -一丁点 3 m -一丁点儿 24 m -一七 22 m -一七八不 3 l -一万 442 m -一万一千 4 m -一万一千五百二十颗 2 m -一万一千八百八十斤 2 m -一万一千多间 2 m -一万一千零九十五册 4 m -一万七千 5 m -一万七千余 2 m -一万七千多 2 m -一万七千多户 2 m -一万万 4 m -一万万两 4 m -一万三千 8 m -一万三千五百一十七 2 m -一万三千五百斤 4 m -一万三千余种 2 m -一万三千块 2 m -一万两 124 m -一万两万 4 m -一万两千 3 m -一万个 62 m -一万九千 2 m -一万九千余 2 m -一万二 10 m -一万二千 7 m -一万二千两 2 m -一万二千五百 4 m -龛 223 ng -龜 2 zg -龟 903 ns -龟儿子 123 n -龟兆 3 nz -龟兹 215 ns -龟兹王 3 nrt -龟冷搘床 3 v -龟冷支床 3 n -龟卜 3 n -龟厌不告 3 l -龟壳 33 n -龟壳花 3 n -龟头 34 n -龟头炎 3 n -龟山 23 ns -龟山乡 3 ns -龟山岛 3 ns -龟年鹤寿 3 ns -龟年鹤算 3 l -龟文 3 nz -龟文写迹 3 n -龟文鸟迹 3 n -龟板 10 n -龟毛免角 3 n -龟毛兔角 3 n -龟溪 3 ns -龟玉 3 nz -龟王 3 nz -龟甲 92 ns -龟甲胶 3 nz -龟筮 3 n -龟纹 3 n -龟缩 29 v -龟肉 3 n -龟背 21 n -龟背竹 3 n -龟苓膏 3 n -龟苗 3 n -龟裂 34 v -龟足 5 v -龟鉴 2 n -龟镜 3 nz -龟鳖 3 n -龟鹤遐寿 3 l -龟龄鹤算 3 n -龟龙片甲 3 nz -龟龙麟凤 3 ns -龠 5 g -龢 732 zg diff --git a/cppcommon/testdata/dict.utf8 b/cppcommon/testdata/dict.utf8 deleted file mode 100644 index cc0ace2..0000000 --- a/cppcommon/testdata/dict.utf8 +++ /dev/null @@ -1,102 +0,0 @@ -鍖呴偖鎷夎彶鑽18cm澶ф獝杩涘彛鑽夊附瀛愯秴寮洪伄闃抽槻鏅掓缇庢棩闊╂柊娆惧澶 濂 -AT&T 3 nz - -B瓒 3 n -c# 3 nz -C# 3 nz -c++ 3 nz -C++ 3 nz -T鎭 4 n -涓 217830 m -涓涓 1670 m -涓涓浜 11 m -涓涓渚 3 m -涓涓鍒 8 m -涓涓鍒椾妇 34 i -涓涓瀵 9 m -涓涓瀵瑰簲 43 l -涓涓璁 2 m -涓涓閬撴潵 4 l -涓涓 18 d -涓涓佷笉璇 3 i -涓涓佺偣 3 m -涓涓佺偣鍎 24 m -涓涓 22 m -涓涓冨叓涓 3 l -涓涓 442 m -涓涓囦竴鍗 4 m -涓涓囦竴鍗冧簲鐧句簩鍗侀 2 m -涓涓囦竴鍗冨叓鐧惧叓鍗佹枻 2 m -涓涓囦竴鍗冨闂 2 m -涓涓囦竴鍗冮浂涔濆崄浜斿唽 4 m -涓涓囦竷鍗 5 m -涓涓囦竷鍗冧綑 2 m -涓涓囦竷鍗冨 2 m -涓涓囦竷鍗冨鎴 2 m -涓涓囦竾 4 m -涓涓囦竾涓 4 m -涓涓囦笁鍗 8 m -涓涓囦笁鍗冧簲鐧句竴鍗佷竷 2 m -涓涓囦笁鍗冧簲鐧炬枻 4 m -涓涓囦笁鍗冧綑绉 2 m -涓涓囦笁鍗冨潡 2 m -涓涓囦袱 124 m -涓涓囦袱涓 4 m -涓涓囦袱鍗 3 m -涓涓囦釜 62 m -涓涓囦節鍗 2 m -涓涓囦節鍗冧綑 2 m -涓涓囦簩 10 m -涓涓囦簩鍗 7 m -涓涓囦簩鍗冧袱 2 m -涓涓囦簩鍗冧簲鐧 4 m -榫 223 ng -榫 2 zg -榫 903 ns -榫熷効瀛 123 n -榫熷厗 3 nz -榫熷吂 215 ns -榫熷吂鐜 3 nrt -榫熷喎鎼樺簥 3 v -榫熷喎鏀簥 3 n -榫熷崪 3 n -榫熷帉涓嶅憡 3 l -榫熷3 33 n -榫熷3鑺 3 n -榫熷ご 34 n -榫熷ご鐐 3 n -榫熷北 23 ns -榫熷北涔 3 ns -榫熷北宀 3 ns -榫熷勾楣ゅ 3 ns -榫熷勾楣ょ畻 3 l -榫熸枃 3 nz -榫熸枃鍐欒抗 3 n -榫熸枃楦熻抗 3 n -榫熸澘 10 n -榫熸瘺鍏嶈 3 n -榫熸瘺鍏旇 3 n -榫熸邯 3 ns -榫熺帀 3 nz -榫熺帇 3 nz -榫熺敳 92 ns -榫熺敳鑳 3 nz -榫熺 3 n -榫熺汗 3 n -榫熺缉 29 v -榫熻倝 3 n -榫熻儗 21 n -榫熻儗绔 3 n -榫熻嫇鑶 3 n -榫熻嫍 3 n -榫熻 34 v -榫熻冻 5 v -榫熼壌 2 n -榫熼暅 3 nz -榫熼硸 3 n -榫熼工閬愬 3 l -榫熼緞楣ょ畻 3 n -榫熼緳鐗囩敳 3 nz -榫熼緳楹熷嚖 3 ns -榫 5 g -榫 732 zg diff --git a/cppcommon/vec_functs.cpp b/cppcommon/vec_functs.cpp deleted file mode 100644 index 75d188c..0000000 --- a/cppcommon/vec_functs.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/************************************ - * file enc : utf8 - * author : wuyanyi09@gmail.com -************************************/ -#include "vec_functs.h" - -#ifdef TEST_VEC_FUNCTS -using namespace CPPCOMMON; -int main() -{ - vector vec; - for(int i=0;i<5;i++) - vec.push_back(i); - vector pats; - pats.push_back(0); - pats.push_back(3); - //pats.push_back(4); - vector > > res; - splitVec(vec,res,pats); - cout<(vec, 0)<(vec, -1)< -#include -#include -#include -#include - -#define FOR_VECTOR(vec, i) for(size_t i = 0; i < vec.size(); i++) - -#define PRINT_VECTOR(vec) FOR_VECTOR(vec, i)\ -{\ - cout< +#include +#include "str_functs.hpp" +#include "map_functs.hpp" +#include "vec_functs.hpp" + +namespace Limonp +{ + using namespace std; + class ArgvContext + { + public : + ArgvContext(int argc, const char* const * argv) + { + + for(int i = 0; i < argc; i++) + { + if(strStartsWith(argv[i], "-")) + { + if(i + 1 < argc && !strStartsWith(argv[i + 1], "-")) + { + _mpss[argv[i]] = argv[i+1]; + i++; + } + else + { + _sset.insert(argv[i]); + } + } + else + { + _args.push_back(argv[i]); + } + } + } + ~ArgvContext(){}; + public: + string toString() + { + stringstream ss; + ss<(_args)<(_mpss)<(_sset); + return ss.str(); + } + string operator [](uint i) + { + if(i < _args.size()) + { + return _args[i]; + } + return ""; + } + string operator [](const string& key) + { + map::const_iterator it = _mpss.find(key); + if(it != _mpss.end()) + { + return it->second; + } + return ""; + } + public: + bool hasKey(const string& key) + { + if(_mpss.find(key) != _mpss.end() || _sset.find(key) != _sset.end()) + { + return true; + } + return false; + } + private: + vector _args; + map _mpss; + set _sset; + + }; +} + +#endif diff --git a/limonp/MysqlClient.hpp b/limonp/MysqlClient.hpp new file mode 100644 index 0000000..c157157 --- /dev/null +++ b/limonp/MysqlClient.hpp @@ -0,0 +1,107 @@ +#ifndef LIMONP_MYSQLCLIENT_H +#define LIMONP_MYSQLCLIENT_H + +#include +#include +#include +#include +#include "logger.hpp" +#include "vec_functs.hpp" + +namespace Limonp +{ + using namespace std; + class MysqlClient + { + public: + typedef vector< vector > RowsType; + private: + const char * const HOST; + const unsigned int PORT; + const char * const USER; + const char * const PASSWD; + const char * const DB; + public: + MysqlClient(const char* host, uint port, const char* user, const char* passwd, const char* db): HOST(host), PORT(port), USER(user), PASSWD(passwd), DB(db){ _conn = NULL;}; + ~MysqlClient(){dispose();}; + public: + bool init() + { + //cout< vec; + for(uint i = 0; i < num_fields; i ++) + { + row[i] ? vec.push_back(row[i]) : vec.push_back("NULL"); + } + rows.push_back(vec); + } + mysql_free_result(result); + return true; + } + + private: + MYSQL * _conn; + + }; +} + +#endif diff --git a/limonp/cast_functs.hpp b/limonp/cast_functs.hpp new file mode 100644 index 0000000..cf8c54f --- /dev/null +++ b/limonp/cast_functs.hpp @@ -0,0 +1,87 @@ +#ifndef LIMONP_CAST_FUNCTS_H +#define LIMONP_CAST_FUNCTS_H + +namespace Limonp +{ + //logical and or + static const int sign_32 = 0xC0000000; + static const int exponent_32 = 0x07800000; + static const int mantissa_32 = 0x007FE000; + static const int sign_exponent_32 = 0x40000000; + static const int loss_32 = 0x38000000; + + static const short sign_16 = (short)0xC000; + static const short exponent_16 = (short)0x3C00; + static const short mantissa_16 = (short)0x03FF; + static const short sign_exponent_16 = (short)0x4000; + static const int exponent_fill_32 = 0x38000000; + + //infinite + static const short infinite_16 = (short) 0x7FFF; + static const short infinitesmall_16 = (short) 0x0000; + + inline float intBitsToFloat(unsigned int x) + { + union + { + float f; + int i; + }u; + u.i = x; + return u.f; + } + + inline int floatToIntBits(float f) + { + union + { + float f; + int i ; + }u; + u.f = f; + return u.i; + } + + inline short floatToShortBits(float f) + { + int fi = floatToIntBits(f); + + // 鎻愬彇鍏抽敭淇℃伅 + short sign = (short) ((unsigned int)(fi & sign_32) >> 16); + short exponent = (short) ((unsigned int)(fi & exponent_32) >> 13); + short mantissa = (short) ((unsigned int)(fi & mantissa_32) >> 13); + // 鐢熸垚缂栫爜缁撴灉 + short code = (short) (sign | exponent | mantissa); + // 鏃犵┓澶ч噺銆佹棤绌峰皬閲忕殑澶勭悊 + if ((fi & loss_32) > 0 && (fi & sign_exponent_32) > 0) { + // 褰撴寚鏁扮鍙蜂负1鏃(姝f鏂)锛屼笖宸234浣嶄负1锛岃繑鍥炴棤绌峰ぇ閲 + return (short) (code | infinite_16); + } + if (((fi & loss_32) ^ loss_32) > 0 && (fi & sign_exponent_32) == 0) { + // 褰撴寚鏁扮鍙蜂綅0鏃(璐熸鏂)锛屼笖宸234浣嶄负0(涓111寮傛垨>0)锛岃繑鍥炴棤绌峰皬閲 + return infinitesmall_16; + } + + return code; + } + + inline float shortBitsToFloat(short s) + { + /* + * 鎸囨暟绌轰綑3浣嶏細鑻ョ鍙蜂綅涓1锛岃ˉ0锛涜嫢绗﹀彿浣嶄负0锛岃ˉ1銆 灏炬暟浣嶅湪鍚庤ˉ0(13涓) + */ + int sign = ((int) (s & sign_16)) << 16; + int exponent = ((int) (s & exponent_16)) << 13; + // 鎸囨暟绗﹀彿浣嶄负0锛234浣嶈ˉ1 + if ((s & sign_exponent_16) == 0 && s != 0) { + exponent |= exponent_fill_32; + } + int mantissa = ((int) (s & mantissa_16)) << 13; + // 鐢熸垚瑙g爜缁撴灉 + int code = sign | exponent | mantissa; + return intBitsToFloat(code); + + } +} + +#endif diff --git a/limonp/config.hpp b/limonp/config.hpp new file mode 100644 index 0000000..3e45e6d --- /dev/null +++ b/limonp/config.hpp @@ -0,0 +1,107 @@ +/************************************ + * file enc : utf8 + * author : wuyanyi09@gmail.com + ************************************/ +#ifndef LIMONP_CONFIG_H +#define LIMONP_CONFIG_H + + +#include +#include +#include +#include "logger.hpp" +#include "str_functs.hpp" + +namespace Limonp +{ + using std::map; + using std::string; + using std::cout; + using std::endl; + using std::ifstream; + class Config + { + public: + Config(){_isInit = false;}; + ~Config(){}; + bool init(const string& configFile) + { + if(_isInit) + { + LogFatal("already have been initialized. "); + return false; + } + ifstream ifile(configFile.c_str()); + if(!ifile) + { + LogFatal("open configFile[%s] failed.", configFile.c_str()); + return false; + } + string line, key, value; + vector vecBuf; + while(getline(ifile, line)) + { + //line = _stripComment(line); + if(line.empty()) + { + continue; + } + vecBuf.clear(); + if(!splitStr(line, vecBuf, "=") || 2 != vecBuf.size()) + { + LogFatal("line[%s] is illegal.", line.c_str()); + return false; + } + key = vecBuf[0]; + value = vecBuf[1]; + if(_map.end() != _map.find(key)) + { + LogFatal("key[%s] already exists.", key.c_str()); + return false; + } + _map[key] = value; + } + ifile.close(); + _isInit = true; + return true; + } + void display() + { + for(map::iterator it = _map.begin(); it != _map.end(); it++) + { + cout<<"("<first<<","<second<<")"< _map; + bool _isInit; + + }; +} + +namespace Limonp +{ + extern Config gConfig; +} + +#endif diff --git a/limonp/io_functs.hpp b/limonp/io_functs.hpp new file mode 100644 index 0000000..0128e62 --- /dev/null +++ b/limonp/io_functs.hpp @@ -0,0 +1,82 @@ +/************************************ + * file enc : utf8 + * author : wuyanyi09@gmail.com +************************************/ +#ifndef LIMONP_IO_FUNCTS_H +#define LIMONP_IO_FUNCTS_H +#include +#include +#include +namespace Limonp +{ + using namespace std; + inline string loadFile2Str(const char * const filepath) + { + ifstream in(filepath); + if(!in) + { + return ""; + } + istreambuf_iterator beg(in), end; + string str(beg, end); + in.close(); + return str; + } + + inline void loadStr2File(const char * const filename, ios_base::openmode mode, const string& str) + { + ofstream out(filename, mode); + ostreambuf_iterator itr (out); + copy(str.begin(), str.end(), itr); + out.close(); + } + + inline int ReadFromFile(const char * fileName, char* buf, int maxCount, const char* mode) + { + FILE* fp = fopen(fileName, mode); + if (!fp) + return 0; + int ret; + fgets(buf, maxCount, fp) ? ret = 1 : ret = 0; + fclose(fp); + return ret; + } + + inline int WriteStr2File(const char* fileName, const char* buf, const char* mode) + { + FILE* fp = fopen(fileName, mode); + if (!fp) + return 0; + int n = fprintf(fp, "%s", buf); + fclose(fp); + return n; + } + + inline bool checkFileExist(const string& filePath) + { + fstream _file; + _file.open(filePath.c_str(), ios::in); + if(_file) + return true; + return false; + } + + inline bool createDir(const string& dirPath, bool p = true) + { + string dir_str(dirPath); + string cmd = "mkdir"; + if(p) + { + cmd += " -p"; + } + cmd += " " + dir_str; + int res = system(cmd.c_str()); + return res; + } + + inline bool checkDirExist(const string& dirPath) + { + return checkFileExist(dirPath); + } +} +#endif diff --git a/limonp/logger.hpp b/limonp/logger.hpp new file mode 100644 index 0000000..5ccfdec --- /dev/null +++ b/limonp/logger.hpp @@ -0,0 +1,79 @@ +/************************************ + * file enc : utf8 + * author : wuyanyi09@gmail.com + ************************************/ +#ifndef LIMONP_LOGGER_H +#define LIMONP_LOGGER_H + +#include +#include +#include +#include +#include +#include +#include "io_functs.hpp" +#include "str_functs.hpp" +#include "typedefs.h" + +#define LogDebug(fmt, ...) Logger::LoggingF(LL_DEBUG, __FILE__, __LINE__, fmt, ## __VA_ARGS__) +#define LogInfo(fmt, ...) Logger::LoggingF(LL_INFO, __FILE__, __LINE__, fmt, ## __VA_ARGS__) +#define LogWarn(fmt, ...) Logger::LoggingF(LL_WARN, __FILE__, __LINE__, fmt, ## __VA_ARGS__) +#define LogError(fmt, ...) Logger::LoggingF(LL_ERROR, __FILE__, __LINE__, fmt, ## __VA_ARGS__) +#define LogFatal(fmt, ...) Logger::LoggingF(LL_FATAL, __FILE__, __LINE__, fmt, ## __VA_ARGS__) + + +namespace Limonp +{ + using namespace std; + enum {LL_DEBUG = 0, LL_INFO = 1, LL_WARN = 2, LL_ERROR = 3, LL_FATAL = 4, LEVEL_ARRAY_SIZE = 5, CSTR_BUFFER_SIZE = 1024}; + static const char * LOG_LEVEL_ARRAY[LEVEL_ARRAY_SIZE]= {"DEBUG","INFO","WARN","ERROR","FATAL"}; + static const char * LOG_FORMAT = "%s %s:%d %s %s\n"; + static const char * LOG_TIME_FORMAT = "%Y-%m-%d %H:%M:%S"; + + class Logger + { + public: + static bool Logging(uint level, const string& msg, const char* fileName, int lineNo) + { + if(level > LL_FATAL) + { + cerr<<"level's value is out of range"< -1 && n < size) { + msg.resize(n); + break; + } + if (n > -1) + size = n + 1; + else + size *= 2; + } + return Logging(level, msg, fileName, lineNo); + } + }; +} + +#endif diff --git a/limonp/macro_def.hpp b/limonp/macro_def.hpp new file mode 100644 index 0000000..58fe835 --- /dev/null +++ b/limonp/macro_def.hpp @@ -0,0 +1,22 @@ +#ifndef LIMONP_MACRO_DEF_H +#define LIMONP_MACRO_DEF_H + +#define XX_GET_SET(varType, varName, funName)\ +private: varType varName;\ +public: inline varType get##funName(void) const {return varName;}\ +public: inline void set##funName(varType var) {varName = var;} + +#define XX_GET(varType, varName, funName)\ +private: varType varName;\ +public: inline varType get##funName(void) const {return varName;} + +#define XX_SET(varType, varName, funName)\ +private: varType varName;\ +public: inline void set##funName(varType var) {varName = var;} + +#define XX_GET_SET_BY_REF(varType, varName, funName)\ +private: varType varName;\ +public: inline const varType& get##funName(void) const {return varName;}\ +public: inline void set##funName(const varType& var){varName = var;} + +#endif diff --git a/cppcommon/map_functs.h b/limonp/map_functs.hpp similarity index 68% rename from cppcommon/map_functs.h rename to limonp/map_functs.hpp index 787d78d..9479691 100644 --- a/cppcommon/map_functs.h +++ b/limonp/map_functs.hpp @@ -4,18 +4,20 @@ ************************************/ -#ifndef CPPCOMMON_MAP_FUNCTS_H -#define CPPCOMMON_MAP_FUNCTS_H +#ifndef LIMONP_MAP_FUNCTS_H +#define LIMONP_MAP_FUNCTS_H #include #include #include #include +#include "typedefs.h" -namespace CPPCOMMON +namespace Limonp { using namespace std; + template string setToString(const set& st) { @@ -58,6 +60,26 @@ namespace CPPCOMMON return ss.str(); } + template + string HashMapToString(const HashMap& mp) + { + if(mp.empty()) + { + return "{}"; + } + stringstream ss; + ss<<'{'; + typename HashMap::const_iterator it = mp.begin(); + ss<first<<": "<second; + it++; + while(it != mp.end()) + { + ss<<", "<first<<": "<second; + it++; + } + ss<<'}'; + return ss.str(); + } template string pairToString(const pair& p) { @@ -87,6 +109,15 @@ namespace CPPCOMMON return it->second; } + template + void map2Vec(const map& mp, vector > & res) + { + typename map::const_iterator it = mp.begin(); + for(; it != mp.end(); it++) + { + res.push_back(*it); + } + } } #endif diff --git a/limonp/str_functs.hpp b/limonp/str_functs.hpp new file mode 100644 index 0000000..0116995 --- /dev/null +++ b/limonp/str_functs.hpp @@ -0,0 +1,223 @@ +/************************************ + * file enc : ascii + * author : wuyanyi09@gmail.com + ************************************/ +#ifndef LIMONP_STR_FUNCTS_H +#define LIMONP_STR_FUNCTS_H +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "typedefs.h" +#include +#include +namespace Limonp +{ + using namespace std; + inline string string_format(const char* fmt, ...) + { + int size = 256; + std::string str; + va_list ap; + while (1) { + str.resize(size); + va_start(ap, fmt); + int n = vsnprintf((char *)str.c_str(), size, fmt, ap); + va_end(ap); + if (n > -1 && n < size) { + str.resize(n); + return str; + } + if (n > -1) + size = n + 1; + else + size *= 2; + } + return str; + } + + inline void string_format(string& res, const char* fmt, ...) + { + int size = 256; + va_list ap; + while (1) { + res.resize(size); + va_start(ap, fmt); + int n = vsnprintf((char *)res.c_str(), size, fmt, ap); + va_end(ap); + if (n > -1 && n < size) { + res.resize(n); + return; + } + if (n > -1) + size = n + 1; + else + size *= 2; + } + } + + inline bool joinStr(const vector& src, string& dest, const string& connectorStr) + { + if(src.empty()) + { + return false; + } + for(uint i = 0; i < src.size() - 1; i++) + { + dest += src[i]; + dest += connectorStr; + } + dest += src[src.size() - 1]; + return true; + } + + inline string joinStr(const vector& source, const string& connector) + { + string res; + joinStr(source, res, connector); + return res; + } + + inline bool splitStr(const string& src, vector& res, const string& pattern) + { + if(src.empty()) + { + return false; + } + res.clear(); + + size_t start = 0; + size_t end = 0; + while(start < src.size()) + { + end = src.find_first_of(pattern, start); + if(string::npos == end) + { + res.push_back(src.substr(start)); + return true; + } + res.push_back(src.substr(start, end - start)); + if(end == src.size() - 1) + { + res.push_back(""); + break; + } + start = end + 1; + } + return true; + } + + inline string upperStr(const string& strIn) + { + string str = strIn; + transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper); + return str; + } + + inline string lowerStr(const string& strIn) + { + string str = strIn; + transform(str.begin(), str.end(), str.begin(), (int (*)(int))tolower); + return str; + } + + inline std::string <rim(std::string &s) + { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(std::isspace)))); + return s; + } + + inline std::string &rtrim(std::string &s) + { + s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun(std::isspace))).base(), s.end()); + return s; + } + + inline std::string &trim(std::string &s) + { + return ltrim(rtrim(s)); + } + + + inline uint16_t twocharToUint16(char high, char low) + { + return (((uint16_t(high) & 0x00ff ) << 8) | (uint16_t(low) & 0x00ff)); + } + + inline pair uint16ToChar2(uint16_t in) + { + pair res; + res.first = (in>>8) & 0x00ff; //high + res.second = (in) & 0x00ff; //low + return res; + } + + inline bool strStartsWith(const string& str, const string& prefix) + { + //return str.substr(0, prefix.size()) == prefix; + if(prefix.length() > str.length()) + { + return false; + } + return 0 == str.compare(0, prefix.length(), prefix); + } + + inline bool strEndsWith(const string& str, const string& suffix) + { + if(suffix.length() > str.length()) + { + return false; + } + return 0 == str.compare(str.length() - suffix.length(), suffix.length(), suffix); + } + + inline bool isInStr(const string& str, char ch) + { + return str.find(ch) != string::npos; + } + + inline void extractWords(const string& sentence, vector& words) + { + bool flag = false; + uint lhs = 0, len = 0; + for(uint i = 0; i < sentence.size(); i++) + { + char x = sentence[i]; + if((0x0030 <= x && x<= 0x0039) || (0x0041 <= x && x <= 0x005a ) || (0x0061 <= x && x <= 0x007a)) + { + if(flag) + { + len ++; + } + else + { + lhs = i; + len = 1; + } + flag = true; + } + else + { + if(flag) + { + words.push_back(string(sentence, lhs, len)); + } + flag = false; + } + } + if(flag) + { + words.push_back(string(sentence, lhs, len)); + } + } + + +} +#endif diff --git a/cppcommon/typedefs.h b/limonp/typedefs.h similarity index 69% rename from cppcommon/typedefs.h rename to limonp/typedefs.h index 41b888a..a8da002 100644 --- a/cppcommon/typedefs.h +++ b/limonp/typedefs.h @@ -2,14 +2,16 @@ * file enc : utf8 * author : wuyanyi09@gmail.com ************************************/ -#ifndef CPPCOMMON_TYPEDEFS_H -#define CPPCOMMON_TYPEDEFS_H +#ifndef LIMONP_TYPEDEFS_H +#define LIMONP_TYPEDEFS_H #include #include #include +#include +#define HashMap std::tr1::unordered_map -namespace CPPCOMMON +namespace Limonp { typedef std::vector Unicode; typedef std::vector::const_iterator UnicodeConstIterator; diff --git a/cppcommon/vec_functs.tcc b/limonp/vec_functs.hpp similarity index 60% rename from cppcommon/vec_functs.tcc rename to limonp/vec_functs.hpp index ae06339..ac18548 100644 --- a/cppcommon/vec_functs.tcc +++ b/limonp/vec_functs.hpp @@ -1,36 +1,66 @@ /************************************ - * file enc : utf8 + * file enc : ascii * author : wuyanyi09@gmail.com ************************************/ - -#ifndef CPPCOMMON_VEC_FUNCTS_TCC -#define CPPCOMMON_VEC_FUNCTS_TCC - +#ifndef LIMONP_VEC_FUNCTS_H +#define LIMONP_VEC_FUNCTS_H +#include +#include +#include +#include +#include #include #include #include #include #include #include -namespace CPPCOMMON + +#define FOR_VECTOR(vec, i) for(size_t i = 0; i < vec.size(); i++) + +#define PRINT_VECTOR(vec) FOR_VECTOR(vec, i)\ +{\ + cout< - string vecToString(const vector& vec) - { - if(vec.empty()) - { - return "[]"; - } - stringstream ss; - ss<<"["< + bool vecToString(const vector& vec, string& res) + { + if(vec.empty()) + { + res = "[]"; + return false; + } + stringstream ss; + ss<<"[\""< + string vecToString(const vector& vec) + { + string res; + vecToString(vec, res); + return res; + } + template bool isInVec(const vector& vec, const T& item) { @@ -107,7 +137,6 @@ namespace CPPCOMMON outVec.push_back(tmp); } } - } -#endif +#endif