Made on Kubuntu
00001 // Copyright (C) 2009-2010 Ferdinand Majerech 00002 // This file is part of MiniINI 00003 // For conditions of distribution and use, see copyright notice in LICENSE.txt 00004 00005 #ifndef INISECTION_H_INCLUDED 00006 #define INISECTION_H_INCLUDED 00007 00008 #include <cstring> 00009 #include <cstdlib> 00010 00011 #include "typedefs.h" 00012 #include "miniini_assert.h" 00013 #include "allocator.h" 00014 #include "linetoken.h" 00015 #ifndef MINIINI_NO_STL 00016 #include <string> 00017 #include <vector> 00018 #endif 00019 00020 00022 00025 class INISection 00026 { 00027 friend class INIFile; 00028 private: 00029 00031 miniini_private::c * Name; 00033 miniini_private::ui Length; 00035 00039 miniini_private::c * * Tags; 00041 miniini_private::Allocator * Alloc; 00043 miniini_private::i Iter; 00044 00045 00048 static miniini_private::ui temptagscap; 00050 static miniini_private::c * * temptags; 00052 static miniini_private::ui tagcap; 00054 static miniini_private::c * tagbuf; 00055 00056 public: 00057 00059 const char * GetName() const 00060 { 00061 return Name; 00062 } 00063 00064 #ifndef MINIINI_NO_STL 00065 00066 std::string GetNameSTL() const 00067 { 00068 return std::string(Name); 00069 } 00070 #endif 00071 00073 unsigned GetLength() const 00074 { 00075 return static_cast<unsigned>(Length); 00076 } 00077 00079 void Reset() 00080 { 00081 Iter = -1; 00082 } 00083 00085 00089 bool Next() 00090 { 00091 if(Iter < static_cast<miniini_private::i>(Length) - 1) 00092 { 00093 ++Iter; 00094 return true; 00095 } 00096 return false; 00097 } 00098 00100 const char * CurrentTag() const 00101 { 00102 MINIINI_ASSERT(Iter >= static_cast<miniini_private::i>(0) && 00103 Iter < static_cast<miniini_private::i>(Length), 00104 "Called INISection::CurrentTag() with iteration index " 00105 "out of range"); 00106 return Tags[Iter]; 00107 } 00108 00110 #ifndef MINIINI_NO_STL 00111 std::string CurrentTagSTL() const 00112 { 00113 MINIINI_ASSERT(Iter >= static_cast<miniini_private::i>(0) && 00114 Iter < static_cast<miniini_private::i>(Length), 00115 "Called INISection::CurrentTagSTL() with iteration " 00116 "index out of range"); 00117 return std::string(Tags[Iter]); 00118 } 00119 #endif 00120 00122 00132 bool ReadString(const char * const name, const char * & out) const; 00133 00135 00145 #ifndef MINIINI_NO_STL 00146 bool ReadString(const std::string & name, std::string & out) const 00147 { 00148 const miniini_private::c * tempout; 00149 bool ret = ReadString(name.c_str(), tempout); 00150 if(ret) 00151 { 00152 out = tempout; 00153 } 00154 return ret; 00155 } 00156 #endif 00157 00159 00166 bool ReadInt(const char * const name, int & out) const; 00167 00169 00176 #ifndef MINIINI_NO_STL 00177 bool ReadInt(const std::string & name, int & out) const 00178 { 00179 return ReadInt(name.c_str(), out); 00180 } 00181 #endif 00182 00184 00191 bool ReadFloat(const char * const name, float & out) const; 00192 00194 00201 #ifndef MINIINI_NO_STL 00202 bool ReadFloat(const std::string & name, float & out) const 00203 { 00204 return ReadFloat(name.c_str(), out); 00205 } 00206 #endif 00207 00209 00216 bool ReadBool(const char * const name, bool & out) const; 00217 00219 00226 #ifndef MINIINI_NO_STL 00227 bool ReadBool(const std::string & name, bool & out) const 00228 { 00229 return ReadBool(name.c_str(), out); 00230 } 00231 #endif 00232 00234 00238 unsigned MultiValSize(const char * const name) const; 00239 00241 00251 unsigned ReadMultiString(const char * const name, const char * * out, 00252 const unsigned cap) const; 00253 00255 00265 #ifndef MINIINI_NO_STL 00266 unsigned ReadMultiString(const std::string & name, 00267 std::vector<std::string> & out) const; 00268 #endif 00269 00271 00278 unsigned ReadMultiInt(const char * const name, int * out, 00279 const unsigned cap) const; 00280 00282 00289 #ifndef MINIINI_NO_STL 00290 unsigned ReadMultiInt(const std::string & name, 00291 std::vector<int> & out) const; 00292 #endif 00293 00295 00302 unsigned ReadMultiFloat(const char * const name, float * out, 00303 const unsigned cap) const; 00304 00306 00313 #ifndef MINIINI_NO_STL 00314 unsigned ReadMultiFloat(const std::string & name, 00315 std::vector<float> & out) const; 00316 #endif 00317 00319 00326 unsigned ReadMultiBool(const char * const name, bool * out, 00327 const unsigned cap) const; 00328 00330 00337 #ifndef MINIINI_NO_STL 00338 unsigned ReadMultiBool(const std::string & name, 00339 std::vector<bool> & out) const; 00340 #endif 00341 00343 00349 unsigned ArraySize(const char * const name) const; 00350 00352 00366 unsigned ReadStrings(const char * const name, const char * * out, 00367 const unsigned cap) const; 00368 00370 00383 #ifndef MINIINI_NO_STL 00384 unsigned ReadStrings(const std::string & name, 00385 std::vector<std::string> & out) const; 00386 #endif 00387 00389 00398 unsigned ReadInts(const char * const name, int * out, 00399 const unsigned cap) const; 00400 00402 00412 #ifndef MINIINI_NO_STL 00413 unsigned ReadInts(const std::string & name, 00414 std::vector<int> & out) const; 00415 #endif 00416 00418 00428 unsigned ReadFloats(const char * const name, float * out, 00429 const unsigned cap) const; 00430 00432 00442 #ifndef MINIINI_NO_STL 00443 unsigned ReadFloats(const std::string & name, 00444 std::vector<float> & out) const; 00445 #endif 00446 00448 00457 unsigned ReadBools(const char * const name, bool * out, 00458 const unsigned cap) const; 00459 00461 00471 #ifndef MINIINI_NO_STL 00472 unsigned ReadBools(const std::string & name, 00473 std::vector<bool> & out) const; 00474 #endif 00475 00476 private: 00477 00479 INISection() 00480 :Name(NULL) 00481 ,Length(0) 00482 ,Tags(NULL) 00483 ,Alloc(NULL) 00484 ,Iter(-1) 00485 {} 00486 00488 ~INISection(); 00489 00491 00505 static inline miniini_private::LineToken TagName(const miniini_private::c * & currentcharref, 00506 miniini_private::ui & tagsizeref); 00507 00509 00517 static inline miniini_private::ui TagValue(const miniini_private::c * & currentcharref, 00518 miniini_private::ui tagsize); 00519 00521 00526 static inline bool Header(const miniini_private::c * & currentcharref); 00527 00529 00536 static inline miniini_private::ui ParseInts(const miniini_private::c * * strings, 00537 int * out, 00538 const miniini_private::ui numstrings); 00539 00541 00548 static inline miniini_private::ui ParseFloats(const miniini_private::c * * strings, 00549 float * out, 00550 const miniini_private::ui numstrings); 00551 00553 00560 static inline miniini_private::ui ParseBools(const miniini_private::c * * strings, 00561 bool * out, 00562 const miniini_private::ui numstrings); 00563 00565 /* 00566 * Loads the section and changes currentcharptr so that the caller can 00567 * start with another section. 00568 * @param sectionname Name of the section to initialize 00569 * @param currentcharptr Start of the section in a raw ini file buffer. 00570 * @return true if the section is valid. 00571 * @return false if the section is empty. 00572 */ 00573 void Init(const miniini_private::c * const sectionname, 00574 const miniini_private::c * * const currentcharptr, 00575 miniini_private::Allocator * const alloc); 00576 00578 static void InitTempData() 00579 { 00580 MINIINI_ASSERT(!temptags, "INISection::InitTempData() : temptags " 00581 "is already allocated"); 00582 MINIINI_ASSERT(!tagbuf, "INISection::InitTempData() : tagbuf is " 00583 "already allocated"); 00584 temptagscap = 8; 00585 temptags = new miniini_private::c * [temptagscap]; 00586 //MUST be over 6 00587 tagcap = 64; 00588 tagbuf = new miniini_private::c [tagcap]; 00589 } 00590 00592 static void DestroyTempData() 00593 { 00594 MINIINI_ASSERT(temptags, "INISection::DestroyTempData() : temptags " 00595 "is not allocated"); 00596 MINIINI_ASSERT(tagbuf, "INISection::DestroyTempData() : tagbuf is " 00597 "not allocated"); 00598 temptagscap = 0; 00599 delete [] temptags; 00600 temptags = NULL; 00601 tagcap = 0; 00602 delete [] tagbuf; 00603 tagbuf = NULL; 00604 } 00605 00607 00608 INISection(const INISection &); 00609 00610 void operator = (const INISection &); 00611 }; 00612 00613 #endif