void test_helpers_detail::check_equal(const char* expected[], const string_vector& actual) { const char** expected_iter = expected; string_vector::const_iterator actual_iter = actual.begin(); bool equals = true; while (equals && *expected_iter != NULL && actual_iter != actual.end()) { if (*expected_iter != *actual_iter) { equals = false; } else { expected_iter++; actual_iter++; } } if (equals && ((*expected_iter == NULL && actual_iter != actual.end()) || (*expected_iter != NULL && actual_iter == actual.end()))) equals = false; if (!equals) { std::cerr << "EXPECTED:\n"; for (expected_iter = expected; *expected_iter != NULL; expected_iter++) std::cerr << *expected_iter << "\n"; std::cerr << "ACTUAL:\n"; for (actual_iter = actual.begin(); actual_iter != actual.end(); actual_iter++) std::cerr << *actual_iter << "\n"; ATF_FAIL("Expected results differ to actual values"); } }
inline void write_file( const boost::filesystem::path& path, const string_vector& lines ) { std::ofstream ostr(path.string()); std::copy( lines.begin(), lines.end(), std::ostream_iterator<std::string>(ostr, "\n") ); }
string print_layouts(const string_vector& sv) { ostringstream oss; bool fst = true; oss << "["; for(string_vector::const_iterator i=sv.begin(); i!=sv.end(); i++) { if(!fst) oss << " "; oss << *i; fst = false; } oss << "]"; return oss.str(); }
// returns true if symbol is ok bool filter(const string_vector& nonsyms, const std::string& symbol) { if(symbol.empty()) return false; // Filter out all prohibited words string_vector::const_iterator r = find(nonsyms.begin(), nonsyms.end(), symbol); if(r != nonsyms.end()) return false; // Filter out all numbers groups started with number if(isdigit(symbol[0])) return false; return true; }
//功能:文件转换成string类的vector容器,文件中每一行字符串对应容器中的一个元素 //参数: // fileName: string类型,文件名 // sVec;string_vector& 类型,文件转换得到的容器 //返回值:int类型,0-文件转换正常,非0值表示转换异常 int CXxwCppPub::FileToVector(string fileName, string_vector& sVec) { //1 先讲文件转换成文件流 ifstream inFile(fileName.c_str()); if (!inFile) { return 1; } //2 将文件内容读入到string类的vector容器中,每一行对应容器中的一个元素 string s; while(getline(inFile, s)) { sVec.push_back(s); } inFile.close();//关闭文件 if (inFile.eof()) { return 0;//文件结束 } else if (inFile.bad()) { return 2;//发生系统故障 } else if (inFile.fail()) { return 3;//读入数据失败 } return 0; }
void safe_push_back(string_vector& v, std::string s, std::string note) { if(s.empty()) return; if(!note.empty()) { s += "(" + note + ")"; } v.push_back(s); }
string_vector FindFiles(const string_vector &files, time_t changed_since) { string_vector result; for (int i = 0; i < files.size(); i++) { FindFiles(files[i], &result, changed_since); } sort(result.begin(), result.end()); return result; }
inline void read_file( const boost::filesystem::path& path, string_vector& lines ) { std::string line; std::ifstream istr(path.string()); while (std::getline(istr, line)) lines.push_back(line); }
client::int_type client::recv_multi_bulk_reply_(string_vector & out) { int_type length = recv_bulk_reply_(prefix_multi_bulk_reply); if (length == -1) throw key_error("no such key"); for (int_type i = 0; i < length; ++i) out.push_back(recv_bulk_reply_()); return length; }
//功能:在源字符串中查找指定的多个字符串,如果找到任意一个就返回true,一个都没找到返回false //参数: // sSrc: string & 类型,源字符串 // vecSub: const string_vector &类型,查找的多个字符串 //返回值:bool类型,找到任意一个就返回true,一个都没找到返回false bool CXxwCppPub::FindSubStr(string& sSrc, const string_vector &vecSub) { bool bFind = false; if (sSrc.empty() || vecSub.empty()) { return bFind; } typedef string_vector::const_iterator ItSub;//常迭代器,不会修改容器 for (ItSub it = vecSub.begin(); it != vecSub.end(); ++it) { string sSub = *it; if (sSrc.find(sSub) != string::npos) { return true;//找到了一个字符串,返回true } } return bFind; }
void XKeyboard::BuildLayout(string_vector& vec) { XkbRF_VarDefsRec vdr; int i = 0; char str[20] = {0}; const char s[2] = ","; char *token; char* tmp = NULL; strcpy(str, ( !_display ? NO_KEYBOARD : (XkbRF_GetNamesProp(_display, &tmp, &vdr) && vdr.layout) ? vdr.layout : DFLT_XKB_LAYOUT)); /* get the first token */ token = strtok(str, s); /* walk through other tokens */ while( token != NULL ) { vec.push_back(token); token = strtok(NULL, s); } }
void argument_parser::parse(string_vector & tokens) { for(std::size_t i = 0, end = tokens.size(); i < end; i++) { std::string const & token = tokens[i]; if(token.empty()) continue; if(token[0] == '-') { std::string name = token.substr(1); variable_vector::iterator iterator = std::find(variables.begin(), variables.end(), name); if(iterator == variables.end()) continue; argument_variable & variable = *iterator; i++; if(i == end) throw exception("Variable \"" + name + "\" is lacking an argument"); std::string argument = tokens[i]; switch(variable.type) { case argument_variable_type_bool: { bool value; if(!string_to_bool(argument, value)) throw exception("Invalid boolean value specified for variable \"" + name + "\""); *reinterpret_cast<bool *>(variable.address) = value; break; } case argument_variable_type_string: *reinterpret_cast<std::string *>(variable.address) = argument; break; default: throw exception("Unknown variable type encountered in argument parser"); break; } variable.handled = true; } } BOOST_FOREACH(argument_variable & variable, variables) { if(!variable.handled) { if(variable.required) throw exception("Required variable \"" + variable.name + "\" has not been specified"); else if(variable.default_value) { switch(variable.type) { case argument_variable_type_bool: variable.set_default_value<bool>(); break; case argument_variable_type_string: variable.set_default_value<std::string>(); break; } } } } }
bool console_command::match(std::string const & match_command, string_vector const & arguments) const { return command == match_command && argument_count == arguments.size(); }