update cppcommon

This commit is contained in:
gwdwyy 2013-08-12 19:55:52 +08:00
parent 15a06fad9b
commit f6142c6ff0
9 changed files with 113 additions and 14 deletions

View File

@ -15,6 +15,11 @@ void testKeyWordExt(const char * dictPath, const char * filePath)
cerr<<__FILE__<<__LINE__<<endl; cerr<<__FILE__<<__LINE__<<endl;
return ; return ;
} }
if(!ext.loadStopWords("../dicts/stopwords.gbk.v1.0"))
{
cerr<<__FILE__<<__LINE__<<endl;
return ;
}
ifstream ifile(filePath); ifstream ifile(filePath);
vector<string> res; vector<string> res;
string line; string line;

View File

@ -29,6 +29,8 @@ io_functs.test: io_functs.cpp io_functs.h
g++ -o $@ $< -DTEST_IO_FUNCTS g++ -o $@ $< -DTEST_IO_FUNCTS
str_functs.ut: str_functs.cpp str_functs.h str_functs.ut: str_functs.cpp str_functs.h
g++ -o $@ $< -DTEST_STR_FUNCTS 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 encoding.ut: encoding.cpp str_functs.cpp str_functs.h encoding.h
g++ -o $@ encoding.cpp str_functs.cpp -DENCODING_UT g++ -o $@ encoding.cpp str_functs.cpp -DENCODING_UT
vec_functs.test: vec_functs.cpp vec_functs.h vec_functs.tcc vec_functs.test: vec_functs.cpp vec_functs.h vec_functs.tcc

View File

@ -0,0 +1,42 @@
#include "argv_functs.h"
namespace CPPCOMMON
{
bool getArgvMap(int argc, char** 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;
}
}
#ifdef ARGV_FUNCTS_UT
using namespace CPPCOMMON;
int main(int argc, char** argv)
{
map<string,string> argvMap;
getArgvMap(argc, argv, argvMap);
PRINT_MAPSS(argvMap);
return 0;
}
#endif

View File

@ -0,0 +1,18 @@
/************************************
* file enc : ascii
* author : wuyanyi09@gmail.com
************************************/
#ifndef CPPCOMMON_ARGV_FUNCTS_H
#define CPPCOMMON_ARGV_FUNCTS_H
#include "str_functs.h"
#include "map_functs.h"
namespace CPPCOMMON
{
using namespace std;
bool getArgvMap(int argc, char** argv, map<string, string>& mpss);
}
#endif

View File

@ -9,5 +9,7 @@
#include "config.h" #include "config.h"
#include "typedefs.h" #include "typedefs.h"
#include "str_functs.h" #include "str_functs.h"
#include "map_functs.h"
#include "arfv_functs.h"
#endif #endif

View File

@ -0,0 +1,18 @@
/************************************
* file enc : ascii
* author : wuyanyi09@gmail.com
************************************/
#ifndef CPPCOMMON_MAP_FUNCTS_H
#define CPPCOMMON_MAP_FUNCTS_H
#define PRINT_MAPSS(mpss) \
{\
for(map<string, string>::const_iterator it = mpss.begin(); it != mpss.end(); it++)\
{\
cout<<it->first<<' '<<it->second<<endl;\
}\
}
#endif

View File

@ -7,7 +7,7 @@
namespace CPPCOMMON namespace CPPCOMMON
{ {
//http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf //http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf
std::string string_format(const std::string fmt, ...) string string_format(const string fmt, ...)
{ {
int size = 100; int size = 100;
std::string str; std::string str;
@ -407,6 +407,11 @@ namespace CPPCOMMON
return res; return res;
} }
bool strStartsWith(const string& str, const string& prefix)
{
return str.substr(0, prefix.size()) == prefix;
}
} }
#ifdef TEST_STR_FUNCTS #ifdef TEST_STR_FUNCTS
@ -452,16 +457,16 @@ int main()
// cout<<utf8str<<endl; // cout<<utf8str<<endl;
//} //}
//cout<<string_format("hehe%s11asd%dasf","[here]",2); //cout<<string_format("hehe%s11asd%dasf","[here]",2);
ifstream ifile("testdata/dict.gbk"); //ifstream ifile("testdata/dict.gbk");
string line; //string line;
Unicode unicode; //Unicode unicode;
while(getline(ifile, line)) //while(getline(ifile, line))
{ //{
cout<<line<<endl; // cout<<line<<endl;
utf8ToUnicode(line, unicode); // utf8ToUnicode(line, unicode);
printUnicode(unicode); // printUnicode(unicode);
cout<<unicodeToUtf8(unicode)<<endl;; // cout<<unicodeToUtf8(unicode)<<endl;;
} //}
//vector<string> tmp; //vector<string> tmp;
//tmp.push_back("1"); //tmp.push_back("1");
////tmp.push_back("2"); ////tmp.push_back("2");
@ -476,6 +481,12 @@ int main()
// s = utf8ToGbk(s); // s = utf8ToGbk(s);
// cout<<s<<endl; // cout<<s<<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;
return 0; return 0;
} }
#endif #endif

View File

@ -1,5 +1,5 @@
/************************************ /************************************
* file enc : utf8 * file enc : ascii
* author : wuyanyi09@gmail.com * author : wuyanyi09@gmail.com
************************************/ ************************************/
#ifndef CPPCOMMON_STR_FUNCTS_H #ifndef CPPCOMMON_STR_FUNCTS_H
@ -20,7 +20,7 @@
namespace CPPCOMMON namespace CPPCOMMON
{ {
using namespace std; using namespace std;
std::string string_format(const std::string fmt, ...) ; string string_format(const string fmt, ...) ;
string joinStr(const vector<string>& source, const string& connector); string joinStr(const vector<string>& source, const string& connector);
vector<string> splitStr(const string& source, const string& pattern = " \t\n"); vector<string> splitStr(const string& source, const string& pattern = " \t\n");
void splitStr(const string& source, vector<string>& out_vec, const string& pattern = " \t\n"); void splitStr(const string& source, vector<string>& out_vec, const string& pattern = " \t\n");
@ -67,6 +67,7 @@ namespace CPPCOMMON
cout<<uniVecToStr(unicode)<<endl; cout<<uniVecToStr(unicode)<<endl;
} }
bool strStartsWith(const string& str, const string& prefix);
} }
#endif #endif

View File

@ -1,5 +1,5 @@
/************************************ /************************************
* file enc : utf8 * file enc : ascii
* author : wuyanyi09@gmail.com * author : wuyanyi09@gmail.com
************************************/ ************************************/
#ifndef CPPCOMMON_VEC_FUNCTS_H #ifndef CPPCOMMON_VEC_FUNCTS_H