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 00021 00024 class INISection 00025 { 00026 friend class INIFile; 00027 private: 00028 00030 miniini_private::c * Name; 00032 miniini_private::ui Length; 00038 miniini_private::c * * Tags; 00040 miniini_private::Allocator * Alloc; 00042 miniini_private::i Iter; 00043 00044 00047 static miniini_private::ui temptagscap; 00049 static miniini_private::c * * temptags; 00051 static miniini_private::ui tagcap; 00053 static miniini_private::c * tagbuf; 00054 00055 public: 00056 00060 const char * GetName() const 00061 { 00062 return Name; 00063 } 00064 00065 #ifndef MINIINI_NO_STL 00066 00069 std::string GetNameSTL() const 00070 { 00071 return std::string(Name); 00072 } 00073 #endif 00074 00078 unsigned GetLength() const 00079 { 00080 return static_cast<unsigned>(Length); 00081 } 00082 00084 void Reset() 00085 { 00086 Iter = -1; 00087 } 00088 00093 bool Next() 00094 { 00095 if(Iter < static_cast<miniini_private::i>(Length) - 1) 00096 { 00097 ++Iter; 00098 return true; 00099 } 00100 return false; 00101 } 00105 const char * CurrentTag() const 00106 { 00107 MINIINI_ASSERT(Iter >= static_cast<miniini_private::i>(0) && 00108 Iter < static_cast<miniini_private::i>(Length), 00109 "Called INISection::CurrentTag() with iteration index " 00110 "out of range"); 00111 return Tags[Iter]; 00112 } 00113 00117 #ifndef MINIINI_NO_STL 00118 std::string CurrentTagSTL() const 00119 { 00120 MINIINI_ASSERT(Iter >= static_cast<miniini_private::i>(0) && 00121 Iter < static_cast<miniini_private::i>(Length), 00122 "Called INISection::CurrentTagSTL() with iteration " 00123 "index out of range"); 00124 return std::string(Tags[Iter]); 00125 } 00126 #endif 00127 00138 bool ReadString(const char * const name, const char * & out) const; 00139 00147 #ifndef MINIINI_NO_STL 00148 bool ReadString(const std::string & name, std::string & out) const 00149 { 00150 const miniini_private::c * tempout; 00151 bool ret = ReadString(name.c_str(), tempout); 00152 if(ret) 00153 { 00154 out = tempout; 00155 } 00156 return ret; 00157 } 00158 #endif 00159 00167 bool ReadInt(const char * const name, int & out) const; 00168 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 00190 bool ReadFloat(const char * const name, float & out) const; 00191 00199 #ifndef MINIINI_NO_STL 00200 bool ReadFloat(const std::string & name, float & out) const 00201 { 00202 return ReadFloat(name.c_str(), out); 00203 } 00204 #endif 00205 00213 bool ReadBool(const char * const name, bool & out) const; 00214 00222 #ifndef MINIINI_NO_STL 00223 bool ReadBool(const std::string & name, bool & out) const 00224 { 00225 return ReadBool(name.c_str(), out); 00226 } 00227 #endif 00228 00233 unsigned MultiValSize(const char * const name) const; 00234 00245 unsigned ReadMultiString(const char * const name, const char * * out, 00246 const unsigned cap) const; 00247 00255 #ifndef MINIINI_NO_STL 00256 unsigned ReadMultiString(const std::string & name, 00257 std::vector<std::string> & out) const; 00258 #endif 00259 00267 unsigned ReadMultiInt(const char * const name, int * out, 00268 const unsigned cap) const; 00269 00277 #ifndef MINIINI_NO_STL 00278 unsigned ReadMultiInt(const std::string & name, 00279 std::vector<int> & out) const; 00280 #endif 00281 00289 unsigned ReadMultiFloat(const char * const name, float * out, 00290 const unsigned cap) const; 00291 00299 #ifndef MINIINI_NO_STL 00300 unsigned ReadMultiFloat(const std::string & name, 00301 std::vector<float> & out) const; 00302 #endif 00303 00311 unsigned ReadMultiBool(const char * const name, bool * out, 00312 const unsigned cap) const; 00313 00321 #ifndef MINIINI_NO_STL 00322 unsigned ReadMultiBool(const std::string & name, 00323 std::vector<bool> & out) const; 00324 #endif 00325 00332 unsigned ArraySize(const char * const name) const; 00333 00348 unsigned ReadStrings(const char * const name, const char * * out, 00349 const unsigned cap) const; 00350 00360 #ifndef MINIINI_NO_STL 00361 unsigned ReadStrings(const std::string & name, 00362 std::vector<std::string> & out) const; 00363 #endif 00364 00374 unsigned ReadInts(const char * const name, int * out, 00375 const unsigned cap) const; 00376 00386 #ifndef MINIINI_NO_STL 00387 unsigned ReadInts(const std::string & name, 00388 std::vector<int> & out) const; 00389 #endif 00390 00400 unsigned ReadFloats(const char * const name, float * out, 00401 const unsigned cap) const; 00402 00412 #ifndef MINIINI_NO_STL 00413 unsigned ReadFloats(const std::string & name, 00414 std::vector<float> & out) const; 00415 #endif 00416 00426 unsigned ReadBools(const char * const name, bool * out, 00427 const unsigned cap) const; 00428 00438 #ifndef MINIINI_NO_STL 00439 unsigned ReadBools(const std::string & name, 00440 std::vector<bool> & out) const; 00441 #endif 00442 00443 private: 00444 00446 INISection() 00447 :Name(NULL) 00448 ,Length(0) 00449 ,Tags(NULL) 00450 ,Alloc(NULL) 00451 ,Iter(-1) 00452 {} 00453 00455 ~INISection(); 00456 00471 static inline miniini_private::LineToken TagName(const miniini_private::c * & currentcharref, 00472 miniini_private::ui & tagsizeref); 00473 00482 static inline miniini_private::ui TagValue(const miniini_private::c * & currentcharref, 00483 miniini_private::ui tagsize); 00484 00490 static inline bool Header(const miniini_private::c * & currentcharref); 00491 00499 static inline miniini_private::ui ParseInts(const miniini_private::c * * strings, 00500 int * out, 00501 const miniini_private::ui numstrings); 00502 00510 static inline miniini_private::ui ParseFloats(const miniini_private::c * * strings, 00511 float * out, 00512 const miniini_private::ui numstrings); 00513 00521 static inline miniini_private::ui ParseBools(const miniini_private::c * * strings, 00522 bool * out, 00523 const miniini_private::ui numstrings); 00524 00533 void Init(const miniini_private::c * const sectionname, 00534 const miniini_private::c * * const currentcharptr, 00535 miniini_private::Allocator * const alloc); 00536 00538 static void InitTempData() 00539 { 00540 MINIINI_ASSERT(!temptags, "INISection::InitTempData() : temptags " 00541 "is already allocated"); 00542 MINIINI_ASSERT(!tagbuf, "INISection::InitTempData() : tagbuf is " 00543 "already allocated"); 00544 temptagscap = 8; 00545 temptags = new miniini_private::c * [temptagscap]; 00546 //MUST be over 6 00547 tagcap = 64; 00548 tagbuf = new miniini_private::c [tagcap]; 00549 } 00550 00552 static void DestroyTempData() 00553 { 00554 MINIINI_ASSERT(temptags, "INISection::DestroyTempData() : temptags " 00555 "is not allocated"); 00556 MINIINI_ASSERT(tagbuf, "INISection::DestroyTempData() : tagbuf is " 00557 "not allocated"); 00558 temptagscap = 0; 00559 delete [] temptags; 00560 temptags = NULL; 00561 tagcap = 0; 00562 delete [] tagbuf; 00563 tagbuf = NULL; 00564 } 00565 00567 00568 INISection(const INISection &); 00569 00570 void operator = (const INISection &); 00571 }; 00572 00573 #endif