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.

221 lines
7.3 KiB

  1. #include "precomp.h"
  2. // this string is appended to key names to preserve their values when grayed out in
  3. // a tri-state dlg control so that the branding dll doesn't process the value
  4. #define LEGACY_SUFFIX TEXT("_Gray")
  5. #define WritePrivateProfileStringEx(pszSection, pszKey, pszValue, pszIns) \
  6. (WritePrivateProfileString((pszSection), (pszKey), \
  7. ((pszValue) != NULL) ? (*(pszValue) != TEXT('\0') ? (pszValue) : NULL) : NULL, (pszIns)))
  8. BOOL InsIsSectionEmpty(LPCTSTR pszSection, LPCTSTR pszFile)
  9. {
  10. TCHAR szBuf[4];
  11. ASSERT(pszSection != NULL);
  12. return (0 == GetPrivateProfileSection(pszSection, szBuf, countof(szBuf), pszFile));
  13. }
  14. BOOL InsIsKeyEmpty(LPCTSTR pszSection, LPCTSTR pszKey, LPCTSTR pszFile)
  15. {
  16. TCHAR szBuf[2];
  17. ASSERT(pszSection != NULL && pszKey != NULL);
  18. return (0 == GetPrivateProfileString(pszSection, pszKey, TEXT(""), szBuf, countof(szBuf), pszFile));
  19. }
  20. BOOL InsKeyExists(LPCTSTR pszSection, LPCTSTR pszKey, LPCTSTR pszFile)
  21. {
  22. TCHAR szBuf[2];
  23. BOOL fResult;
  24. ASSERT(pszSection != NULL && pszKey != NULL);
  25. fResult = TRUE;
  26. GetPrivateProfileString(pszSection, pszKey, TEXT("x"), szBuf, countof(szBuf), pszFile);
  27. if (szBuf[0] == TEXT('x')) {
  28. GetPrivateProfileString(pszSection, pszKey, TEXT("y"), szBuf, countof(szBuf), pszFile);
  29. fResult = (szBuf[0] != TEXT('y'));
  30. ASSERT(!fResult || szBuf[0] == TEXT('x'));
  31. }
  32. return fResult;
  33. }
  34. DWORD InsGetSubstString(LPCTSTR pszSection, LPCTSTR pszKey, LPTSTR pszValue, DWORD cchValue, LPCTSTR pszFile)
  35. {
  36. LPTSTR pszLastPercent;
  37. DWORD cch;
  38. cch = GetPrivateProfileString(pszSection, pszKey, TEXT(""), pszValue, cchValue, pszFile);
  39. if (*pszValue != TEXT('%'))
  40. return cch;
  41. pszLastPercent = StrChr(pszValue+1, TEXT('%'));
  42. if (pszLastPercent != NULL)
  43. *pszLastPercent = TEXT('\0');
  44. return GetPrivateProfileString(IS_STRINGS, pszValue+1, TEXT(""), pszValue, cchValue, pszFile);
  45. }
  46. BOOL InsGetYesNo(LPCTSTR pcszSec, LPCTSTR pcszKey, BOOL fDefault, LPCTSTR pcszInf)
  47. {
  48. TCHAR szBuf[4];
  49. if (GetPrivateProfileString(pcszSec, pcszKey, TEXT(""), szBuf, countof(szBuf), pcszInf) == 0)
  50. return fDefault;
  51. return ((StrCmpI(szBuf, TEXT("Yes")) == 0) || (StrCmpI(szBuf, TEXT("1")) == 0));
  52. }
  53. void SetDlgItemTextFromIns(HWND hDlg, INT nIDDlgText, INT nIDDlgCheck, LPCTSTR lpAppName,
  54. LPCTSTR lpKeyName, LPCTSTR pcszInsFile, LPCTSTR pcszServerFile,
  55. DWORD dwFlags)
  56. {
  57. TCHAR szBuf[INTERNET_MAX_URL_LENGTH];
  58. BOOL fChecked;
  59. InsGetString(lpAppName, lpKeyName, szBuf, countof(szBuf),
  60. pcszInsFile, pcszServerFile, &fChecked);
  61. if (HasFlag(dwFlags, INSIO_TRISTATE))
  62. SetDlgItemTextTriState(hDlg, nIDDlgText, nIDDlgCheck, szBuf, fChecked);
  63. else
  64. SetDlgItemText(hDlg, nIDDlgText, szBuf);
  65. }
  66. DWORD InsGetString(LPCTSTR pszSection, LPCTSTR pszKey, LPTSTR pszValue, DWORD cchValue, LPCTSTR pszIns,
  67. LPCTSTR pszServerFile /* = NULL */, LPBOOL lpfChecked /* = NULL */)
  68. {
  69. BOOL fChecked;
  70. DWORD dwRet = 0;
  71. if (lpfChecked != NULL)
  72. *lpfChecked = FALSE;
  73. if (NULL == pszValue)
  74. return 0;
  75. *pszValue = TEXT('\0');
  76. fChecked = InsKeyExists(pszSection, pszKey, pszIns);
  77. if (fChecked)
  78. dwRet = GetPrivateProfileString(pszSection, pszKey, TEXT(""), pszValue, cchValue, pszIns);
  79. else {
  80. if (NULL != pszServerFile)
  81. dwRet = SHGetIniString(pszSection, pszKey, pszValue, cchValue, pszServerFile);
  82. // legacy format for representing grayed out value in a file
  83. if (TEXT('\0') == *pszValue) {
  84. TCHAR szLegacyKey[MAX_PATH];
  85. StrCpy(szLegacyKey, pszKey);
  86. StrCat(szLegacyKey, LEGACY_SUFFIX);
  87. dwRet = GetPrivateProfileString(pszSection, szLegacyKey, TEXT(""), pszValue, cchValue, pszIns);
  88. }
  89. }
  90. if (lpfChecked != NULL)
  91. *lpfChecked = fChecked;
  92. return dwRet;
  93. }
  94. void WriteDlgItemTextToIns(HWND hDlg, INT nIDDlgText, INT nIDDlgCheck, LPCTSTR lpAppName,
  95. LPCTSTR lpKeyName, LPCTSTR pcszInsFile, LPCTSTR pcszServerFile,
  96. DWORD dwFlags)
  97. {
  98. TCHAR szBuf[INTERNET_MAX_URL_LENGTH];
  99. BOOL fChecked = TRUE;
  100. if (HasFlag(dwFlags, INSIO_TRISTATE))
  101. fChecked = GetDlgItemTextTriState(hDlg, nIDDlgText, nIDDlgCheck, szBuf, countof(szBuf));
  102. else
  103. GetDlgItemText(hDlg, nIDDlgText, szBuf, countof(szBuf));
  104. InsWriteString(lpAppName, lpKeyName, szBuf, pcszInsFile, fChecked, pcszServerFile, dwFlags);
  105. }
  106. void InsWriteString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString,
  107. LPCTSTR pcszInsFile, BOOL fChecked /* = TRUE */, LPCTSTR pcszServerFile /* = NULL */,
  108. DWORD dwFlags /* = 0 */)
  109. {
  110. TCHAR szLegacyKey[MAX_PATH];
  111. BOOL fServerFile = (pcszServerFile != NULL);
  112. BOOL fFileFormat = (fServerFile && HasFlag(dwFlags, INSIO_PATH));
  113. StrCpy(szLegacyKey, lpKeyName);
  114. StrCat(szLegacyKey, LEGACY_SUFFIX);
  115. if (fServerFile && HasFlag(dwFlags, INSIO_SERVERONLY))
  116. {
  117. WritePrivateProfileString(lpAppName, lpKeyName, NULL, pcszInsFile);
  118. WritePrivateProfileString(lpAppName, szLegacyKey, NULL, pcszInsFile);
  119. }
  120. else
  121. {
  122. if (fChecked)
  123. {
  124. // cannot use Ex version for tristate because we must write an empty key
  125. if (HasFlag(dwFlags, INSIO_TRISTATE))
  126. WritePrivateProfileString(lpAppName, lpKeyName,
  127. fFileFormat ? PathFindFileName(lpString) : lpString, pcszInsFile);
  128. else
  129. WritePrivateProfileStringEx(lpAppName, lpKeyName,
  130. fFileFormat ? PathFindFileName(lpString) : lpString, pcszInsFile);
  131. WritePrivateProfileString(lpAppName, szLegacyKey, NULL, pcszInsFile);
  132. }
  133. else
  134. {
  135. WritePrivateProfileString(lpAppName, lpKeyName, NULL, pcszInsFile);
  136. // only write legacy key if we don't have the server-side file
  137. WritePrivateProfileStringEx(lpAppName, szLegacyKey,
  138. (pcszServerFile == NULL) ? lpString : NULL, pcszInsFile);
  139. }
  140. }
  141. if (fServerFile)
  142. SHSetIniString(lpAppName, lpKeyName, lpString, pcszServerFile);
  143. }
  144. void InsWriteInt(LPCTSTR pszSection, LPCTSTR pszKey, int iValue, LPCTSTR pszIns)
  145. {
  146. TCHAR szValue[33];
  147. wnsprintf(szValue, countof(szValue), TEXT("%u"), iValue);
  148. WritePrivateProfileString(pszSection, pszKey, szValue, pszIns);
  149. }
  150. int InsWriteQuotedString(LPCTSTR pszSection, LPCTSTR pszKey, LPCTSTR pszVal, LPCTSTR pszFile)
  151. {
  152. TCHAR szQuotedVal[MAX_STRING + 2],
  153. szSafeBuf[MAX_STRING];
  154. if (!pszVal)
  155. {
  156. WritePrivateProfileString(pszSection, pszKey, NULL, pszFile);
  157. return 0;
  158. }
  159. if (StrLen(pszVal) >= MAX_PATH) {
  160. StrCpyN(szSafeBuf, pszVal, countof(szSafeBuf));
  161. pszVal = szSafeBuf;
  162. }
  163. if (*pszVal != TEXT('\"'))
  164. wnsprintf(szQuotedVal, countof(szQuotedVal), TEXT("\"%s\""), pszVal);
  165. else
  166. StrCpy(szQuotedVal, pszVal);
  167. return WritePrivateProfileString(pszSection, pszKey, szQuotedVal, pszFile);
  168. }