#ifndef _SSTREAM
#define _SSTREAM

#include "string"
#include <string.h>
#include <ctype.h>

namespace std {

class stringstream {
   string buffer;
   bool did_input;

   size_t get_space(const char * buf) {
      size_t n = 0; 
      for(size_t i = 0; isspace(buf[i]); i++) 
         n++;
      return n;
   }
public:
   stringstream() {
      did_input = false;
   }
   ~stringstream() {
   }
   stringstream & operator>>(bool & b) {
cout << "sstream in bool" << endl; cin.get();cin.get();
      char * buf = new char[buffer.size()+1];
      memset(buf, 0, buffer.size()+1);
      strcpy(buf, buffer.c_str());
      size_t ptr = sscanf(buf, "%d", &b);
      ptr += get_space(buf);
      if(ptr != 0) did_input = true;
      buffer = string(buf + ptr);
      delete [] buf;
      return *this;
   }
   stringstream & operator>>(int & i) {
cout << "sstream in int" << endl; cin.get();cin.get();
      char * buf = new char[buffer.size()+1];
      memset(buf, 0, buffer.size()+1);
      strcpy(buf, buffer.c_str());
      size_t ptr;
      for(ptr = 0U; isspace(buf[ptr]); ptr++)
         ;
      ptr += sscanf(buf, "%d", &i);
      if(ptr != 0) did_input = true;
      buffer = string(buf + ptr);
      delete [] buf;
      return *this;
   }
   stringstream & operator>>(char & i) {
cout << "sstream in char" << endl; cin.get();cin.get();
      char * buf = new char[buffer.size()+1];
      memset(buf, 0, buffer.size()+1);
      strcpy(buf, buffer.c_str());
      size_t ptr = sscanf(buf, "%c", &i);
      if(ptr != 0) did_input = true;
      buffer = string(buf + ptr);
      delete [] buf;
      return *this;
   }
   stringstream & operator>>(string & str) {
//cout << "sstream in string" << endl; cin.get();cin.get();
      char * buf = new char[buffer.size()+1];
      memset(buf, 0, buffer.size()+1);
      strcpy(buf, buffer.c_str());
      char temp[1024];
      memset(temp, 0, 1024);
      sscanf(buf, "%s", temp);
//cout << "temp=[" << temp << "]" << endl;
      size_t ptr = strlen(temp);
      size_t i;
      for(i = 0; isspace(buf[i]) && buf[i] != '\0'; i++) 
         ;
      ptr += i;
      buffer = string(buf + ptr);
      if(strlen(temp)) did_input = true;
      delete [] buf;
      str = string(temp);
//cout << "str=[" << str << "]" << endl;
//cin.get();cin.get();
      return *this;
   }
   stringstream & operator>>(size_t & i) {
cout << "sstream in size_t" << endl; cin.get();cin.get();
      char * buf = new char[buffer.size()+1];
      memset(buf, 0, buffer.size()+1);
      strcpy(buf, buffer.c_str());
      size_t ptr = sscanf(buf, "%u", &i);
      ptr += get_space(buf);
      if(ptr != 0) did_input = true;
      buffer = string(buf + ptr);
      delete [] buf;
      return *this;
   }
   stringstream & operator>>(double & i) {
cout << "sstream in double" << endl; cin.get();cin.get();
      char * buf = new char[buffer.size()+1];
      memset(buf, 0, buffer.size()+1);
      strcpy(buf, buffer.c_str());
      size_t ptr = sscanf(buf, "%f", &i);
      ptr += get_space(buf);
      if(ptr != 0) did_input = true;
      buffer = string(buf + ptr);
      delete [] buf;
      return *this;
   }
   stringstream & operator>>(float & i) {
cout << "sstream in float" << endl; cin.get();cin.get();
      char * buf = new char[buffer.size()+1];
      memset(buf, 0, buffer.size()+1);
      strcpy(buf, buffer.c_str());
      size_t ptr = sscanf(buf, "%f", &i);
      ptr += get_space(buf);
      if(ptr != 0) did_input = true;
      buffer = string(buf + ptr);
      delete [] buf;
      return *this;
   }
   stringstream & operator>>(long & i) {
cout << "sstream in long" << endl; cin.get();cin.get();
      char * buf = new char[buffer.size()+1];
      memset(buf, 0, buffer.size()+1);
      strcpy(buf, buffer.c_str());
      size_t ptr = sscanf(buf, "%ld", &i);
      ptr += get_space(buf);
      if(ptr != 0) did_input = true;
      buffer = string(buf + ptr);
      delete [] buf;
      return *this;
   }
   stringstream & operator<<(const char * str) {
cout << "STRING=" << str << endl;
cout << "sstream out string" << endl; cin.get();cin.get();
      buffer += string(str);
      return *this;
   }
   stringstream & operator<<(const bool & i) {
cout << "sstream out bool" << endl; cin.get();cin.get();
      char buf[100];
      if(i == true) strcpy(buf, "1");
      else strcpy(buf, "0");
      buffer += buf;
      return *this;
   }
   stringstream & operator<<(const size_t & i) {
cout << "sstream out size_t" << endl; cin.get();cin.get();
      char buf[100];
      sprintf(buf, "%d", i);
      buffer += buf;
      return *this;
   }
   stringstream & operator<<(const float & f) {
cout << "sstream out float" << endl; cin.get();cin.get();
      char buf[100];
      sprintf(buf, "%f", f);
      buffer += buf;
      return *this;
   }
   stringstream & operator<<(const double & d) {
cout << "sstream out double" << endl; cin.get();cin.get();
      char buf[100];
      sprintf(buf, "%f", d);
      buffer += buf;
      return *this;
   }
   stringstream & operator<<(const long & l) {
cout << "sstream out long" << endl; cin.get();cin.get();
      char buf[100];
      sprintf(buf, "%f", l);
      buffer += buf;
      return *this;
   }
   stringstream & operator<<(const string & str) {
      buffer += str;
      return *this;
   }
   stringstream & operator<<(const int arg) {
cout << "sstream out int" << endl; cin.get();cin.get();
      char buf[100];
      sprintf(buf, "%d", arg);
      buffer += buf;
      return *this;
   }
   operator void*() {
      if(did_input) {
         did_input = false;
         return (void*)(0xFFFFFFFF);
      }
      else {
         return (void*)(buffer.size());
      }
   }
};

}

#endif
