////////////////////////////////////////////////////////////////////// // String splitter & other useful string tools // Written by Jonathan A. Foster // Started April 23rd, 2021 // Copyright JF Possibilities, Inc. All rights reserved. // Copied with permission from JF Possibilities's C++ lib. ////////////////////////////////////////////////////////////////////// #include #include #include // Sounds an awful lot like a German pastry #include "strutil.h" ////////////////////////////////////////////////////////////////////// // Generic string transformations ////////////////////////////////////////////////////////////////////// std::string trim(const std::string &s) { int x, y; for(x=0; x=x && s[y]<=' '; y--); if(y=LineMax) throw std::runtime_error("Splits::Splits(char*): string is longer than buffer"); strncpy(line, _line, LineMax-1); split(); return *this; } int Splits::split() { len = count = 0; if(!*line) return count; fields[0] = line; while(len=LineMax) throw std::runtime_error("Splits::split: end of buffer null missing!"); fields[count] = line+len; } else throw std::runtime_error("Splits::split: Too many fields in the line"); } else len++; } return count++; } std::istream &operator>>(std::istream &in, Splits &sp) { if(in.getline(sp.line, sp.LineMax-1)) sp.split(); return in; } ////////////////////////////////////////////////////////////////////// // pre_match() ////////////////////////////////////////////////////////////////////// bool pre_match(const char **list, const std::string &s) { const char *p = s.c_str(); for(; *list; list++) if(!strncmp(*list, p, strlen(*list))) return true; return false; } // And if vectors can be used... bool pre_match(const StringList &list, const std::string &s) { for( StringList::const_iterator p=list.begin(); p!=list.end(); p++ ) if(s.substr(0, p->size())==*p) return true; return false; } ////////////////////////////////////////////////////////////////////// // misc ////////////////////////////////////////////////////////////////////// std::string strip(const std::string &s) { int x, y; for(x=0; x=x && s[y]<=' '; y--); if(y