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.

194 lines
5.0 KiB

  1. /****************************************************************************\
  2. INIAPI.C / Common Routines Library
  3. Microsoft Confidential
  4. Copyright (c) Microsoft Corporation 1999
  5. All rights reserved
  6. INI API source file for custom INI APIs used to easily interface with the
  7. private profile APIs and INI files.
  8. 05/01 - Jason Cohen (JCOHEN)
  9. Added this new source file.
  10. \****************************************************************************/
  11. //
  12. // Include file(s)
  13. //
  14. #include "pch.h"
  15. //
  16. // Internal Function Prototype(s):
  17. //
  18. static LPTSTR IniGetStr(LPTSTR lpszIniFile, LPTSTR lpszSection, LPTSTR lpszKey, LPTSTR lpszDefault, BOOL bSection, LPDWORD lpdwSize);
  19. //
  20. // External Function(s):
  21. //
  22. LPTSTR IniGetExpand(LPTSTR lpszIniFile, LPTSTR lpszSection, LPTSTR lpszKey, LPTSTR lpszDefault)
  23. {
  24. LPTSTR lpszString = IniGetStr(lpszIniFile, lpszSection, lpszKey, lpszDefault, FALSE, NULL);
  25. // Make sure we go something from the ini file.
  26. //
  27. if ( lpszString )
  28. {
  29. LPTSTR lpszExpand = AllocateExpand(lpszString);
  30. // If we are able to expand it out, then free our original
  31. // buffer and return the expanded one.
  32. //
  33. if ( lpszExpand )
  34. {
  35. FREE(lpszString);
  36. return lpszExpand;
  37. }
  38. }
  39. return lpszString;
  40. }
  41. LPTSTR IniGetString(LPTSTR lpszIniFile, LPTSTR lpszSection, LPTSTR lpszKey, LPTSTR lpszDefault)
  42. {
  43. return IniGetStr(lpszIniFile, lpszSection, lpszKey, lpszDefault, FALSE, NULL);
  44. }
  45. LPTSTR IniGetSection(LPTSTR lpszIniFile, LPTSTR lpszSection)
  46. {
  47. return IniGetStr(lpszIniFile, lpszSection, NULL, NULL, TRUE, NULL);
  48. }
  49. LPTSTR IniGetStringEx(LPTSTR lpszIniFile, LPTSTR lpszSection, LPTSTR lpszKey, LPTSTR lpszDefault, LPDWORD lpdwSize)
  50. {
  51. return IniGetStr(lpszIniFile, lpszSection, lpszKey, lpszDefault, FALSE, lpdwSize);
  52. }
  53. LPTSTR IniGetSectionEx(LPTSTR lpszIniFile, LPTSTR lpszSection, LPDWORD lpdwSize)
  54. {
  55. return IniGetStr(lpszIniFile, lpszSection, NULL, NULL, TRUE, lpdwSize);
  56. }
  57. BOOL IniSettingExists(LPCTSTR lpszFile, LPCTSTR lpszSection, LPCTSTR lpszKey, LPCTSTR lpszValue)
  58. {
  59. TCHAR szBuffer[256] = NULLSTR;
  60. // Make sure there is an ini file.
  61. //
  62. if ( !(lpszFile && *lpszFile) )
  63. {
  64. return FALSE;
  65. }
  66. // There also has to be a section.
  67. //
  68. if ( !(lpszSection && *lpszSection) )
  69. {
  70. return FileExists(lpszFile);
  71. }
  72. // See if they are checking for a key, or just the section.
  73. //
  74. if ( lpszKey && *lpszKey )
  75. {
  76. // Make sure the key exists.
  77. //
  78. GetPrivateProfileString(lpszSection, lpszKey, NULLSTR, szBuffer, AS(szBuffer), lpszFile);
  79. // The may want also check to see if the key is a particular value.
  80. //
  81. if ( lpszValue && *lpszValue )
  82. {
  83. return ( lstrcmpi(szBuffer, lpszValue) == 0 );
  84. }
  85. }
  86. else
  87. {
  88. // No key specified, so we just check for the entire section.
  89. //
  90. GetPrivateProfileSection(lpszSection, szBuffer, AS(szBuffer), lpszFile);
  91. }
  92. return ( NULLCHR != szBuffer[0] );
  93. }
  94. //
  95. // Internal Function(s):
  96. //
  97. static LPTSTR IniGetStr(LPTSTR lpszIniFile, LPTSTR lpszSection, LPTSTR lpszKey, LPTSTR lpszDefault, BOOL bSection, LPDWORD lpdwSize)
  98. {
  99. LPTSTR lpszRet = NULL;
  100. DWORD dwChars = 128,
  101. dwExtra = bSection ? 2 : 1,
  102. dwReturn;
  103. // Get the string from the INI file.
  104. //
  105. do
  106. {
  107. // Start with 256 characters, doubling each time.
  108. //
  109. dwChars *= 2;
  110. // Free the previous buffer, if there was one.
  111. //
  112. if ( lpszRet )
  113. {
  114. // FREE() macro resets pointer to NULL.
  115. //
  116. FREE(lpszRet);
  117. }
  118. // Allocate a new buffer.
  119. //
  120. if ( lpszRet = (LPTSTR) MALLOC(dwChars * sizeof(TCHAR)) )
  121. {
  122. if ( bSection )
  123. {
  124. dwReturn = GetPrivateProfileSection(lpszSection, lpszRet, dwChars, lpszIniFile);
  125. }
  126. else
  127. {
  128. dwReturn = GetPrivateProfileString(lpszSection, lpszKey, lpszDefault ? lpszDefault : NULLSTR, lpszRet, dwChars, lpszIniFile);
  129. }
  130. }
  131. else
  132. {
  133. dwReturn = 0;
  134. }
  135. }
  136. while ( dwReturn >= (dwChars - dwExtra) );
  137. // If the don't want anything for the default value, we will always
  138. // free the string and pass back NULL if there was nothing returned by
  139. // the private profile API.
  140. //
  141. if ( ( NULL == lpszDefault ) &&
  142. ( lpszRet ) &&
  143. ( 0 == dwReturn ) )
  144. {
  145. // FREE() macro resets pointer to NULL.
  146. //
  147. FREE(lpszRet);
  148. }
  149. // See if we need to return the size of the buffer allocated.
  150. //
  151. if ( lpszRet && lpdwSize )
  152. {
  153. *lpdwSize = dwChars;
  154. }
  155. // Return the string, will be NULL if we didn't allocate anything.
  156. //
  157. return lpszRet;
  158. }