#ifndef UTILITIES_H #define UTILITIES_H /////////////////////////////////////////////////////////////////////// // Utilities.h - small, generally useful, helper classes // // ver 1.3 // // Language: C++, Visual Studio 2015 // // Application: Most Projects, CSE687 - Object Oriented Design // // Author: Jim Fawcett, Syracuse University, CST 4-187 // // jfawcett@twcny.rr.com // /////////////////////////////////////////////////////////////////////// /* * Package Operations: * ------------------- * This package provides classes StringHelper and Converter and a global * function putline(). This class will be extended continuously for * awhile to provide convenience functions for general C++ applications. * * Build Process: * -------------- * Required Files: Utilities.h, Utilities.cpp * * Build Command: devenv Utilities.sln /rebuild debug * * Maintenance History: * -------------------- * ver 1.3 : 01 Jan 2018 * - fixed indexing bugs in StringHelper::trim * ver 1.2 : 22 Feb 2015 * - changed default underline char in Title(const std::string&, Char) * ver 1.1 : 06 Feb 2015 * - fixed bug in split which turns a comma separated string into * a vector of tokens. * - added comments * ver 1.0 : 05 Feb 2016 * - first release * * Planned Additions and Changes: * ------------------------------ * - none yet */ #include #include #include #include #include namespace Utilities { class StringHelper { public: static void Title(const std::string& src, char underline = '='); static void title(const std::string& src); template static std::basic_string trim(const std::basic_string& toTrim); template static std::vector> split(const std::basic_string& toSplit, T splitOn = ','); }; /*--- remove whitespace from front and back of string argument ---*/ template std::basic_string StringHelper::trim(const std::basic_string& toTrim) { if (toTrim.size() == 0) return toTrim; std::basic_string temp; std::locale loc; std::basic_string::const_iterator iter = toTrim.begin(); while (isspace(*iter, loc)) { if (++iter == toTrim.end()) { break; } } for (; iter != toTrim.end(); ++iter) { temp += *iter; } std::basic_string::reverse_iterator riter; size_t pos = temp.size(); for (riter = temp.rbegin(); riter != temp.rend(); ++riter) { --pos; if (!isspace(*riter, loc)) { break; } } if(0 <= pos && pos < temp.size()) temp.erase(++pos); return temp; } /*--- split sentinel separated strings into a vector of trimmed strings ---*/ template std::vector> StringHelper::split(const std::basic_string& toSplit, T splitOn) { std::vector> splits; std::basic_string temp; std::basic_string::const_iterator iter; for (iter = toSplit.begin(); iter != toSplit.end(); ++iter) { if (*iter != splitOn) { temp += *iter; } else { splits.push_back(trim(temp)); temp.clear(); } } if (temp.length() > 0) splits.push_back(trim(temp)); return splits; } template class Tester { public: bool execute(T t,const std::string& name, std::ostream& out = std::cout); private: void check(bool result, std::ostream& out); }; template bool Tester::execute(T t, const std::string& name, std::ostream& out) { bool result = false; try { out << "\n test \"" << name << "\""; result = t(); } catch (std::exception& ex) { out << "\n " << ex.what(); } check(result, out); return result; } template void Tester::check(bool result, std::ostream& out) { if (result) out << " passed\n"; else out << " failed\n"; } void putline(); template class Converter { public: static std::string toString(const T& t); static T toValue(const std::string& src); }; template std::string Converter::toString(const T& t) { std::ostringstream out; out << t; return out.str(); } template T Converter::toValue(const std::string& src) { std::istringstream in(src); T t; in >> t; return t; } } #endif