INIFile.h

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // $Id: INIFile.h,v 1.41.20.1 2007/08/09 17:11:53 amoll Exp $
00005 //
00006 
00007 #ifndef BALL_FORMAT_INIFILE_H
00008 #define BALL_FORMAT_INIFILE_H
00009 
00010 #ifndef BALL_DATATYPE_STRINGHASHMAP_H
00011 # include <BALL/DATATYPE/stringHashMap.h>
00012 #endif
00013 
00014 #ifndef BALL_CONCEPT_PROCESSOR_H
00015 # include <BALL/CONCEPT/processor.h>
00016 #endif
00017 
00018 #ifndef BALL_DATATYPE_LIST_H
00019 # include <BALL/DATATYPE/list.h>
00020 #endif
00021 
00022 namespace BALL 
00023 {
00024   class INIFile;
00025 
00032   class BALL_EXPORT INIFile
00033   {
00034     public:
00035 
00038     enum
00039     {
00042       MAX_LINE_LENGTH = 1048576
00043     };
00044 
00045     class IteratorTraits_;
00046   
00048     class BALL_EXPORT Section
00049     {
00050       public:
00051 
00052       friend class INIFile;
00053       friend class IteratorTraits_;
00054 
00056       const String& getName() const
00057       {
00058         return name_;
00059       }
00060 
00062       bool operator == (const Section& section) const
00063       {
00064         return (name_    == section.name_   &&
00065                 lines_   == section.lines_);
00066       }
00067 
00069       bool operator < (const Section& section) const
00070         ;
00071 
00073       bool operator > (const Section& section) const
00074         ;
00075 
00076       protected:
00077 
00078       // name of the section
00079       String                                  name_;
00080 
00081       // all lines of the section
00082       List<String>                            lines_;
00083 
00084       // hashmap with all keys
00085       StringHashMap<List<String>::Iterator>   key_map_;
00086     };
00087 
00089     typedef List<Section>::Iterator SectionIterator;
00090 
00095     typedef IteratorTraits_ LineIterator;
00096 
00100     static const String UNDEFINED;
00101     
00105     static const String HEADER;   
00106     
00110 
00115     INIFile();
00116 
00120     INIFile(const String& filename);
00121 
00124     virtual ~INIFile();
00125 
00130     void clear();
00131 
00133 
00136 
00156     bool read();
00157 
00164     bool write();
00165 
00168     const String& getFilename() const;
00169 
00173     void setFilename(const String& filename);
00174 
00176 
00179 
00183     bool isValid() const;
00184 
00186 
00193 
00202     LineIterator getLine(Size line_number);
00203 
00205 
00209     
00219     bool setLine(LineIterator line_it, const String& line);
00220     
00227     bool deleteLine(LineIterator line_it);
00228     
00238     bool insertLine(LineIterator line_it, const String& line);
00239     
00241 
00244 
00260     bool appendLine(const String& section_name, const String& line);
00261 
00263     bool appendLine(const String& line);
00264 
00267     Size getNumberOfLines() const;
00268         
00275     bool hasSection(const String& section_name) const;
00276 
00282     SectionIterator getSection(const String& section_name);
00283 
00288     SectionIterator getSection(Position pos);
00289 
00293     Size getNumberOfSections() const;
00294 
00303     LineIterator getSectionFirstLine(const String& section_name);
00304 
00311     LineIterator getSectionLastLine(const String& section_name);  
00312 
00319     Size getSectionLength(const String& section_name) const;
00320 
00326     bool deleteSection(const String& section);
00327 
00331     bool appendSection(const String& section);
00332 
00334 
00337 
00345     bool hasEntry(const String& section, const String& key) const;
00346     
00356     String getValue(const String& section, const String& key) const;
00357 
00368     bool setValue(const String& section, const String& key, const String& value);
00369 
00379     bool insertValue(const String& section, const String& key, const String& value);
00380 
00382     const INIFile& operator = (const INIFile& file)
00383       ;
00384 
00386 
00389 
00394     bool operator == (const INIFile& inifile) const;
00395 
00398     bool isValid(const LineIterator& it) const;
00399 
00402     bool isValid(const SectionIterator& it) const;
00403 
00405 
00408     bool apply(UnaryProcessor<LineIterator>& processor);
00409 
00412     void setDuplicateKeyCheck(bool mode);
00413 
00416     bool duplicateKeyCheckEnabled() const;
00417 
00419     List<String> getContent() const
00420       ;
00421 
00423     bool setContent(const List<String>& lines)
00424       ;
00425 
00426     protected:
00427 
00428     bool                              check_duplicate_keys_;
00429 
00430     bool                              valid_;
00431 
00432     String                            filename_;  
00433 
00434     // all sections, 0. section is HEADER
00435     List<Section>                     sections_;
00436 
00437     // hashmap with the section names  => index
00438     StringHashMap<SectionIterator>    section_index_;
00439 
00440     public:
00441 
00443     class BALL_EXPORT IteratorTraits_
00444     {
00445       friend class INIFile;
00446 
00447       public:
00448 
00449       BALL_CREATE(IteratorTraits_)
00450 
00451       
00452       IteratorTraits_();
00453 
00455       IteratorTraits_(const IteratorTraits_& traits);
00456 
00458       virtual ~IteratorTraits_();
00459       
00461       const IteratorTraits_& operator = (const IteratorTraits_ &traits);
00462 
00464       List<String>::Iterator getPosition();
00465 
00467       SectionIterator getSection();
00468     
00470       const String& operator * () const;
00471 
00473       IteratorTraits_& operator ++ ();
00474 
00476       IteratorTraits_& operator -- ();
00477 
00479       IteratorTraits_& getSectionNextLine();
00480 
00482       bool operator == (const IteratorTraits_& traits) const;
00483 
00485       bool operator != (const IteratorTraits_& traits) const;
00486       
00488       bool operator + () const;
00489 
00491       bool isValid() const;
00492 
00494       void toSectionFirstLine();
00495 
00497       void toSectionLastLine();
00498 
00500       void toSectionEnd();
00501 
00503       bool isSectionFirstLine() const;
00504 
00506       bool isSectionLastLine() const;
00507 
00509       bool isSectionEnd() const;
00510 
00512       void toFirstLine();
00513 
00515       void toLastLine();
00516 
00518       void toEnd();
00519 
00520       protected:
00521 
00522       //_
00523       IteratorTraits_(List<Section>& list, 
00524                       SectionIterator section, 
00525                       List<String>::Iterator line);
00526       
00527       //_
00528       const List<Section>* getBound_() const;
00529 
00530       //_
00531       void setLine_(const String& line);
00532 
00533       private:
00534 
00535       List<Section>*          bound_;
00536       SectionIterator         section_;
00537       List<String>::Iterator  position_;
00538     };
00539   };
00540 } // namespace BALL
00541 
00542 #endif // BALL_FORMAT_INIFILE_H