Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
4.1 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. inifile.cpp
  5. Abstract:
  6. SIS Groveler initialization file interface
  7. Authors:
  8. John Douceur, 1998
  9. Environment:
  10. User Mode
  11. Revision History:
  12. --*/
  13. #include "all.hxx"
  14. _TCHAR IniFile::id_buffer[id_buffer_length];
  15. bool
  16. IniFile::read(
  17. const _TCHAR *filename,
  18. const _TCHAR *section,
  19. int num_entries,
  20. EntrySpec *entries)
  21. {
  22. ASSERT(filename != 0);
  23. ASSERT(section != 0);
  24. ASSERT(num_entries > 0);
  25. ASSERT(entries != 0);
  26. for (int index = 0; index < num_entries; index++)
  27. {
  28. ASSERT(entries[index].identifier != 0);
  29. ASSERT(entries[index].default_value != 0);
  30. ASSERT(entries[index].pointer != 0);
  31. DWORD num_chars = GetPrivateProfileString(
  32. section,
  33. entries[index].identifier,
  34. entries[index].default_value,
  35. id_buffer,
  36. id_buffer_length,
  37. filename);
  38. load_string_into_value(entries[index].type, id_buffer,
  39. entries[index].pointer);
  40. }
  41. return true;
  42. }
  43. bool
  44. IniFile::overwrite(
  45. const _TCHAR *filename,
  46. const _TCHAR *section,
  47. int num_entries,
  48. EntrySpec *entries)
  49. {
  50. ASSERT(filename != 0);
  51. ASSERT(section != 0);
  52. ASSERT(num_entries > 0);
  53. ASSERT(entries != 0);
  54. for (int index = 0; index < num_entries; index++)
  55. {
  56. ASSERT(entries[index].identifier != 0);
  57. ASSERT(entries[index].pointer != 0);
  58. store_value_in_string(entries[index].type, entries[index].pointer,
  59. id_buffer);
  60. BOOL success = WritePrivateProfileString(
  61. section,
  62. entries[index].identifier,
  63. id_buffer,
  64. filename);
  65. if (!success)
  66. {
  67. DWORD err = GetLastError();
  68. PRINT_DEBUG_MSG((
  69. _T("GROVELER: WritePrivateProvileString() failed with error %d\n"), err));
  70. }
  71. }
  72. return true;
  73. }
  74. bool
  75. IniFile::read_string_set(
  76. const _TCHAR *filename,
  77. const _TCHAR *section,
  78. int *num_strings,
  79. _TCHAR ***strings,
  80. _TCHAR **buffer)
  81. {
  82. ASSERT(filename != 0);
  83. ASSERT(section != 0);
  84. ASSERT(num_strings != 0);
  85. ASSERT(strings != 0);
  86. ASSERT(buffer != 0);
  87. *num_strings = 0;
  88. *strings = 0;
  89. *buffer = 0;
  90. DWORD copied_chars;
  91. DWORD buffer_size = 128;
  92. _TCHAR *string_buffer = 0;
  93. do
  94. {
  95. if (string_buffer != 0)
  96. {
  97. delete[] string_buffer;
  98. }
  99. buffer_size *= 2;
  100. string_buffer = new _TCHAR[buffer_size];
  101. copied_chars = GetPrivateProfileSection(
  102. section,
  103. string_buffer,
  104. buffer_size,
  105. filename);
  106. } while (copied_chars >= buffer_size - 2);
  107. int num_values = 0;
  108. _TCHAR **string_set = 0;
  109. if (string_buffer[0] != 0)
  110. {
  111. num_values = 1;
  112. for (int index = 0;
  113. string_buffer[index] != 0 || string_buffer[index + 1] != 0;
  114. index++)
  115. {
  116. if (string_buffer[index] == 0)
  117. {
  118. num_values++;
  119. }
  120. }
  121. string_set = new _TCHAR *[num_values];
  122. int buffer_offset = 0;
  123. for (index = 0; index < num_values; index++)
  124. {
  125. while (string_buffer[buffer_offset] != _T('='))
  126. {
  127. ASSERT(string_buffer[buffer_offset] != 0);
  128. buffer_offset++;
  129. }
  130. string_set[index] = &string_buffer[buffer_offset + 1];
  131. buffer_offset += _tcslen(string_set[index]) + 2;
  132. }
  133. }
  134. *num_strings = num_values;
  135. *strings = string_set;
  136. *buffer = string_buffer;
  137. return true;
  138. }
  139. void
  140. IniFile::load_string_into_value(
  141. EntryType type,
  142. const _TCHAR *string,
  143. void *value)
  144. {
  145. ASSERT(string != 0);
  146. ASSERT(value != 0);
  147. switch (type)
  148. {
  149. case entry_bool:
  150. *((bool *)value) = _ttoi(string) != 0;
  151. break;
  152. case entry_char:
  153. *((_TCHAR *)value) = string[0];
  154. break;
  155. case entry_int:
  156. _stscanf(string, _T("%d"), (int *)value);
  157. break;
  158. case entry_int64:
  159. _stscanf(string, _T("%I64d"), (__int64 *)value);
  160. break;
  161. case entry_double:
  162. _stscanf(string, _T("%lf"), (double *)value);
  163. break;
  164. default:
  165. ASSERT(false);
  166. }
  167. }
  168. void
  169. IniFile::store_value_in_string(
  170. EntryType type,
  171. void *value,
  172. _TCHAR *string)
  173. {
  174. ASSERT(string != 0);
  175. ASSERT(value != 0);
  176. switch (type)
  177. {
  178. case entry_bool:
  179. _stprintf(string, *((bool *)value) ? _T("1") : _T("0"));
  180. break;
  181. case entry_char:
  182. _stprintf(string, _T("%c"), *((_TCHAR *)value));
  183. break;
  184. case entry_int:
  185. _stprintf(string, _T("%d"), *((int *)value));
  186. break;
  187. case entry_int64:
  188. _stprintf(string, _T("%I64d"), *((__int64 *)value));
  189. break;
  190. case entry_double:
  191. _stprintf(string, _T("%g"), *((double *)value));
  192. break;
  193. default:
  194. ASSERT(false);
  195. }
  196. }