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.

186 lines
6.0 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: gppswithalloc.cpp
  4. //
  5. // Module: CMDIAL32.DLL, CMAK.EXE
  6. //
  7. // Synopsis: GetPrivateProfileStringWithAlloc and AddAllKeysInCurrentSectionToCombo
  8. // are implemented here
  9. //
  10. // Copyright (c) 2000-2001 Microsoft Corporation
  11. //
  12. // Author: quintinb Created 11/01/00
  13. //
  14. //+----------------------------------------------------------------------------
  15. #ifndef _CMUTOA
  16. #ifndef GetPrivateProfileStringU
  17. #ifdef UNICODE
  18. #define GetPrivateProfileStringU GetPrivateProfileStringW
  19. #else
  20. #define GetPrivateProfileStringU GetPrivateProfileStringA
  21. #endif
  22. #endif
  23. #ifndef lstrlenU
  24. #ifdef UNICODE
  25. #define lstrlenU lstrlenW
  26. #else
  27. #define lstrlenU lstrlenA
  28. #endif
  29. #endif
  30. #ifndef SendDlgItemMessageU
  31. #ifdef UNICODE
  32. #define SendDlgItemMessageU SendDlgItemMessageW
  33. #else
  34. #define SendDlgItemMessageU SendDlgItemMessageA
  35. #endif
  36. #endif
  37. #endif
  38. //+---------------------------------------------------------------------------
  39. //
  40. // Function: GetPrivateProfileStringWithAlloc
  41. //
  42. // Synopsis: A wrapper function to encapsulate calling GetPrivateProfileString
  43. // with string allocation code so the caller doesn't have to worry
  44. // about buffer sizing.
  45. //
  46. // Arguments: LPCTSTR pszSection - section to retrieve the key from
  47. // LPCTSTR pszKey - keyname to retrieve the value of
  48. // LPCTSTR pszDefault - default value to use if the key isn't there
  49. // LPCTSTR pszFile - file to get the data from
  50. //
  51. // Returns: LPTSTR - string retrieved from the file or NULL on failure
  52. //
  53. // History: quintinb - Created - 11/01/00
  54. //----------------------------------------------------------------------------
  55. LPTSTR GetPrivateProfileStringWithAlloc(LPCTSTR pszSection, LPCTSTR pszKey, LPCTSTR pszDefault, LPCTSTR pszFile)
  56. {
  57. if ((NULL == pszDefault) || (NULL == pszFile))
  58. {
  59. CMASSERTMSG(FALSE, TEXT("GetPrivateProfileStringWithAlloc -- null default or pszFile passed"));
  60. return NULL;
  61. }
  62. BOOL bExitLoop = FALSE;
  63. DWORD dwSize = MAX_PATH;
  64. DWORD dwReturnedSize;
  65. LPTSTR pszStringToReturn = NULL;
  66. pszStringToReturn = (TCHAR*)CmMalloc(dwSize*sizeof(TCHAR));
  67. do
  68. {
  69. MYDBGASSERT(pszStringToReturn);
  70. if (pszStringToReturn)
  71. {
  72. dwReturnedSize = GetPrivateProfileStringU(pszSection, pszKey, pszDefault, pszStringToReturn,
  73. dwSize, pszFile);
  74. if (((dwReturnedSize == (dwSize - 2)) && ((NULL == pszSection) || (NULL == pszKey))) ||
  75. ((dwReturnedSize == (dwSize - 1)) && ((NULL != pszSection) && (NULL != pszKey))))
  76. {
  77. //
  78. // The buffer is too small, lets allocate a bigger one
  79. //
  80. dwSize = 2*dwSize;
  81. if (dwSize > 1024*1024)
  82. {
  83. CMASSERTMSG(FALSE, TEXT("GetPrivateProfileStringWithAlloc -- Allocation above 1MB, bailing out."));
  84. goto exit;
  85. }
  86. pszStringToReturn = (TCHAR*)CmRealloc(pszStringToReturn, dwSize*sizeof(TCHAR));
  87. }
  88. else if (0 == dwReturnedSize)
  89. {
  90. //
  91. // Either we got an error, or more likely there was no data to get
  92. //
  93. CmFree(pszStringToReturn);
  94. pszStringToReturn = NULL;
  95. goto exit;
  96. }
  97. else
  98. {
  99. bExitLoop = TRUE;
  100. }
  101. }
  102. else
  103. {
  104. goto exit;
  105. }
  106. } while (!bExitLoop);
  107. exit:
  108. return pszStringToReturn;
  109. }
  110. //+---------------------------------------------------------------------------
  111. //
  112. // Function: AddAllKeysInCurrentSectionToCombo
  113. //
  114. // Synopsis: This function reads in all the keynames from the given section
  115. // and file name and populates them into the combo box specified
  116. // by the hDlg and uComboId params.
  117. //
  118. // Arguments: HWND hDlg - window handle of the dialog containing the combobox
  119. // UINT uComboId - control ID of the combobox
  120. // LPCTSTR pszSection - section to get the key names from
  121. // LPCTSTR pszFile - file to pull the key names from
  122. //
  123. // Returns: Nothing
  124. //
  125. // History: quintinb - Created - 11/01/00
  126. //----------------------------------------------------------------------------
  127. void AddAllKeysInCurrentSectionToCombo(HWND hDlg, UINT uComboId, LPCTSTR pszSection, LPCTSTR pszFile)
  128. {
  129. if ((NULL == hDlg) || (0 == uComboId) || (NULL == pszFile))
  130. {
  131. CMASSERTMSG(FALSE, TEXT("AddAllKeysInCurrentSectionToCombo -- Invalid Parameter passed."));
  132. return;
  133. }
  134. //
  135. // Reset the combobox contents
  136. //
  137. SendDlgItemMessageU(hDlg, uComboId, CB_RESETCONTENT, 0, 0); //lint !e534 CB_RESETCONTENT doesn't return anything useful
  138. //
  139. // If the section is NULL, just reset the combobox contents and exit
  140. //
  141. if (NULL != pszSection)
  142. {
  143. //
  144. // Lets get all of the keys in the current section
  145. //
  146. LPTSTR pszAllKeysInCurrentSection = GetPrivateProfileStringWithAlloc(pszSection, NULL, TEXT(""), pszFile);
  147. //
  148. // Now process all of the keys in the current section
  149. //
  150. LPTSTR pszCurrentKey = pszAllKeysInCurrentSection;
  151. while (pszCurrentKey && TEXT('\0') != pszCurrentKey[0])
  152. {
  153. //
  154. // Okay, lets add all of the keys that we found
  155. //
  156. MYVERIFY(CB_ERR!= SendDlgItemMessageU(hDlg, uComboId, CB_ADDSTRING, 0, (LPARAM)pszCurrentKey));
  157. //
  158. // Advance to the next key in pszAllKeysInCurrentSection
  159. //
  160. pszCurrentKey = pszCurrentKey + lstrlenU(pszCurrentKey) + 1;
  161. }
  162. CmFree(pszAllKeysInCurrentSection);
  163. }
  164. }