//////////////////////////////////////////////////////////////////////// // $Id: UtilString.cxx,v 1.1 2011/01/18 05:49:20 finch Exp $ // // A collection of string utilities beyond what's normally available // // messier@huhepl.harvard.edu //////////////////////////////////////////////////////////////////////// #include #include #include "UtilString.hxx" #include #include using std::endl; //...................................................................... void COMET::UtilString::MakePrintable(const char* in, std::string& out) { //====================================================================== // Append string 'in' to 'out ' in a form that can be printed within // double or single quotes. //====================================================================== bool hasSpecial = false; int index = 0; while ( unsigned char c = in[index++] ) { if ( c == '\\' || c == '\n' || c == '\t' || c == '\'' || c == '\"' || c <= '\x08' || (c >= '\x0b' && c <= '\x1f' ) || c >= '\x7f' ) { hasSpecial = true; break; } } if ( ! hasSpecial ) { out += in; return; } index = 0; while ( unsigned char c = in[index++] ) { // Skip really unprintable ones. if ( c <= '\x08' || (c >= '\x0b' && c <= '\x1f' ) || c >= '\x7f' ) continue; if ( c == '\\' || c == '\n' || c == '\t' || c == '\'' || c == '\"') { switch ( c ) { case '\\': out += "\\\\"; break; case '\n': out += "\\n"; break; case '\t': out += "\\t"; break; case '\'': out += "\\\'"; break; case '\"': out += "\\\""; break; } } else out += c; } } //...................................................................... void COMET::UtilString::StringTok(std::vector& ls, const std::string& str, const std::string& tok) { //====================================================================== // Split a long string into a set of shorter strings spliting along // divisions makers by the characters listed in the token string //====================================================================== const string::size_type S = str.size(); const string::size_type toksz = tok.size(); string::size_type i = 0; while (i < S) { // eat leading whitespace while ( (i