BALL  1.4.1
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 namespace BALL 
00019 {
00020   class INIFile;
00021 
00028   class BALL_EXPORT INIFile
00029   {
00030     public:
00031 
00034     enum
00035     {
00038       MAX_LINE_LENGTH = 1048576
00039     };
00040 
00041     class IteratorTraits_;
00042   
00044     class BALL_EXPORT Section
00045     {
00046       public:
00047 
00048       friend class INIFile;
00049       friend class IteratorTraits_;
00050 
00052       const String& getName() const
00053       {
00054         return name_;
00055       }
00056 
00058       bool operator == (const Section& section) const
00059       {
00060         return (name_    == section.name_   &&
00061                 lines_   == section.lines_);
00062       }
00063 
00065       bool operator < (const Section& section) const
00066         ;
00067 
00069       bool operator > (const Section& section) const
00070         ;
00071 
00072       protected:
00073 
00074       // name of the section
00075       String                                  name_;
00076 
00077       // all lines of the section
00078       std::list<String>                           lines_;
00079 
00080       // hashmap with all keys
00081       StringHashMap<std::list<String>::iterator>    key_map_;
00082     };
00083 
00085     typedef std::list<Section>::iterator SectionIterator;
00086 
00091     typedef IteratorTraits_ LineIterator;
00092 
00096     static const String UNDEFINED;
00097     
00101     static const String HEADER;   
00102     
00106 
00111     INIFile();
00112 
00116     INIFile(const String& filename);
00117 
00120     virtual ~INIFile();
00121 
00126     void clear();
00127 
00129 
00132 
00152     bool read();
00153 
00160     bool write();
00161 
00164     const String& getFilename() const;
00165 
00169     void setFilename(const String& filename);
00170 
00172 
00175 
00179     bool isValid() const;
00180 
00182 
00189 
00198     LineIterator getLine(Size line_number);
00199 
00201 
00205     
00215     bool setLine(LineIterator line_it, const String& line);
00216     
00223     bool deleteLine(LineIterator line_it);
00224     
00234     bool insertLine(LineIterator line_it, const String& line);
00235     
00237 
00240 
00256     bool appendLine(const String& section_name, const String& line);
00257 
00259     bool appendLine(const String& line);
00260 
00263     Size getNumberOfLines() const;
00264         
00271     bool hasSection(const String& section_name) const;
00272 
00278     SectionIterator getSection(const String& section_name);
00279 
00284     SectionIterator getSection(Position pos);
00285 
00289     Size getNumberOfSections() const;
00290 
00299     LineIterator getSectionFirstLine(const String& section_name);
00300 
00307     LineIterator getSectionLastLine(const String& section_name);  
00308 
00315     Size getSectionLength(const String& section_name) const;
00316 
00322     bool deleteSection(const String& section);
00323 
00327     bool appendSection(const String& section);
00328 
00330 
00333 
00341     bool hasEntry(const String& section, const String& key) const;
00342     
00352     String getValue(const String& section, const String& key) const;
00353 
00364     bool setValue(const String& section, const String& key, const String& value);
00365 
00375     bool insertValue(const String& section, const String& key, const String& value);
00376 
00378     const INIFile& operator = (const INIFile& file)
00379       ;
00380 
00382 
00385 
00390     bool operator == (const INIFile& inifile) const;
00391 
00394     bool isValid(const LineIterator& it) const;
00395 
00398     bool isValid(const SectionIterator& it) const;
00399 
00401 
00404     bool apply(UnaryProcessor<LineIterator>& processor);
00405 
00408     void setDuplicateKeyCheck(bool mode);
00409 
00412     bool duplicateKeyCheckEnabled() const;
00413 
00415     std::list<String> getContent() const
00416       ;
00417 
00419     bool setContent(const std::list<String>& lines)
00420       ;
00421 
00422     protected:
00423 
00424     bool                              check_duplicate_keys_;
00425 
00426     bool                              valid_;
00427 
00428     String                            filename_;  
00429 
00430     // all sections, 0. section is HEADER
00431     std::list<Section>                      sections_;
00432 
00433     // hashmap with the section names  => index
00434     StringHashMap<SectionIterator>    section_index_;
00435 
00436     public:
00437 
00439     class BALL_EXPORT IteratorTraits_
00440     {
00441       friend class INIFile;
00442 
00443       public:
00444 
00445       BALL_CREATE(IteratorTraits_)
00446 
00447       
00448       IteratorTraits_();
00449 
00451       IteratorTraits_(const IteratorTraits_& traits);
00452 
00454       virtual ~IteratorTraits_();
00455       
00457       const IteratorTraits_& operator = (const IteratorTraits_ &traits);
00458 
00460       std::list<String>::iterator getPosition();
00461 
00463       SectionIterator getSection();
00464     
00466       const String& operator * () const;
00467 
00469       IteratorTraits_& operator ++ ();
00470 
00472       IteratorTraits_& operator -- ();
00473 
00475       IteratorTraits_& getSectionNextLine();
00476 
00478       bool operator == (const IteratorTraits_& traits) const;
00479 
00481       bool operator != (const IteratorTraits_& traits) const;
00482       
00484       bool operator + () const;
00485 
00487       bool isValid() const;
00488 
00490       void toSectionFirstLine();
00491 
00493       void toSectionLastLine();
00494 
00496       void toSectionEnd();
00497 
00499       bool isSectionFirstLine() const;
00500 
00502       bool isSectionLastLine() const;
00503 
00505       bool isSectionEnd() const;
00506 
00508       void toFirstLine();
00509 
00511       void toLastLine();
00512 
00514       void toEnd();
00515 
00516       protected:
00517 
00518       //_
00519       IteratorTraits_(std::list<Section>& list,
00520                       SectionIterator section, 
00521                       std::list<String>::iterator line);
00522       
00523       //_
00524       const std::list<Section>* getBound_() const;
00525 
00526       //_
00527       void setLine_(const String& line);
00528 
00529       private:
00530 
00531       std::list<Section>*         bound_;
00532       SectionIterator         section_;
00533       std::list<String>::iterator position_;
00534     };
00535   };
00536 } // namespace BALL
00537 
00538 #endif // BALL_FORMAT_INIFILE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines