Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <vector>
- #include <istream>
- #include <cstdarg>
- using namespace std;
- // [...]
- vector<string> tokenize (istream &in, bool cont, int num, ...) {
- vector<string> tokens;
- if (!in.good () || num < 1) return tokens;
- string line;
- bool foundLine = false;
- // read lines until we find something tokenizable
- while (in.good ()) {
- line = "";
- while (line == "" && in.good ()) // skip blank lines
- getline (in, line, '\n');
- if (line == "") break; // eof
- // comments are defined as lines where the first
- // non-whitespace character is #
- // we would also like to skip lines containing only whitespace
- int firstChar = 0; // index of first non-whitespace
- while (firstChar < line.size ()) {
- if (line[firstChar] == ' ')
- firstChar++;
- else break;
- }
- if (firstChar >= line.size ()) // line contains only whitespace
- continue;
- if (line[firstChar] == '#') // comment
- continue;
- foundLine = true;
- break;
- }
- if (!foundLine) // eof
- return tokens;
- // tokenizing to a vector of size 1 is easy
- if (num == 1) {
- tokens.push_back (line);
- return tokens;
- }
- // tokenize!
- va_list delimList;
- va_start (delimList, num);
- char current; // last-used delimiter saved for later
- int lastDelim = -1; // index of last-used delimiter
- for (int x = 0; x < (num-1); x++) {
- // cstdarg doesn't like casting to char, so we cast to int first
- current = va_arg (delimList, int);
- int nextDelim = line.find (current, lastDelim+1);
- string tok = line.substr (lastDelim+1, nextDelim-lastDelim-1);
- tokens.push_back (tok);
- lastDelim = nextDelim;
- // break if we didn't actually find the last delimiter
- if (lastDelim == string::npos) break;
- }
- va_end (delimList);
- // don't continue - we want exactly num tokens (or less)
- if (!cont) {
- // add the final token, unless we've already run out of line
- if (lastDelim != string::npos)
- tokens.push_back (line.substr (lastDelim+1));
- return tokens;
- }
- // continue tokenizing using final delimiter until line is exhausted
- while (lastDelim != string::npos) {
- int nextDelim = line.find (current, lastDelim+1);
- string tok = line.substr (lastDelim+1, nextDelim-lastDelim-1);
- if (tok != "") tokens.push_back (tok);
- lastDelim = nextDelim;
- }
- return tokens;
- }
Add Comment
Please, Sign In to add comment