Octane v1.01.20 - The Open Compression Toolkit for C++ | http://octane.sourceforge.net/ |
00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 //--------------------------------------------------------------------------- 00013 00014 //--------------------------------------------------------------------------- 00015 // Application includes 00016 #include "sampleparser.hpp" 00017 // System includes 00018 #include <string> 00019 //--------------------------------------------------------------------------- 00020 00021 00022 00023 00024 00025 //--------------------------------------------------------------------------- 00026 // PARSER PUBLIC API - MAIN PARSING FUNCTIONS 00027 00028 bool SampleParser::ParseNextSymbolFromInput(bitreader &from, int &symbolnum) 00029 { 00030 // grab an input stream symbol and set its INDEX (in symbol vector) for symbolnum 00031 // return false after EOS 00032 unsigned char c; 00033 00034 // are we at end of from input - this is the only non-intuitive step 00035 // the issue is that only the parser knows about end-of-stream symbols, and when it hits and end of input 00036 // it basically needs to reply TWICE, first with an end-of-stream, and then with a reply saying 'no more symbols' 00037 if (from.empty()) 00038 { 00039 // no more symbols left - BUT the question now is, do we return an EOS symbol, or false for no symbols left 00040 if (senteos) 00041 { 00042 // we already sent an EOS so from now on any requests for a symbol returns false saying no more symbols available 00043 return false; 00044 } 00045 else 00046 { 00047 // we are going to drop down to return the EOS signal, but we set flag so we don't do it again 00048 senteos=true; 00049 } 00050 // end of stream symbol number 00051 symbolnum=SampleParser_EndOfStreamSYMBOL; 00052 } 00053 else 00054 { 00055 // grab an unsigned character from input and assign the symbol # to its ascii number 00056 from.get(c); 00057 symbolnum=(int)c; 00058 } 00059 00060 // return true, saying we read a symbol 00061 return true; 00062 } 00063 00064 00065 bool SampleParser::WriteSymbolText(bitwriter &to, int symbolnum,bool &isendofstreamsymbol) 00066 { 00067 // write the symbol indexed by symbolnum 00068 // sets isendofostreamsymbol to true or false depending on if the symbol written is the EOS symbol 00069 // return true on success 00070 00071 if (symbolnum==SampleParser_EndOfStreamSYMBOL) 00072 { 00073 // this is end of stream symbol, so do nothing but set EOS flag 00074 isendofstreamsymbol=true; 00075 } 00076 else 00077 { 00078 // not EOS, so write it and set EOS flag false 00079 to.put((unsigned char)symbolnum); 00080 isendofstreamsymbol=false; 00081 } 00082 00083 // return success 00084 return true; 00085 } 00086 00087 00088 string SampleParser::LookupSymbolText(int symbolnum) 00089 { 00090 // return the string text corresponding to symbol 'symbolnum' 00091 if (symbolnum==SampleParser_EndOfStreamSYMBOL) 00092 return ""; 00093 char cstr[2]; 00094 cstr[0]=(unsigned char)symbolnum; 00095 cstr[1]='\0'; 00096 return string(cstr); 00097 } 00098 //--------------------------------------------------------------------------- 00099 00100 00101 00102 //--------------------------------------------------------------------------- 00103 void SampleParser::SynchronizeStateForNewStream() 00104 { 00105 // synchronize state for a new stream 00106 // this MUST be called before beginning a new parsing stream 00107 // reset input buffer and end-of-stream sent flag 00108 senteos=false; 00109 } 00110 //---------------------------------------------------------------------------