minIni.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* minIni - Multi-Platform INI file parser, suitable for embedded systems
  2. *
  3. * Copyright (c) CompuPhase, 2008-2021
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. * use this file except in compliance with the License. You may obtain a copy
  7. * of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations
  15. * under the License.
  16. *
  17. * Version: $Id: minIni.h 53 2015-01-18 13:35:11Z thiadmer.riemersma@gmail.com $
  18. */
  19. #ifndef MININI_H
  20. #define MININI_H
  21. #include "minGlue.h"
  22. #if (defined _UNICODE || defined __UNICODE__ || defined UNICODE) && !defined INI_ANSIONLY
  23. #include <tchar.h>
  24. #define mTCHAR TCHAR
  25. #else
  26. /* force TCHAR to be "char", but only for minIni */
  27. #define mTCHAR char
  28. #endif
  29. #if !defined INI_BUFFERSIZE
  30. #define INI_BUFFERSIZE 512
  31. #endif
  32. #if defined __cplusplus
  33. extern "C" {
  34. #endif
  35. int ini_getbool(const mTCHAR *Section, const mTCHAR *Key, int DefValue, const mTCHAR *Filename);
  36. long ini_getl(const mTCHAR *Section, const mTCHAR *Key, long DefValue, const mTCHAR *Filename);
  37. int ini_gets(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *DefValue, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename);
  38. int ini_getsection(int idx, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename);
  39. int ini_getkey(const mTCHAR *Section, int idx, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename);
  40. int ini_hassection(const mTCHAR *Section, const mTCHAR *Filename);
  41. int ini_haskey(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *Filename);
  42. #if defined INI_REAL
  43. INI_REAL ini_getf(const mTCHAR *Section, const mTCHAR *Key, INI_REAL DefValue, const mTCHAR *Filename);
  44. #endif
  45. #if !defined INI_READONLY
  46. int ini_putl(const mTCHAR *Section, const mTCHAR *Key, long Value, const mTCHAR *Filename);
  47. int ini_puts(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *Value, const mTCHAR *Filename);
  48. #if defined INI_REAL
  49. int ini_putf(const mTCHAR *Section, const mTCHAR *Key, INI_REAL Value, const mTCHAR *Filename);
  50. #endif
  51. #endif /* INI_READONLY */
  52. #if !defined INI_NOBROWSE
  53. typedef int (*INI_CALLBACK)(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *Value, void *UserData);
  54. int ini_browse(INI_CALLBACK Callback, void *UserData, const mTCHAR *Filename);
  55. #endif /* INI_NOBROWSE */
  56. #if defined __cplusplus
  57. }
  58. #endif
  59. #if defined __cplusplus
  60. #if defined __WXWINDOWS__
  61. #include "wxMinIni.h"
  62. #else
  63. #include <string>
  64. /* The C++ class in minIni.h was contributed by Steven Van Ingelgem. */
  65. class minIni
  66. {
  67. public:
  68. minIni(const std::string& filename) : iniFilename(filename)
  69. { }
  70. bool getbool(const std::string& Section, const std::string& Key, bool DefValue=false) const
  71. { return ini_getbool(Section.c_str(), Key.c_str(), int(DefValue), iniFilename.c_str()) != 0; }
  72. long getl(const std::string& Section, const std::string& Key, long DefValue=0) const
  73. { return ini_getl(Section.c_str(), Key.c_str(), DefValue, iniFilename.c_str()); }
  74. int geti(const std::string& Section, const std::string& Key, int DefValue=0) const
  75. { return static_cast<int>(this->getl(Section, Key, long(DefValue))); }
  76. std::string gets(const std::string& Section, const std::string& Key, const std::string& DefValue="") const
  77. {
  78. char buffer[INI_BUFFERSIZE];
  79. ini_gets(Section.c_str(), Key.c_str(), DefValue.c_str(), buffer, INI_BUFFERSIZE, iniFilename.c_str());
  80. return buffer;
  81. }
  82. std::string getsection(int idx) const
  83. {
  84. char buffer[INI_BUFFERSIZE];
  85. ini_getsection(idx, buffer, INI_BUFFERSIZE, iniFilename.c_str());
  86. return buffer;
  87. }
  88. std::string getkey(const std::string& Section, int idx) const
  89. {
  90. char buffer[INI_BUFFERSIZE];
  91. ini_getkey(Section.c_str(), idx, buffer, INI_BUFFERSIZE, iniFilename.c_str());
  92. return buffer;
  93. }
  94. bool hassection(const std::string& Section) const
  95. { return ini_hassection(Section.c_str(), iniFilename.c_str()) != 0; }
  96. bool haskey(const std::string& Section, const std::string& Key) const
  97. { return ini_haskey(Section.c_str(), Key.c_str(), iniFilename.c_str()) != 0; }
  98. #if defined INI_REAL
  99. INI_REAL getf(const std::string& Section, const std::string& Key, INI_REAL DefValue=0) const
  100. { return ini_getf(Section.c_str(), Key.c_str(), DefValue, iniFilename.c_str()); }
  101. #endif
  102. #if ! defined INI_READONLY
  103. bool put(const std::string& Section, const std::string& Key, long Value)
  104. { return ini_putl(Section.c_str(), Key.c_str(), Value, iniFilename.c_str()) != 0; }
  105. bool put(const std::string& Section, const std::string& Key, int Value)
  106. { return ini_putl(Section.c_str(), Key.c_str(), (long)Value, iniFilename.c_str()) != 0; }
  107. bool put(const std::string& Section, const std::string& Key, bool Value)
  108. { return ini_putl(Section.c_str(), Key.c_str(), (long)Value, iniFilename.c_str()) != 0; }
  109. bool put(const std::string& Section, const std::string& Key, const std::string& Value)
  110. { return ini_puts(Section.c_str(), Key.c_str(), Value.c_str(), iniFilename.c_str()) != 0; }
  111. bool put(const std::string& Section, const std::string& Key, const char* Value)
  112. { return ini_puts(Section.c_str(), Key.c_str(), Value, iniFilename.c_str()) != 0; }
  113. #if defined INI_REAL
  114. bool put(const std::string& Section, const std::string& Key, INI_REAL Value)
  115. { return ini_putf(Section.c_str(), Key.c_str(), Value, iniFilename.c_str()) != 0; }
  116. #endif
  117. bool del(const std::string& Section, const std::string& Key)
  118. { return ini_puts(Section.c_str(), Key.c_str(), 0, iniFilename.c_str()) != 0; }
  119. bool del(const std::string& Section)
  120. { return ini_puts(Section.c_str(), 0, 0, iniFilename.c_str()) != 0; }
  121. #endif
  122. #if !defined INI_NOBROWSE
  123. bool browse(INI_CALLBACK Callback, void *UserData) const
  124. { return ini_browse(Callback, UserData, iniFilename.c_str()) != 0; }
  125. #endif
  126. private:
  127. std::string iniFilename;
  128. };
  129. #endif /* __WXWINDOWS__ */
  130. #endif /* __cplusplus */
  131. #endif /* MININI_H */