SimpleIni.h 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476
  1. /** @mainpage
  2. <table>
  3. <tr><th>Library <td>SimpleIni
  4. <tr><th>File <td>SimpleIni.h
  5. <tr><th>Author <td>Brodie Thiesfield [brofield at gmail dot com]
  6. <tr><th>Source <td>https://github.com/brofield/simpleini
  7. <tr><th>Version <td>4.17
  8. </table>
  9. Jump to the @link CSimpleIniTempl CSimpleIni @endlink interface documentation.
  10. @section intro INTRODUCTION
  11. This component allows an INI-style configuration file to be used on both
  12. Windows and Linux/Unix. It is fast, simple and source code using this
  13. component will compile unchanged on either OS.
  14. @section features FEATURES
  15. - MIT Licence allows free use in all software (including GPL and commercial)
  16. - multi-platform (Windows CE/9x/NT..10/etc, Linux, MacOSX, Unix)
  17. - loading and saving of INI-style configuration files
  18. - configuration files can have any newline format on all platforms
  19. - liberal acceptance of file format
  20. - key/values with no section
  21. - removal of whitespace around sections, keys and values
  22. - support for multi-line values (values with embedded newline characters)
  23. - optional support for multiple keys with the same name
  24. - optional case-insensitive sections and keys (for ASCII characters only)
  25. - saves files with sections and keys in the same order as they were loaded
  26. - preserves comments on the file, section and keys where possible.
  27. - supports both char or wchar_t programming interfaces
  28. - supports both MBCS (system locale) and UTF-8 file encodings
  29. - system locale does not need to be UTF-8 on Linux/Unix to load UTF-8 file
  30. - support for non-ASCII characters in section, keys, values and comments
  31. - support for non-standard character types or file encodings
  32. via user-written converter classes
  33. - support for adding/modifying values programmatically
  34. - compiles cleanly in the following compilers:
  35. - Windows/VC6 (warning level 3)
  36. - Windows/VC.NET 2003 (warning level 4)
  37. - Windows/VC 2005 (warning level 4)
  38. - Linux/gcc (-Wall)
  39. @section usage USAGE SUMMARY
  40. -# Define the appropriate symbol for the converter you wish to use and
  41. include the SimpleIni.h header file. If no specific converter is defined
  42. then the default converter is used. The default conversion mode uses
  43. SI_CONVERT_WIN32 on Windows and SI_CONVERT_GENERIC on all other
  44. platforms. If you are using ICU then SI_CONVERT_ICU is supported on all
  45. platforms.
  46. -# Declare an instance the appropriate class. Note that the following
  47. definitions are just shortcuts for commonly used types. Other types
  48. (PRUnichar, unsigned short, unsigned char) are also possible.
  49. <table>
  50. <tr><th>Interface <th>Case-sensitive <th>Load UTF-8 <th>Load MBCS <th>Typedef
  51. <tr><th>SI_CONVERT_GENERIC
  52. <tr><td>char <td>No <td>Yes <td>Yes #1 <td>CSimpleIniA
  53. <tr><td>char <td>Yes <td>Yes <td>Yes <td>CSimpleIniCaseA
  54. <tr><td>wchar_t <td>No <td>Yes <td>Yes <td>CSimpleIniW
  55. <tr><td>wchar_t <td>Yes <td>Yes <td>Yes <td>CSimpleIniCaseW
  56. <tr><th>SI_CONVERT_WIN32
  57. <tr><td>char <td>No <td>No #2 <td>Yes <td>CSimpleIniA
  58. <tr><td>char <td>Yes <td>Yes <td>Yes <td>CSimpleIniCaseA
  59. <tr><td>wchar_t <td>No <td>Yes <td>Yes <td>CSimpleIniW
  60. <tr><td>wchar_t <td>Yes <td>Yes <td>Yes <td>CSimpleIniCaseW
  61. <tr><th>SI_CONVERT_ICU
  62. <tr><td>char <td>No <td>Yes <td>Yes <td>CSimpleIniA
  63. <tr><td>char <td>Yes <td>Yes <td>Yes <td>CSimpleIniCaseA
  64. <tr><td>UChar <td>No <td>Yes <td>Yes <td>CSimpleIniW
  65. <tr><td>UChar <td>Yes <td>Yes <td>Yes <td>CSimpleIniCaseW
  66. </table>
  67. #1 On Windows you are better to use CSimpleIniA with SI_CONVERT_WIN32.<br>
  68. #2 Only affects Windows. On Windows this uses MBCS functions and
  69. so may fold case incorrectly leading to uncertain results.
  70. -# Call LoadData() or LoadFile() to load and parse the INI configuration file
  71. -# Access and modify the data of the file using the following functions
  72. <table>
  73. <tr><td>GetAllSections <td>Return all section names
  74. <tr><td>GetAllKeys <td>Return all key names within a section
  75. <tr><td>GetAllValues <td>Return all values within a section & key
  76. <tr><td>GetSection <td>Return all key names and values in a section
  77. <tr><td>GetSectionSize <td>Return the number of keys in a section
  78. <tr><td>GetValue <td>Return a value for a section & key
  79. <tr><td>SetValue <td>Add or update a value for a section & key
  80. <tr><td>Delete <td>Remove a section, or a key from a section
  81. </table>
  82. -# Call Save() or SaveFile() to save the INI configuration data
  83. @section iostreams IO STREAMS
  84. SimpleIni supports reading from and writing to STL IO streams. Enable this
  85. by defining SI_SUPPORT_IOSTREAMS before including the SimpleIni.h header
  86. file. Ensure that if the streams are backed by a file (e.g. ifstream or
  87. ofstream) then the flag ios_base::binary has been used when the file was
  88. opened.
  89. @section multiline MULTI-LINE VALUES
  90. Values that span multiple lines are created using the following format.
  91. <pre>
  92. key = <<<ENDTAG
  93. .... multiline value ....
  94. ENDTAG
  95. </pre>
  96. Note the following:
  97. - The text used for ENDTAG can be anything and is used to find
  98. where the multi-line text ends.
  99. - The newline after ENDTAG in the start tag, and the newline
  100. before ENDTAG in the end tag is not included in the data value.
  101. - The ending tag must be on it's own line with no whitespace before
  102. or after it.
  103. - The multi-line value is modified at load so that each line in the value
  104. is delimited by a single '\\n' character on all platforms. At save time
  105. it will be converted into the newline format used by the current
  106. platform.
  107. @section comments COMMENTS
  108. Comments are preserved in the file within the following restrictions:
  109. - Every file may have a single "file comment". It must start with the
  110. first character in the file, and will end with the first non-comment
  111. line in the file.
  112. - Every section may have a single "section comment". It will start
  113. with the first comment line following the file comment, or the last
  114. data entry. It ends at the beginning of the section.
  115. - Every key may have a single "key comment". This comment will start
  116. with the first comment line following the section start, or the file
  117. comment if there is no section name.
  118. - Comments are set at the time that the file, section or key is first
  119. created. The only way to modify a comment on a section or a key is to
  120. delete that entry and recreate it with the new comment. There is no
  121. way to change the file comment.
  122. @section save SAVE ORDER
  123. The sections and keys are written out in the same order as they were
  124. read in from the file. Sections and keys added to the data after the
  125. file has been loaded will be added to the end of the file when it is
  126. written. There is no way to specify the location of a section or key
  127. other than in first-created, first-saved order.
  128. @section notes NOTES
  129. - To load UTF-8 data on Windows 95, you need to use Microsoft Layer for
  130. Unicode, or SI_CONVERT_GENERIC, or SI_CONVERT_ICU.
  131. - When using SI_CONVERT_GENERIC, ConvertUTF.c must be compiled and linked.
  132. - When using SI_CONVERT_ICU, ICU header files must be on the include
  133. path and icuuc.lib must be linked in.
  134. - To load a UTF-8 file on Windows AND expose it with SI_CHAR == char,
  135. you should use SI_CONVERT_GENERIC.
  136. - The collation (sorting) order used for sections and keys returned from
  137. iterators is NOT DEFINED. If collation order of the text is important
  138. then it should be done yourself by either supplying a replacement
  139. SI_STRLESS class, or by sorting the strings external to this library.
  140. - Usage of the <mbstring.h> header on Windows can be disabled by defining
  141. SI_NO_MBCS. This is defined automatically on Windows CE platforms.
  142. - Not thread-safe so manage your own locking
  143. @section contrib CONTRIBUTIONS
  144. - 2010/05/03: Tobias Gehrig: added GetDoubleValue()
  145. @section licence MIT LICENCE
  146. The licence text below is the boilerplate "MIT Licence" used from:
  147. http://www.opensource.org/licenses/mit-license.php
  148. Copyright (c) 2006-2012, Brodie Thiesfield
  149. Permission is hereby granted, free of charge, to any person obtaining a copy
  150. of this software and associated documentation files (the "Software"), to deal
  151. in the Software without restriction, including without limitation the rights
  152. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  153. copies of the Software, and to permit persons to whom the Software is furnished
  154. to do so, subject to the following conditions:
  155. The above copyright notice and this permission notice shall be included in
  156. all copies or substantial portions of the Software.
  157. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  158. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  159. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  160. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  161. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  162. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  163. */
  164. #ifndef INCLUDED_SimpleIni_h
  165. #define INCLUDED_SimpleIni_h
  166. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  167. # pragma once
  168. #endif
  169. // Disable these warnings in MSVC:
  170. // 4127 "conditional expression is constant" as the conversion classes trigger
  171. // it with the statement if (sizeof(SI_CHAR) == sizeof(char)). This test will
  172. // be optimized away in a release build.
  173. // 4503 'insert' : decorated name length exceeded, name was truncated
  174. // 4702 "unreachable code" as the MS STL header causes it in release mode.
  175. // Again, the code causing the warning will be cleaned up by the compiler.
  176. // 4786 "identifier truncated to 256 characters" as this is thrown hundreds
  177. // of times VC6 as soon as STL is used.
  178. #ifdef _MSC_VER
  179. # pragma warning (push)
  180. # pragma warning (disable: 4127 4503 4702 4786)
  181. #endif
  182. #include <cstring>
  183. #include <cstdlib>
  184. #include <string>
  185. #include <map>
  186. #include <list>
  187. #include <algorithm>
  188. #include <stdio.h>
  189. #ifdef SI_SUPPORT_IOSTREAMS
  190. # include <iostream>
  191. #endif // SI_SUPPORT_IOSTREAMS
  192. #ifdef _DEBUG
  193. # ifndef assert
  194. # include <cassert>
  195. # endif
  196. # define SI_ASSERT(x) assert(x)
  197. #else
  198. # define SI_ASSERT(x)
  199. #endif
  200. enum SI_Error {
  201. SI_OK = 0, //!< No error
  202. SI_UPDATED = 1, //!< An existing value was updated
  203. SI_INSERTED = 2, //!< A new value was inserted
  204. // note: test for any error with (retval < 0)
  205. SI_FAIL = -1, //!< Generic failure
  206. SI_NOMEM = -2, //!< Out of memory error
  207. SI_FILE = -3 //!< File error (see errno for detail error)
  208. };
  209. #define SI_UTF8_SIGNATURE "\xEF\xBB\xBF"
  210. #ifdef _WIN32
  211. # define SI_NEWLINE_A "\r\n"
  212. # define SI_NEWLINE_W L"\r\n"
  213. #else // !_WIN32
  214. # define SI_NEWLINE_A "\n"
  215. # define SI_NEWLINE_W L"\n"
  216. #endif // _WIN32
  217. #if defined(SI_CONVERT_ICU)
  218. # include <unicode/ustring.h>
  219. #endif
  220. #if defined(_WIN32)
  221. # define SI_HAS_WIDE_FILE
  222. # define SI_WCHAR_T wchar_t
  223. #elif defined(SI_CONVERT_ICU)
  224. # define SI_HAS_WIDE_FILE
  225. # define SI_WCHAR_T UChar
  226. #endif
  227. // ---------------------------------------------------------------------------
  228. // MAIN TEMPLATE CLASS
  229. // ---------------------------------------------------------------------------
  230. /** Simple INI file reader.
  231. This can be instantiated with the choice of unicode or native characterset,
  232. and case sensitive or insensitive comparisons of section and key names.
  233. The supported combinations are pre-defined with the following typedefs:
  234. <table>
  235. <tr><th>Interface <th>Case-sensitive <th>Typedef
  236. <tr><td>char <td>No <td>CSimpleIniA
  237. <tr><td>char <td>Yes <td>CSimpleIniCaseA
  238. <tr><td>wchar_t <td>No <td>CSimpleIniW
  239. <tr><td>wchar_t <td>Yes <td>CSimpleIniCaseW
  240. </table>
  241. Note that using other types for the SI_CHAR is supported. For instance,
  242. unsigned char, unsigned short, etc. Note that where the alternative type
  243. is a different size to char/wchar_t you may need to supply new helper
  244. classes for SI_STRLESS and SI_CONVERTER.
  245. */
  246. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  247. class CSimpleIniTempl
  248. {
  249. public:
  250. typedef SI_CHAR SI_CHAR_T;
  251. /** key entry */
  252. struct Entry {
  253. const SI_CHAR * pItem;
  254. const SI_CHAR * pComment;
  255. int nOrder;
  256. Entry(const SI_CHAR * a_pszItem = NULL, int a_nOrder = 0)
  257. : pItem(a_pszItem)
  258. , pComment(NULL)
  259. , nOrder(a_nOrder)
  260. { }
  261. Entry(const SI_CHAR * a_pszItem, const SI_CHAR * a_pszComment, int a_nOrder)
  262. : pItem(a_pszItem)
  263. , pComment(a_pszComment)
  264. , nOrder(a_nOrder)
  265. { }
  266. Entry(const Entry & rhs) { operator=(rhs); }
  267. Entry & operator=(const Entry & rhs) {
  268. pItem = rhs.pItem;
  269. pComment = rhs.pComment;
  270. nOrder = rhs.nOrder;
  271. return *this;
  272. }
  273. #if defined(_MSC_VER) && _MSC_VER <= 1200
  274. /** STL of VC6 doesn't allow me to specify my own comparator for list::sort() */
  275. bool operator<(const Entry & rhs) const { return LoadOrder()(*this, rhs); }
  276. bool operator>(const Entry & rhs) const { return LoadOrder()(rhs, *this); }
  277. #endif
  278. /** Strict less ordering by name of key only */
  279. struct KeyOrder {
  280. bool operator()(const Entry & lhs, const Entry & rhs) const {
  281. const static SI_STRLESS isLess = SI_STRLESS();
  282. return isLess(lhs.pItem, rhs.pItem);
  283. }
  284. };
  285. /** Strict less ordering by order, and then name of key */
  286. struct LoadOrder {
  287. bool operator()(const Entry & lhs, const Entry & rhs) const {
  288. if (lhs.nOrder != rhs.nOrder) {
  289. return lhs.nOrder < rhs.nOrder;
  290. }
  291. return KeyOrder()(lhs.pItem, rhs.pItem);
  292. }
  293. };
  294. };
  295. /** map keys to values */
  296. typedef std::multimap<Entry,const SI_CHAR *,typename Entry::KeyOrder> TKeyVal;
  297. /** map sections to key/value map */
  298. typedef std::map<Entry,TKeyVal,typename Entry::KeyOrder> TSection;
  299. /** set of dependent string pointers. Note that these pointers are
  300. dependent on memory owned by CSimpleIni.
  301. */
  302. typedef std::list<Entry> TNamesDepend;
  303. /** interface definition for the OutputWriter object to pass to Save()
  304. in order to output the INI file data.
  305. */
  306. class OutputWriter {
  307. public:
  308. OutputWriter() { }
  309. virtual ~OutputWriter() { }
  310. virtual void Write(const char * a_pBuf) = 0;
  311. private:
  312. OutputWriter(const OutputWriter &); // disable
  313. OutputWriter & operator=(const OutputWriter &); // disable
  314. };
  315. /** OutputWriter class to write the INI data to a file */
  316. class FileWriter : public OutputWriter {
  317. FILE * m_file;
  318. public:
  319. FileWriter(FILE * a_file) : m_file(a_file) { }
  320. void Write(const char * a_pBuf) {
  321. fputs(a_pBuf, m_file);
  322. }
  323. private:
  324. FileWriter(const FileWriter &); // disable
  325. FileWriter & operator=(const FileWriter &); // disable
  326. };
  327. /** OutputWriter class to write the INI data to a string */
  328. class StringWriter : public OutputWriter {
  329. std::string & m_string;
  330. public:
  331. StringWriter(std::string & a_string) : m_string(a_string) { }
  332. void Write(const char * a_pBuf) {
  333. m_string.append(a_pBuf);
  334. }
  335. private:
  336. StringWriter(const StringWriter &); // disable
  337. StringWriter & operator=(const StringWriter &); // disable
  338. };
  339. #ifdef SI_SUPPORT_IOSTREAMS
  340. /** OutputWriter class to write the INI data to an ostream */
  341. class StreamWriter : public OutputWriter {
  342. std::ostream & m_ostream;
  343. public:
  344. StreamWriter(std::ostream & a_ostream) : m_ostream(a_ostream) { }
  345. void Write(const char * a_pBuf) {
  346. m_ostream << a_pBuf;
  347. }
  348. private:
  349. StreamWriter(const StreamWriter &); // disable
  350. StreamWriter & operator=(const StreamWriter &); // disable
  351. };
  352. #endif // SI_SUPPORT_IOSTREAMS
  353. /** Characterset conversion utility class to convert strings to the
  354. same format as is used for the storage.
  355. */
  356. class Converter : private SI_CONVERTER {
  357. public:
  358. Converter(bool a_bStoreIsUtf8) : SI_CONVERTER(a_bStoreIsUtf8) {
  359. m_scratch.resize(1024);
  360. }
  361. Converter(const Converter & rhs) { operator=(rhs); }
  362. Converter & operator=(const Converter & rhs) {
  363. m_scratch = rhs.m_scratch;
  364. return *this;
  365. }
  366. bool ConvertToStore(const SI_CHAR * a_pszString) {
  367. size_t uLen = SI_CONVERTER::SizeToStore(a_pszString);
  368. if (uLen == (size_t)(-1)) {
  369. return false;
  370. }
  371. while (uLen > m_scratch.size()) {
  372. m_scratch.resize(m_scratch.size() * 2);
  373. }
  374. return SI_CONVERTER::ConvertToStore(
  375. a_pszString,
  376. const_cast<char*>(m_scratch.data()),
  377. m_scratch.size());
  378. }
  379. const char * Data() { return m_scratch.data(); }
  380. private:
  381. std::string m_scratch;
  382. };
  383. public:
  384. /*-----------------------------------------------------------------------*/
  385. /** Default constructor.
  386. @param a_bIsUtf8 See the method SetUnicode() for details.
  387. @param a_bMultiKey See the method SetMultiKey() for details.
  388. @param a_bMultiLine See the method SetMultiLine() for details.
  389. */
  390. CSimpleIniTempl(
  391. bool a_bIsUtf8 = false,
  392. bool a_bMultiKey = false,
  393. bool a_bMultiLine = false
  394. );
  395. /** Destructor */
  396. ~CSimpleIniTempl();
  397. /** Deallocate all memory stored by this object */
  398. void Reset();
  399. /** Has any data been loaded */
  400. bool IsEmpty() const { return m_data.empty(); }
  401. /*-----------------------------------------------------------------------*/
  402. /** @{ @name Settings */
  403. /** Set the storage format of the INI data. This affects both the loading
  404. and saving of the INI data using all of the Load/Save API functions.
  405. This value cannot be changed after any INI data has been loaded.
  406. If the file is not set to Unicode (UTF-8), then the data encoding is
  407. assumed to be the OS native encoding. This encoding is the system
  408. locale on Linux/Unix and the legacy MBCS encoding on Windows NT/2K/XP.
  409. If the storage format is set to Unicode then the file will be loaded
  410. as UTF-8 encoded data regardless of the native file encoding. If
  411. SI_CHAR == char then all of the char* parameters take and return UTF-8
  412. encoded data regardless of the system locale.
  413. \param a_bIsUtf8 Assume UTF-8 encoding for the source?
  414. */
  415. void SetUnicode(bool a_bIsUtf8 = true) {
  416. if (!m_pData) m_bStoreIsUtf8 = a_bIsUtf8;
  417. }
  418. /** Get the storage format of the INI data. */
  419. bool IsUnicode() const { return m_bStoreIsUtf8; }
  420. /** Should multiple identical keys be permitted in the file. If set to false
  421. then the last value encountered will be used as the value of the key.
  422. If set to true, then all values will be available to be queried. For
  423. example, with the following input:
  424. <pre>
  425. [section]
  426. test=value1
  427. test=value2
  428. </pre>
  429. Then with SetMultiKey(true), both of the values "value1" and "value2"
  430. will be returned for the key test. If SetMultiKey(false) is used, then
  431. the value for "test" will only be "value2". This value may be changed
  432. at any time.
  433. \param a_bAllowMultiKey Allow multi-keys in the source?
  434. */
  435. void SetMultiKey(bool a_bAllowMultiKey = true) {
  436. m_bAllowMultiKey = a_bAllowMultiKey;
  437. }
  438. /** Get the storage format of the INI data. */
  439. bool IsMultiKey() const { return m_bAllowMultiKey; }
  440. /** Should data values be permitted to span multiple lines in the file. If
  441. set to false then the multi-line construct <<<TAG as a value will be
  442. returned as is instead of loading the data. This value may be changed
  443. at any time.
  444. \param a_bAllowMultiLine Allow multi-line values in the source?
  445. */
  446. void SetMultiLine(bool a_bAllowMultiLine = true) {
  447. m_bAllowMultiLine = a_bAllowMultiLine;
  448. }
  449. /** Query the status of multi-line data */
  450. bool IsMultiLine() const { return m_bAllowMultiLine; }
  451. /** Should spaces be added around the equals sign when writing key/value
  452. pairs out. When true, the result will be "key = value". When false,
  453. the result will be "key=value". This value may be changed at any time.
  454. \param a_bSpaces Add spaces around the equals sign?
  455. */
  456. void SetSpaces(bool a_bSpaces = true) {
  457. m_bSpaces = a_bSpaces;
  458. }
  459. /** Query the status of spaces output */
  460. bool UsingSpaces() const { return m_bSpaces; }
  461. /*-----------------------------------------------------------------------*/
  462. /** @}
  463. @{ @name Loading INI Data */
  464. /** Load an INI file from disk into memory
  465. @param a_pszFile Path of the file to be loaded. This will be passed
  466. to fopen() and so must be a valid path for the
  467. current platform.
  468. @return SI_Error See error definitions
  469. */
  470. SI_Error LoadFile(
  471. const char * a_pszFile
  472. );
  473. #ifdef SI_HAS_WIDE_FILE
  474. /** Load an INI file from disk into memory
  475. @param a_pwszFile Path of the file to be loaded in UTF-16.
  476. @return SI_Error See error definitions
  477. */
  478. SI_Error LoadFile(
  479. const SI_WCHAR_T * a_pwszFile
  480. );
  481. #endif // SI_HAS_WIDE_FILE
  482. /** Load the file from a file pointer.
  483. @param a_fpFile Valid file pointer to read the file data from. The
  484. file will be read until end of file.
  485. @return SI_Error See error definitions
  486. */
  487. SI_Error LoadFile(
  488. FILE * a_fpFile
  489. );
  490. #ifdef SI_SUPPORT_IOSTREAMS
  491. /** Load INI file data from an istream.
  492. @param a_istream Stream to read from
  493. @return SI_Error See error definitions
  494. */
  495. SI_Error LoadData(
  496. std::istream & a_istream
  497. );
  498. #endif // SI_SUPPORT_IOSTREAMS
  499. /** Load INI file data direct from a std::string
  500. @param a_strData Data to be loaded
  501. @return SI_Error See error definitions
  502. */
  503. SI_Error LoadData(const std::string & a_strData) {
  504. return LoadData(a_strData.c_str(), a_strData.size());
  505. }
  506. /** Load INI file data direct from memory
  507. @param a_pData Data to be loaded
  508. @param a_uDataLen Length of the data in bytes
  509. @return SI_Error See error definitions
  510. */
  511. SI_Error LoadData(
  512. const char * a_pData,
  513. size_t a_uDataLen
  514. );
  515. /*-----------------------------------------------------------------------*/
  516. /** @}
  517. @{ @name Saving INI Data */
  518. /** Save an INI file from memory to disk
  519. @param a_pszFile Path of the file to be saved. This will be passed
  520. to fopen() and so must be a valid path for the
  521. current platform.
  522. @param a_bAddSignature Prepend the UTF-8 BOM if the output data is
  523. in UTF-8 format. If it is not UTF-8 then
  524. this parameter is ignored.
  525. @return SI_Error See error definitions
  526. */
  527. SI_Error SaveFile(
  528. const char * a_pszFile,
  529. bool a_bAddSignature = true
  530. ) const;
  531. #ifdef SI_HAS_WIDE_FILE
  532. /** Save an INI file from memory to disk
  533. @param a_pwszFile Path of the file to be saved in UTF-16.
  534. @param a_bAddSignature Prepend the UTF-8 BOM if the output data is
  535. in UTF-8 format. If it is not UTF-8 then
  536. this parameter is ignored.
  537. @return SI_Error See error definitions
  538. */
  539. SI_Error SaveFile(
  540. const SI_WCHAR_T * a_pwszFile,
  541. bool a_bAddSignature = true
  542. ) const;
  543. #endif // _WIN32
  544. /** Save the INI data to a file. See Save() for details.
  545. @param a_pFile Handle to a file. File should be opened for
  546. binary output.
  547. @param a_bAddSignature Prepend the UTF-8 BOM if the output data is in
  548. UTF-8 format. If it is not UTF-8 then this value is
  549. ignored. Do not set this to true if anything has
  550. already been written to the file.
  551. @return SI_Error See error definitions
  552. */
  553. SI_Error SaveFile(
  554. FILE * a_pFile,
  555. bool a_bAddSignature = false
  556. ) const;
  557. /** Save the INI data. The data will be written to the output device
  558. in a format appropriate to the current data, selected by:
  559. <table>
  560. <tr><th>SI_CHAR <th>FORMAT
  561. <tr><td>char <td>same format as when loaded (MBCS or UTF-8)
  562. <tr><td>wchar_t <td>UTF-8
  563. <tr><td>other <td>UTF-8
  564. </table>
  565. Note that comments from the original data is preserved as per the
  566. documentation on comments. The order of the sections and values
  567. from the original file will be preserved.
  568. Any data prepended or appended to the output device must use the the
  569. same format (MBCS or UTF-8). You may use the GetConverter() method to
  570. convert text to the correct format regardless of the output format
  571. being used by SimpleIni.
  572. To add a BOM to UTF-8 data, write it out manually at the very beginning
  573. like is done in SaveFile when a_bUseBOM is true.
  574. @param a_oOutput Output writer to write the data to.
  575. @param a_bAddSignature Prepend the UTF-8 BOM if the output data is in
  576. UTF-8 format. If it is not UTF-8 then this value is
  577. ignored. Do not set this to true if anything has
  578. already been written to the OutputWriter.
  579. @return SI_Error See error definitions
  580. */
  581. SI_Error Save(
  582. OutputWriter & a_oOutput,
  583. bool a_bAddSignature = false
  584. ) const;
  585. #ifdef SI_SUPPORT_IOSTREAMS
  586. /** Save the INI data to an ostream. See Save() for details.
  587. @param a_ostream String to have the INI data appended to.
  588. @param a_bAddSignature Prepend the UTF-8 BOM if the output data is in
  589. UTF-8 format. If it is not UTF-8 then this value is
  590. ignored. Do not set this to true if anything has
  591. already been written to the stream.
  592. @return SI_Error See error definitions
  593. */
  594. SI_Error Save(
  595. std::ostream & a_ostream,
  596. bool a_bAddSignature = false
  597. ) const
  598. {
  599. StreamWriter writer(a_ostream);
  600. return Save(writer, a_bAddSignature);
  601. }
  602. #endif // SI_SUPPORT_IOSTREAMS
  603. /** Append the INI data to a string. See Save() for details.
  604. @param a_sBuffer String to have the INI data appended to.
  605. @param a_bAddSignature Prepend the UTF-8 BOM if the output data is in
  606. UTF-8 format. If it is not UTF-8 then this value is
  607. ignored. Do not set this to true if anything has
  608. already been written to the string.
  609. @return SI_Error See error definitions
  610. */
  611. SI_Error Save(
  612. std::string & a_sBuffer,
  613. bool a_bAddSignature = false
  614. ) const
  615. {
  616. StringWriter writer(a_sBuffer);
  617. return Save(writer, a_bAddSignature);
  618. }
  619. /*-----------------------------------------------------------------------*/
  620. /** @}
  621. @{ @name Accessing INI Data */
  622. /** Retrieve all section names. The list is returned as an STL vector of
  623. names and can be iterated or searched as necessary. Note that the
  624. sort order of the returned strings is NOT DEFINED. You can sort
  625. the names into the load order if desired. Search this file for ".sort"
  626. for an example.
  627. NOTE! This structure contains only pointers to strings. The actual
  628. string data is stored in memory owned by CSimpleIni. Ensure that the
  629. CSimpleIni object is not destroyed or Reset() while these pointers
  630. are in use!
  631. @param a_names Vector that will receive all of the section
  632. names. See note above!
  633. */
  634. void GetAllSections(
  635. TNamesDepend & a_names
  636. ) const;
  637. /** Retrieve all unique key names in a section. The sort order of the
  638. returned strings is NOT DEFINED. You can sort the names into the load
  639. order if desired. Search this file for ".sort" for an example. Only
  640. unique key names are returned.
  641. NOTE! This structure contains only pointers to strings. The actual
  642. string data is stored in memory owned by CSimpleIni. Ensure that the
  643. CSimpleIni object is not destroyed or Reset() while these strings
  644. are in use!
  645. @param a_pSection Section to request data for
  646. @param a_names List that will receive all of the key
  647. names. See note above!
  648. @return true Section was found.
  649. @return false Matching section was not found.
  650. */
  651. bool GetAllKeys(
  652. const SI_CHAR * a_pSection,
  653. TNamesDepend & a_names
  654. ) const;
  655. /** Retrieve all values for a specific key. This method can be used when
  656. multiple keys are both enabled and disabled. Note that the sort order
  657. of the returned strings is NOT DEFINED. You can sort the names into
  658. the load order if desired. Search this file for ".sort" for an example.
  659. NOTE! The returned values are pointers to string data stored in memory
  660. owned by CSimpleIni. Ensure that the CSimpleIni object is not destroyed
  661. or Reset while you are using this pointer!
  662. @param a_pSection Section to search
  663. @param a_pKey Key to search for
  664. @param a_values List to return if the key is not found
  665. @return true Key was found.
  666. @return false Matching section/key was not found.
  667. */
  668. bool GetAllValues(
  669. const SI_CHAR * a_pSection,
  670. const SI_CHAR * a_pKey,
  671. TNamesDepend & a_values
  672. ) const;
  673. /** Query the number of keys in a specific section. Note that if multiple
  674. keys are enabled, then this value may be different to the number of
  675. keys returned by GetAllKeys.
  676. @param a_pSection Section to request data for
  677. @return -1 Section does not exist in the file
  678. @return >=0 Number of keys in the section
  679. */
  680. int GetSectionSize(
  681. const SI_CHAR * a_pSection
  682. ) const;
  683. /** Retrieve all key and value pairs for a section. The data is returned
  684. as a pointer to an STL map and can be iterated or searched as
  685. desired. Note that multiple entries for the same key may exist when
  686. multiple keys have been enabled.
  687. NOTE! This structure contains only pointers to strings. The actual
  688. string data is stored in memory owned by CSimpleIni. Ensure that the
  689. CSimpleIni object is not destroyed or Reset() while these strings
  690. are in use!
  691. @param a_pSection Name of the section to return
  692. @return boolean Was a section matching the supplied
  693. name found.
  694. */
  695. const TKeyVal * GetSection(
  696. const SI_CHAR * a_pSection
  697. ) const;
  698. /** Retrieve the value for a specific key. If multiple keys are enabled
  699. (see SetMultiKey) then only the first value associated with that key
  700. will be returned, see GetAllValues for getting all values with multikey.
  701. NOTE! The returned value is a pointer to string data stored in memory
  702. owned by CSimpleIni. Ensure that the CSimpleIni object is not destroyed
  703. or Reset while you are using this pointer!
  704. @param a_pSection Section to search
  705. @param a_pKey Key to search for
  706. @param a_pDefault Value to return if the key is not found
  707. @param a_pHasMultiple Optionally receive notification of if there are
  708. multiple entries for this key.
  709. @return a_pDefault Key was not found in the section
  710. @return other Value of the key
  711. */
  712. const SI_CHAR * GetValue(
  713. const SI_CHAR * a_pSection,
  714. const SI_CHAR * a_pKey,
  715. const SI_CHAR * a_pDefault = NULL,
  716. bool * a_pHasMultiple = NULL
  717. ) const;
  718. /** Retrieve a numeric value for a specific key. If multiple keys are enabled
  719. (see SetMultiKey) then only the first value associated with that key
  720. will be returned, see GetAllValues for getting all values with multikey.
  721. @param a_pSection Section to search
  722. @param a_pKey Key to search for
  723. @param a_nDefault Value to return if the key is not found
  724. @param a_pHasMultiple Optionally receive notification of if there are
  725. multiple entries for this key.
  726. @return a_nDefault Key was not found in the section
  727. @return other Value of the key
  728. */
  729. long GetLongValue(
  730. const SI_CHAR * a_pSection,
  731. const SI_CHAR * a_pKey,
  732. long a_nDefault = 0,
  733. bool * a_pHasMultiple = NULL
  734. ) const;
  735. /** Retrieve a numeric value for a specific key. If multiple keys are enabled
  736. (see SetMultiKey) then only the first value associated with that key
  737. will be returned, see GetAllValues for getting all values with multikey.
  738. @param a_pSection Section to search
  739. @param a_pKey Key to search for
  740. @param a_nDefault Value to return if the key is not found
  741. @param a_pHasMultiple Optionally receive notification of if there are
  742. multiple entries for this key.
  743. @return a_nDefault Key was not found in the section
  744. @return other Value of the key
  745. */
  746. double GetDoubleValue(
  747. const SI_CHAR * a_pSection,
  748. const SI_CHAR * a_pKey,
  749. double a_nDefault = 0,
  750. bool * a_pHasMultiple = NULL
  751. ) const;
  752. /** Retrieve a boolean value for a specific key. If multiple keys are enabled
  753. (see SetMultiKey) then only the first value associated with that key
  754. will be returned, see GetAllValues for getting all values with multikey.
  755. Strings starting with "t", "y", "on" or "1" are returned as logically true.
  756. Strings starting with "f", "n", "of" or "0" are returned as logically false.
  757. For all other values the default is returned. Character comparisons are
  758. case-insensitive.
  759. @param a_pSection Section to search
  760. @param a_pKey Key to search for
  761. @param a_bDefault Value to return if the key is not found
  762. @param a_pHasMultiple Optionally receive notification of if there are
  763. multiple entries for this key.
  764. @return a_nDefault Key was not found in the section
  765. @return other Value of the key
  766. */
  767. bool GetBoolValue(
  768. const SI_CHAR * a_pSection,
  769. const SI_CHAR * a_pKey,
  770. bool a_bDefault = false,
  771. bool * a_pHasMultiple = NULL
  772. ) const;
  773. /** Add or update a section or value. This will always insert
  774. when multiple keys are enabled.
  775. @param a_pSection Section to add or update
  776. @param a_pKey Key to add or update. Set to NULL to
  777. create an empty section.
  778. @param a_pValue Value to set. Set to NULL to create an
  779. empty section.
  780. @param a_pComment Comment to be associated with the section or the
  781. key. If a_pKey is NULL then it will be associated
  782. with the section, otherwise the key. Note that a
  783. comment may be set ONLY when the section or key is
  784. first created (i.e. when this function returns the
  785. value SI_INSERTED). If you wish to create a section
  786. with a comment then you need to create the section
  787. separately to the key. The comment string must be
  788. in full comment form already (have a comment
  789. character starting every line).
  790. @param a_bForceReplace Should all existing values in a multi-key INI
  791. file be replaced with this entry. This option has
  792. no effect if not using multi-key files. The
  793. difference between Delete/SetValue and SetValue
  794. with a_bForceReplace = true, is that the load
  795. order and comment will be preserved this way.
  796. @return SI_Error See error definitions
  797. @return SI_UPDATED Value was updated
  798. @return SI_INSERTED Value was inserted
  799. */
  800. SI_Error SetValue(
  801. const SI_CHAR * a_pSection,
  802. const SI_CHAR * a_pKey,
  803. const SI_CHAR * a_pValue,
  804. const SI_CHAR * a_pComment = NULL,
  805. bool a_bForceReplace = false
  806. )
  807. {
  808. return AddEntry(a_pSection, a_pKey, a_pValue, a_pComment, a_bForceReplace, true);
  809. }
  810. /** Add or update a numeric value. This will always insert
  811. when multiple keys are enabled.
  812. @param a_pSection Section to add or update
  813. @param a_pKey Key to add or update.
  814. @param a_nValue Value to set.
  815. @param a_pComment Comment to be associated with the key. See the
  816. notes on SetValue() for comments.
  817. @param a_bUseHex By default the value will be written to the file
  818. in decimal format. Set this to true to write it
  819. as hexadecimal.
  820. @param a_bForceReplace Should all existing values in a multi-key INI
  821. file be replaced with this entry. This option has
  822. no effect if not using multi-key files. The
  823. difference between Delete/SetLongValue and
  824. SetLongValue with a_bForceReplace = true, is that
  825. the load order and comment will be preserved this
  826. way.
  827. @return SI_Error See error definitions
  828. @return SI_UPDATED Value was updated
  829. @return SI_INSERTED Value was inserted
  830. */
  831. SI_Error SetLongValue(
  832. const SI_CHAR * a_pSection,
  833. const SI_CHAR * a_pKey,
  834. long a_nValue,
  835. const SI_CHAR * a_pComment = NULL,
  836. bool a_bUseHex = false,
  837. bool a_bForceReplace = false
  838. );
  839. /** Add or update a double value. This will always insert
  840. when multiple keys are enabled.
  841. @param a_pSection Section to add or update
  842. @param a_pKey Key to add or update.
  843. @param a_nValue Value to set.
  844. @param a_pComment Comment to be associated with the key. See the
  845. notes on SetValue() for comments.
  846. @param a_bForceReplace Should all existing values in a multi-key INI
  847. file be replaced with this entry. This option has
  848. no effect if not using multi-key files. The
  849. difference between Delete/SetDoubleValue and
  850. SetDoubleValue with a_bForceReplace = true, is that
  851. the load order and comment will be preserved this
  852. way.
  853. @return SI_Error See error definitions
  854. @return SI_UPDATED Value was updated
  855. @return SI_INSERTED Value was inserted
  856. */
  857. SI_Error SetDoubleValue(
  858. const SI_CHAR * a_pSection,
  859. const SI_CHAR * a_pKey,
  860. double a_nValue,
  861. const SI_CHAR * a_pComment = NULL,
  862. bool a_bForceReplace = false
  863. );
  864. /** Add or update a boolean value. This will always insert
  865. when multiple keys are enabled.
  866. @param a_pSection Section to add or update
  867. @param a_pKey Key to add or update.
  868. @param a_bValue Value to set.
  869. @param a_pComment Comment to be associated with the key. See the
  870. notes on SetValue() for comments.
  871. @param a_bForceReplace Should all existing values in a multi-key INI
  872. file be replaced with this entry. This option has
  873. no effect if not using multi-key files. The
  874. difference between Delete/SetBoolValue and
  875. SetBoolValue with a_bForceReplace = true, is that
  876. the load order and comment will be preserved this
  877. way.
  878. @return SI_Error See error definitions
  879. @return SI_UPDATED Value was updated
  880. @return SI_INSERTED Value was inserted
  881. */
  882. SI_Error SetBoolValue(
  883. const SI_CHAR * a_pSection,
  884. const SI_CHAR * a_pKey,
  885. bool a_bValue,
  886. const SI_CHAR * a_pComment = NULL,
  887. bool a_bForceReplace = false
  888. );
  889. /** Delete an entire section, or a key from a section. Note that the
  890. data returned by GetSection is invalid and must not be used after
  891. anything has been deleted from that section using this method.
  892. Note when multiple keys is enabled, this will delete all keys with
  893. that name; to selectively delete individual key/values, use
  894. DeleteValue.
  895. @param a_pSection Section to delete key from, or if
  896. a_pKey is NULL, the section to remove.
  897. @param a_pKey Key to remove from the section. Set to
  898. NULL to remove the entire section.
  899. @param a_bRemoveEmpty If the section is empty after this key has
  900. been deleted, should the empty section be
  901. removed?
  902. @return true Key or section was deleted.
  903. @return false Key or section was not found.
  904. */
  905. bool Delete(
  906. const SI_CHAR * a_pSection,
  907. const SI_CHAR * a_pKey,
  908. bool a_bRemoveEmpty = false
  909. );
  910. /** Delete an entire section, or a key from a section. If value is
  911. provided, only remove keys with the value. Note that the data
  912. returned by GetSection is invalid and must not be used after
  913. anything has been deleted from that section using this method.
  914. Note when multiple keys is enabled, all keys with the value will
  915. be deleted.
  916. @param a_pSection Section to delete key from, or if
  917. a_pKey is NULL, the section to remove.
  918. @param a_pKey Key to remove from the section. Set to
  919. NULL to remove the entire section.
  920. @param a_pValue Value of key to remove from the section.
  921. Set to NULL to remove all keys.
  922. @param a_bRemoveEmpty If the section is empty after this key has
  923. been deleted, should the empty section be
  924. removed?
  925. @return true Key/value or section was deleted.
  926. @return false Key/value or section was not found.
  927. */
  928. bool DeleteValue(
  929. const SI_CHAR * a_pSection,
  930. const SI_CHAR * a_pKey,
  931. const SI_CHAR * a_pValue,
  932. bool a_bRemoveEmpty = false
  933. );
  934. /*-----------------------------------------------------------------------*/
  935. /** @}
  936. @{ @name Converter */
  937. /** Return a conversion object to convert text to the same encoding
  938. as is used by the Save(), SaveFile() and SaveString() functions.
  939. Use this to prepare the strings that you wish to append or prepend
  940. to the output INI data.
  941. */
  942. Converter GetConverter() const {
  943. return Converter(m_bStoreIsUtf8);
  944. }
  945. /*-----------------------------------------------------------------------*/
  946. /** @} */
  947. private:
  948. // copying is not permitted
  949. CSimpleIniTempl(const CSimpleIniTempl &); // disabled
  950. CSimpleIniTempl & operator=(const CSimpleIniTempl &); // disabled
  951. /** Parse the data looking for a file comment and store it if found.
  952. */
  953. SI_Error FindFileComment(
  954. SI_CHAR *& a_pData,
  955. bool a_bCopyStrings
  956. );
  957. /** Parse the data looking for the next valid entry. The memory pointed to
  958. by a_pData is modified by inserting NULL characters. The pointer is
  959. updated to the current location in the block of text.
  960. */
  961. bool FindEntry(
  962. SI_CHAR *& a_pData,
  963. const SI_CHAR *& a_pSection,
  964. const SI_CHAR *& a_pKey,
  965. const SI_CHAR *& a_pVal,
  966. const SI_CHAR *& a_pComment
  967. ) const;
  968. /** Add the section/key/value to our data.
  969. @param a_pSection Section name. Sections will be created if they
  970. don't already exist.
  971. @param a_pKey Key name. May be NULL to create an empty section.
  972. Existing entries will be updated. New entries will
  973. be created.
  974. @param a_pValue Value for the key.
  975. @param a_pComment Comment to be associated with the section or the
  976. key. If a_pKey is NULL then it will be associated
  977. with the section, otherwise the key. This must be
  978. a string in full comment form already (have a
  979. comment character starting every line).
  980. @param a_bForceReplace Should all existing values in a multi-key INI
  981. file be replaced with this entry. This option has
  982. no effect if not using multi-key files. The
  983. difference between Delete/AddEntry and AddEntry
  984. with a_bForceReplace = true, is that the load
  985. order and comment will be preserved this way.
  986. @param a_bCopyStrings Should copies of the strings be made or not.
  987. If false then the pointers will be used as is.
  988. */
  989. SI_Error AddEntry(
  990. const SI_CHAR * a_pSection,
  991. const SI_CHAR * a_pKey,
  992. const SI_CHAR * a_pValue,
  993. const SI_CHAR * a_pComment,
  994. bool a_bForceReplace,
  995. bool a_bCopyStrings
  996. );
  997. /** Is the supplied character a whitespace character? */
  998. inline bool IsSpace(SI_CHAR ch) const {
  999. return (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n');
  1000. }
  1001. /** Does the supplied character start a comment line? */
  1002. inline bool IsComment(SI_CHAR ch) const {
  1003. return (ch == ';' || ch == '#');
  1004. }
  1005. /** Skip over a newline character (or characters) for either DOS or UNIX */
  1006. inline void SkipNewLine(SI_CHAR *& a_pData) const {
  1007. a_pData += (*a_pData == '\r' && *(a_pData+1) == '\n') ? 2 : 1;
  1008. }
  1009. /** Make a copy of the supplied string, replacing the original pointer */
  1010. SI_Error CopyString(const SI_CHAR *& a_pString);
  1011. /** Delete a string from the copied strings buffer if necessary */
  1012. void DeleteString(const SI_CHAR * a_pString);
  1013. /** Internal use of our string comparison function */
  1014. bool IsLess(const SI_CHAR * a_pLeft, const SI_CHAR * a_pRight) const {
  1015. const static SI_STRLESS isLess = SI_STRLESS();
  1016. return isLess(a_pLeft, a_pRight);
  1017. }
  1018. bool IsMultiLineTag(const SI_CHAR * a_pData) const;
  1019. bool IsMultiLineData(const SI_CHAR * a_pData) const;
  1020. bool LoadMultiLineText(
  1021. SI_CHAR *& a_pData,
  1022. const SI_CHAR *& a_pVal,
  1023. const SI_CHAR * a_pTagName,
  1024. bool a_bAllowBlankLinesInComment = false
  1025. ) const;
  1026. bool IsNewLineChar(SI_CHAR a_c) const;
  1027. bool OutputMultiLineText(
  1028. OutputWriter & a_oOutput,
  1029. Converter & a_oConverter,
  1030. const SI_CHAR * a_pText
  1031. ) const;
  1032. private:
  1033. /** Copy of the INI file data in our character format. This will be
  1034. modified when parsed to have NULL characters added after all
  1035. interesting string entries. All of the string pointers to sections,
  1036. keys and values point into this block of memory.
  1037. */
  1038. SI_CHAR * m_pData;
  1039. /** Length of the data that we have stored. Used when deleting strings
  1040. to determine if the string is stored here or in the allocated string
  1041. buffer.
  1042. */
  1043. size_t m_uDataLen;
  1044. /** File comment for this data, if one exists. */
  1045. const SI_CHAR * m_pFileComment;
  1046. /** Parsed INI data. Section -> (Key -> Value). */
  1047. TSection m_data;
  1048. /** This vector stores allocated memory for copies of strings that have
  1049. been supplied after the file load. It will be empty unless SetValue()
  1050. has been called.
  1051. */
  1052. TNamesDepend m_strings;
  1053. /** Is the format of our datafile UTF-8 or MBCS? */
  1054. bool m_bStoreIsUtf8;
  1055. /** Are multiple values permitted for the same key? */
  1056. bool m_bAllowMultiKey;
  1057. /** Are data values permitted to span multiple lines? */
  1058. bool m_bAllowMultiLine;
  1059. /** Should spaces be written out surrounding the equals sign? */
  1060. bool m_bSpaces;
  1061. /** Next order value, used to ensure sections and keys are output in the
  1062. same order that they are loaded/added.
  1063. */
  1064. int m_nOrder;
  1065. };
  1066. // ---------------------------------------------------------------------------
  1067. // IMPLEMENTATION
  1068. // ---------------------------------------------------------------------------
  1069. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1070. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::CSimpleIniTempl(
  1071. bool a_bIsUtf8,
  1072. bool a_bAllowMultiKey,
  1073. bool a_bAllowMultiLine
  1074. )
  1075. : m_pData(0)
  1076. , m_uDataLen(0)
  1077. , m_pFileComment(NULL)
  1078. , m_bStoreIsUtf8(a_bIsUtf8)
  1079. , m_bAllowMultiKey(a_bAllowMultiKey)
  1080. , m_bAllowMultiLine(a_bAllowMultiLine)
  1081. , m_bSpaces(true)
  1082. , m_nOrder(0)
  1083. { }
  1084. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1085. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::~CSimpleIniTempl()
  1086. {
  1087. Reset();
  1088. }
  1089. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1090. void
  1091. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::Reset()
  1092. {
  1093. // remove all data
  1094. delete[] m_pData;
  1095. m_pData = NULL;
  1096. m_uDataLen = 0;
  1097. m_pFileComment = NULL;
  1098. if (!m_data.empty()) {
  1099. m_data.erase(m_data.begin(), m_data.end());
  1100. }
  1101. // remove all strings
  1102. if (!m_strings.empty()) {
  1103. typename TNamesDepend::iterator i = m_strings.begin();
  1104. for (; i != m_strings.end(); ++i) {
  1105. delete[] const_cast<SI_CHAR*>(i->pItem);
  1106. }
  1107. m_strings.erase(m_strings.begin(), m_strings.end());
  1108. }
  1109. }
  1110. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1111. SI_Error
  1112. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::LoadFile(
  1113. const char * a_pszFile
  1114. )
  1115. {
  1116. FILE * fp = NULL;
  1117. #if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE
  1118. fopen_s(&fp, a_pszFile, "rb");
  1119. #else // !__STDC_WANT_SECURE_LIB__
  1120. fp = fopen(a_pszFile, "rb");
  1121. #endif // __STDC_WANT_SECURE_LIB__
  1122. if (!fp) {
  1123. return SI_FILE;
  1124. }
  1125. SI_Error rc = LoadFile(fp);
  1126. fclose(fp);
  1127. return rc;
  1128. }
  1129. #ifdef SI_HAS_WIDE_FILE
  1130. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1131. SI_Error
  1132. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::LoadFile(
  1133. const SI_WCHAR_T * a_pwszFile
  1134. )
  1135. {
  1136. #ifdef _WIN32
  1137. FILE * fp = NULL;
  1138. #if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE
  1139. _wfopen_s(&fp, a_pwszFile, L"rb");
  1140. #else // !__STDC_WANT_SECURE_LIB__
  1141. fp = _wfopen(a_pwszFile, L"rb");
  1142. #endif // __STDC_WANT_SECURE_LIB__
  1143. if (!fp) return SI_FILE;
  1144. SI_Error rc = LoadFile(fp);
  1145. fclose(fp);
  1146. return rc;
  1147. #else // !_WIN32 (therefore SI_CONVERT_ICU)
  1148. char szFile[256];
  1149. u_austrncpy(szFile, a_pwszFile, sizeof(szFile));
  1150. return LoadFile(szFile);
  1151. #endif // _WIN32
  1152. }
  1153. #endif // SI_HAS_WIDE_FILE
  1154. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1155. SI_Error
  1156. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::LoadFile(
  1157. FILE * a_fpFile
  1158. )
  1159. {
  1160. // load the raw file data
  1161. int retval = fseek(a_fpFile, 0, SEEK_END);
  1162. if (retval != 0) {
  1163. return SI_FILE;
  1164. }
  1165. long lSize = ftell(a_fpFile);
  1166. if (lSize < 0) {
  1167. return SI_FILE;
  1168. }
  1169. if (lSize == 0) {
  1170. return SI_OK;
  1171. }
  1172. // allocate and ensure NULL terminated
  1173. char * pData = new(std::nothrow) char[lSize+1];
  1174. if (!pData) {
  1175. return SI_NOMEM;
  1176. }
  1177. pData[lSize] = 0;
  1178. // load data into buffer
  1179. fseek(a_fpFile, 0, SEEK_SET);
  1180. size_t uRead = fread(pData, sizeof(char), lSize, a_fpFile);
  1181. if (uRead != (size_t) lSize) {
  1182. delete[] pData;
  1183. return SI_FILE;
  1184. }
  1185. // convert the raw data to unicode
  1186. SI_Error rc = LoadData(pData, uRead);
  1187. delete[] pData;
  1188. return rc;
  1189. }
  1190. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1191. SI_Error
  1192. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::LoadData(
  1193. const char * a_pData,
  1194. size_t a_uDataLen
  1195. )
  1196. {
  1197. if (!a_pData) {
  1198. return SI_OK;
  1199. }
  1200. // if the UTF-8 BOM exists, consume it and set mode to unicode, if we have
  1201. // already loaded data and try to change mode half-way through then this will
  1202. // be ignored and we will assert in debug versions
  1203. if (a_uDataLen >= 3 && memcmp(a_pData, SI_UTF8_SIGNATURE, 3) == 0) {
  1204. a_pData += 3;
  1205. a_uDataLen -= 3;
  1206. SI_ASSERT(m_bStoreIsUtf8 || !m_pData); // we don't expect mixed mode data
  1207. SetUnicode();
  1208. }
  1209. if (a_uDataLen == 0) {
  1210. return SI_OK;
  1211. }
  1212. // determine the length of the converted data
  1213. SI_CONVERTER converter(m_bStoreIsUtf8);
  1214. size_t uLen = converter.SizeFromStore(a_pData, a_uDataLen);
  1215. if (uLen == (size_t)(-1)) {
  1216. return SI_FAIL;
  1217. }
  1218. // allocate memory for the data, ensure that there is a NULL
  1219. // terminator wherever the converted data ends
  1220. SI_CHAR * pData = new(std::nothrow) SI_CHAR[uLen+1];
  1221. if (!pData) {
  1222. return SI_NOMEM;
  1223. }
  1224. memset(pData, 0, sizeof(SI_CHAR)*(uLen+1));
  1225. // convert the data
  1226. if (!converter.ConvertFromStore(a_pData, a_uDataLen, pData, uLen)) {
  1227. delete[] pData;
  1228. return SI_FAIL;
  1229. }
  1230. // parse it
  1231. const static SI_CHAR empty = 0;
  1232. SI_CHAR * pWork = pData;
  1233. const SI_CHAR * pSection = &empty;
  1234. const SI_CHAR * pItem = NULL;
  1235. const SI_CHAR * pVal = NULL;
  1236. const SI_CHAR * pComment = NULL;
  1237. // We copy the strings if we are loading data into this class when we
  1238. // already have stored some.
  1239. bool bCopyStrings = (m_pData != NULL);
  1240. // find a file comment if it exists, this is a comment that starts at the
  1241. // beginning of the file and continues until the first blank line.
  1242. SI_Error rc = FindFileComment(pWork, bCopyStrings);
  1243. if (rc < 0) return rc;
  1244. // add every entry in the file to the data table
  1245. while (FindEntry(pWork, pSection, pItem, pVal, pComment)) {
  1246. rc = AddEntry(pSection, pItem, pVal, pComment, false, bCopyStrings);
  1247. if (rc < 0) return rc;
  1248. }
  1249. // store these strings if we didn't copy them
  1250. if (bCopyStrings) {
  1251. delete[] pData;
  1252. }
  1253. else {
  1254. m_pData = pData;
  1255. m_uDataLen = uLen+1;
  1256. }
  1257. return SI_OK;
  1258. }
  1259. #ifdef SI_SUPPORT_IOSTREAMS
  1260. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1261. SI_Error
  1262. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::LoadData(
  1263. std::istream & a_istream
  1264. )
  1265. {
  1266. std::string strData;
  1267. char szBuf[512];
  1268. do {
  1269. a_istream.get(szBuf, sizeof(szBuf), '\0');
  1270. strData.append(szBuf);
  1271. }
  1272. while (a_istream.good());
  1273. return LoadData(strData);
  1274. }
  1275. #endif // SI_SUPPORT_IOSTREAMS
  1276. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1277. SI_Error
  1278. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::FindFileComment(
  1279. SI_CHAR *& a_pData,
  1280. bool a_bCopyStrings
  1281. )
  1282. {
  1283. // there can only be a single file comment
  1284. if (m_pFileComment) {
  1285. return SI_OK;
  1286. }
  1287. // Load the file comment as multi-line text, this will modify all of
  1288. // the newline characters to be single \n chars
  1289. if (!LoadMultiLineText(a_pData, m_pFileComment, NULL, false)) {
  1290. return SI_OK;
  1291. }
  1292. // copy the string if necessary
  1293. if (a_bCopyStrings) {
  1294. SI_Error rc = CopyString(m_pFileComment);
  1295. if (rc < 0) return rc;
  1296. }
  1297. return SI_OK;
  1298. }
  1299. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1300. bool
  1301. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::FindEntry(
  1302. SI_CHAR *& a_pData,
  1303. const SI_CHAR *& a_pSection,
  1304. const SI_CHAR *& a_pKey,
  1305. const SI_CHAR *& a_pVal,
  1306. const SI_CHAR *& a_pComment
  1307. ) const
  1308. {
  1309. a_pComment = NULL;
  1310. SI_CHAR * pTrail = NULL;
  1311. while (*a_pData) {
  1312. // skip spaces and empty lines
  1313. while (*a_pData && IsSpace(*a_pData)) {
  1314. ++a_pData;
  1315. }
  1316. if (!*a_pData) {
  1317. break;
  1318. }
  1319. // skip processing of comment lines but keep a pointer to
  1320. // the start of the comment.
  1321. if (IsComment(*a_pData)) {
  1322. LoadMultiLineText(a_pData, a_pComment, NULL, true);
  1323. continue;
  1324. }
  1325. // process section names
  1326. if (*a_pData == '[') {
  1327. // skip leading spaces
  1328. ++a_pData;
  1329. while (*a_pData && IsSpace(*a_pData)) {
  1330. ++a_pData;
  1331. }
  1332. // find the end of the section name (it may contain spaces)
  1333. // and convert it to lowercase as necessary
  1334. a_pSection = a_pData;
  1335. while (*a_pData && *a_pData != ']' && !IsNewLineChar(*a_pData)) {
  1336. ++a_pData;
  1337. }
  1338. // if it's an invalid line, just skip it
  1339. if (*a_pData != ']') {
  1340. continue;
  1341. }
  1342. // remove trailing spaces from the section
  1343. pTrail = a_pData - 1;
  1344. while (pTrail >= a_pSection && IsSpace(*pTrail)) {
  1345. --pTrail;
  1346. }
  1347. ++pTrail;
  1348. *pTrail = 0;
  1349. // skip to the end of the line
  1350. ++a_pData; // safe as checked that it == ']' above
  1351. while (*a_pData && !IsNewLineChar(*a_pData)) {
  1352. ++a_pData;
  1353. }
  1354. a_pKey = NULL;
  1355. a_pVal = NULL;
  1356. return true;
  1357. }
  1358. // find the end of the key name (it may contain spaces)
  1359. // and convert it to lowercase as necessary
  1360. a_pKey = a_pData;
  1361. while (*a_pData && *a_pData != '=' && !IsNewLineChar(*a_pData)) {
  1362. ++a_pData;
  1363. }
  1364. // if it's an invalid line, just skip it
  1365. if (*a_pData != '=') {
  1366. continue;
  1367. }
  1368. // empty keys are invalid
  1369. if (a_pKey == a_pData) {
  1370. while (*a_pData && !IsNewLineChar(*a_pData)) {
  1371. ++a_pData;
  1372. }
  1373. continue;
  1374. }
  1375. // remove trailing spaces from the key
  1376. pTrail = a_pData - 1;
  1377. while (pTrail >= a_pKey && IsSpace(*pTrail)) {
  1378. --pTrail;
  1379. }
  1380. ++pTrail;
  1381. *pTrail = 0;
  1382. // skip leading whitespace on the value
  1383. ++a_pData; // safe as checked that it == '=' above
  1384. while (*a_pData && !IsNewLineChar(*a_pData) && IsSpace(*a_pData)) {
  1385. ++a_pData;
  1386. }
  1387. // find the end of the value which is the end of this line
  1388. a_pVal = a_pData;
  1389. while (*a_pData && !IsNewLineChar(*a_pData)) {
  1390. ++a_pData;
  1391. }
  1392. // remove trailing spaces from the value
  1393. pTrail = a_pData - 1;
  1394. if (*a_pData) { // prepare for the next round
  1395. SkipNewLine(a_pData);
  1396. }
  1397. while (pTrail >= a_pVal && IsSpace(*pTrail)) {
  1398. --pTrail;
  1399. }
  1400. ++pTrail;
  1401. *pTrail = 0;
  1402. // check for multi-line entries
  1403. if (m_bAllowMultiLine && IsMultiLineTag(a_pVal)) {
  1404. // skip the "<<<" to get the tag that will end the multiline
  1405. const SI_CHAR * pTagName = a_pVal + 3;
  1406. return LoadMultiLineText(a_pData, a_pVal, pTagName);
  1407. }
  1408. // return the standard entry
  1409. return true;
  1410. }
  1411. return false;
  1412. }
  1413. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1414. bool
  1415. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::IsMultiLineTag(
  1416. const SI_CHAR * a_pVal
  1417. ) const
  1418. {
  1419. // check for the "<<<" prefix for a multi-line entry
  1420. if (*a_pVal++ != '<') return false;
  1421. if (*a_pVal++ != '<') return false;
  1422. if (*a_pVal++ != '<') return false;
  1423. return true;
  1424. }
  1425. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1426. bool
  1427. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::IsMultiLineData(
  1428. const SI_CHAR * a_pData
  1429. ) const
  1430. {
  1431. // data is multi-line if it has any of the following features:
  1432. // * whitespace prefix
  1433. // * embedded newlines
  1434. // * whitespace suffix
  1435. // empty string
  1436. if (!*a_pData) {
  1437. return false;
  1438. }
  1439. // check for prefix
  1440. if (IsSpace(*a_pData)) {
  1441. return true;
  1442. }
  1443. // embedded newlines
  1444. while (*a_pData) {
  1445. if (IsNewLineChar(*a_pData)) {
  1446. return true;
  1447. }
  1448. ++a_pData;
  1449. }
  1450. // check for suffix
  1451. if (IsSpace(*--a_pData)) {
  1452. return true;
  1453. }
  1454. return false;
  1455. }
  1456. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1457. bool
  1458. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::IsNewLineChar(
  1459. SI_CHAR a_c
  1460. ) const
  1461. {
  1462. return (a_c == '\n' || a_c == '\r');
  1463. }
  1464. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1465. bool
  1466. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::LoadMultiLineText(
  1467. SI_CHAR *& a_pData,
  1468. const SI_CHAR *& a_pVal,
  1469. const SI_CHAR * a_pTagName,
  1470. bool a_bAllowBlankLinesInComment
  1471. ) const
  1472. {
  1473. // we modify this data to strip all newlines down to a single '\n'
  1474. // character. This means that on Windows we need to strip out some
  1475. // characters which will make the data shorter.
  1476. // i.e. LINE1-LINE1\r\nLINE2-LINE2\0 will become
  1477. // LINE1-LINE1\nLINE2-LINE2\0
  1478. // The pDataLine entry is the pointer to the location in memory that
  1479. // the current line needs to start to run following the existing one.
  1480. // This may be the same as pCurrLine in which case no move is needed.
  1481. SI_CHAR * pDataLine = a_pData;
  1482. SI_CHAR * pCurrLine;
  1483. // value starts at the current line
  1484. a_pVal = a_pData;
  1485. // find the end tag. This tag must start in column 1 and be
  1486. // followed by a newline. We ignore any whitespace after the end
  1487. // tag but not whitespace before it.
  1488. SI_CHAR cEndOfLineChar = *a_pData;
  1489. for(;;) {
  1490. // if we are loading comments then we need a comment character as
  1491. // the first character on every line
  1492. if (!a_pTagName && !IsComment(*a_pData)) {
  1493. // if we aren't allowing blank lines then we're done
  1494. if (!a_bAllowBlankLinesInComment) {
  1495. break;
  1496. }
  1497. // if we are allowing blank lines then we only include them
  1498. // in this comment if another comment follows, so read ahead
  1499. // to find out.
  1500. SI_CHAR * pCurr = a_pData;
  1501. int nNewLines = 0;
  1502. while (IsSpace(*pCurr)) {
  1503. if (IsNewLineChar(*pCurr)) {
  1504. ++nNewLines;
  1505. SkipNewLine(pCurr);
  1506. }
  1507. else {
  1508. ++pCurr;
  1509. }
  1510. }
  1511. // we have a comment, add the blank lines to the output
  1512. // and continue processing from here
  1513. if (IsComment(*pCurr)) {
  1514. for (; nNewLines > 0; --nNewLines) *pDataLine++ = '\n';
  1515. a_pData = pCurr;
  1516. continue;
  1517. }
  1518. // the comment ends here
  1519. break;
  1520. }
  1521. // find the end of this line
  1522. pCurrLine = a_pData;
  1523. while (*a_pData && !IsNewLineChar(*a_pData)) ++a_pData;
  1524. // move this line down to the location that it should be if necessary
  1525. if (pDataLine < pCurrLine) {
  1526. size_t nLen = (size_t) (a_pData - pCurrLine);
  1527. memmove(pDataLine, pCurrLine, nLen * sizeof(SI_CHAR));
  1528. pDataLine[nLen] = '\0';
  1529. }
  1530. // end the line with a NULL
  1531. cEndOfLineChar = *a_pData;
  1532. *a_pData = 0;
  1533. // if are looking for a tag then do the check now. This is done before
  1534. // checking for end of the data, so that if we have the tag at the end
  1535. // of the data then the tag is removed correctly.
  1536. if (a_pTagName) {
  1537. // strip whitespace from the end of this tag
  1538. SI_CHAR* pc = a_pData - 1;
  1539. while (pc > pDataLine && IsSpace(*pc)) --pc;
  1540. SI_CHAR ch = *++pc;
  1541. *pc = 0;
  1542. if (!IsLess(pDataLine, a_pTagName) && !IsLess(a_pTagName, pDataLine)) {
  1543. break;
  1544. }
  1545. *pc = ch;
  1546. }
  1547. // if we are at the end of the data then we just automatically end
  1548. // this entry and return the current data.
  1549. if (!cEndOfLineChar) {
  1550. return true;
  1551. }
  1552. // otherwise we need to process this newline to ensure that it consists
  1553. // of just a single \n character.
  1554. pDataLine += (a_pData - pCurrLine);
  1555. *a_pData = cEndOfLineChar;
  1556. SkipNewLine(a_pData);
  1557. *pDataLine++ = '\n';
  1558. }
  1559. // if we didn't find a comment at all then return false
  1560. if (a_pVal == a_pData) {
  1561. a_pVal = NULL;
  1562. return false;
  1563. }
  1564. // the data (which ends at the end of the last line) needs to be
  1565. // null-terminated BEFORE before the newline character(s). If the
  1566. // user wants a new line in the multi-line data then they need to
  1567. // add an empty line before the tag.
  1568. *--pDataLine = '\0';
  1569. // if looking for a tag and if we aren't at the end of the data,
  1570. // then move a_pData to the start of the next line.
  1571. if (a_pTagName && cEndOfLineChar) {
  1572. SI_ASSERT(IsNewLineChar(cEndOfLineChar));
  1573. *a_pData = cEndOfLineChar;
  1574. SkipNewLine(a_pData);
  1575. }
  1576. return true;
  1577. }
  1578. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1579. SI_Error
  1580. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::CopyString(
  1581. const SI_CHAR *& a_pString
  1582. )
  1583. {
  1584. size_t uLen = 0;
  1585. if (sizeof(SI_CHAR) == sizeof(char)) {
  1586. uLen = strlen((const char *)a_pString);
  1587. }
  1588. else if (sizeof(SI_CHAR) == sizeof(wchar_t)) {
  1589. uLen = wcslen((const wchar_t *)a_pString);
  1590. }
  1591. else {
  1592. for ( ; a_pString[uLen]; ++uLen) /*loop*/ ;
  1593. }
  1594. ++uLen; // NULL character
  1595. SI_CHAR * pCopy = new(std::nothrow) SI_CHAR[uLen];
  1596. if (!pCopy) {
  1597. return SI_NOMEM;
  1598. }
  1599. memcpy(pCopy, a_pString, sizeof(SI_CHAR)*uLen);
  1600. m_strings.push_back(pCopy);
  1601. a_pString = pCopy;
  1602. return SI_OK;
  1603. }
  1604. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1605. SI_Error
  1606. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::AddEntry(
  1607. const SI_CHAR * a_pSection,
  1608. const SI_CHAR * a_pKey,
  1609. const SI_CHAR * a_pValue,
  1610. const SI_CHAR * a_pComment,
  1611. bool a_bForceReplace,
  1612. bool a_bCopyStrings
  1613. )
  1614. {
  1615. SI_Error rc;
  1616. bool bInserted = false;
  1617. SI_ASSERT(!a_pComment || IsComment(*a_pComment));
  1618. // if we are copying strings then make a copy of the comment now
  1619. // because we will need it when we add the entry.
  1620. if (a_bCopyStrings && a_pComment) {
  1621. rc = CopyString(a_pComment);
  1622. if (rc < 0) return rc;
  1623. }
  1624. // create the section entry if necessary
  1625. typename TSection::iterator iSection = m_data.find(a_pSection);
  1626. if (iSection == m_data.end()) {
  1627. // if the section doesn't exist then we need a copy as the
  1628. // string needs to last beyond the end of this function
  1629. if (a_bCopyStrings) {
  1630. rc = CopyString(a_pSection);
  1631. if (rc < 0) return rc;
  1632. }
  1633. // only set the comment if this is a section only entry
  1634. Entry oSection(a_pSection, ++m_nOrder);
  1635. if (a_pComment && (!a_pKey || !a_pValue)) {
  1636. oSection.pComment = a_pComment;
  1637. }
  1638. typename TSection::value_type oEntry(oSection, TKeyVal());
  1639. typedef typename TSection::iterator SectionIterator;
  1640. std::pair<SectionIterator,bool> i = m_data.insert(oEntry);
  1641. iSection = i.first;
  1642. bInserted = true;
  1643. }
  1644. if (!a_pKey || !a_pValue) {
  1645. // section only entries are specified with pItem and pVal as NULL
  1646. return bInserted ? SI_INSERTED : SI_UPDATED;
  1647. }
  1648. // check for existence of the key
  1649. TKeyVal & keyval = iSection->second;
  1650. typename TKeyVal::iterator iKey = keyval.find(a_pKey);
  1651. bInserted = iKey == keyval.end();
  1652. // remove all existing entries but save the load order and
  1653. // comment of the first entry
  1654. int nLoadOrder = ++m_nOrder;
  1655. if (iKey != keyval.end() && m_bAllowMultiKey && a_bForceReplace) {
  1656. const SI_CHAR * pComment = NULL;
  1657. while (iKey != keyval.end() && !IsLess(a_pKey, iKey->first.pItem)) {
  1658. if (iKey->first.nOrder < nLoadOrder) {
  1659. nLoadOrder = iKey->first.nOrder;
  1660. pComment = iKey->first.pComment;
  1661. }
  1662. ++iKey;
  1663. }
  1664. if (pComment) {
  1665. DeleteString(a_pComment);
  1666. a_pComment = pComment;
  1667. CopyString(a_pComment);
  1668. }
  1669. Delete(a_pSection, a_pKey);
  1670. iKey = keyval.end();
  1671. }
  1672. // make string copies if necessary
  1673. bool bForceCreateNewKey = m_bAllowMultiKey && !a_bForceReplace;
  1674. if (a_bCopyStrings) {
  1675. if (bForceCreateNewKey || iKey == keyval.end()) {
  1676. // if the key doesn't exist then we need a copy as the
  1677. // string needs to last beyond the end of this function
  1678. // because we will be inserting the key next
  1679. rc = CopyString(a_pKey);
  1680. if (rc < 0) return rc;
  1681. }
  1682. // we always need a copy of the value
  1683. rc = CopyString(a_pValue);
  1684. if (rc < 0) return rc;
  1685. }
  1686. // create the key entry
  1687. if (iKey == keyval.end() || bForceCreateNewKey) {
  1688. Entry oKey(a_pKey, nLoadOrder);
  1689. if (a_pComment) {
  1690. oKey.pComment = a_pComment;
  1691. }
  1692. typename TKeyVal::value_type oEntry(oKey, static_cast<const SI_CHAR *>(NULL));
  1693. iKey = keyval.insert(oEntry);
  1694. }
  1695. iKey->second = a_pValue;
  1696. return bInserted ? SI_INSERTED : SI_UPDATED;
  1697. }
  1698. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1699. const SI_CHAR *
  1700. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::GetValue(
  1701. const SI_CHAR * a_pSection,
  1702. const SI_CHAR * a_pKey,
  1703. const SI_CHAR * a_pDefault,
  1704. bool * a_pHasMultiple
  1705. ) const
  1706. {
  1707. if (a_pHasMultiple) {
  1708. *a_pHasMultiple = false;
  1709. }
  1710. if (!a_pSection || !a_pKey) {
  1711. return a_pDefault;
  1712. }
  1713. typename TSection::const_iterator iSection = m_data.find(a_pSection);
  1714. if (iSection == m_data.end()) {
  1715. return a_pDefault;
  1716. }
  1717. typename TKeyVal::const_iterator iKeyVal = iSection->second.find(a_pKey);
  1718. if (iKeyVal == iSection->second.end()) {
  1719. return a_pDefault;
  1720. }
  1721. // check for multiple entries with the same key
  1722. if (m_bAllowMultiKey && a_pHasMultiple) {
  1723. typename TKeyVal::const_iterator iTemp = iKeyVal;
  1724. if (++iTemp != iSection->second.end()) {
  1725. if (!IsLess(a_pKey, iTemp->first.pItem)) {
  1726. *a_pHasMultiple = true;
  1727. }
  1728. }
  1729. }
  1730. return iKeyVal->second;
  1731. }
  1732. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1733. long
  1734. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::GetLongValue(
  1735. const SI_CHAR * a_pSection,
  1736. const SI_CHAR * a_pKey,
  1737. long a_nDefault,
  1738. bool * a_pHasMultiple
  1739. ) const
  1740. {
  1741. // return the default if we don't have a value
  1742. const SI_CHAR * pszValue = GetValue(a_pSection, a_pKey, NULL, a_pHasMultiple);
  1743. if (!pszValue || !*pszValue) return a_nDefault;
  1744. // convert to UTF-8/MBCS which for a numeric value will be the same as ASCII
  1745. char szValue[64] = { 0 };
  1746. SI_CONVERTER c(m_bStoreIsUtf8);
  1747. if (!c.ConvertToStore(pszValue, szValue, sizeof(szValue))) {
  1748. return a_nDefault;
  1749. }
  1750. // handle the value as hex if prefaced with "0x"
  1751. long nValue = a_nDefault;
  1752. char * pszSuffix = szValue;
  1753. if (szValue[0] == '0' && (szValue[1] == 'x' || szValue[1] == 'X')) {
  1754. if (!szValue[2]) return a_nDefault;
  1755. nValue = strtol(&szValue[2], &pszSuffix, 16);
  1756. }
  1757. else {
  1758. nValue = strtol(szValue, &pszSuffix, 10);
  1759. }
  1760. // any invalid strings will return the default value
  1761. if (*pszSuffix) {
  1762. return a_nDefault;
  1763. }
  1764. return nValue;
  1765. }
  1766. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1767. SI_Error
  1768. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::SetLongValue(
  1769. const SI_CHAR * a_pSection,
  1770. const SI_CHAR * a_pKey,
  1771. long a_nValue,
  1772. const SI_CHAR * a_pComment,
  1773. bool a_bUseHex,
  1774. bool a_bForceReplace
  1775. )
  1776. {
  1777. // use SetValue to create sections
  1778. if (!a_pSection || !a_pKey) return SI_FAIL;
  1779. // convert to an ASCII string
  1780. char szInput[64];
  1781. #if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE
  1782. sprintf_s(szInput, a_bUseHex ? "0x%lx" : "%ld", a_nValue);
  1783. #else // !__STDC_WANT_SECURE_LIB__
  1784. sprintf(szInput, a_bUseHex ? "0x%lx" : "%ld", a_nValue);
  1785. #endif // __STDC_WANT_SECURE_LIB__
  1786. // convert to output text
  1787. SI_CHAR szOutput[64];
  1788. SI_CONVERTER c(m_bStoreIsUtf8);
  1789. c.ConvertFromStore(szInput, strlen(szInput) + 1,
  1790. szOutput, sizeof(szOutput) / sizeof(SI_CHAR));
  1791. // actually add it
  1792. return AddEntry(a_pSection, a_pKey, szOutput, a_pComment, a_bForceReplace, true);
  1793. }
  1794. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1795. double
  1796. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::GetDoubleValue(
  1797. const SI_CHAR * a_pSection,
  1798. const SI_CHAR * a_pKey,
  1799. double a_nDefault,
  1800. bool * a_pHasMultiple
  1801. ) const
  1802. {
  1803. // return the default if we don't have a value
  1804. const SI_CHAR * pszValue = GetValue(a_pSection, a_pKey, NULL, a_pHasMultiple);
  1805. if (!pszValue || !*pszValue) return a_nDefault;
  1806. // convert to UTF-8/MBCS which for a numeric value will be the same as ASCII
  1807. char szValue[64] = { 0 };
  1808. SI_CONVERTER c(m_bStoreIsUtf8);
  1809. if (!c.ConvertToStore(pszValue, szValue, sizeof(szValue))) {
  1810. return a_nDefault;
  1811. }
  1812. char * pszSuffix = NULL;
  1813. double nValue = strtod(szValue, &pszSuffix);
  1814. // any invalid strings will return the default value
  1815. if (!pszSuffix || *pszSuffix) {
  1816. return a_nDefault;
  1817. }
  1818. return nValue;
  1819. }
  1820. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1821. SI_Error
  1822. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::SetDoubleValue(
  1823. const SI_CHAR * a_pSection,
  1824. const SI_CHAR * a_pKey,
  1825. double a_nValue,
  1826. const SI_CHAR * a_pComment,
  1827. bool a_bForceReplace
  1828. )
  1829. {
  1830. // use SetValue to create sections
  1831. if (!a_pSection || !a_pKey) return SI_FAIL;
  1832. // convert to an ASCII string
  1833. char szInput[64];
  1834. #if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE
  1835. sprintf_s(szInput, "%f", a_nValue);
  1836. #else // !__STDC_WANT_SECURE_LIB__
  1837. sprintf(szInput, "%f", a_nValue);
  1838. #endif // __STDC_WANT_SECURE_LIB__
  1839. // convert to output text
  1840. SI_CHAR szOutput[64];
  1841. SI_CONVERTER c(m_bStoreIsUtf8);
  1842. c.ConvertFromStore(szInput, strlen(szInput) + 1,
  1843. szOutput, sizeof(szOutput) / sizeof(SI_CHAR));
  1844. // actually add it
  1845. return AddEntry(a_pSection, a_pKey, szOutput, a_pComment, a_bForceReplace, true);
  1846. }
  1847. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1848. bool
  1849. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::GetBoolValue(
  1850. const SI_CHAR * a_pSection,
  1851. const SI_CHAR * a_pKey,
  1852. bool a_bDefault,
  1853. bool * a_pHasMultiple
  1854. ) const
  1855. {
  1856. // return the default if we don't have a value
  1857. const SI_CHAR * pszValue = GetValue(a_pSection, a_pKey, NULL, a_pHasMultiple);
  1858. if (!pszValue || !*pszValue) return a_bDefault;
  1859. // we only look at the minimum number of characters
  1860. switch (pszValue[0]) {
  1861. case 't': case 'T': // true
  1862. case 'y': case 'Y': // yes
  1863. case '1': // 1 (one)
  1864. return true;
  1865. case 'f': case 'F': // false
  1866. case 'n': case 'N': // no
  1867. case '0': // 0 (zero)
  1868. return false;
  1869. case 'o': case 'O':
  1870. if (pszValue[1] == 'n' || pszValue[1] == 'N') return true; // on
  1871. if (pszValue[1] == 'f' || pszValue[1] == 'F') return false; // off
  1872. break;
  1873. }
  1874. // no recognized value, return the default
  1875. return a_bDefault;
  1876. }
  1877. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1878. SI_Error
  1879. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::SetBoolValue(
  1880. const SI_CHAR * a_pSection,
  1881. const SI_CHAR * a_pKey,
  1882. bool a_bValue,
  1883. const SI_CHAR * a_pComment,
  1884. bool a_bForceReplace
  1885. )
  1886. {
  1887. // use SetValue to create sections
  1888. if (!a_pSection || !a_pKey) return SI_FAIL;
  1889. // convert to an ASCII string
  1890. const char * pszInput = a_bValue ? "true" : "false";
  1891. // convert to output text
  1892. SI_CHAR szOutput[64];
  1893. SI_CONVERTER c(m_bStoreIsUtf8);
  1894. c.ConvertFromStore(pszInput, strlen(pszInput) + 1,
  1895. szOutput, sizeof(szOutput) / sizeof(SI_CHAR));
  1896. // actually add it
  1897. return AddEntry(a_pSection, a_pKey, szOutput, a_pComment, a_bForceReplace, true);
  1898. }
  1899. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1900. bool
  1901. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::GetAllValues(
  1902. const SI_CHAR * a_pSection,
  1903. const SI_CHAR * a_pKey,
  1904. TNamesDepend & a_values
  1905. ) const
  1906. {
  1907. a_values.clear();
  1908. if (!a_pSection || !a_pKey) {
  1909. return false;
  1910. }
  1911. typename TSection::const_iterator iSection = m_data.find(a_pSection);
  1912. if (iSection == m_data.end()) {
  1913. return false;
  1914. }
  1915. typename TKeyVal::const_iterator iKeyVal = iSection->second.find(a_pKey);
  1916. if (iKeyVal == iSection->second.end()) {
  1917. return false;
  1918. }
  1919. // insert all values for this key
  1920. a_values.push_back(Entry(iKeyVal->second, iKeyVal->first.pComment, iKeyVal->first.nOrder));
  1921. if (m_bAllowMultiKey) {
  1922. ++iKeyVal;
  1923. while (iKeyVal != iSection->second.end() && !IsLess(a_pKey, iKeyVal->first.pItem)) {
  1924. a_values.push_back(Entry(iKeyVal->second, iKeyVal->first.pComment, iKeyVal->first.nOrder));
  1925. ++iKeyVal;
  1926. }
  1927. }
  1928. return true;
  1929. }
  1930. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1931. int
  1932. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::GetSectionSize(
  1933. const SI_CHAR * a_pSection
  1934. ) const
  1935. {
  1936. if (!a_pSection) {
  1937. return -1;
  1938. }
  1939. typename TSection::const_iterator iSection = m_data.find(a_pSection);
  1940. if (iSection == m_data.end()) {
  1941. return -1;
  1942. }
  1943. const TKeyVal & section = iSection->second;
  1944. // if multi-key isn't permitted then the section size is
  1945. // the number of keys that we have.
  1946. if (!m_bAllowMultiKey || section.empty()) {
  1947. return (int) section.size();
  1948. }
  1949. // otherwise we need to count them
  1950. int nCount = 0;
  1951. const SI_CHAR * pLastKey = NULL;
  1952. typename TKeyVal::const_iterator iKeyVal = section.begin();
  1953. for (int n = 0; iKeyVal != section.end(); ++iKeyVal, ++n) {
  1954. if (!pLastKey || IsLess(pLastKey, iKeyVal->first.pItem)) {
  1955. ++nCount;
  1956. pLastKey = iKeyVal->first.pItem;
  1957. }
  1958. }
  1959. return nCount;
  1960. }
  1961. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1962. const typename CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::TKeyVal *
  1963. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::GetSection(
  1964. const SI_CHAR * a_pSection
  1965. ) const
  1966. {
  1967. if (a_pSection) {
  1968. typename TSection::const_iterator i = m_data.find(a_pSection);
  1969. if (i != m_data.end()) {
  1970. return &(i->second);
  1971. }
  1972. }
  1973. return 0;
  1974. }
  1975. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1976. void
  1977. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::GetAllSections(
  1978. TNamesDepend & a_names
  1979. ) const
  1980. {
  1981. a_names.clear();
  1982. typename TSection::const_iterator i = m_data.begin();
  1983. for (int n = 0; i != m_data.end(); ++i, ++n ) {
  1984. a_names.push_back(i->first);
  1985. }
  1986. }
  1987. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  1988. bool
  1989. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::GetAllKeys(
  1990. const SI_CHAR * a_pSection,
  1991. TNamesDepend & a_names
  1992. ) const
  1993. {
  1994. a_names.clear();
  1995. if (!a_pSection) {
  1996. return false;
  1997. }
  1998. typename TSection::const_iterator iSection = m_data.find(a_pSection);
  1999. if (iSection == m_data.end()) {
  2000. return false;
  2001. }
  2002. const TKeyVal & section = iSection->second;
  2003. const SI_CHAR * pLastKey = NULL;
  2004. typename TKeyVal::const_iterator iKeyVal = section.begin();
  2005. for (int n = 0; iKeyVal != section.end(); ++iKeyVal, ++n ) {
  2006. if (!pLastKey || IsLess(pLastKey, iKeyVal->first.pItem)) {
  2007. a_names.push_back(iKeyVal->first);
  2008. pLastKey = iKeyVal->first.pItem;
  2009. }
  2010. }
  2011. return true;
  2012. }
  2013. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  2014. SI_Error
  2015. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::SaveFile(
  2016. const char * a_pszFile,
  2017. bool a_bAddSignature
  2018. ) const
  2019. {
  2020. FILE * fp = NULL;
  2021. #if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE
  2022. fopen_s(&fp, a_pszFile, "wb");
  2023. #else // !__STDC_WANT_SECURE_LIB__
  2024. fp = fopen(a_pszFile, "wb");
  2025. #endif // __STDC_WANT_SECURE_LIB__
  2026. if (!fp) return SI_FILE;
  2027. SI_Error rc = SaveFile(fp, a_bAddSignature);
  2028. fclose(fp);
  2029. return rc;
  2030. }
  2031. #ifdef SI_HAS_WIDE_FILE
  2032. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  2033. SI_Error
  2034. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::SaveFile(
  2035. const SI_WCHAR_T * a_pwszFile,
  2036. bool a_bAddSignature
  2037. ) const
  2038. {
  2039. #ifdef _WIN32
  2040. FILE * fp = NULL;
  2041. #if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE
  2042. _wfopen_s(&fp, a_pwszFile, L"wb");
  2043. #else // !__STDC_WANT_SECURE_LIB__
  2044. fp = _wfopen(a_pwszFile, L"wb");
  2045. #endif // __STDC_WANT_SECURE_LIB__
  2046. if (!fp) return SI_FILE;
  2047. SI_Error rc = SaveFile(fp, a_bAddSignature);
  2048. fclose(fp);
  2049. return rc;
  2050. #else // !_WIN32 (therefore SI_CONVERT_ICU)
  2051. char szFile[256];
  2052. u_austrncpy(szFile, a_pwszFile, sizeof(szFile));
  2053. return SaveFile(szFile, a_bAddSignature);
  2054. #endif // _WIN32
  2055. }
  2056. #endif // SI_HAS_WIDE_FILE
  2057. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  2058. SI_Error
  2059. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::SaveFile(
  2060. FILE * a_pFile,
  2061. bool a_bAddSignature
  2062. ) const
  2063. {
  2064. FileWriter writer(a_pFile);
  2065. return Save(writer, a_bAddSignature);
  2066. }
  2067. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  2068. SI_Error
  2069. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::Save(
  2070. OutputWriter & a_oOutput,
  2071. bool a_bAddSignature
  2072. ) const
  2073. {
  2074. Converter convert(m_bStoreIsUtf8);
  2075. // add the UTF-8 signature if it is desired
  2076. if (m_bStoreIsUtf8 && a_bAddSignature) {
  2077. a_oOutput.Write(SI_UTF8_SIGNATURE);
  2078. }
  2079. // get all of the sections sorted in load order
  2080. TNamesDepend oSections;
  2081. GetAllSections(oSections);
  2082. #if defined(_MSC_VER) && _MSC_VER <= 1200
  2083. oSections.sort();
  2084. #elif defined(__BORLANDC__)
  2085. oSections.sort(Entry::LoadOrder());
  2086. #else
  2087. oSections.sort(typename Entry::LoadOrder());
  2088. #endif
  2089. // if there is an empty section name, then it must be written out first
  2090. // regardless of the load order
  2091. typename TNamesDepend::iterator is = oSections.begin();
  2092. for (; is != oSections.end(); ++is) {
  2093. if (!*is->pItem) {
  2094. // move the empty section name to the front of the section list
  2095. if (is != oSections.begin()) {
  2096. oSections.splice(oSections.begin(), oSections, is, std::next(is));
  2097. }
  2098. break;
  2099. }
  2100. }
  2101. // write the file comment if we have one
  2102. bool bNeedNewLine = false;
  2103. if (m_pFileComment) {
  2104. if (!OutputMultiLineText(a_oOutput, convert, m_pFileComment)) {
  2105. return SI_FAIL;
  2106. }
  2107. bNeedNewLine = true;
  2108. }
  2109. // iterate through our sections and output the data
  2110. typename TNamesDepend::const_iterator iSection = oSections.begin();
  2111. for ( ; iSection != oSections.end(); ++iSection ) {
  2112. // write out the comment if there is one
  2113. if (iSection->pComment) {
  2114. if (bNeedNewLine) {
  2115. a_oOutput.Write(SI_NEWLINE_A);
  2116. a_oOutput.Write(SI_NEWLINE_A);
  2117. }
  2118. if (!OutputMultiLineText(a_oOutput, convert, iSection->pComment)) {
  2119. return SI_FAIL;
  2120. }
  2121. bNeedNewLine = false;
  2122. }
  2123. if (bNeedNewLine) {
  2124. a_oOutput.Write(SI_NEWLINE_A);
  2125. a_oOutput.Write(SI_NEWLINE_A);
  2126. bNeedNewLine = false;
  2127. }
  2128. // write the section (unless there is no section name)
  2129. if (*iSection->pItem) {
  2130. if (!convert.ConvertToStore(iSection->pItem)) {
  2131. return SI_FAIL;
  2132. }
  2133. a_oOutput.Write("[");
  2134. a_oOutput.Write(convert.Data());
  2135. a_oOutput.Write("]");
  2136. a_oOutput.Write(SI_NEWLINE_A);
  2137. }
  2138. // get all of the keys sorted in load order
  2139. TNamesDepend oKeys;
  2140. GetAllKeys(iSection->pItem, oKeys);
  2141. #if defined(_MSC_VER) && _MSC_VER <= 1200
  2142. oKeys.sort();
  2143. #elif defined(__BORLANDC__)
  2144. oKeys.sort(Entry::LoadOrder());
  2145. #else
  2146. oKeys.sort(typename Entry::LoadOrder());
  2147. #endif
  2148. // write all keys and values
  2149. typename TNamesDepend::const_iterator iKey = oKeys.begin();
  2150. for ( ; iKey != oKeys.end(); ++iKey) {
  2151. // get all values for this key
  2152. TNamesDepend oValues;
  2153. GetAllValues(iSection->pItem, iKey->pItem, oValues);
  2154. typename TNamesDepend::const_iterator iValue = oValues.begin();
  2155. for ( ; iValue != oValues.end(); ++iValue) {
  2156. // write out the comment if there is one
  2157. if (iValue->pComment) {
  2158. a_oOutput.Write(SI_NEWLINE_A);
  2159. if (!OutputMultiLineText(a_oOutput, convert, iValue->pComment)) {
  2160. return SI_FAIL;
  2161. }
  2162. }
  2163. // write the key
  2164. if (!convert.ConvertToStore(iKey->pItem)) {
  2165. return SI_FAIL;
  2166. }
  2167. a_oOutput.Write(convert.Data());
  2168. // write the value
  2169. if (!convert.ConvertToStore(iValue->pItem)) {
  2170. return SI_FAIL;
  2171. }
  2172. a_oOutput.Write(m_bSpaces ? " = " : "=");
  2173. if (m_bAllowMultiLine && IsMultiLineData(iValue->pItem)) {
  2174. // multi-line data needs to be processed specially to ensure
  2175. // that we use the correct newline format for the current system
  2176. a_oOutput.Write("<<<END_OF_TEXT" SI_NEWLINE_A);
  2177. if (!OutputMultiLineText(a_oOutput, convert, iValue->pItem)) {
  2178. return SI_FAIL;
  2179. }
  2180. a_oOutput.Write("END_OF_TEXT");
  2181. }
  2182. else {
  2183. a_oOutput.Write(convert.Data());
  2184. }
  2185. a_oOutput.Write(SI_NEWLINE_A);
  2186. }
  2187. }
  2188. bNeedNewLine = true;
  2189. }
  2190. return SI_OK;
  2191. }
  2192. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  2193. bool
  2194. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::OutputMultiLineText(
  2195. OutputWriter & a_oOutput,
  2196. Converter & a_oConverter,
  2197. const SI_CHAR * a_pText
  2198. ) const
  2199. {
  2200. const SI_CHAR * pEndOfLine;
  2201. SI_CHAR cEndOfLineChar = *a_pText;
  2202. while (cEndOfLineChar) {
  2203. // find the end of this line
  2204. pEndOfLine = a_pText;
  2205. for (; *pEndOfLine && *pEndOfLine != '\n'; ++pEndOfLine) /*loop*/ ;
  2206. cEndOfLineChar = *pEndOfLine;
  2207. // temporarily null terminate, convert and output the line
  2208. *const_cast<SI_CHAR*>(pEndOfLine) = 0;
  2209. if (!a_oConverter.ConvertToStore(a_pText)) {
  2210. return false;
  2211. }
  2212. *const_cast<SI_CHAR*>(pEndOfLine) = cEndOfLineChar;
  2213. a_pText += (pEndOfLine - a_pText) + 1;
  2214. a_oOutput.Write(a_oConverter.Data());
  2215. a_oOutput.Write(SI_NEWLINE_A);
  2216. }
  2217. return true;
  2218. }
  2219. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  2220. bool
  2221. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::Delete(
  2222. const SI_CHAR * a_pSection,
  2223. const SI_CHAR * a_pKey,
  2224. bool a_bRemoveEmpty
  2225. )
  2226. {
  2227. return DeleteValue(a_pSection, a_pKey, NULL, a_bRemoveEmpty);
  2228. }
  2229. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  2230. bool
  2231. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::DeleteValue(
  2232. const SI_CHAR * a_pSection,
  2233. const SI_CHAR * a_pKey,
  2234. const SI_CHAR * a_pValue,
  2235. bool a_bRemoveEmpty
  2236. )
  2237. {
  2238. if (!a_pSection) {
  2239. return false;
  2240. }
  2241. typename TSection::iterator iSection = m_data.find(a_pSection);
  2242. if (iSection == m_data.end()) {
  2243. return false;
  2244. }
  2245. // remove a single key if we have a keyname
  2246. if (a_pKey) {
  2247. typename TKeyVal::iterator iKeyVal = iSection->second.find(a_pKey);
  2248. if (iKeyVal == iSection->second.end()) {
  2249. return false;
  2250. }
  2251. const static SI_STRLESS isLess = SI_STRLESS();
  2252. // remove any copied strings and then the key
  2253. typename TKeyVal::iterator iDelete;
  2254. bool bDeleted = false;
  2255. do {
  2256. iDelete = iKeyVal++;
  2257. if(a_pValue == NULL ||
  2258. (isLess(a_pValue, iDelete->second) == false &&
  2259. isLess(iDelete->second, a_pValue) == false)) {
  2260. DeleteString(iDelete->first.pItem);
  2261. DeleteString(iDelete->second);
  2262. iSection->second.erase(iDelete);
  2263. bDeleted = true;
  2264. }
  2265. }
  2266. while (iKeyVal != iSection->second.end()
  2267. && !IsLess(a_pKey, iKeyVal->first.pItem));
  2268. if(!bDeleted) {
  2269. return false;
  2270. }
  2271. // done now if the section is not empty or we are not pruning away
  2272. // the empty sections. Otherwise let it fall through into the section
  2273. // deletion code
  2274. if (!a_bRemoveEmpty || !iSection->second.empty()) {
  2275. return true;
  2276. }
  2277. }
  2278. else {
  2279. // delete all copied strings from this section. The actual
  2280. // entries will be removed when the section is removed.
  2281. typename TKeyVal::iterator iKeyVal = iSection->second.begin();
  2282. for ( ; iKeyVal != iSection->second.end(); ++iKeyVal) {
  2283. DeleteString(iKeyVal->first.pItem);
  2284. DeleteString(iKeyVal->second);
  2285. }
  2286. }
  2287. // delete the section itself
  2288. DeleteString(iSection->first.pItem);
  2289. m_data.erase(iSection);
  2290. return true;
  2291. }
  2292. template<class SI_CHAR, class SI_STRLESS, class SI_CONVERTER>
  2293. void
  2294. CSimpleIniTempl<SI_CHAR,SI_STRLESS,SI_CONVERTER>::DeleteString(
  2295. const SI_CHAR * a_pString
  2296. )
  2297. {
  2298. // strings may exist either inside the data block, or they will be
  2299. // individually allocated and stored in m_strings. We only physically
  2300. // delete those stored in m_strings.
  2301. if (a_pString < m_pData || a_pString >= m_pData + m_uDataLen) {
  2302. typename TNamesDepend::iterator i = m_strings.begin();
  2303. for (;i != m_strings.end(); ++i) {
  2304. if (a_pString == i->pItem) {
  2305. delete[] const_cast<SI_CHAR*>(i->pItem);
  2306. m_strings.erase(i);
  2307. break;
  2308. }
  2309. }
  2310. }
  2311. }
  2312. // ---------------------------------------------------------------------------
  2313. // CONVERSION FUNCTIONS
  2314. // ---------------------------------------------------------------------------
  2315. // Defines the conversion classes for different libraries. Before including
  2316. // SimpleIni.h, set the converter that you wish you use by defining one of the
  2317. // following symbols.
  2318. //
  2319. // SI_NO_CONVERSION Do not make the "W" wide character version of the
  2320. // library available. Only CSimpleIniA etc is defined.
  2321. // SI_CONVERT_GENERIC Use the Unicode reference conversion library in
  2322. // the accompanying files ConvertUTF.h/c
  2323. // SI_CONVERT_ICU Use the IBM ICU conversion library. Requires
  2324. // ICU headers on include path and icuuc.lib
  2325. // SI_CONVERT_WIN32 Use the Win32 API functions for conversion.
  2326. #if !defined(SI_NO_CONVERSION) && !defined(SI_CONVERT_GENERIC) && !defined(SI_CONVERT_WIN32) && !defined(SI_CONVERT_ICU)
  2327. # ifdef _WIN32
  2328. # define SI_CONVERT_WIN32
  2329. # else
  2330. # define SI_CONVERT_GENERIC
  2331. # endif
  2332. #endif
  2333. /**
  2334. * Generic case-sensitive less than comparison. This class returns numerically
  2335. * ordered ASCII case-sensitive text for all possible sizes and types of
  2336. * SI_CHAR.
  2337. */
  2338. template<class SI_CHAR>
  2339. struct SI_GenericCase {
  2340. bool operator()(const SI_CHAR * pLeft, const SI_CHAR * pRight) const {
  2341. long cmp;
  2342. for ( ;*pLeft && *pRight; ++pLeft, ++pRight) {
  2343. cmp = (long) *pLeft - (long) *pRight;
  2344. if (cmp != 0) {
  2345. return cmp < 0;
  2346. }
  2347. }
  2348. return *pRight != 0;
  2349. }
  2350. };
  2351. /**
  2352. * Generic ASCII case-insensitive less than comparison. This class returns
  2353. * numerically ordered ASCII case-insensitive text for all possible sizes
  2354. * and types of SI_CHAR. It is not safe for MBCS text comparison where
  2355. * ASCII A-Z characters are used in the encoding of multi-byte characters.
  2356. */
  2357. template<class SI_CHAR>
  2358. struct SI_GenericNoCase {
  2359. inline SI_CHAR locase(SI_CHAR ch) const {
  2360. return (ch < 'A' || ch > 'Z') ? ch : (ch - 'A' + 'a');
  2361. }
  2362. bool operator()(const SI_CHAR * pLeft, const SI_CHAR * pRight) const {
  2363. long cmp;
  2364. for ( ;*pLeft && *pRight; ++pLeft, ++pRight) {
  2365. cmp = (long) locase(*pLeft) - (long) locase(*pRight);
  2366. if (cmp != 0) {
  2367. return cmp < 0;
  2368. }
  2369. }
  2370. return *pRight != 0;
  2371. }
  2372. };
  2373. /**
  2374. * Null conversion class for MBCS/UTF-8 to char (or equivalent).
  2375. */
  2376. template<class SI_CHAR>
  2377. class SI_ConvertA {
  2378. bool m_bStoreIsUtf8;
  2379. protected:
  2380. SI_ConvertA() { }
  2381. public:
  2382. SI_ConvertA(bool a_bStoreIsUtf8) : m_bStoreIsUtf8(a_bStoreIsUtf8) { }
  2383. /* copy and assignment */
  2384. SI_ConvertA(const SI_ConvertA & rhs) { operator=(rhs); }
  2385. SI_ConvertA & operator=(const SI_ConvertA & rhs) {
  2386. m_bStoreIsUtf8 = rhs.m_bStoreIsUtf8;
  2387. return *this;
  2388. }
  2389. /** Calculate the number of SI_CHAR required for converting the input
  2390. * from the storage format. The storage format is always UTF-8 or MBCS.
  2391. *
  2392. * @param a_pInputData Data in storage format to be converted to SI_CHAR.
  2393. * @param a_uInputDataLen Length of storage format data in bytes. This
  2394. * must be the actual length of the data, including
  2395. * NULL byte if NULL terminated string is required.
  2396. * @return Number of SI_CHAR required by the string when
  2397. * converted. If there are embedded NULL bytes in the
  2398. * input data, only the string up and not including
  2399. * the NULL byte will be converted.
  2400. * @return -1 cast to size_t on a conversion error.
  2401. */
  2402. size_t SizeFromStore(
  2403. const char * a_pInputData,
  2404. size_t a_uInputDataLen)
  2405. {
  2406. (void)a_pInputData;
  2407. SI_ASSERT(a_uInputDataLen != (size_t) -1);
  2408. // ASCII/MBCS/UTF-8 needs no conversion
  2409. return a_uInputDataLen;
  2410. }
  2411. /** Convert the input string from the storage format to SI_CHAR.
  2412. * The storage format is always UTF-8 or MBCS.
  2413. *
  2414. * @param a_pInputData Data in storage format to be converted to SI_CHAR.
  2415. * @param a_uInputDataLen Length of storage format data in bytes. This
  2416. * must be the actual length of the data, including
  2417. * NULL byte if NULL terminated string is required.
  2418. * @param a_pOutputData Pointer to the output buffer to received the
  2419. * converted data.
  2420. * @param a_uOutputDataSize Size of the output buffer in SI_CHAR.
  2421. * @return true if all of the input data was successfully
  2422. * converted.
  2423. */
  2424. bool ConvertFromStore(
  2425. const char * a_pInputData,
  2426. size_t a_uInputDataLen,
  2427. SI_CHAR * a_pOutputData,
  2428. size_t a_uOutputDataSize)
  2429. {
  2430. // ASCII/MBCS/UTF-8 needs no conversion
  2431. if (a_uInputDataLen > a_uOutputDataSize) {
  2432. return false;
  2433. }
  2434. memcpy(a_pOutputData, a_pInputData, a_uInputDataLen);
  2435. return true;
  2436. }
  2437. /** Calculate the number of char required by the storage format of this
  2438. * data. The storage format is always UTF-8 or MBCS.
  2439. *
  2440. * @param a_pInputData NULL terminated string to calculate the number of
  2441. * bytes required to be converted to storage format.
  2442. * @return Number of bytes required by the string when
  2443. * converted to storage format. This size always
  2444. * includes space for the terminating NULL character.
  2445. * @return -1 cast to size_t on a conversion error.
  2446. */
  2447. size_t SizeToStore(
  2448. const SI_CHAR * a_pInputData)
  2449. {
  2450. // ASCII/MBCS/UTF-8 needs no conversion
  2451. return strlen((const char *)a_pInputData) + 1;
  2452. }
  2453. /** Convert the input string to the storage format of this data.
  2454. * The storage format is always UTF-8 or MBCS.
  2455. *
  2456. * @param a_pInputData NULL terminated source string to convert. All of
  2457. * the data will be converted including the
  2458. * terminating NULL character.
  2459. * @param a_pOutputData Pointer to the buffer to receive the converted
  2460. * string.
  2461. * @param a_uOutputDataSize Size of the output buffer in char.
  2462. * @return true if all of the input data, including the
  2463. * terminating NULL character was successfully
  2464. * converted.
  2465. */
  2466. bool ConvertToStore(
  2467. const SI_CHAR * a_pInputData,
  2468. char * a_pOutputData,
  2469. size_t a_uOutputDataSize)
  2470. {
  2471. // calc input string length (SI_CHAR type and size independent)
  2472. size_t uInputLen = strlen((const char *)a_pInputData) + 1;
  2473. if (uInputLen > a_uOutputDataSize) {
  2474. return false;
  2475. }
  2476. // ascii/UTF-8 needs no conversion
  2477. memcpy(a_pOutputData, a_pInputData, uInputLen);
  2478. return true;
  2479. }
  2480. };
  2481. // ---------------------------------------------------------------------------
  2482. // SI_CONVERT_GENERIC
  2483. // ---------------------------------------------------------------------------
  2484. #ifdef SI_CONVERT_GENERIC
  2485. #define SI_Case SI_GenericCase
  2486. #define SI_NoCase SI_GenericNoCase
  2487. #include <wchar.h>
  2488. //#include "ConvertUTF.h"
  2489. /**
  2490. * Converts UTF-8 to a wchar_t (or equivalent) using the Unicode reference
  2491. * library functions. This can be used on all platforms.
  2492. */
  2493. template<class SI_CHAR>
  2494. class SI_ConvertW {
  2495. bool m_bStoreIsUtf8;
  2496. protected:
  2497. SI_ConvertW() { }
  2498. public:
  2499. SI_ConvertW(bool a_bStoreIsUtf8) : m_bStoreIsUtf8(a_bStoreIsUtf8) { }
  2500. /* copy and assignment */
  2501. SI_ConvertW(const SI_ConvertW & rhs) { operator=(rhs); }
  2502. SI_ConvertW & operator=(const SI_ConvertW & rhs) {
  2503. m_bStoreIsUtf8 = rhs.m_bStoreIsUtf8;
  2504. return *this;
  2505. }
  2506. /** Calculate the number of SI_CHAR required for converting the input
  2507. * from the storage format. The storage format is always UTF-8 or MBCS.
  2508. *
  2509. * @param a_pInputData Data in storage format to be converted to SI_CHAR.
  2510. * @param a_uInputDataLen Length of storage format data in bytes. This
  2511. * must be the actual length of the data, including
  2512. * NULL byte if NULL terminated string is required.
  2513. * @return Number of SI_CHAR required by the string when
  2514. * converted. If there are embedded NULL bytes in the
  2515. * input data, only the string up and not including
  2516. * the NULL byte will be converted.
  2517. * @return -1 cast to size_t on a conversion error.
  2518. */
  2519. size_t SizeFromStore(
  2520. const char * a_pInputData,
  2521. size_t a_uInputDataLen)
  2522. {
  2523. SI_ASSERT(a_uInputDataLen != (size_t) -1);
  2524. if (m_bStoreIsUtf8) {
  2525. // worst case scenario for UTF-8 to wchar_t is 1 char -> 1 wchar_t
  2526. // so we just return the same number of characters required as for
  2527. // the source text.
  2528. return a_uInputDataLen;
  2529. }
  2530. #if defined(SI_NO_MBSTOWCS_NULL) || (!defined(_MSC_VER) && !defined(_linux))
  2531. // fall back processing for platforms that don't support a NULL dest to mbstowcs
  2532. // worst case scenario is 1:1, this will be a sufficient buffer size
  2533. (void)a_pInputData;
  2534. return a_uInputDataLen;
  2535. #else
  2536. // get the actual required buffer size
  2537. return mbstowcs(NULL, a_pInputData, a_uInputDataLen);
  2538. #endif
  2539. }
  2540. /** Convert the input string from the storage format to SI_CHAR.
  2541. * The storage format is always UTF-8 or MBCS.
  2542. *
  2543. * @param a_pInputData Data in storage format to be converted to SI_CHAR.
  2544. * @param a_uInputDataLen Length of storage format data in bytes. This
  2545. * must be the actual length of the data, including
  2546. * NULL byte if NULL terminated string is required.
  2547. * @param a_pOutputData Pointer to the output buffer to received the
  2548. * converted data.
  2549. * @param a_uOutputDataSize Size of the output buffer in SI_CHAR.
  2550. * @return true if all of the input data was successfully
  2551. * converted.
  2552. */
  2553. bool ConvertFromStore(
  2554. const char * a_pInputData,
  2555. size_t a_uInputDataLen,
  2556. SI_CHAR * a_pOutputData,
  2557. size_t a_uOutputDataSize)
  2558. {
  2559. if (m_bStoreIsUtf8) {
  2560. // This uses the Unicode reference implementation to do the
  2561. // conversion from UTF-8 to wchar_t. The required files are
  2562. // ConvertUTF.h and ConvertUTF.c which should be included in
  2563. // the distribution but are publically available from unicode.org
  2564. // at http://www.unicode.org/Public/PROGRAMS/CVTUTF/
  2565. // ConversionResult retval;
  2566. // const UTF8 * pUtf8 = (const UTF8 *) a_pInputData;
  2567. // if (sizeof(wchar_t) == sizeof(UTF32)) {
  2568. // UTF32 * pUtf32 = (UTF32 *) a_pOutputData;
  2569. // retval = ConvertUTF8toUTF32(
  2570. // &pUtf8, pUtf8 + a_uInputDataLen,
  2571. // &pUtf32, pUtf32 + a_uOutputDataSize,
  2572. // lenientConversion);
  2573. // }
  2574. // else if (sizeof(wchar_t) == sizeof(UTF16)) {
  2575. // UTF16 * pUtf16 = (UTF16 *) a_pOutputData;
  2576. // retval = ConvertUTF8toUTF16(
  2577. // &pUtf8, pUtf8 + a_uInputDataLen,
  2578. // &pUtf16, pUtf16 + a_uOutputDataSize,
  2579. // lenientConversion);
  2580. // }
  2581. // return retval == conversionOK;
  2582. return true;
  2583. }
  2584. // convert to wchar_t
  2585. size_t retval = mbstowcs(a_pOutputData,
  2586. a_pInputData, a_uOutputDataSize);
  2587. return retval != (size_t)(-1);
  2588. }
  2589. /** Calculate the number of char required by the storage format of this
  2590. * data. The storage format is always UTF-8 or MBCS.
  2591. *
  2592. * @param a_pInputData NULL terminated string to calculate the number of
  2593. * bytes required to be converted to storage format.
  2594. * @return Number of bytes required by the string when
  2595. * converted to storage format. This size always
  2596. * includes space for the terminating NULL character.
  2597. * @return -1 cast to size_t on a conversion error.
  2598. */
  2599. size_t SizeToStore(
  2600. const SI_CHAR * a_pInputData)
  2601. {
  2602. if (m_bStoreIsUtf8) {
  2603. // worst case scenario for wchar_t to UTF-8 is 1 wchar_t -> 6 char
  2604. size_t uLen = 0;
  2605. while (a_pInputData[uLen]) {
  2606. ++uLen;
  2607. }
  2608. return (6 * uLen) + 1;
  2609. }
  2610. else {
  2611. size_t uLen = wcstombs(NULL, a_pInputData, 0);
  2612. if (uLen == (size_t)(-1)) {
  2613. return uLen;
  2614. }
  2615. return uLen + 1; // include NULL terminator
  2616. }
  2617. }
  2618. /** Convert the input string to the storage format of this data.
  2619. * The storage format is always UTF-8 or MBCS.
  2620. *
  2621. * @param a_pInputData NULL terminated source string to convert. All of
  2622. * the data will be converted including the
  2623. * terminating NULL character.
  2624. * @param a_pOutputData Pointer to the buffer to receive the converted
  2625. * string.
  2626. * @param a_uOutputDataSize Size of the output buffer in char.
  2627. * @return true if all of the input data, including the
  2628. * terminating NULL character was successfully
  2629. * converted.
  2630. */
  2631. bool ConvertToStore(
  2632. const SI_CHAR * a_pInputData,
  2633. char * a_pOutputData,
  2634. size_t a_uOutputDataSize
  2635. )
  2636. {
  2637. if (m_bStoreIsUtf8) {
  2638. // calc input string length (SI_CHAR type and size independent)
  2639. size_t uInputLen = 0;
  2640. while (a_pInputData[uInputLen]) {
  2641. ++uInputLen;
  2642. }
  2643. ++uInputLen; // include the NULL char
  2644. // This uses the Unicode reference implementation to do the
  2645. // conversion from wchar_t to UTF-8. The required files are
  2646. // ConvertUTF.h and ConvertUTF.c which should be included in
  2647. // the distribution but are publically available from unicode.org
  2648. // at http://www.unicode.org/Public/PROGRAMS/CVTUTF/
  2649. // ConversionResult retval;
  2650. // UTF8 * pUtf8 = (UTF8 *) a_pOutputData;
  2651. // if (sizeof(wchar_t) == sizeof(UTF32)) {
  2652. // const UTF32 * pUtf32 = (const UTF32 *) a_pInputData;
  2653. // retval = ConvertUTF32toUTF8(
  2654. // &pUtf32, pUtf32 + uInputLen,
  2655. // &pUtf8, pUtf8 + a_uOutputDataSize,
  2656. // lenientConversion);
  2657. // }
  2658. // else if (sizeof(wchar_t) == sizeof(UTF16)) {
  2659. // const UTF16 * pUtf16 = (const UTF16 *) a_pInputData;
  2660. // retval = ConvertUTF16toUTF8(
  2661. // &pUtf16, pUtf16 + uInputLen,
  2662. // &pUtf8, pUtf8 + a_uOutputDataSize,
  2663. // lenientConversion);
  2664. // }
  2665. // return retval == conversionOK;
  2666. return true;
  2667. }
  2668. else {
  2669. size_t retval = wcstombs(a_pOutputData,
  2670. a_pInputData, a_uOutputDataSize);
  2671. return retval != (size_t) -1;
  2672. }
  2673. }
  2674. };
  2675. #endif // SI_CONVERT_GENERIC
  2676. // ---------------------------------------------------------------------------
  2677. // SI_CONVERT_ICU
  2678. // ---------------------------------------------------------------------------
  2679. #ifdef SI_CONVERT_ICU
  2680. #define SI_Case SI_GenericCase
  2681. #define SI_NoCase SI_GenericNoCase
  2682. #include <unicode/ucnv.h>
  2683. /**
  2684. * Converts MBCS/UTF-8 to UChar using ICU. This can be used on all platforms.
  2685. */
  2686. template<class SI_CHAR>
  2687. class SI_ConvertW {
  2688. const char * m_pEncoding;
  2689. UConverter * m_pConverter;
  2690. protected:
  2691. SI_ConvertW() : m_pEncoding(NULL), m_pConverter(NULL) { }
  2692. public:
  2693. SI_ConvertW(bool a_bStoreIsUtf8) : m_pConverter(NULL) {
  2694. m_pEncoding = a_bStoreIsUtf8 ? "UTF-8" : NULL;
  2695. }
  2696. /* copy and assignment */
  2697. SI_ConvertW(const SI_ConvertW & rhs) { operator=(rhs); }
  2698. SI_ConvertW & operator=(const SI_ConvertW & rhs) {
  2699. m_pEncoding = rhs.m_pEncoding;
  2700. m_pConverter = NULL;
  2701. return *this;
  2702. }
  2703. ~SI_ConvertW() { if (m_pConverter) ucnv_close(m_pConverter); }
  2704. /** Calculate the number of UChar required for converting the input
  2705. * from the storage format. The storage format is always UTF-8 or MBCS.
  2706. *
  2707. * @param a_pInputData Data in storage format to be converted to UChar.
  2708. * @param a_uInputDataLen Length of storage format data in bytes. This
  2709. * must be the actual length of the data, including
  2710. * NULL byte if NULL terminated string is required.
  2711. * @return Number of UChar required by the string when
  2712. * converted. If there are embedded NULL bytes in the
  2713. * input data, only the string up and not including
  2714. * the NULL byte will be converted.
  2715. * @return -1 cast to size_t on a conversion error.
  2716. */
  2717. size_t SizeFromStore(
  2718. const char * a_pInputData,
  2719. size_t a_uInputDataLen)
  2720. {
  2721. SI_ASSERT(a_uInputDataLen != (size_t) -1);
  2722. UErrorCode nError;
  2723. if (!m_pConverter) {
  2724. nError = U_ZERO_ERROR;
  2725. m_pConverter = ucnv_open(m_pEncoding, &nError);
  2726. if (U_FAILURE(nError)) {
  2727. return (size_t) -1;
  2728. }
  2729. }
  2730. nError = U_ZERO_ERROR;
  2731. int32_t nLen = ucnv_toUChars(m_pConverter, NULL, 0,
  2732. a_pInputData, (int32_t) a_uInputDataLen, &nError);
  2733. if (U_FAILURE(nError) && nError != U_BUFFER_OVERFLOW_ERROR) {
  2734. return (size_t) -1;
  2735. }
  2736. return (size_t) nLen;
  2737. }
  2738. /** Convert the input string from the storage format to UChar.
  2739. * The storage format is always UTF-8 or MBCS.
  2740. *
  2741. * @param a_pInputData Data in storage format to be converted to UChar.
  2742. * @param a_uInputDataLen Length of storage format data in bytes. This
  2743. * must be the actual length of the data, including
  2744. * NULL byte if NULL terminated string is required.
  2745. * @param a_pOutputData Pointer to the output buffer to received the
  2746. * converted data.
  2747. * @param a_uOutputDataSize Size of the output buffer in UChar.
  2748. * @return true if all of the input data was successfully
  2749. * converted.
  2750. */
  2751. bool ConvertFromStore(
  2752. const char * a_pInputData,
  2753. size_t a_uInputDataLen,
  2754. UChar * a_pOutputData,
  2755. size_t a_uOutputDataSize)
  2756. {
  2757. UErrorCode nError;
  2758. if (!m_pConverter) {
  2759. nError = U_ZERO_ERROR;
  2760. m_pConverter = ucnv_open(m_pEncoding, &nError);
  2761. if (U_FAILURE(nError)) {
  2762. return false;
  2763. }
  2764. }
  2765. nError = U_ZERO_ERROR;
  2766. ucnv_toUChars(m_pConverter,
  2767. a_pOutputData, (int32_t) a_uOutputDataSize,
  2768. a_pInputData, (int32_t) a_uInputDataLen, &nError);
  2769. if (U_FAILURE(nError)) {
  2770. return false;
  2771. }
  2772. return true;
  2773. }
  2774. /** Calculate the number of char required by the storage format of this
  2775. * data. The storage format is always UTF-8 or MBCS.
  2776. *
  2777. * @param a_pInputData NULL terminated string to calculate the number of
  2778. * bytes required to be converted to storage format.
  2779. * @return Number of bytes required by the string when
  2780. * converted to storage format. This size always
  2781. * includes space for the terminating NULL character.
  2782. * @return -1 cast to size_t on a conversion error.
  2783. */
  2784. size_t SizeToStore(
  2785. const UChar * a_pInputData)
  2786. {
  2787. UErrorCode nError;
  2788. if (!m_pConverter) {
  2789. nError = U_ZERO_ERROR;
  2790. m_pConverter = ucnv_open(m_pEncoding, &nError);
  2791. if (U_FAILURE(nError)) {
  2792. return (size_t) -1;
  2793. }
  2794. }
  2795. nError = U_ZERO_ERROR;
  2796. int32_t nLen = ucnv_fromUChars(m_pConverter, NULL, 0,
  2797. a_pInputData, -1, &nError);
  2798. if (U_FAILURE(nError) && nError != U_BUFFER_OVERFLOW_ERROR) {
  2799. return (size_t) -1;
  2800. }
  2801. return (size_t) nLen + 1;
  2802. }
  2803. /** Convert the input string to the storage format of this data.
  2804. * The storage format is always UTF-8 or MBCS.
  2805. *
  2806. * @param a_pInputData NULL terminated source string to convert. All of
  2807. * the data will be converted including the
  2808. * terminating NULL character.
  2809. * @param a_pOutputData Pointer to the buffer to receive the converted
  2810. * string.
  2811. * @param a_pOutputDataSize Size of the output buffer in char.
  2812. * @return true if all of the input data, including the
  2813. * terminating NULL character was successfully
  2814. * converted.
  2815. */
  2816. bool ConvertToStore(
  2817. const UChar * a_pInputData,
  2818. char * a_pOutputData,
  2819. size_t a_uOutputDataSize)
  2820. {
  2821. UErrorCode nError;
  2822. if (!m_pConverter) {
  2823. nError = U_ZERO_ERROR;
  2824. m_pConverter = ucnv_open(m_pEncoding, &nError);
  2825. if (U_FAILURE(nError)) {
  2826. return false;
  2827. }
  2828. }
  2829. nError = U_ZERO_ERROR;
  2830. ucnv_fromUChars(m_pConverter,
  2831. a_pOutputData, (int32_t) a_uOutputDataSize,
  2832. a_pInputData, -1, &nError);
  2833. if (U_FAILURE(nError)) {
  2834. return false;
  2835. }
  2836. return true;
  2837. }
  2838. };
  2839. #endif // SI_CONVERT_ICU
  2840. // ---------------------------------------------------------------------------
  2841. // SI_CONVERT_WIN32
  2842. // ---------------------------------------------------------------------------
  2843. #ifdef SI_CONVERT_WIN32
  2844. #define SI_Case SI_GenericCase
  2845. // Windows CE doesn't have errno or MBCS libraries
  2846. #ifdef _WIN32_WCE
  2847. # ifndef SI_NO_MBCS
  2848. # define SI_NO_MBCS
  2849. # endif
  2850. #endif
  2851. #include <windows.h>
  2852. #ifdef SI_NO_MBCS
  2853. # define SI_NoCase SI_GenericNoCase
  2854. #else // !SI_NO_MBCS
  2855. /**
  2856. * Case-insensitive comparison class using Win32 MBCS functions. This class
  2857. * returns a case-insensitive semi-collation order for MBCS text. It may not
  2858. * be safe for UTF-8 text returned in char format as we don't know what
  2859. * characters will be folded by the function! Therefore, if you are using
  2860. * SI_CHAR == char and SetUnicode(true), then you need to use the generic
  2861. * SI_NoCase class instead.
  2862. */
  2863. #include <mbstring.h>
  2864. template<class SI_CHAR>
  2865. struct SI_NoCase {
  2866. bool operator()(const SI_CHAR * pLeft, const SI_CHAR * pRight) const {
  2867. if (sizeof(SI_CHAR) == sizeof(char)) {
  2868. return _mbsicmp((const unsigned char *)pLeft,
  2869. (const unsigned char *)pRight) < 0;
  2870. }
  2871. if (sizeof(SI_CHAR) == sizeof(wchar_t)) {
  2872. return _wcsicmp((const wchar_t *)pLeft,
  2873. (const wchar_t *)pRight) < 0;
  2874. }
  2875. return SI_GenericNoCase<SI_CHAR>()(pLeft, pRight);
  2876. }
  2877. };
  2878. #endif // SI_NO_MBCS
  2879. /**
  2880. * Converts MBCS and UTF-8 to a wchar_t (or equivalent) on Windows. This uses
  2881. * only the Win32 functions and doesn't require the external Unicode UTF-8
  2882. * conversion library. It will not work on Windows 95 without using Microsoft
  2883. * Layer for Unicode in your application.
  2884. */
  2885. template<class SI_CHAR>
  2886. class SI_ConvertW {
  2887. UINT m_uCodePage;
  2888. protected:
  2889. SI_ConvertW() { }
  2890. public:
  2891. SI_ConvertW(bool a_bStoreIsUtf8) {
  2892. m_uCodePage = a_bStoreIsUtf8 ? CP_UTF8 : CP_ACP;
  2893. }
  2894. /* copy and assignment */
  2895. SI_ConvertW(const SI_ConvertW & rhs) { operator=(rhs); }
  2896. SI_ConvertW & operator=(const SI_ConvertW & rhs) {
  2897. m_uCodePage = rhs.m_uCodePage;
  2898. return *this;
  2899. }
  2900. /** Calculate the number of SI_CHAR required for converting the input
  2901. * from the storage format. The storage format is always UTF-8 or MBCS.
  2902. *
  2903. * @param a_pInputData Data in storage format to be converted to SI_CHAR.
  2904. * @param a_uInputDataLen Length of storage format data in bytes. This
  2905. * must be the actual length of the data, including
  2906. * NULL byte if NULL terminated string is required.
  2907. * @return Number of SI_CHAR required by the string when
  2908. * converted. If there are embedded NULL bytes in the
  2909. * input data, only the string up and not including
  2910. * the NULL byte will be converted.
  2911. * @return -1 cast to size_t on a conversion error.
  2912. */
  2913. size_t SizeFromStore(
  2914. const char * a_pInputData,
  2915. size_t a_uInputDataLen)
  2916. {
  2917. SI_ASSERT(a_uInputDataLen != (size_t) -1);
  2918. int retval = MultiByteToWideChar(
  2919. m_uCodePage, 0,
  2920. a_pInputData, (int) a_uInputDataLen,
  2921. 0, 0);
  2922. return (size_t)(retval > 0 ? retval : -1);
  2923. }
  2924. /** Convert the input string from the storage format to SI_CHAR.
  2925. * The storage format is always UTF-8 or MBCS.
  2926. *
  2927. * @param a_pInputData Data in storage format to be converted to SI_CHAR.
  2928. * @param a_uInputDataLen Length of storage format data in bytes. This
  2929. * must be the actual length of the data, including
  2930. * NULL byte if NULL terminated string is required.
  2931. * @param a_pOutputData Pointer to the output buffer to received the
  2932. * converted data.
  2933. * @param a_uOutputDataSize Size of the output buffer in SI_CHAR.
  2934. * @return true if all of the input data was successfully
  2935. * converted.
  2936. */
  2937. bool ConvertFromStore(
  2938. const char * a_pInputData,
  2939. size_t a_uInputDataLen,
  2940. SI_CHAR * a_pOutputData,
  2941. size_t a_uOutputDataSize)
  2942. {
  2943. int nSize = MultiByteToWideChar(
  2944. m_uCodePage, 0,
  2945. a_pInputData, (int) a_uInputDataLen,
  2946. (wchar_t *) a_pOutputData, (int) a_uOutputDataSize);
  2947. return (nSize > 0);
  2948. }
  2949. /** Calculate the number of char required by the storage format of this
  2950. * data. The storage format is always UTF-8.
  2951. *
  2952. * @param a_pInputData NULL terminated string to calculate the number of
  2953. * bytes required to be converted to storage format.
  2954. * @return Number of bytes required by the string when
  2955. * converted to storage format. This size always
  2956. * includes space for the terminating NULL character.
  2957. * @return -1 cast to size_t on a conversion error.
  2958. */
  2959. size_t SizeToStore(
  2960. const SI_CHAR * a_pInputData)
  2961. {
  2962. int retval = WideCharToMultiByte(
  2963. m_uCodePage, 0,
  2964. (const wchar_t *) a_pInputData, -1,
  2965. 0, 0, 0, 0);
  2966. return (size_t) (retval > 0 ? retval : -1);
  2967. }
  2968. /** Convert the input string to the storage format of this data.
  2969. * The storage format is always UTF-8 or MBCS.
  2970. *
  2971. * @param a_pInputData NULL terminated source string to convert. All of
  2972. * the data will be converted including the
  2973. * terminating NULL character.
  2974. * @param a_pOutputData Pointer to the buffer to receive the converted
  2975. * string.
  2976. * @param a_pOutputDataSize Size of the output buffer in char.
  2977. * @return true if all of the input data, including the
  2978. * terminating NULL character was successfully
  2979. * converted.
  2980. */
  2981. bool ConvertToStore(
  2982. const SI_CHAR * a_pInputData,
  2983. char * a_pOutputData,
  2984. size_t a_uOutputDataSize)
  2985. {
  2986. int retval = WideCharToMultiByte(
  2987. m_uCodePage, 0,
  2988. (const wchar_t *) a_pInputData, -1,
  2989. a_pOutputData, (int) a_uOutputDataSize, 0, 0);
  2990. return retval > 0;
  2991. }
  2992. };
  2993. #endif // SI_CONVERT_WIN32
  2994. // ---------------------------------------------------------------------------
  2995. // TYPE DEFINITIONS
  2996. // ---------------------------------------------------------------------------
  2997. typedef CSimpleIniTempl<char,
  2998. SI_NoCase<char>,SI_ConvertA<char> > CSimpleIniA;
  2999. typedef CSimpleIniTempl<char,
  3000. SI_Case<char>,SI_ConvertA<char> > CSimpleIniCaseA;
  3001. #if defined(SI_NO_CONVERSION)
  3002. // if there is no wide char conversion then we don't need to define the
  3003. // widechar "W" versions of CSimpleIni
  3004. # define CSimpleIni CSimpleIniA
  3005. # define CSimpleIniCase CSimpleIniCaseA
  3006. # define SI_NEWLINE SI_NEWLINE_A
  3007. #else
  3008. # if defined(SI_CONVERT_ICU)
  3009. typedef CSimpleIniTempl<UChar,
  3010. SI_NoCase<UChar>,SI_ConvertW<UChar> > CSimpleIniW;
  3011. typedef CSimpleIniTempl<UChar,
  3012. SI_Case<UChar>,SI_ConvertW<UChar> > CSimpleIniCaseW;
  3013. # else
  3014. typedef CSimpleIniTempl<wchar_t,
  3015. SI_NoCase<wchar_t>,SI_ConvertW<wchar_t> > CSimpleIniW;
  3016. typedef CSimpleIniTempl<wchar_t,
  3017. SI_Case<wchar_t>,SI_ConvertW<wchar_t> > CSimpleIniCaseW;
  3018. # endif
  3019. # ifdef _UNICODE
  3020. # define CSimpleIni CSimpleIniW
  3021. # define CSimpleIniCase CSimpleIniCaseW
  3022. # define SI_NEWLINE SI_NEWLINE_W
  3023. # else // !_UNICODE
  3024. # define CSimpleIni CSimpleIniA
  3025. # define CSimpleIniCase CSimpleIniCaseA
  3026. # define SI_NEWLINE SI_NEWLINE_A
  3027. # endif // _UNICODE
  3028. #endif
  3029. #ifdef _MSC_VER
  3030. # pragma warning (pop)
  3031. #endif
  3032. #endif // INCLUDED_SimpleIni_h