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.

244 lines
4.7 KiB

  1. // Copyright (C) 1997 Microsoft Corporation
  2. //
  3. // answerfile reader object
  4. //
  5. // 12-15-97 sburns
  6. #include "headers.hxx"
  7. #include "AnswerFile.hpp"
  8. AnswerFile::AnswerFile(const String& filename_)
  9. :
  10. filename(filename_)
  11. {
  12. LOG_CTOR(AnswerFile);
  13. ASSERT(!filename.empty());
  14. }
  15. AnswerFile::~AnswerFile()
  16. {
  17. LOG_DTOR(AnswerFile);
  18. }
  19. String
  20. AnswerFile::ReadKey(const String& section, const String& key)
  21. {
  22. LOG_FUNCTION2(
  23. AnswerFile::ReadKey,
  24. String::format(
  25. L"Section=%1 Key=%2",
  26. section.c_str(),
  27. key.c_str()));
  28. ASSERT(!section.empty());
  29. ASSERT(!key.empty());
  30. String result =
  31. Win::GetPrivateProfileString(section, key, String(), filename);
  32. // Don't log the value, as it may be a password.
  33. // LOG(L"value=" + result);
  34. return result.strip(String::BOTH);
  35. }
  36. EncodedString
  37. AnswerFile::EncodedReadKey(const String& section, const String& key)
  38. {
  39. LOG_FUNCTION2(
  40. AnswerFile::EncodedReadKey,
  41. String::format(
  42. L"Section=%1 Key=%2",
  43. section.c_str(),
  44. key.c_str()));
  45. ASSERT(!section.empty());
  46. ASSERT(!key.empty());
  47. EncodedString retval;
  48. unsigned bufSize = 1023;
  49. PWSTR buffer = 0;
  50. do
  51. {
  52. buffer = new WCHAR[bufSize + 1];
  53. ::ZeroMemory(buffer, (bufSize + 1) * sizeof(WCHAR));
  54. DWORD result =
  55. ::GetPrivateProfileString(
  56. section.c_str(),
  57. key.c_str(),
  58. L"",
  59. buffer,
  60. bufSize,
  61. filename.c_str());
  62. if (!result)
  63. {
  64. break;
  65. }
  66. // values were found. check to see if they were truncated.
  67. if (result == bufSize - 2)
  68. {
  69. // buffer was too small, so the value was truncated. Resize the
  70. // buffer and try again.
  71. // Since the buffer may have contained passwords, scribble it
  72. // out
  73. ::ZeroMemory(buffer, sizeof(WCHAR) * (bufSize + 1));
  74. delete[] buffer;
  75. bufSize *= 2;
  76. continue;
  77. }
  78. // don't need to trim whitespace, GetPrivateProfileString does that
  79. // for us.
  80. retval.Encode(buffer);
  81. break;
  82. }
  83. while (true);
  84. // Since the buffer may have contained passwords, scribble it
  85. // out
  86. ::ZeroMemory(buffer, sizeof(WCHAR) * (bufSize + 1));
  87. delete[] buffer;
  88. // Don't log the value, as it may be a password.
  89. // LOG(L"value=" + result);
  90. return retval;
  91. }
  92. void
  93. AnswerFile::WriteKey(
  94. const String& section,
  95. const String& key,
  96. const String& value)
  97. {
  98. LOG_FUNCTION2(
  99. AnswerFile::WriteKey,
  100. String::format(
  101. L"Section=%1 Key=%2",
  102. section.c_str(),
  103. key.c_str()));
  104. ASSERT(!section.empty());
  105. ASSERT(!key.empty());
  106. HRESULT hr =
  107. Win::WritePrivateProfileString(section, key, value, filename);
  108. ASSERT(SUCCEEDED(hr));
  109. }
  110. bool
  111. AnswerFile::IsKeyPresent(
  112. const String& section,
  113. const String& key)
  114. {
  115. LOG_FUNCTION2(
  116. AnswerFile::IsKeyPresent,
  117. String::format(
  118. L"Section=%1 Key=%2",
  119. section.c_str(),
  120. key.c_str()));
  121. ASSERT(!section.empty());
  122. ASSERT(!key.empty());
  123. bool present = false;
  124. // our first call is with a large buffer, hoping that it will suffice...
  125. StringList results;
  126. unsigned bufSize = 1023;
  127. PWSTR buffer = 0;
  128. do
  129. {
  130. buffer = new WCHAR[bufSize + 1];
  131. ::ZeroMemory(buffer, (bufSize + 1) * sizeof(WCHAR));
  132. DWORD result =
  133. ::GetPrivateProfileString(
  134. section.c_str(),
  135. 0,
  136. L"default",
  137. buffer,
  138. bufSize,
  139. filename.c_str());
  140. if (!result)
  141. {
  142. break;
  143. }
  144. // values were found. check to see if they were truncated.
  145. if (result == bufSize - 2)
  146. {
  147. // buffer was too small, so the value was truncated. Resize the
  148. // buffer and try again.
  149. // Since the buffer may have contained passwords, scribble it
  150. // out
  151. ::ZeroMemory(buffer, (bufSize + 1) * sizeof(WCHAR));
  152. delete[] buffer;
  153. bufSize *= 2;
  154. continue;
  155. }
  156. // copy out the strings results into list elements
  157. PWSTR p = buffer;
  158. while (*p)
  159. {
  160. results.push_back(p);
  161. p += wcslen(p) + 1;
  162. }
  163. break;
  164. }
  165. //lint -e506 ok that this looks like "loop forever"
  166. while (true);
  167. // Since the buffer may have contained passwords, scribble it
  168. // out
  169. ::ZeroMemory(buffer, (bufSize + 1) * sizeof(WCHAR));
  170. delete[] buffer;
  171. if (std::find(results.begin(), results.end(), key) != results.end())
  172. {
  173. present = true;
  174. }
  175. LOG(present ? L"true" : L"false");
  176. return present;
  177. }