Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

TaggedParser.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002  |                                                                      |
00003  |                      __   __    ____ _____ ____                      |
00004  |                      \ \ / /_ _/ ___|_   _|___ \                     |
00005  |                       \ V / _` \___ \ | |   __) |                    |
00006  |                        | | (_| |___) || |  / __/                     |
00007  |                        |_|\__,_|____/ |_| |_____|                    |
00008  |                                                                      |
00009  |                               core system                            |
00010  |                                                        (C) SuSE GmbH |
00011  \----------------------------------------------------------------------/
00012 
00013  File:       TaggedParser.h
00014 
00015  Author:     Michael Andres <ma@suse.de>
00016  Maintainer: Klaus Kaempf <kkaempf@suse.de>
00017 
00018  Parser for tagged file format as used in SuSE/UnitedLinux
00019  media data
00020 
00021  /-*/
00022 #ifndef TaggedParser_h
00023 #define TaggedParser_h
00024 
00025 #include <iosfwd>
00026 #include <string>
00027 #include <list>
00028 #include <vector>
00029 #include <map>
00030 
00031 using std::vector;
00032 using std::map;
00033 using std::streamoff;
00034 using std::istream;
00035 
00037 //
00038 //      CLASS NAME : TaggedParser
00039 //
00040 //
00041 //      DESCRIPTION :
00042 //              parses file in "tagged" format
00043 //              a tag starts at the beginning of a line with
00044 //              '=' (single line tag, tag_type SINGLE),
00045 //              '+' (start of multi-line tag, tag_type START), or
00046 //              '-' (end of multi line tag, tag_type END)
00047 //              followed by an arbitrary string and a colon (':')
00048 //
00049 //              The tag parser 'lookupTag()' searches through an open
00050 //              stream for such a tag
00051 //
00052 //              It also recognizes all other "<tag>:<blank>" lines as
00053 //              tag_type OLDSTYLE. However, since this style requires
00054 //              a full-line scan (opposed to a initial char check only),
00055 //              the lookupTag() and lookupEndTag() have an extra oldstyle
00056 //              parameter for this.
00057 //
00058 //
00059 class TaggedParser {
00060   public:
00061     enum tag_type {
00062         NONE=0,         // no tag
00063         SINGLE,         // single value
00064         START,          // start of multi value
00065         END,            // end of multi value
00066         OLDSINGLE,      // tag has no prefix but a value
00067         OLDMULTI        // tag has no prefix and no value
00068     };
00069     typedef tag_type TagType;
00070 
00071   private:
00072 
00073     static const unsigned bufferLen_i;
00074     static char           buffer_ac[];
00075 
00076     std::string currentLine;
00077 
00078   private:
00079 
00080     streamoff _tagPos;          // position of tag
00081     streamoff _startPos;        // start postition of data
00082     streamoff _endPos;          // end position of data
00083 
00084     int _bufferPos;             // position of data in buffer
00085     unsigned int _bufferLen;    // length of data in buffer (unsigned for string::npos comparison)
00086 
00087     int _lineNumber;
00088 
00089     std::string _currentTag;
00090     std::string _currentLocale;
00091     std::string _currentData;           // substr of currentLine, set by data()
00092 
00093     bool _oldstyle;
00094 
00095     // set from start of line to start of tag
00096     int _offset;
00097 
00098   private:
00099 
00100     void _reset();
00101     void _datareset();
00102 
00103     // read line from stream
00104     static streamoff readLine (istream & stream_fr, std::string & cline_tr );
00105 
00106     // check line for tag
00107     TagType tagOnLine (const std::string & cline_tr, std::string & tag_tr,
00108                         std::string::size_type & delim_ir, std::string & lang_tr);
00109 
00110   public:
00111 
00112     TaggedParser();
00113     virtual ~TaggedParser();
00114 
00115     void asOldstyle (bool oldstyle) { _oldstyle = oldstyle; _offset = (oldstyle?0:1); }
00116     static const streamoff nopos;
00117 
00118     int lineNumber () const { return _lineNumber; }
00119 
00120     streamoff tagPos() const { return _tagPos; }
00121     const std::string & currentTag() const { return _currentTag; }
00122     const std::string & currentLocale() const { return _currentLocale; }
00123 
00124   public:
00125 
00126     streamoff      dataStartPos () const { return _startPos; }
00127     streamoff      dataEndPos ()   const { return _endPos; }
00128     unsigned       dataLength ()   const { return _endPos - _startPos; }
00129 
00130     // valid after parseData()
00131     const std::string& data() { return (_currentData = currentLine.substr (_bufferPos, _bufferLen)); }
00132 
00133   public:
00134 
00141     TagType lookupTag( istream & stream_fr, const std::string & stag_tr = "", const std::string & slang_tr = "");
00142 
00147     TagType lookupEndTag ( istream & stream_fr, const std::string & etag_tr,
00148                            const std::string & elang_tr = "", bool reverseLocale = false );
00149 
00150     // helper functions
00151     static std::string data2string( const std::list<std::string> & data_Vtr );
00152     static vector<std::string> split2words( const std::string & line_tr, const std::string & sepchars_tr = " \t\n" );
00153 };
00154 
00155 #endif // TaggedParser_h
00156 
00157 // vim:sw=2

Generated on Thu Feb 23 23:56:10 2006 for liby2util by doxygen 1.3.6