/* This file is part of cardwords
   (c) 1999 Tobias Peters
   see file COPYING for the copyright terms.
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// cardwords_machinestring.cc

#include <iterator.h>
#include <algo.h>
#include "cardwords_machinestring.hh"

istream & operator >> (istream & istream, Machine_String & str)
{
  bool skipped_ws = (istream.flags() & ios::skipws);
  if (skipped_ws) {
    istream.unsetf(ios::skipws);
  }

  Machine_Char ch;
  str.erase(str.begin(), str.end());
  assert (str.size() == 0);

  do {
    istream >> ch;
  } while (istream.good() && (ch == ' ' || ch == '\t' || ch == '\n'));

  while (istream.good()
         && ch != ' '
         && ch != '\t'
         && ch != '\n'
         && ch != '\0') {
    str.push_back(ch);
    istream >> ch;
  }
  if (skipped_ws) {
    istream.setf(ios::skipws);
  }
  str.push_back('\0');
  return istream;
}

ostream & operator << (ostream & ostream, const Machine_String & str)
{
  copy (str.begin(),str.end()-1, ostream_iterator<Machine_Char>(ostream,""));
  return ostream;
}

// convert a number to a string:
Machine_String &
Machine_String::operator = (unsigned u)
{
  ostrstream ostr;
  ostr << u << ends;
  ostr.freeze(true);
  istrstream istr(ostr.str());
  istr >> *this;
  ostr.freeze(false);
  return *this;
}
Machine_String &
Machine_String::operator = (int i)
{  ostrstream ostr;
  ostr << i << ends;
  ostr.freeze(true);
  istrstream istr(ostr.str());
  istr >> *this;
  ostr.freeze(false);
  return *this;
}
