Octane v1.01.20 - The Open Compression Toolkit for C++ http://octane.sourceforge.net/
Homepage | Main | Modules | Class Hierarchy | Compound List | File List | Compound Members | Related Pages

parsers/parser_bitlevel/bitparser.cpp

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 //---------------------------------------------------------------------------
00016 
00017 //---------------------------------------------------------------------------
00018 // Application includes
00019 #include "bitparser.hpp"
00020 // System includes
00021 #include <string>
00022 //---------------------------------------------------------------------------
00023 
00024 
00025 
00026 
00027 
00028 
00029 //---------------------------------------------------------------------------
00030 // PARSER PUBLIC API - MAIN PARSING FUNCTIONS
00031 
00032 bool BitParser::ParseNextSymbolFromInput(bitreader &from, int &symbolnum)
00033 {
00034         // grab an input stream symbol and set its INDEX (in symbol vector) for symbolnum
00035         // return false after EOS
00036         unsigned char c;
00037 
00038         // are we at end of from input - this is the only non-intuitive step
00039         // the issue is that only the parser knows about end-of-stream symbols, and when it hits and end of input
00040         // it basically needs to reply TWICE, first with an end-of-stream, and then with a reply saying 'no more symbols'
00041         if (from.empty())
00042                 {
00043                 // no more symbols left - BUT the question now is, do we return an EOS symbol, or false for no symbols left
00044                 if (senteos)
00045                         {
00046                         // we already sent an EOS so from now on any requests for a symbol returns false saying no more symbols available
00047                         return false;
00048                         }
00049                 else
00050                         {
00051                         // we are going to drop down to return the EOS signal, but we set flag so we don't do it again
00052                         senteos=true;
00053                         }
00054                 // end of stream symbol number
00055                 symbolnum=SampleParser_EndOfStreamSYMBOL;
00056                 }
00057         else
00058                 {
00059                 // grab an unsigned character from input and assign the symbol # to its ascii number
00060                 from.get(c);
00061                 symbolnum=(int)c;
00062                 }
00063 
00064         // return true, saying we read a symbol
00065         return true;
00066 }
00067 
00068 
00069 bool BitParser::WriteSymbolText(bitwriter &to, int symbolnum,bool &isendofstreamsymbol)
00070 {
00071         // write the symbol indexed by symbolnum
00072         // sets isendofostreamsymbol to true or false depending on if the symbol written is the EOS symbol
00073         // return true on success
00074 
00075         if (symbolnum==SampleParser_EndOfStreamSYMBOL)
00076                 {
00077                 // this is end of stream symbol, so do nothing but set EOS flag
00078                 isendofstreamsymbol=true;
00079                 }
00080         else
00081                 {
00082                 // not EOS, so write it and set EOS flag false
00083                 to.put((unsigned char)symbolnum);
00084                 isendofstreamsymbol=false;
00085                 }
00086 
00087         // return success
00088         return true;
00089 }
00090 
00091 
00092 string BitParser::LookupSymbolText(int symbolnum)
00093         {
00094         // return the string text corresponding to symbol 'symbolnum'
00095         if (symbolnum==SampleParser_EndOfStreamSYMBOL)
00096                 return "";
00097         char cstr[2];
00098         cstr[0]=(unsigned char)symbolnum;
00099         cstr[1]='\0';
00100         return string(cstr);
00101         }
00102 //---------------------------------------------------------------------------
00103 
00104 
00105 
00106 
00107 
00108 
00109 //---------------------------------------------------------------------------
00110 void BitParser::SynchronizeStateForNewStream()
00111 {
00112         // synchronize state for a new stream
00113         // this MUST be called before beginning a new parsing stream
00114         // reset input buffer and end-of-stream sent flag
00115         senteos=false;
00116 }
00117 //---------------------------------------------------------------------------
00118 
00119 
00120 
00121 
00122 //---------------------------------------------------------------------------
00123 std::string BitParser::GetParametersInformation()
00124 {
00125         // display parameter settings
00126         string returnstring;
00127         cout << setiosflags(ios::left) << setw(17) << "bitlength" << setiosflags(ios::left) << " " << setw(10) << Paremeter_bitlength << " " << "number of bits per chunk (per symbol)" << endl;
00128         return returnstring;
00129 }
00130 
00131 void BitParser::SetDefaultParameters()
00132 {
00133         // default bitlength for a chunk is just 1 bit
00134         Parameter_bitlength=1;
00135 }
00136 
00137 bool BitParser::SetParameter(const std::string &parametername,const std::string &parametervalue)
00138 {
00139         // set parameter(s)
00140         bool bretv=false;
00141         if (parametername=="bitlength")
00142                 bretv=ParseParameter(parametervalue,Parameter_bitlength);
00143         return bretv;
00144 }
00145 
00146 bool BitParser::SaveState(bitwriter &to,bool fortempdecompressiononly)
00147 {
00148         // save state
00149         to.put(Parameter_bitlength);
00150         return true;
00151 }
00152 
00153 bool BitParser::LoadState(bitreader &from)
00154 {
00155         // load state
00156         to.get(Parameter_bitlength);
00157         return true;
00158 }
00159 //---------------------------------------------------------------------------
 
Generated on 20 May 2004 by doxygen 1.3.3