00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef TaggedFile_h
00021 #define TaggedFile_h
00022 #include <iostream>
00023 #include <string>
00024 #include <map>
00025 #include <list>
00026 #include <y2util/TaggedParser.h>
00027 #include <y2util/TagRetrievalPos.h>
00028
00029 namespace TaggedFile
00030 {
00031
00044 enum tagtype {
00045 REJECTLOCALE, START, ALLOWLOCALE, FORCELOCALE
00046 };
00047
00056 enum datatype {
00057 SINGLE, SINGLEPOS, MULTI, MULTIOLD, MULTIYOU
00058 };
00059
00078 enum assignstatus {
00079 ACCEPTED,
00080 ACCEPTED_FULL,
00081 REJECTED_EOF,
00082 REJECTED_NOMATCH,
00083 REJECTED_LOCALE,
00084 REJECTED_NOLOCALE,
00085 REJECTED_FULL,
00086 REJECTED_NOENDTAG
00087 };
00088
00089 static const streamoff nopos = streamoff(-1);
00090
00095 class Tag
00096 {
00097 public:
00098 typedef std::map<std::string, TagRetrievalPos> posmaptype;
00099
00100 private:
00102 std::string _name;
00103
00105 std::string _end;
00106
00111 posmaptype _pos;
00112
00117 std::string _data;
00118
00122 datatype _datatype;
00123
00127 tagtype _tagtype;
00128
00129 public:
00134 Tag (const std::string& name, datatype dtype, tagtype ttype = REJECTLOCALE);
00135
00142 void setEndTag (std::string end) { _end = end; }
00143
00155 assignstatus assign (const std::string& locale, TaggedParser& parser, std::istream& istr);
00156
00159 void clear()
00160 {
00161 _pos.clear();
00162 _data.erase();
00163 }
00164
00169 const std::string& Name() const
00170 {
00171 return _name;
00172 }
00173
00177 const std::string& Data() const
00178 {
00179 return _data;
00180 }
00181
00185 const TagRetrievalPos Pos (const std::string& locale = "") const;
00186
00190 const posmaptype PosMap () const { return _pos; }
00191
00195 std::streamoff posDataStart (const std::string& locale = "") const { return Pos(locale).begin(); }
00196
00200 std::streamoff posDataEnd (const std::string& locale = "") const { return Pos(locale).end(); }
00201
00202 friend std::ostream & operator<<( std::ostream & str, const TaggedFile::Tag & obj );
00203 };
00204
00208 class TagSet
00209 {
00210 private:
00212 bool _allow_multiple_sets;
00213
00215 bool _allow_unknown_tags;
00216
00218 typedef std::map<std::string, Tag *> tagmaptype;
00219
00221 tagmaptype _tags;
00222
00228 typedef std::vector<Tag*> tagvectortype;
00229 tagvectortype _tagv;
00230
00236 void setTagByIndex (int idx, Tag* t)
00237 {
00238 if (idx < 0)
00239 return;
00240 if (_tagv.size() <= (unsigned int)idx)
00241 {
00242 _tagv.resize ((unsigned int)idx+1);
00243 }
00244 _tagv[(unsigned int)idx] = t;
00245 }
00246
00252 bool _reuse_previous_tag;
00253
00261 assignstatus assign (const std::string& starttag, const std::string& startlocale, TaggedParser& parser, std::istream& istr);
00262
00263 public:
00264 TagSet();
00265 virtual ~TagSet();
00266
00270 void setAllowMultipleSets (bool flag) { _allow_multiple_sets = flag; }
00271
00275 void setAllowUnknownTags (bool flag) { _allow_unknown_tags = flag; }
00276
00284 void addTag (const std::string& name, int idx, datatype dtype, tagtype ttype = REJECTLOCALE)
00285 {
00286 Tag *tag = new Tag (name, dtype, ttype);
00287 _tags[name] = tag;
00288 setTagByIndex (idx, tag);
00289 }
00290
00297 assignstatus assignSet (TaggedParser& parser, std::istream& istr);
00298
00304 Tag* getTagByIndex (unsigned int idx)
00305 {
00306 if (idx < _tagv.size ())
00307 return _tagv[idx];
00308 else
00309 return NULL;
00310 }
00311
00312 friend std::ostream & operator<<( std::ostream & str, const TaggedFile::TagSet & obj );
00313 };
00314
00315 }
00316
00317 #endif // TaggedFile_h
00318
00319