/*
 * Copyright (c) 2007 Vittorio De Tomasi <vittorio at detomasi dot it>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */


#ifndef __VDTPARSER__
#define __VDTPARSER__

#include <vector>
#include <string>

class VDTParser {
public:
// constructor - by default ' ' is used as token separator
   VDTParser(std::string s = "", std::string c = " "){term = c;parse(s);}
// this method replaces the previous parsed string with string s
	void reset(std::string s) {st.clear();parse(s);}
// this method appends tokens of string s to the existing ones
   void append(std::string s) {parse(s);}
// this methods returns the number of tokens available
	unsigned int size() {return st.size();}
// this operator returns the desired token. token[0] is the first one, etc.
// If the desired token does not exist, "" is returned.
   std::string Get(unsigned int i)	{
   	if (i < st.size())
	     	return st[i];
		else
		   return std::string("");
      }

private:
   std::string 	 				term;
   std::vector<std::string> 	st;

   void parse(std::string s) {
      unsigned int i, j, k;

      i = s.find_first_not_of(term);
      j = s.find_last_not_of(term);

      while(i < j) {
         k = s.find_first_of(term, i);
         k = k > j ? j + 1: k;
         st.push_back(s.substr(i, k - i));
         i = s.find_first_not_of(term, k);
      }
   }

};

#endif



