|
- //////////////////////////////////////////////////////////////////////
- // String splitter
- // Written by Jonathan A. Foster <ChipMaster@YeOlPiShack.net>
- // Started April 23rd, 2021
- // Copyright JF Possibilities, Inc. All rights reserved.
- //////////////////////////////////////////////////////////////////////
- #include <string.h>
- #include <stdexcept>
- // Sounds an awful lot like a German pastry
- #include "strutil.h"
-
-
-
- //////////////////////////////////////////////////////////////////////
- // Splits
- //////////////////////////////////////////////////////////////////////
-
- Splits &Splits::operator=(const char *_line) {
- if(strlen(_line)>=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 && line[len]) {
- if(line[len]==sep) {
- line[len++]=0;
- if(combine) while(len<LineMax && line[len]==sep) len++;
- if(++count<FieldMax) {
- // this shouldn't happen
- if(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<s.size() && s[x]<=' '; x++);
- for(y=s.size()-1; y>=x && s[y]<=' '; y--);
- if(y<x) return "";
- return s.substr(x, y-x+1);
- }
|