musictok.h

musicmud-2.1.6/include/musictok.h · 788 B

From the archive musicmud2.1.6.tar.gz

Download the archive   « Back to the archive contents

#ifndef MUSICTOK
#define MUSICTOK

//! a string tokenizer - wrapper for strtok_r.
class StrTok {
  char *d;
  char *usr;
  int i;

public:

  StrTok(const char *str) {
    if (str)
      d = strdup(str);
    else
      d = 0;
    i = 1;
  }
  ~StrTok() {
    if (d)
      strfree(d);
  }
  
  //! get the next token in the sting. delimiter /tok/.
  const char *next(const char *tok) {
    if (!d) return NULL;
    const char *f=strtok_r(i?d:NULL, tok, &usr);
    i = 0;
    return f;
  }
};

inline std::vector<string> tokenize(string s) {
  std::vector<string> v;
  while (1) {
    string::size_type n = s.find(' ');
    if (n == string::npos)
      break;
    v.push_back(s.substr(0, n));
    s = s.substr(n+1);
  }

  if (s.length()) {
    v.push_back(s);
  }
  
  return v;
}

#endif

Site Stats

Snippets: 1791
Codebases: 571