From 8b3a1920371f86114a1254cf8a9ddcecf9ad37a9 Mon Sep 17 00:00:00 2001 From: gwdwyy Date: Sat, 31 Aug 2013 13:07:15 +0800 Subject: [PATCH] update cppcommon for argv --- src/cppcommon/argv_functs.cpp | 23 ++++++++++++++++------- src/cppcommon/argv_functs.h | 6 ++++++ src/cppcommon/map_functs.h | 24 +++++++++++++++++++++++- src/cppcommon/vec_functs.tcc | 17 +++++++++++++++++ 4 files changed, 62 insertions(+), 8 deletions(-) diff --git a/src/cppcommon/argv_functs.cpp b/src/cppcommon/argv_functs.cpp index 8b7d17f..ad17393 100644 --- a/src/cppcommon/argv_functs.cpp +++ b/src/cppcommon/argv_functs.cpp @@ -1,5 +1,6 @@ #include "argv_functs.h" + namespace CPPCOMMON { bool getArgvMap(int argc, const char* const * argv, map& mpss) @@ -27,16 +28,16 @@ namespace CPPCOMMON { for(int i = 0; i < argc; i++) { - if(strStartsWith(argv[i], "--")) + if(strStartsWith(argv[i], "-")) { - if(i + 1 < argc && !strStartsWith(argv[i+1], "--")) + if(i + 1 < argc && !strStartsWith(argv[i + 1], "-")) { _mpss[argv[i]] = argv[i+1]; i++; } else { - break; + _sset.insert(argv[i]); } } else @@ -52,10 +53,9 @@ namespace CPPCOMMON string ArgvContext::toString() { - string res; - res += string_format("[%s]\n", joinStr(_args, ", ").c_str()); - res += mapToString(_mpss); - return res; + stringstream ss; + ss<(_args)<(_mpss)<(_sset); + return ss.str(); } string ArgvContext::operator [](uint i) @@ -77,6 +77,14 @@ namespace CPPCOMMON return ""; } + bool ArgvContext::isKeyExist(const string& key) + { + if(_mpss.find(key) != _mpss.end() || _sset.find(key) != _sset.end()) + { + return true; + } + return false; + } } @@ -94,6 +102,7 @@ int main(int argc, char** argv) cout<(pair(1,1.2))< +#include #include "str_functs.h" #include "map_functs.h" +#include "vec_functs.h" namespace CPPCOMMON { @@ -22,9 +25,12 @@ namespace CPPCOMMON string toString(); string operator [](uint i); string operator [](const string& key); + public: + bool isKeyExist(const string& key); private: vector _args; map _mpss; + set _sset; }; } diff --git a/src/cppcommon/map_functs.h b/src/cppcommon/map_functs.h index f7775bd..e37cf5d 100644 --- a/src/cppcommon/map_functs.h +++ b/src/cppcommon/map_functs.h @@ -8,6 +8,7 @@ #define CPPCOMMON_MAP_FUNCTS_H #include +#include #include #include @@ -15,6 +16,27 @@ namespace CPPCOMMON { using namespace std; + template + string setToString(const set& st) + { + if(st.empty()) + { + return "{}"; + } + stringstream ss; + ss<<'{'; + typename set::const_iterator it = st.begin(); + ss<<*it; + it++; + while(it != st.end()) + { + ss<<", "<<*it; + it++; + } + ss<<'}'; + return ss.str(); + } + template string mapToString(const map& mp) { @@ -32,7 +54,7 @@ namespace CPPCOMMON ss<<", "<first<<": "<second; it++; } - ss<<"}"; + ss<<'}'; return ss.str(); } diff --git a/src/cppcommon/vec_functs.tcc b/src/cppcommon/vec_functs.tcc index f262971..ae06339 100644 --- a/src/cppcommon/vec_functs.tcc +++ b/src/cppcommon/vec_functs.tcc @@ -11,9 +11,26 @@ #include #include #include +#include namespace CPPCOMMON { using namespace std; + template + string vecToString(const vector& vec) + { + if(vec.empty()) + { + return "[]"; + } + stringstream ss; + ss<<"["< bool isInVec(const vector& vec, const T& item) {