| 1 | // Copyright (C) 2009-2010 Ferdinand Majerech |
|---|
| 2 | // This file is part of MiniINI |
|---|
| 3 | // For conditions of distribution and use, see copyright notice in LICENSE.txt |
|---|
| 4 | |
|---|
| 5 | #ifndef INIFILE_H_INCLUDED |
|---|
| 6 | #define INIFILE_H_INCLUDED |
|---|
| 7 | |
|---|
| 8 | #include "typedefs.h" |
|---|
| 9 | #include "allocator.h" |
|---|
| 10 | #include "inisection.h" |
|---|
| 11 | #include <cassert> |
|---|
| 12 | /// @cond PRIVATE |
|---|
| 13 | |
|---|
| 14 | ///Type of benchmark to perform. Self-explanatory. |
|---|
| 15 | enum BenchmarkType |
|---|
| 16 | { |
|---|
| 17 | BT_STRING, |
|---|
| 18 | BT_INT, |
|---|
| 19 | BT_FLOAT, |
|---|
| 20 | BT_BOOL, |
|---|
| 21 | BT_STRINGS, |
|---|
| 22 | BT_INTS, |
|---|
| 23 | BT_FLOATS, |
|---|
| 24 | BT_BOOLS |
|---|
| 25 | }; |
|---|
| 26 | /// @endcond |
|---|
| 27 | |
|---|
| 28 | ///INI File |
|---|
| 29 | /** |
|---|
| 30 | * Reads and stores data from an ini file and gives access to its sections. |
|---|
| 31 | */ |
|---|
| 32 | class MINIINI_EXPORT INIFile |
|---|
| 33 | { |
|---|
| 34 | friend class ReadMark; |
|---|
| 35 | friend class INIFileSTL; |
|---|
| 36 | |
|---|
| 37 | private: |
|---|
| 38 | |
|---|
| 39 | ///Number of sections in the file. |
|---|
| 40 | miniini_private::ui Length; |
|---|
| 41 | ///Array of pointers to sections. |
|---|
| 42 | INISection * * Sections; |
|---|
| 43 | ///Allocator for character data of this file. |
|---|
| 44 | miniini_private::Allocator * Alloc; |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | public: |
|---|
| 48 | |
|---|
| 49 | ///Empty constructor. |
|---|
| 50 | INIFile() |
|---|
| 51 | :Length(0) |
|---|
| 52 | ,Sections(NULL) |
|---|
| 53 | ,Alloc(NULL) |
|---|
| 54 | {} |
|---|
| 55 | |
|---|
| 56 | ///Loads given INI file. |
|---|
| 57 | /** |
|---|
| 58 | * @note File will be loaded even if it contains no ini sections, and even empty sections will be read. |
|---|
| 59 | * @param fname Filename of the file to load. Must be a valid, zero terminated string. |
|---|
| 60 | * @return true if the file is succesfully loaded. |
|---|
| 61 | * @return false if the file does not exist, can not be accessed or is otherwise invalid. |
|---|
| 62 | */ |
|---|
| 63 | bool OpenFile(const char * const fname); |
|---|
| 64 | |
|---|
| 65 | ///Loads given INI file. (STL version) |
|---|
| 66 | /** |
|---|
| 67 | * @note File will be loaded even if it contains no ini sections, and even empty sections will be read. |
|---|
| 68 | * @param fname Filename of the file to load. |
|---|
| 69 | * @return true if the file is succesfully loaded. |
|---|
| 70 | * @return false if the file does not exist, can not be accessed or is otherwise invalid. |
|---|
| 71 | */ |
|---|
| 72 | #ifndef INI_NO_STL |
|---|
| 73 | bool OpenFile (const std::string & fname) |
|---|
| 74 | { |
|---|
| 75 | return OpenFile(fname.c_str()); |
|---|
| 76 | } |
|---|
| 77 | #endif |
|---|
| 78 | |
|---|
| 79 | ///Loads INI file from given zero terminated buffer. |
|---|
| 80 | /** |
|---|
| 81 | * @note File will be loaded even if it contains no ini sections, and even empty sections will be read. |
|---|
| 82 | * Useful e.g. for loading INI files from compressed files. |
|---|
| 83 | * @param buf Buffer to load from. Must be zero terminated. |
|---|
| 84 | * @param size Size of the buffer (including terminating zero). |
|---|
| 85 | */ |
|---|
| 86 | bool LoadBuffer(const char * buf, unsigned size); |
|---|
| 87 | |
|---|
| 88 | ///Destructor. |
|---|
| 89 | /** |
|---|
| 90 | * Deletes loaded INI file (if any) |
|---|
| 91 | */ |
|---|
| 92 | ~INIFile(); |
|---|
| 93 | |
|---|
| 94 | ///Gets pointer to requested section. |
|---|
| 95 | /** |
|---|
| 96 | * @param name Name of the section to get. Must be a valid, zero terminated string. |
|---|
| 97 | * @return pointer to requested section if the section exists. |
|---|
| 98 | * @return NULL if the section does not exist. |
|---|
| 99 | */ |
|---|
| 100 | INISection * GetSection(const char * const name) const; |
|---|
| 101 | |
|---|
| 102 | ///Loads given INI file. (STL version) |
|---|
| 103 | /** |
|---|
| 104 | * @param name Name of the section to get. |
|---|
| 105 | * @return pointer to requested section if the section exists. |
|---|
| 106 | * @return NULL if the section does not exist. |
|---|
| 107 | */ |
|---|
| 108 | #ifndef INI_NO_STL |
|---|
| 109 | INISection * GetSection(const std::string & name) const |
|---|
| 110 | { |
|---|
| 111 | assert(IsValid()); |
|---|
| 112 | return GetSection(name.c_str()); |
|---|
| 113 | } |
|---|
| 114 | #endif |
|---|
| 115 | |
|---|
| 116 | ///[] operator : alias for GetSection(). |
|---|
| 117 | /** |
|---|
| 118 | * @param name Name of the section to get. Must be a valid, zero terminated string. |
|---|
| 119 | * @return pointer to requested section if the section exists. |
|---|
| 120 | * @return NULL if the section does not exist. |
|---|
| 121 | */ |
|---|
| 122 | INISection * operator [] (const char * const name) const |
|---|
| 123 | { |
|---|
| 124 | assert(IsValid()); |
|---|
| 125 | assert(name); |
|---|
| 126 | return GetSection(name); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | ///[] operator : alias for GetSection() (STL version). |
|---|
| 130 | /** |
|---|
| 131 | * @param name Name of the section to get. |
|---|
| 132 | * @return pointer to requested section if the section exists. |
|---|
| 133 | * @return NULL if the section does not exist. |
|---|
| 134 | */ |
|---|
| 135 | #ifndef INI_NO_STL |
|---|
| 136 | INISection * operator [] (const std::string & name) const |
|---|
| 137 | { |
|---|
| 138 | assert(IsValid()); |
|---|
| 139 | return GetSection(name); |
|---|
| 140 | } |
|---|
| 141 | #endif |
|---|
| 142 | |
|---|
| 143 | ///@return number of sections in the file. |
|---|
| 144 | unsigned GetLength() const |
|---|
| 145 | { |
|---|
| 146 | return static_cast<unsigned>(Length); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | ///@return true if this INIFile() is initialised, false otherwise. |
|---|
| 150 | bool IsValid() const |
|---|
| 151 | { |
|---|
| 152 | //If any of these is 0/NULL, this INIFile was constructed |
|---|
| 153 | //but not initialised. |
|---|
| 154 | return Length && Sections && Alloc; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | private: |
|---|
| 158 | |
|---|
| 159 | INIFile(const INIFile &); |
|---|
| 160 | |
|---|
| 161 | void operator = (const INIFile &); |
|---|
| 162 | |
|---|
| 163 | ///Perform specified benchmark on this file. |
|---|
| 164 | /** |
|---|
| 165 | * @param type Benchmark to perform. |
|---|
| 166 | */ |
|---|
| 167 | void Benchmark(BenchmarkType type, bool stl); |
|---|
| 168 | }; |
|---|
| 169 | |
|---|
| 170 | #endif |
|---|