Leaked source code of windows server 2003
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.

229 lines
5.5 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. bool
  15. IniFile::read(
  16. const _TCHAR *filename,
  17. const _TCHAR *section,
  18. int num_entries,
  19. EntrySpec *entries)
  20. {
  21. WCHAR id_buffer[id_buffer_length];
  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. WCHAR id_buffer[id_buffer_length];
  51. ASSERT(filename != 0);
  52. ASSERT(section != 0);
  53. ASSERT(num_entries > 0);
  54. ASSERT(entries != 0);
  55. for (int index = 0; index < num_entries; index++)
  56. {
  57. ASSERT(entries[index].identifier != 0);
  58. ASSERT(entries[index].pointer != 0);
  59. store_value_in_string(entries[index].type, entries[index].pointer,
  60. id_buffer, sizeof(id_buffer));
  61. BOOL success = WritePrivateProfileString(
  62. section,
  63. entries[index].identifier,
  64. id_buffer,
  65. filename);
  66. if (!success)
  67. {
  68. DWORD err = GetLastError();
  69. PRINT_DEBUG_MSG((
  70. _T("GROVELER: WritePrivateProvileString() failed with error %d\n"), err));
  71. }
  72. }
  73. return true;
  74. }
  75. bool
  76. IniFile::read_string_set(
  77. const _TCHAR *filename,
  78. const _TCHAR *section,
  79. int *num_strings,
  80. _TCHAR ***strings,
  81. _TCHAR **buffer)
  82. {
  83. ASSERT(filename != 0);
  84. ASSERT(section != 0);
  85. ASSERT(num_strings != 0);
  86. ASSERT(strings != 0);
  87. ASSERT(buffer != 0);
  88. *num_strings = 0;
  89. *strings = 0;
  90. *buffer = 0;
  91. DWORD copied_chars;
  92. DWORD buffer_size = 128;
  93. _TCHAR *string_buffer = 0;
  94. do
  95. {
  96. if (string_buffer != 0)
  97. {
  98. delete[] string_buffer;
  99. }
  100. buffer_size *= 2;
  101. if (buffer_size >= (32*1024))
  102. return false;
  103. string_buffer = new _TCHAR[buffer_size];
  104. copied_chars = GetPrivateProfileSection(
  105. section,
  106. string_buffer,
  107. buffer_size,
  108. filename);
  109. } while (copied_chars >= buffer_size - 2);
  110. int num_values = 0;
  111. _TCHAR **string_set = 0;
  112. if (string_buffer[0] != 0)
  113. {
  114. num_values = 1;
  115. for (int index = 0;
  116. string_buffer[index] != 0 || string_buffer[index + 1] != 0;
  117. index++)
  118. {
  119. if (string_buffer[index] == 0)
  120. {
  121. num_values++;
  122. }
  123. }
  124. string_set = new _TCHAR *[num_values];
  125. int buffer_offset = 0;
  126. for (index = 0; index < num_values; index++)
  127. {
  128. while (string_buffer[buffer_offset] != _T('='))
  129. {
  130. if (string_buffer[buffer_offset] == 0) {
  131. delete[] string_set;
  132. delete[] string_buffer;
  133. return false;
  134. }
  135. buffer_offset++;
  136. }
  137. string_set[index] = &string_buffer[buffer_offset + 1];
  138. buffer_offset += _tcslen(string_set[index]) + 2;
  139. }
  140. }
  141. *num_strings = num_values;
  142. *strings = string_set;
  143. *buffer = string_buffer;
  144. return true;
  145. }
  146. void
  147. IniFile::load_string_into_value(
  148. EntryType type,
  149. const _TCHAR *string,
  150. void *value)
  151. {
  152. ASSERT(string != 0);
  153. ASSERT(value != 0);
  154. switch (type)
  155. {
  156. case entry_bool:
  157. *((bool *)value) = _ttoi(string) != 0;
  158. break;
  159. case entry_char:
  160. *((_TCHAR *)value) = string[0];
  161. break;
  162. case entry_int:
  163. (VOID)_stscanf(string, _T("%d"), (int *)value);
  164. break;
  165. case entry_int64:
  166. (VOID)_stscanf(string, _T("%I64d"), (__int64 *)value);
  167. break;
  168. case entry_double:
  169. (VOID)_stscanf(string, _T("%lf"), (double *)value);
  170. break;
  171. default:
  172. ASSERT(false);
  173. }
  174. }
  175. void
  176. IniFile::store_value_in_string(
  177. EntryType type,
  178. void *value,
  179. _TCHAR *string,
  180. int stringLen) //in bytes
  181. {
  182. ASSERT(string != 0);
  183. ASSERT(value != 0);
  184. switch (type)
  185. {
  186. case entry_bool:
  187. (void)StringCbPrintf(string, stringLen, *((bool *)value) ? _T("1") : _T("0"));
  188. break;
  189. case entry_char:
  190. (void)StringCbPrintf(string, stringLen, _T("%c"), *((_TCHAR *)value));
  191. break;
  192. case entry_int:
  193. (void)StringCbPrintf(string, stringLen, _T("%d"), *((int *)value));
  194. break;
  195. case entry_int64:
  196. (void)StringCbPrintf(string, stringLen, _T("%I64d"), *((__int64 *)value));
  197. break;
  198. case entry_double:
  199. (void)StringCbPrintf(string, stringLen, _T("%g"), *((double *)value));
  200. break;
  201. default:
  202. ASSERT(false);
  203. }
  204. }