Warning, /include/Geant4/tools/tokenize is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_tokenize
0005 #define tools_tokenize
0006
0007 #include <string>
0008 #include <vector>
0009
0010 namespace tools {
0011
0012 inline bool double_quotes_get_token(const std::string& a_cmd,std::string::size_type a_begIdx,
0013 std::string::size_type& a_endIdx,std::string& a_token) {
0014 while ( a_cmd[a_begIdx] == ' ') ++a_begIdx; // Loop checking, 23.06.2015, I. Hrivnacova
0015 if ( a_cmd[a_begIdx] == '"' ) {
0016 a_endIdx = a_cmd.find('"', a_begIdx+1);
0017 if ( a_endIdx == std::string::npos ) a_endIdx = a_cmd.length();
0018 a_token = a_cmd.substr(a_begIdx+1, (a_endIdx-1)-a_begIdx);
0019 ++a_endIdx;
0020 }
0021 else {
0022 a_endIdx = a_cmd.find(' ', a_begIdx);
0023 if ( a_endIdx == std::string::npos ) a_endIdx = a_cmd.length();
0024 a_token = a_cmd.substr(a_begIdx, a_endIdx-a_begIdx);
0025 }
0026 return ( a_token.length() > 0 );
0027 }
0028
0029 // From a string, double_quotes_tokenize permits to get arguments
0030 // by taking into account strings enclosed by double quotes and potentially
0031 // containing spaces (for example to specify a file path containing spaces).
0032 // Example:
0033 // if a_cmd is the string: aa bbb "ccc ddd" ee "fff gg"
0034 // Returned a_args string list will be:
0035 // "aa", "bb", "ccc ddd", "ee", "fff gg"
0036 // Algorithm from Geant4/G4AnalysisUtilities.cc/Tokenize() done by Ivana Hrivnacova.
0037
0038 inline void double_quotes_tokenize(const std::string& a_cmd, std::vector<std::string>& a_args) {
0039 std::string::size_type begIdx = 0;
0040 std::string::size_type endIdx = 0;
0041 std::string token;
0042 do {
0043 if ( double_quotes_get_token(a_cmd, begIdx, endIdx, token) ) {
0044 a_args.push_back(token);
0045 }
0046 begIdx = endIdx + 1;
0047 }
0048 while ( endIdx < a_cmd.length() ); // Loop checking, 23.06.2015, I. Hrivnacova
0049 }
0050
0051
0052 inline void words(const std::string& a_string,const std::string& a_sep,bool a_take_empty,
0053 std::vector<std::string>& a_words,bool a_clear = true){
0054 // If a_sep is for exa "|" and for "xxx||xxx" :
0055 // - a_take_empty false : {"xxx","xxx"} will be created
0056 // (and NOT {"xxx","","xxx"}).
0057 // - a_take_empty true : {"xxx","","xxx"} will be created.
0058 if(a_clear) a_words.clear();
0059 if(a_string.empty()) return;
0060 std::string::size_type lim = (a_take_empty?0:1);
0061 if(a_sep.empty()) {
0062 a_words.push_back(a_string);
0063 } else {
0064 std::string::size_type l = a_string.length();
0065 std::string::size_type llimiter = a_sep.length();
0066 std::string::size_type pos = 0;
0067 while(true) {
0068 std::string::size_type index = a_string.find(a_sep,pos);
0069 if(index==std::string::npos){ // Last word.
0070 if((l-pos)>=lim) a_words.push_back(a_string.substr(pos,l-pos));
0071 break;
0072 } else {
0073 // abcxxxef
0074 // 0 3 67
0075 if((index-pos)>=lim) a_words.push_back(a_string.substr(pos,index-pos));
0076 pos = index + llimiter;
0077 }
0078 }
0079 }
0080 }
0081
0082 }
0083
0084 #endif