#ifndef STREAMCONVERSION_H
#define STREAMCONVERSION_H

#include <iostream>
#include <string>
#include <vector>
using namespace std;

#include <print.h>

/*!
\brief Convert a stream to a string of ASCII digits. 
*/
class asciitodig
{
public:

  /** Convert a stream to a string of ascii digits. */
  static void forward( string & os, istream & is );
  /** Convert a string of ascii digits to a stream. */
  static void reverse( ostream & os, string const & in );
};


/*!
\brief Cut a string up as blocks of strings.

This last string is padded with 0's.  This operation
 is reversible and support for stream writing is included 
 so the string can be written and read from a stream.

The class is designed to process the data as a block of strings.
 Strings are superior to streams for many reasons, mainly simplicity
 and reliability and use. Streams are a lower form of data.
*/
class digdiv
{
  /** The last block generally is padded. */
  uint pad;  
public:

  /** Block size. */
  uint blksz;
  /** A string of blocks. */
  vector<string> vi;

  /** Read the string into vi as blocks. */
  void forward(string const & in);
  /** Write the blocks in vi out as a continuous string. */
  void reverse(string & os);

  /** Save this objects state. eg to a file. */
  void save(ostream & os) const;
  /** Restore this object to the former state. */
  void restore(istream & in);

  /** Apply a function to vi[i]. */
  template< typename T >
  void eval( T & fn );

};

/*
 *---------------------------------------------------------------
 * Implementation
 */

template< typename T >
void digdiv::eval( T & fn )
{
  if (vi.empty())
    return;

  uintc sz = vi.size();
  for (uint i=0; i<sz; ++i)
  {
    fn(vi[i],vi[i]);
  }

}


#endif



