mirror of
https://github.com/yanyiwu/cppjieba.git
synced 2025-07-18 00:00:12 +08:00
update cppcommon -> limonp
This commit is contained in:
parent
ec948309cc
commit
64f2b8812d
@ -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)
|
||||
|
@ -1,110 +0,0 @@
|
||||
#include "argv_functs.h"
|
||||
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
bool getArgvMap(int argc, const char* const * argv, map<string,string>& 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<<vecToString<string>(_args)<<mapToString<string, string>(_mpss)<<setToString<string>(_sset);
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
string ArgvContext::operator [](uint i)
|
||||
{
|
||||
if(i < _args.size())
|
||||
{
|
||||
return _args[i];
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
string ArgvContext::operator [](const string& key)
|
||||
{
|
||||
map<string, string>::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<string,string> argvMap;
|
||||
//getArgvMap(argc, argv, argvMap);
|
||||
//PRINT_MAPSS(argvMap);
|
||||
ArgvContext arg(argc, argv);
|
||||
cout<<arg.toString()<<endl;
|
||||
cout<<arg[1]<<endl;
|
||||
cout<<arg["--hehe"]<<endl;
|
||||
cout<<pairToString<int,double>(pair<int, double>(1,1.2))<<endl;
|
||||
cout<<arg.isKeyExist("-help")<<endl;
|
||||
return 0;
|
||||
>>>>>>> e81a664dae9397a6bf937b6dd6482832f67a6cf6
|
||||
}
|
||||
|
||||
#endif
|
@ -1,38 +0,0 @@
|
||||
/************************************
|
||||
* file enc : ascii
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
|
||||
#ifndef CPPCOMMON_ARGV_FUNCTS_H
|
||||
#define CPPCOMMON_ARGV_FUNCTS_H
|
||||
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#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<string, string>& 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<string> _args;
|
||||
map<string, string> _mpss;
|
||||
set<string> _sset;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -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<string> 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<string, string>::iterator it = _map.begin(); it != _map.end(); it++)
|
||||
{
|
||||
cout<<"("<<it->first<<","<<it->second<<")"<<endl;
|
||||
}
|
||||
}
|
||||
|
||||
string Config::getByKey(const string& key)
|
||||
{
|
||||
if(_map.end() != _map.find(key))
|
||||
{
|
||||
return _map[key];
|
||||
}
|
||||
else
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
string Config::_stripComment(const string& line)
|
||||
{
|
||||
string res = line;
|
||||
string::size_type pos = res.find('#');
|
||||
if(string::npos != pos)
|
||||
{
|
||||
res = res.substr(0, pos);
|
||||
}
|
||||
return stripStr(res);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
Config gConfig;
|
||||
}
|
||||
|
||||
|
||||
#ifdef CONFIG_UT
|
||||
using namespace CPPCOMMON;
|
||||
int main()
|
||||
{
|
||||
gConfig.init("1.conf");
|
||||
gConfig.init("1.conf");
|
||||
gConfig.display();
|
||||
cout<<gConfig.getByKey("a")<<endl;
|
||||
cout<<gConfig.getByKey("cm")<<endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
@ -1,42 +0,0 @@
|
||||
/************************************
|
||||
* file enc : utf8
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
#ifndef CPPCOMMON_CONFIG_H
|
||||
#define CPPCOMMON_CONFIG_H
|
||||
|
||||
|
||||
#include <map>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#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<string, string> _map;
|
||||
bool _isInit;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
extern Config gConfig;
|
||||
}
|
||||
|
||||
#endif
|
@ -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<string>(_encVec, enc))
|
||||
// {
|
||||
// //default
|
||||
// _encoding = UTF8ENC;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// _encoding = enc;
|
||||
// }
|
||||
//}
|
||||
|
||||
//UnicodeEncoding::~UnicodeEncoding()
|
||||
//{
|
||||
//}
|
||||
//
|
||||
//bool UnicodeEncoding::setEncoding(const string& enc)
|
||||
//{
|
||||
// if(!isInVec<string>(_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<<line<<endl;
|
||||
cout<<line.size()<<endl;
|
||||
cout<<enc.getWordLength(line)<<endl;
|
||||
enc.decode(line, unicode);
|
||||
printUnicode(unicode);
|
||||
cout<<enc.encode(unicode)<<endl;
|
||||
}
|
||||
ifile.close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
@ -1,37 +0,0 @@
|
||||
/************************************
|
||||
* file enc : utf8
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
|
||||
#ifndef CPPCOMMON_ENCODING_H
|
||||
#define CPPCOMMON_ENCODING_H
|
||||
|
||||
#include "str_functs.h"
|
||||
#include "vec_functs.h"
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
using namespace std;
|
||||
|
||||
//const char* const UTF8ENC = "utf-8";
|
||||
//const char* const GBKENC = "gbk";
|
||||
|
||||
//class UnicodeEncoding
|
||||
//{
|
||||
// private:
|
||||
// string _encoding;
|
||||
// vector<string> _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
|
@ -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 <iostream>
|
||||
using namespace CPPCOMMON;
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
char filename[] = "1/2/3";
|
||||
if(!checkFileExist(filename))
|
||||
{
|
||||
createDir(filename);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
@ -1,21 +0,0 @@
|
||||
/************************************
|
||||
* file enc : utf8
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
#ifndef CPPCOMMON_FILE_FUNCTS_H
|
||||
#define CPPCOMMON_FILE_FUNCTS_H
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stdlib.h>
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
using namespace std;
|
||||
bool checkFileExist(const string& filePath);
|
||||
bool createDir(const string& dirPath, bool p = true);
|
||||
bool checkDirExist(const string& dirPath);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -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
|
@ -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<char> beg(in), end;
|
||||
string str(beg, end);
|
||||
in.close();
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef TEST_IO_FUNCTS
|
||||
#include <iostream>
|
||||
using namespace CPPCOMMON;
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
char filename[] = "1/2/3";
|
||||
cout<<loadFile2Str("1")<<endl;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
@ -1,14 +0,0 @@
|
||||
/************************************
|
||||
* file enc : utf8
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
#ifndef CPPCOMMON_IO_FUNCTS_H
|
||||
#define CPPCOMMON_IO_FUNCTS_H
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
using namespace std;
|
||||
string loadFile2Str(const char * const filepath);
|
||||
}
|
||||
#endif
|
@ -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"<<endl;
|
||||
return false;
|
||||
}
|
||||
time(&_timeNow);
|
||||
size_t ret = strftime(_cStrBuf, sizeof(_cStrBuf), _timeFormat, localtime(&_timeNow));
|
||||
if(0 == ret)
|
||||
{
|
||||
fprintf(stderr, "stftime failed.\n");
|
||||
return false;
|
||||
}
|
||||
if(level >= 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
|
||||
|
@ -1,61 +0,0 @@
|
||||
/************************************
|
||||
* file enc : utf8
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
#ifndef CPPCOMMON_LOGGER_H
|
||||
#define CPPCOMMON_LOGGER_H
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#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
|
@ -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<string>& 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<string> splitStr(const string& source, const string& pattern)
|
||||
{
|
||||
vector<string> res;
|
||||
splitStr(source, res, pattern);
|
||||
return res;
|
||||
}
|
||||
|
||||
bool splitStr(const string& source, vector<string>& 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<int, int>(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<int, int>(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<string>& outVec,
|
||||
const vector<string>& 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<char,char> pa = uint16ToChar2(vec[i]);
|
||||
res += pa.first;
|
||||
res += pa.second;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#ifdef TEST_STR_FUNCTS
|
||||
#include <iostream>
|
||||
using namespace CPPCOMMON;
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
vector<string> vec;
|
||||
splitStr("1 3 4", vec);
|
||||
for(uint i =0;i < vec.size(); i++)
|
||||
{
|
||||
cout<<vec[i]<<endl;
|
||||
}
|
||||
cout<<strStartsWith("--help","--")<<endl;
|
||||
cout<<strStartsWith("--help","-")<<endl;
|
||||
cout<<strStartsWith("--help","he")<<endl;
|
||||
cout<<strStartsWith("help","help")<<endl;
|
||||
cout<<strStartsWith("","help")<<endl;
|
||||
cout<<strStartsWith("hel","")<<endl;
|
||||
cout<<strEndsWith("hel","")<<endl;
|
||||
cout<<strEndsWith("hel","el")<<endl;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
@ -1,85 +0,0 @@
|
||||
/************************************
|
||||
* file enc : ascii
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
#ifndef CPPCOMMON_STR_FUNCTS_H
|
||||
#define CPPCOMMON_STR_FUNCTS_H
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <map>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <memory.h>
|
||||
#include "typedefs.h"
|
||||
#include <functional>
|
||||
#include <locale>
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
using namespace std;
|
||||
string string_format(const char*, ...) ;
|
||||
string joinStr(const vector<string>& source, const string& connector);
|
||||
vector<string> splitStr(const string& source, const string& pattern = " \t\n");
|
||||
bool splitStr(const string& source, vector<string>& res, const string& pattern = " \t\n");
|
||||
bool splitStrMultiPatterns(
|
||||
const string& strSrc,
|
||||
vector<string>& outVec,
|
||||
const vector<string>& 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<char, char> uint16ToChar2(uint16_t in)
|
||||
{
|
||||
pair<char, char> res;
|
||||
res.first = (in>>8) & 0x00ff; //high
|
||||
res.second = (in) & 0x00ff; //low
|
||||
return res;
|
||||
}
|
||||
|
||||
inline void printUnicode(const Unicode& unicode)
|
||||
{
|
||||
cout<<uniVecToStr(unicode)<<endl;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
101
cppcommon/testdata/dict.gbk
vendored
101
cppcommon/testdata/dict.gbk
vendored
@ -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
|
102
cppcommon/testdata/dict.utf8
vendored
102
cppcommon/testdata/dict.utf8
vendored
@ -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
|
||||
龟壳 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
|
@ -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<int> vec;
|
||||
for(int i=0;i<5;i++)
|
||||
vec.push_back(i);
|
||||
vector<int> pats;
|
||||
pats.push_back(0);
|
||||
pats.push_back(3);
|
||||
//pats.push_back(4);
|
||||
vector<pair<int, vector<int> > > res;
|
||||
splitVec<int>(vec,res,pats);
|
||||
cout<<isInVec<int>(vec, 0)<<endl;
|
||||
cout<<isInVec<int>(vec, -1)<<endl;
|
||||
for(int i =0;i<res.size();i++)
|
||||
{
|
||||
cout<<"first:"<<res[i].first<<endl;
|
||||
cout<<"seconde:"<<endl;
|
||||
for(int j = 0;j<res[i].second.size();j++)
|
||||
{
|
||||
cout<<res[i].second[j]<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
@ -1,35 +0,0 @@
|
||||
/************************************
|
||||
* file enc : ascii
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
#ifndef CPPCOMMON_VEC_FUNCTS_H
|
||||
#define CPPCOMMON_VEC_FUNCTS_H
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
|
||||
#define FOR_VECTOR(vec, i) for(size_t i = 0; i < vec.size(); i++)
|
||||
|
||||
#define PRINT_VECTOR(vec) FOR_VECTOR(vec, i)\
|
||||
{\
|
||||
cout<<vec[i]<<endl;\
|
||||
}
|
||||
|
||||
#define PRINT_MATRIX(mat) FOR_VECTOR(mat, i) \
|
||||
{\
|
||||
FOR_VECTOR(mat[i], j)\
|
||||
{\
|
||||
cout<<"["<<i<<","<<j<<"]:"<<mat[i][j]<<endl;\
|
||||
}\
|
||||
}
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
using namespace std;
|
||||
}
|
||||
|
||||
#include "vec_functs.tcc"
|
||||
|
||||
#endif
|
86
limonp/ArgvContext.hpp
Normal file
86
limonp/ArgvContext.hpp
Normal file
@ -0,0 +1,86 @@
|
||||
/************************************
|
||||
* file enc : ascii
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
|
||||
#ifndef LIMONP_ARGV_FUNCTS_H
|
||||
#define LIMONP_ARGV_FUNCTS_H
|
||||
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#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<<vecToString<string>(_args)<<mapToString<string, string>(_mpss)<<setToString<string>(_sset);
|
||||
return ss.str();
|
||||
}
|
||||
string operator [](uint i)
|
||||
{
|
||||
if(i < _args.size())
|
||||
{
|
||||
return _args[i];
|
||||
}
|
||||
return "";
|
||||
}
|
||||
string operator [](const string& key)
|
||||
{
|
||||
map<string, string>::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<string> _args;
|
||||
map<string, string> _mpss;
|
||||
set<string> _sset;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
107
limonp/MysqlClient.hpp
Normal file
107
limonp/MysqlClient.hpp
Normal file
@ -0,0 +1,107 @@
|
||||
#ifndef LIMONP_MYSQLCLIENT_H
|
||||
#define LIMONP_MYSQLCLIENT_H
|
||||
|
||||
#include <mysql/mysql.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "logger.hpp"
|
||||
#include "vec_functs.hpp"
|
||||
|
||||
namespace Limonp
|
||||
{
|
||||
using namespace std;
|
||||
class MysqlClient
|
||||
{
|
||||
public:
|
||||
typedef vector< vector<string> > 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<<mysql_get_client_info()<<endl;
|
||||
if(NULL == (_conn = mysql_init(NULL)))
|
||||
{
|
||||
LogError("mysql_init faield. %s", mysql_error(_conn));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mysql_real_connect(_conn, HOST, USER, PASSWD, DB, PORT, NULL, 0) == NULL)
|
||||
{
|
||||
LogError("mysql_real_connect failed. %s", mysql_error(_conn));
|
||||
mysql_close(_conn);
|
||||
_conn = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
//set reconenct
|
||||
char value = 1;
|
||||
mysql_options(_conn, MYSQL_OPT_RECONNECT, &value);
|
||||
|
||||
return true;
|
||||
}
|
||||
bool dispose()
|
||||
{
|
||||
if(NULL != _conn)
|
||||
{
|
||||
mysql_close(_conn);
|
||||
}
|
||||
_conn = NULL;
|
||||
return true;
|
||||
}
|
||||
bool executeSql(const char* sql)
|
||||
{
|
||||
if(NULL == _conn)
|
||||
{
|
||||
LogError("_conn is NULL");
|
||||
return false;
|
||||
}
|
||||
if(mysql_query(_conn, sql))
|
||||
{
|
||||
LogError("mysql_query failed. %s", mysql_error(_conn));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool select(const char* sql, RowsType& rows)
|
||||
{
|
||||
if(!executeSql(sql))
|
||||
{
|
||||
LogError("executeSql failed. [%s]", sql);
|
||||
return false;
|
||||
}
|
||||
MYSQL_RES * result = mysql_store_result(_conn);
|
||||
if(NULL == result)
|
||||
{
|
||||
LogError("mysql_store_result failed.[%d]", mysql_error(_conn));
|
||||
}
|
||||
uint num_fields = mysql_num_fields(result);
|
||||
MYSQL_ROW row;
|
||||
while((row = mysql_fetch_row(result)))
|
||||
{
|
||||
vector<string> 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
|
87
limonp/cast_functs.hpp
Normal file
87
limonp/cast_functs.hpp
Normal file
@ -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时(正次方),且左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;
|
||||
// 生成解码结果
|
||||
int code = sign | exponent | mantissa;
|
||||
return intBitsToFloat(code);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
107
limonp/config.hpp
Normal file
107
limonp/config.hpp
Normal file
@ -0,0 +1,107 @@
|
||||
/************************************
|
||||
* file enc : utf8
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
#ifndef LIMONP_CONFIG_H
|
||||
#define LIMONP_CONFIG_H
|
||||
|
||||
|
||||
#include <map>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#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<string> 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<string, string>::iterator it = _map.begin(); it != _map.end(); it++)
|
||||
{
|
||||
cout<<"("<<it->first<<","<<it->second<<")"<<endl;
|
||||
}
|
||||
}
|
||||
bool get(const string& key, string& value)
|
||||
{
|
||||
if(_map.end() != _map.find(key))
|
||||
{
|
||||
value = _map[key];
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private:
|
||||
//bool _stripComment(const string& line, string& res)
|
||||
//{
|
||||
// res = line;
|
||||
// string::size_type pos = res.find('#');
|
||||
// if(string::npos != pos)
|
||||
// {
|
||||
// res = res.substr(0, pos);
|
||||
// }
|
||||
// trim(res);
|
||||
//}
|
||||
private:
|
||||
map<string, string> _map;
|
||||
bool _isInit;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
namespace Limonp
|
||||
{
|
||||
extern Config gConfig;
|
||||
}
|
||||
|
||||
#endif
|
82
limonp/io_functs.hpp
Normal file
82
limonp/io_functs.hpp
Normal file
@ -0,0 +1,82 @@
|
||||
/************************************
|
||||
* file enc : utf8
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
#ifndef LIMONP_IO_FUNCTS_H
|
||||
#define LIMONP_IO_FUNCTS_H
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
namespace Limonp
|
||||
{
|
||||
using namespace std;
|
||||
inline string loadFile2Str(const char * const filepath)
|
||||
{
|
||||
ifstream in(filepath);
|
||||
if(!in)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
istreambuf_iterator<char> 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<char> 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
|
79
limonp/logger.hpp
Normal file
79
limonp/logger.hpp
Normal file
@ -0,0 +1,79 @@
|
||||
/************************************
|
||||
* file enc : utf8
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
#ifndef LIMONP_LOGGER_H
|
||||
#define LIMONP_LOGGER_H
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#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"<<endl;
|
||||
return false;
|
||||
}
|
||||
char buf[CSTR_BUFFER_SIZE];
|
||||
time_t timeNow;
|
||||
time(&timeNow);
|
||||
size_t ret = strftime(buf, sizeof(buf), LOG_TIME_FORMAT, localtime(&timeNow));
|
||||
if(0 == ret)
|
||||
{
|
||||
fprintf(stderr, "stftime failed.\n");
|
||||
return false;
|
||||
}
|
||||
fprintf(stderr, LOG_FORMAT, buf, fileName, lineNo,LOG_LEVEL_ARRAY[level], msg.c_str());
|
||||
return true;
|
||||
}
|
||||
static bool 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
22
limonp/macro_def.hpp
Normal file
22
limonp/macro_def.hpp
Normal file
@ -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
|
@ -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 <map>
|
||||
#include <set>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "typedefs.h"
|
||||
|
||||
namespace CPPCOMMON
|
||||
namespace Limonp
|
||||
{
|
||||
using namespace std;
|
||||
|
||||
|
||||
template <typename T>
|
||||
string setToString(const set<T>& st)
|
||||
{
|
||||
@ -58,6 +60,26 @@ namespace CPPCOMMON
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
template<typename T1, typename T2>
|
||||
string HashMapToString(const HashMap<T1, T2>& mp)
|
||||
{
|
||||
if(mp.empty())
|
||||
{
|
||||
return "{}";
|
||||
}
|
||||
stringstream ss;
|
||||
ss<<'{';
|
||||
typename HashMap<T1, T2>::const_iterator it = mp.begin();
|
||||
ss<<it->first<<": "<<it->second;
|
||||
it++;
|
||||
while(it != mp.end())
|
||||
{
|
||||
ss<<", "<<it->first<<": "<<it->second;
|
||||
it++;
|
||||
}
|
||||
ss<<'}';
|
||||
return ss.str();
|
||||
}
|
||||
template<typename T1, typename T2>
|
||||
string pairToString(const pair<T1, T2>& p)
|
||||
{
|
||||
@ -87,6 +109,15 @@ namespace CPPCOMMON
|
||||
return it->second;
|
||||
}
|
||||
|
||||
template<class kT, class vT>
|
||||
void map2Vec(const map<kT, vT>& mp, vector<pair<kT, vT> > & res)
|
||||
{
|
||||
typename map<kT, vT>::const_iterator it = mp.begin();
|
||||
for(; it != mp.end(); it++)
|
||||
{
|
||||
res.push_back(*it);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
223
limonp/str_functs.hpp
Normal file
223
limonp/str_functs.hpp
Normal file
@ -0,0 +1,223 @@
|
||||
/************************************
|
||||
* file enc : ascii
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
#ifndef LIMONP_STR_FUNCTS_H
|
||||
#define LIMONP_STR_FUNCTS_H
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <map>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <memory.h>
|
||||
#include "typedefs.h"
|
||||
#include <functional>
|
||||
#include <locale>
|
||||
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<string>& 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<string>& source, const string& connector)
|
||||
{
|
||||
string res;
|
||||
joinStr(source, res, connector);
|
||||
return res;
|
||||
}
|
||||
|
||||
inline bool splitStr(const string& src, vector<string>& 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<int, int>(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<int, int>(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<char, char> uint16ToChar2(uint16_t in)
|
||||
{
|
||||
pair<char, char> 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<string>& 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
|
@ -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 <stdint.h>
|
||||
#include <vector>
|
||||
#include <sys/types.h>
|
||||
#include <tr1/unordered_map>
|
||||
#define HashMap std::tr1::unordered_map
|
||||
|
||||
namespace CPPCOMMON
|
||||
namespace Limonp
|
||||
{
|
||||
typedef std::vector<uint16_t> Unicode;
|
||||
typedef std::vector<uint16_t>::const_iterator UnicodeConstIterator;
|
@ -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 <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
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<<vec[i]<<endl;\
|
||||
}
|
||||
|
||||
#define PRINT_MATRIX(mat) FOR_VECTOR(mat, i) \
|
||||
{\
|
||||
FOR_VECTOR(mat[i], j)\
|
||||
{\
|
||||
cout<<"["<<i<<","<<j<<"]:"<<mat[i][j]<<endl;\
|
||||
}\
|
||||
}
|
||||
|
||||
namespace Limonp
|
||||
{
|
||||
using namespace std;
|
||||
template<typename T>
|
||||
string vecToString(const vector<T>& vec)
|
||||
{
|
||||
if(vec.empty())
|
||||
{
|
||||
return "[]";
|
||||
}
|
||||
stringstream ss;
|
||||
ss<<"["<<vec[0];
|
||||
for(unsigned int i = 1; i < vec.size(); i++)
|
||||
{
|
||||
ss<<","<<vec[i];
|
||||
}
|
||||
ss<<"]";
|
||||
return ss.str();
|
||||
}
|
||||
using namespace std;
|
||||
template <typename T>
|
||||
bool vecToString(const vector<T>& vec, string& res)
|
||||
{
|
||||
if(vec.empty())
|
||||
{
|
||||
res = "[]";
|
||||
return false;
|
||||
}
|
||||
stringstream ss;
|
||||
ss<<"[\""<<vec[0];
|
||||
for(uint i = 1; i < vec.size(); i++)
|
||||
{
|
||||
ss<<"\", \""<<vec[i];
|
||||
}
|
||||
ss<<"\"]";
|
||||
res = ss.str();
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
string vecToString(const vector<T>& vec)
|
||||
{
|
||||
string res;
|
||||
vecToString(vec, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool isInVec(const vector<T>& vec, const T& item)
|
||||
{
|
||||
@ -107,7 +137,6 @@ namespace CPPCOMMON
|
||||
outVec.push_back(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user