proj home

Files   Classes   Functions   Hierarchy  

digdiv Class Reference

Cut a string up as blocks of strings. More...

#include <streamconversion.h>

Collaboration diagram for digdiv:

List of all members.

Public Member Functions

void forward (string const &in)
 Read the string into vi as blocks.
void reverse (string &os)
 Write the blocks in vi out as a continuous string.
void save (ostream &os) const
 Save this objects state.
void restore (istream &in)
 Restore this object to the former state.
template<typename T >
void eval (T &fn)
 Apply a function to vi[i].

Public Attributes

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


Detailed Description

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.

Definition at line 36 of file streamconversion.h.


Member Function Documentation

template<typename T >
void digdiv::eval ( T fn  )  [inline]

Apply a function to vi[i].

Definition at line 69 of file streamconversion.h.

References vi.

00070 {
00071   if (vi.empty())
00072     return;
00073 
00074   uintc sz = vi.size();
00075   for (uint i=0; i<sz; ++i)
00076   {
00077     fn(vi[i],vi[i]);
00078   }
00079 
00080 }

void digdiv::forward ( string const &  in  ) 

Read the string into vi as blocks.

Definition at line 74 of file streamconversion.cpp.

References blksz, and vi.

Referenced by rsatest::test01().

00075 {
00076   assert(blksz>0);
00077 
00078   uintc sz = in.size();
00079   uint i=0;
00080   vi.clear();
00081   i += blksz;
00082   for ( ; i<=sz; )
00083   {
00084     vi.push_back(in.substr(i-blksz,blksz));
00085     i += blksz;
00086   }  
00087 
00088   if ((sz%blksz)!=0)
00089   {
00090     pad = blksz - (sz%blksz);
00091     string s;
00092     for (uint k=0; k<pad; ++k)
00093       s += '0';
00094     s += in.substr(i-blksz);
00095     vi.push_back(s);
00096   }
00097   else
00098     pad=0;
00099 }

void digdiv::restore ( istream &  in  ) 

Restore this object to the former state.

Definition at line 154 of file streamconversion.cpp.

References blksz, and vi.

00155 {
00156   if (in.eof()==false)
00157     in >> pad;
00158   else
00159     return;
00160 
00161   if (in.eof()==false)
00162     in >> blksz;
00163   else
00164     return;
00165 
00166   string s;
00167   in >> s;
00168   while (in.eof()==false)
00169   {
00170     vi.push_back(s);
00171     s.clear();
00172     in >> s;
00173   }
00174 }

void digdiv::reverse ( string &  os  ) 

Write the blocks in vi out as a continuous string.

Definition at line 101 of file streamconversion.cpp.

References blksz, and vi.

Referenced by rsatest::test01().

00102 {
00103   if (vi.empty())
00104     return;
00105 
00106   os.clear();
00107   
00108   uintc sz = vi.size();
00109 
00110   // Correct the block sizes
00111   for (uint i=0; i<sz; ++i)
00112   {
00113     uintc si = vi[i].size();
00114     if (si != blksz)
00115     {
00116       if (si<blksz)
00117       {
00118         string tmp;
00119         for (uint k=si; k<blksz; ++k)
00120           tmp += '0';
00121         tmp += vi[i];
00122         vi[i] = tmp;
00123       }
00124       else
00125       {
00126         // A function should reduce the block size.
00127         assert(false);
00128       }
00129     }
00130   }
00131       
00132   if (pad==0)
00133   {
00134     for (uint i=0; i<sz; ++i)
00135       os += vi[i];
00136     return;
00137   }
00138 
00139   string last = vi[sz-1];
00140   last = last.substr(pad);
00141   for (uint i=0; i<sz-1; ++i)
00142     os += vi[i];
00143   os += last;
00144 }

void digdiv::save ( ostream os  )  const

Save this objects state.

eg to a file.

Definition at line 146 of file streamconversion.cpp.

References blksz, and vi.

00147 {
00148   os << pad << endl;
00149   os << blksz << endl;
00150   for (uint i=0; i<vi.size(); ++i)
00151     os << vi[i] << endl;
00152 }


Member Data Documentation

Block size.

Definition at line 43 of file streamconversion.h.

Referenced by forward(), restore(), reverse(), save(), and rsatest::test01().

vector<string> digdiv::vi

A string of blocks.

Definition at line 45 of file streamconversion.h.

Referenced by eval(), forward(), restore(), reverse(), and save().


The documentation for this class was generated from the following files:

Generated on Fri Mar 4 00:49:55 2011 for Chelton Evans Source by  doxygen 1.5.8