prefs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // prefs.h
  19. //
  20. // 12/11/96 JPW Implemented reading and writing of ini files,
  21. // functionallity to get/set variables, and delete/create
  22. // sections and variables.
  23. // 12/16/96 JPW Fixed so it will work with the STL stuff that comes with
  24. // MSVC 4.1 or newer. Also fixed a few psz parameters that
  25. // should have been const's.
  26. //
  27. // 03/28/97 JMI Fixed so this'll work with MSVC 4.2.
  28. //
  29. // 06/29/97 MJR Replaced STL vector with an RSP list. STL is an evil
  30. // entity that should be banished from the face of the earth.
  31. // Whoever suggested we use it should be shot. (Good thing
  32. // I'm the president -- it's against the rules to shoot me.)
  33. //
  34. // Also moved the ranges for RSPiX types into xxxsystem.h.
  35. //
  36. ////////////////////////////////////////////////////////////////////////////////
  37. #ifndef PREFS_H
  38. #define PREFS_H
  39. #include "Blue.h"
  40. #include "prefline.h"
  41. class RPrefs
  42. {
  43. public:
  44. typedef enum ePrefs
  45. {
  46. MaxStrLen = 512
  47. };
  48. private:
  49. FILE* m_pFile; // Currently open file, or NULL if none
  50. short m_sReadOnly; // Read-only flag (1 = read-only, 0 = not)
  51. short m_sErrorStatus; // Error status (non-zero = error, 0 = not)
  52. short m_sModified; // Indicates if ini in memory has been modified
  53. short m_sDidRead; // Indicates if ini file has been read yet
  54. short m_sUseCRLF; // Indicates if ini file is using CR/LF pairs
  55. char *m_pszFileName; // Name of file
  56. char *m_pszFileMode; // Mode file was opened in
  57. RPrefsLineList m_pllLines; // List of lines read from ini file.
  58. // Get iterator to a variable
  59. short GetIteratorToVariable( // Returns 0 if successfull, non-zero otherwise
  60. const char* pszSection, // In: Section name (without brackets)
  61. const char* pszVariable, // In: Variable name
  62. RPrefsLineList::Pointer* i); // Out: iterator to line in list
  63. // Get iterator to a section
  64. short GetIteratorToSection( // Returns 0 if successfull, non-zero otherwise
  65. const char* pszSection, // In: Section name (without brackets)
  66. RPrefsLineList::Pointer* i); // Out: iterator to line in list
  67. public:
  68. // Default (and only) constructor
  69. RPrefs(void);
  70. // Destructor
  71. ~RPrefs();
  72. // Open preference file.
  73. //
  74. // Two overloaded versions of this function exist. The first allows the mode
  75. // to be specified using the same values as used by fopen(). The second
  76. // attempts to open the file in read/write mode. If this fails, it attempts
  77. // to open the file in read-only mode. If this fails, too, it attempts to
  78. // create an empty file.
  79. short Open( // Returns 0 if successfull, non-zero otherwise
  80. const char* pszFile, // In: Name of preference file
  81. const char* pszMode); // In: Open mode (same as fopen())
  82. short Open( // Returns 0 if successfull, non-zero otherwise
  83. const char* pszFile); // In: Name of preference file
  84. // Read ini file into list of lines.
  85. short Read();
  86. // Write ini file back out to disk.
  87. short Write();
  88. // Close current preference file
  89. short Close();
  90. // Check if any I/O errors have occurred. This includes the most recent Open()
  91. // and any I/O errors since then. Once an error occurs, the only way to clear
  92. // it is via a successfull Open().
  93. short IsError(void) // Returns non-zero if error occurred, 0 otherwise
  94. { return m_sErrorStatus; }
  95. // Check if file is marked as "read only". If so, then the file cannot be
  96. // modified or written to. If this is called at a time when there is no open
  97. // file, then the return value will be 0.
  98. short IsReadOnly(void) // Returns non-zero if read-only, 0 otherwise
  99. { return m_sReadOnly; }
  100. // Check if copy of file in memory has been modified
  101. short IsModified()
  102. { return m_sModified; }
  103. // Unset modified status
  104. void SetNotModified()
  105. { m_sModified = 0; }
  106. // Delete the specified variable in the specified section
  107. short DeleteVariable( // Returns 0 if successfull, non-zero otherwise
  108. const char* pszSection, // In: Section name (without brackets)
  109. const char* pszVariable); // In: Variable name
  110. // Delete the specified section
  111. short DeleteSection( // Returns 0 if successfull, non-zero otherwise
  112. const char* pszSection); // In: Section name (without brackets)
  113. // Set specified entry in the specified section to the specified value.
  114. //
  115. // Several overloaded versions of this function exist, each dealing with
  116. // different types, ranging from a string to all the basic integer and
  117. // floating-point types.
  118. //
  119. // If the section does not exist, it will be created. Likewise, if the entry
  120. // does not exist, it will be created.
  121. //
  122. // If an I/O error occurs, the function returns a non-zero negative value.
  123. short SetVal( // Returns 0 if successfull, non-zero otherwise
  124. const char* pszSection, // In: Section name (without brackets)
  125. const char* pszVariable, // In: Variable name
  126. const char* pszValue); // In: Value
  127. short SetVal( // Returns 0 if successfull, non-zero otherwise
  128. const char* pszSection, // In: Section name (without brackets)
  129. const char* pszVariable, // In: Variable name
  130. S8 s8Value); // In: Value
  131. short SetVal( // Returns 0 if successfull, non-zero otherwise
  132. const char* pszSection, // In: Section name (without brackets)
  133. const char* pszVariable, // In: Variable name
  134. U8 u8Value); // In: Value
  135. short SetVal( // Returns 0 if successfull, non-zero otherwise
  136. const char* pszSection, // In: Section name (without brackets)
  137. const char* pszVariable, // In: Variable name
  138. S16 s16Value); // In: Value
  139. short SetVal( // Returns 0 if successfull, non-zero otherwise
  140. const char* pszSection, // In: Section name (without brackets)
  141. const char* pszVariable, // In: Variable name
  142. U16 u16Value); // In: Value
  143. short SetVal( // Returns 0 if successfull, non-zero otherwise
  144. const char* pszSection, // In: Section name (without brackets)
  145. const char* pszVariable, // In: Variable name
  146. S32 s32Value); // In: Value
  147. short SetVal( // Returns 0 if successfull, non-zero otherwise
  148. const char* pszSection, // In: Section name (without brackets)
  149. const char* pszVariable, // In: Variable name
  150. U32 u32Value); // In: Value
  151. short SetVal( // Returns 0 if successfull, non-zero otherwise
  152. const char* pszSection, // In: Section name (without brackets)
  153. const char* pszVariable, // In: Variable name
  154. float fValue); // In: Value
  155. short SetVal( // Returns 0 if successfull, non-zero otherwise
  156. const char* pszSection, // In: Section name (without brackets)
  157. const char* pszVariable, // In: Variable name
  158. double dValue); // In: Value
  159. // Get the value associated with the specified entry in the specified section.
  160. //
  161. // Several overloaded versions of this function exist, each dealing with
  162. // different types, ranging from a string to all the basic integer and
  163. // floating-point types.
  164. //
  165. // If the entry does not exist in the file, then the specified default value
  166. // is used.
  167. //
  168. // If the value cannot be converted to the requested type (invalid characters,
  169. // overflow, etc.) then the value is set to 0 and the function returns a non-
  170. // zero, positive value to indicate an error.
  171. //
  172. // If an I/O error occurs, then the function returns a non-zero, negative
  173. // value to indicate the error.
  174. short GetVal( // Returns 0 if successfull, non-zero otherwise
  175. const char* pszSection, // In: Section name (without brackets)
  176. const char* pszVariable, // In: Variable name
  177. const char* pszDefault, // In: Default value
  178. char* pszValue); // Out: Value returned here
  179. short GetVal( // Returns 0 if successfull, non-zero otherwise
  180. const char* pszSection, // In: Section name (without brackets)
  181. const char* pszVariable, // In: Variable name
  182. S8 s8Default, // In: Default value
  183. S8* s8Value); // Out: Value returned here
  184. short GetVal( // Returns 0 if successfull, non-zero otherwise
  185. const char* pszSection, // In: Section name (without brackets)
  186. const char* pszVariable, // In: Variable name
  187. U8 u8Default, // In: Default value
  188. U8* u8Value); // Out: Value returned here
  189. short GetVal( // Returns 0 if successfull, non-zero otherwise
  190. const char* pszSection, // In: Section name (without brackets)
  191. const char* pszVariable, // In: Variable name
  192. S16 s16Default, // In: Default value
  193. S16* s16Value); // Out: Value returned here
  194. short GetVal( // Returns 0 if successfull, non-zero otherwise
  195. const char* pszSection, // In: Section name (without brackets)
  196. const char* pszVariable, // In: Variable name
  197. U16 u16Default, // In: Default value
  198. U16* u16Value); // Out: Value returned here
  199. short GetVal( // Returns 0 if successfull, non-zero otherwise
  200. const char* pszSection, // In: Section name (without brackets)
  201. const char* pszVariable, // In: Variable name
  202. S32 s32Default, // In: Default value
  203. S32* s32Value); // Out: Value returned here
  204. short GetVal( // Returns 0 if successfull, non-zero otherwise
  205. const char* pszSection, // In: Section name (without brackets)
  206. const char* pszVariable, // In: Variable name
  207. U32 u32Default, // In: Default value
  208. U32* u32Value); // Out: Value returned here
  209. short GetVal( // Returns 0 if successfull, non-zero otherwise
  210. const char* pszSection, // In: Section name (without brackets)
  211. const char* pszVariable, // In: Variable name
  212. float fDefault, // In: Default value
  213. float* fValue); // Out: Value returned here
  214. short GetVal( // Returns 0 if successfull, non-zero otherwise
  215. const char* pszSection, // In: Section name (without brackets)
  216. const char* pszVariable, // In: Variable name
  217. double dDefault, // In: Default value
  218. double* dValue); // Out: Value returned here
  219. short Print(); // Used for error checking
  220. };
  221. #endif //PREFS_H
  222. ////////////////////////////////////////////////////////////////////////////////
  223. // EOF
  224. ////////////////////////////////////////////////////////////////////////////////