#include #include /** String utility functions. @author Stuart D. Gathman Copyright (C) 2000 Business Management Systems, Inc. */ size_t pbrk(string s,string c) { const char *cp = s.c_str(); const char *p = strpbrk(cp,c.c_str()); if (p == 0) return s.npos; return p - cp; } /** Separate a delimited string into an array. */ int split(string s,Array &a,string sep) { int cnt = 0; int lastpos = 0; const char *cp = s.c_str(); const char *sp = sep.c_str(); for (;;) { const char *p = strpbrk(cp + lastpos,sp); if (p == 0) break; int pos = p - cp; a.add(string(s,lastpos,pos - lastpos)); ++cnt; lastpos = pos + 1; } a.add(string(s,lastpos)); return ++cnt; } #if 0 /** Test split with command line args. */ int main(int argc,char **argv) { if (argc != 3) return 1; Array a; string str(argv[1]); string sep(argv[2]); int n = split(str,a,sep); cout << "n = " << n << endl; for (int i = 0; i < n; ++i) { cout << a[i] << endl; } return 0; } #endif