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.

178 lines
5.8 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: util.cpp
  4. //
  5. // Module: CMAK.EXE
  6. //
  7. // Synopsis: CMAK Utility functions
  8. //
  9. // Copyright (c) 2000 Microsoft Corporation
  10. //
  11. // Author: quintinb Created 03/27/00
  12. //
  13. //+----------------------------------------------------------------------------
  14. #include "cmmaster.h"
  15. //+----------------------------------------------------------------------------
  16. //
  17. // Function: GetTunnelDunSettingName
  18. //
  19. // Synopsis: This function retrieves the name of the Tunnel DUN setting. If
  20. // the tunnel dun setting key isn't set then the name of the default
  21. // tunnel DUN setting is returned.
  22. //
  23. // Arguments: LPCTSTR pszCmsFile - full path to the cms file to get the name from
  24. // LPCTSTR pszLongServiceName - long service name of the profile
  25. // LPTSTR pszTunnelDunName - buffer to return the tunnel dun name in
  26. // UINT uNumChars - number of characters in the output buffer
  27. //
  28. // History: quintinb Created 03/27/00
  29. //
  30. //+----------------------------------------------------------------------------
  31. int GetTunnelDunSettingName(LPCTSTR pszCmsFile, LPCTSTR pszLongServiceName, LPTSTR pszTunnelDunName, UINT uNumChars)
  32. {
  33. int iReturn;
  34. if (pszCmsFile && pszLongServiceName && pszTunnelDunName && uNumChars)
  35. {
  36. pszTunnelDunName[0] = TEXT('\0');
  37. iReturn = GetPrivateProfileString(c_pszCmSection, c_pszCmEntryTunnelDun, TEXT(""), pszTunnelDunName, uNumChars, pszCmsFile); //lint !e534
  38. if (TEXT('\0') == pszTunnelDunName[0])
  39. {
  40. MYVERIFY(uNumChars > (UINT)wsprintf(pszTunnelDunName, TEXT("%s %s"), pszLongServiceName, c_pszCmEntryTunnelPrimary));
  41. iReturn = lstrlen(pszTunnelDunName);
  42. }
  43. }
  44. else
  45. {
  46. iReturn = 0;
  47. CMASSERTMSG(FALSE, TEXT("GetTunnelDunSettingName -- invalid parameter."));
  48. }
  49. return iReturn;
  50. }
  51. //+----------------------------------------------------------------------------
  52. //
  53. // Function: GetDefaultDunSettingName
  54. //
  55. // Synopsis: This function retrieves the name of the default DUN setting. If
  56. // the default dun setting key isn't set then the default name of the
  57. // default DUN setting is returned.
  58. //
  59. // Arguments: LPCTSTR pszCmsFile - full path to the cms file to get the name from
  60. // LPCTSTR pszLongServiceName - long service name of the profile
  61. // LPTSTR pszDefaultDunName - buffer to return the default dun name in
  62. // UINT uNumChars - number of characters in the output buffer
  63. //
  64. // History: quintinb Created 03/27/00
  65. //
  66. //+----------------------------------------------------------------------------
  67. int GetDefaultDunSettingName(LPCTSTR pszCmsFile, LPCTSTR pszLongServiceName, LPTSTR pszDefaultDunName, UINT uNumChars)
  68. {
  69. int iReturn;
  70. if (pszCmsFile && pszLongServiceName && pszDefaultDunName && uNumChars)
  71. {
  72. pszDefaultDunName[0] = TEXT('\0');
  73. iReturn = GetPrivateProfileString(c_pszCmSection, c_pszCmEntryDun, TEXT(""), pszDefaultDunName, uNumChars, pszCmsFile); //lint !e534
  74. if (TEXT('\0') == pszDefaultDunName[0])
  75. {
  76. lstrcpyn(pszDefaultDunName, pszLongServiceName, uNumChars);
  77. iReturn = lstrlen(pszDefaultDunName);
  78. }
  79. }
  80. else
  81. {
  82. iReturn = 0;
  83. CMASSERTMSG(FALSE, TEXT("GetDefaultDunSettingName -- invalid parameter."));
  84. }
  85. return iReturn;
  86. }
  87. //+----------------------------------------------------------------------------
  88. //
  89. // Function: GetPrivateProfileSectionWithAlloc
  90. //
  91. // Synopsis: This function returns the section requested just as
  92. // GetPrivateProfileSection does, but it automatically sizes the buffer
  93. // and allocates it for the caller. The caller is responsible for
  94. // freeing the returned buffer.
  95. //
  96. // Arguments: LPCTSTR pszSection - section to get
  97. // LPCTSTR pszFile - file to get it from
  98. //
  99. // Returns: LPTSTR -- requested section or NULL on an error
  100. //
  101. //
  102. // History: quintinb Created 10/28/00
  103. //
  104. //+----------------------------------------------------------------------------
  105. LPTSTR GetPrivateProfileSectionWithAlloc(LPCTSTR pszSection, LPCTSTR pszFile)
  106. {
  107. if ((NULL == pszSection) || (NULL == pszFile))
  108. {
  109. CMASSERTMSG(FALSE, TEXT("GetPrivateProfileSectionWithAlloc -- NULL pszSection or pszFile passed"));
  110. return NULL;
  111. }
  112. BOOL bExitLoop = FALSE;
  113. DWORD dwSize = MAX_PATH;
  114. DWORD dwReturnedSize;
  115. LPTSTR pszStringToReturn = (TCHAR*)CmMalloc(dwSize*sizeof(TCHAR));
  116. do
  117. {
  118. MYDBGASSERT(pszStringToReturn);
  119. if (pszStringToReturn)
  120. {
  121. dwReturnedSize = GetPrivateProfileSection(pszSection, pszStringToReturn, dwSize, pszFile);
  122. if (dwReturnedSize == (dwSize - 2))
  123. {
  124. //
  125. // The buffer is too small, lets allocate a bigger one
  126. //
  127. dwSize = 2*dwSize;
  128. if (dwSize > 1024*1024)
  129. {
  130. CMASSERTMSG(FALSE, TEXT("GetPrivateProfileSectionWithAlloc -- Allocation above 1MB, bailing out."));
  131. CmFree(pszStringToReturn);
  132. pszStringToReturn = NULL;
  133. goto exit;
  134. }
  135. pszStringToReturn = (TCHAR*)CmRealloc(pszStringToReturn, dwSize*sizeof(TCHAR));
  136. }
  137. else if (0 == dwReturnedSize)
  138. {
  139. //
  140. // Either we got an error, or more likely there was no data to get
  141. //
  142. CmFree(pszStringToReturn);
  143. pszStringToReturn = NULL;
  144. goto exit;
  145. }
  146. else
  147. {
  148. bExitLoop = TRUE;
  149. }
  150. }
  151. else
  152. {
  153. goto exit;
  154. }
  155. } while (!bExitLoop);
  156. exit:
  157. return pszStringToReturn;
  158. }