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.

322 lines
9.7 KiB

  1. // Copyright 1995-1997 Microsoft Corporation. All Rights Reserved.
  2. // itssini.cpp : INI file support for InfoTech Structured Storage files
  3. // need for those pesky pre-compiled headers
  4. #include "header.h"
  5. #include "itssini.h"
  6. // InfoTech Include Files
  7. #include "fsclient.h"
  8. // macros
  9. #define upper(x) ( ((x) >= 'a' && (x) <= 'z') ? (x)-'a'+'A' : (x) )
  10. /*****************************************************************************
  11. * CreateStorageFileBuffer()
  12. *
  13. * Function reads in the specified initialization file inside of an
  14. * InfoTech Structured Storage file and buffers it
  15. *
  16. * ENTRY:
  17. * lpFileName - Pointer to the file that contains the initialization
  18. * file.
  19. * pStorage - Pointer to InfoTech Structured Storage file.
  20. *
  21. * EXIT:
  22. * LPCTSTR - The returned buffer.
  23. *
  24. ****************************************************************************/
  25. LPCSTR CreateStorageFileBuffer( LPCTSTR lpFileName, IStorage* pStorage )
  26. {
  27. LPCSTR lpBuffer = NULL;
  28. if( lpFileName && pStorage ) {
  29. IStream* pStream = NULL;
  30. CWStr cszw(lpFileName);
  31. HRESULT hr;
  32. if( (hr = pStorage->OpenStream(cszw, 0, STGM_READ, 0, &pStream )) == S_OK ) {
  33. STATSTG StatStg;
  34. if ((hr = pStream->Stat(&StatStg, STATFLAG_NONAME)) == S_OK) {
  35. lpBuffer = (LPCSTR) lcMalloc(StatStg.cbSize.LowPart);
  36. ULONG cbRead;
  37. hr = pStream->Read((void*) lpBuffer, StatStg.cbSize.LowPart, &cbRead );
  38. if (FAILED(hr) || cbRead != StatStg.cbSize.LowPart) {
  39. lcFree(lpBuffer);
  40. return NULL;
  41. }
  42. }
  43. }
  44. }
  45. return lpBuffer;
  46. }
  47. /*****************************************************************************
  48. * GetStorageProfileString()
  49. *
  50. * Function reads a string from an initialization file inside of an
  51. * InfoTech Structured Storage file
  52. *
  53. * ENTRY:
  54. * lpSectionName - Identifies the section to search.
  55. * lpKeyName - Identifies the "Key" tp search for.
  56. * lpDefault - Default return string if the read fails.
  57. * lpReturnedString - Destination buffer.
  58. * nSize - Specifies the size, in characters, of the buffer
  59. * pointed to by the lpReturnedString parameter.
  60. * lpFileName - Pointer to the file that contains the initialization
  61. * file.
  62. * pStorage - Pointer to InfoTech Structured Storage file.
  63. *
  64. * EXIT:
  65. * DWORD - The return value is the number of characters copied to the
  66. * buffer, not including the terminating null character.
  67. *
  68. ****************************************************************************/
  69. DWORD GetStorageProfileString( LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPCTSTR lpDefault,
  70. LPSTR lpReturnedString, INT nSize, LPCSTR lpFileName,
  71. IStorage* pStorage )
  72. {
  73. // buffer the initialization file
  74. LPCSTR lpBuffer = CreateStorageFileBuffer( lpFileName, pStorage );
  75. if (!lpBuffer)
  76. return 0;
  77. // get the value of the specified key
  78. DWORD dwReturn = GetBufferProfileString( lpSectionName, lpKeyName, lpDefault,
  79. lpReturnedString, nSize, lpBuffer );
  80. // free the initialization file buffer
  81. DestroyStorageFileBuffer(lpBuffer);
  82. return dwReturn;
  83. }
  84. /*****************************************************************************
  85. * GetStorageProfileInt()
  86. *
  87. * Function reads an unsigned integer from an initialization file inside of an
  88. * InfoTech Structured Storage file
  89. *
  90. * ENTRY:
  91. * lpSectionName - Identifies the section to search.
  92. * lpKeyName - Identifies the "Key" tp search for.
  93. * lpDefault - Default return value if read fails.
  94. * lpFileName - Pointer to the file that contains the initialization
  95. * file.
  96. * pStorage - Pointer to InfoTech Structured Storage file.
  97. *
  98. * EXIT:
  99. * UINT - The return value is the integer equivalent of the string
  100. * following the specified key name in the specified initialization
  101. * file. If the key is not found, the return value is the specified
  102. * default value. If the value of the key is less than zero, the
  103. * return value is zero.
  104. *
  105. ****************************************************************************/
  106. UINT GetStorageProfileInt( LPCTSTR lpSectionName, LPCTSTR lpKeyName,
  107. INT nDefault, LPCTSTR lpFileName,
  108. IStorage* pStorage )
  109. {
  110. // buffer the initialization file
  111. LPCSTR lpBuffer = CreateStorageFileBuffer( lpFileName, pStorage );
  112. if (!lpBuffer)
  113. return 0;
  114. // get the value of the specified key
  115. DWORD dwReturn = GetBufferProfileInt( lpSectionName, lpKeyName, nDefault, lpBuffer );
  116. // free the initialization file buffer
  117. DestroyStorageFileBuffer( (LPCSTR) lpBuffer );
  118. return dwReturn;
  119. }
  120. /*****************************************************************************
  121. * GetBufferProfileString()
  122. *
  123. * Function reads a string from an initialization file buffer
  124. *
  125. * ENTRY:
  126. * lpSectionName - Identifies the section to search.
  127. * lpKeyName - Identifies the "Key" tp search for.
  128. * lpDefault - Default return string if the read fails.
  129. * lpReturnedString - Destination buffer.
  130. * nSize - Specifies the size, in characters, of the buffer
  131. * pointed to by the lpReturnedString parameter.
  132. * lpBuffer - Pointer to the buffer that contains the initialization
  133. * file.
  134. *
  135. * EXIT:
  136. * DWORD - The return value is the number of characters copied to the
  137. * buffer, not including the terminating null character.
  138. *
  139. ****************************************************************************/
  140. DWORD GetBufferProfileString( LPCTSTR lpSectionName, LPCTSTR lpKeyName,
  141. LPCTSTR lpDefault, LPSTR lpReturnedString,
  142. INT nSize, LPCSTR lpBuffer )
  143. {
  144. INT count, i;
  145. LPCSTR lpS;
  146. LPCSTR lpT;
  147. if (!lpBuffer)
  148. goto getdef;
  149. for (lpS = lpBuffer; *lpS; lpS++)
  150. {
  151. while (*lpS && *lpS <= ' ')
  152. lpS++;
  153. if (*lpS == '[')
  154. {
  155. lpS++;
  156. while (*lpS == ' ' || *lpS == '\t')
  157. lpS++;
  158. for (lpT = lpSectionName; *lpT && upper(*lpT) == upper(*lpS); lpT++, lpS++)
  159. ;
  160. while (*lpS == ' ' || *lpS == '\t')
  161. lpS++;
  162. if (!*lpT && *lpS == ']')
  163. goto foundsec;
  164. }
  165. while (*lpS && *lpS != '\r' && *lpS != '\n')
  166. lpS++;
  167. // break out once we reach the end
  168. if( !*lpS )
  169. break;
  170. }
  171. goto getdef;
  172. foundsec:
  173. while (*lpS && *lpS != '\r' && *lpS != '\n')
  174. lpS++;
  175. count = 0;
  176. while (*lpS)
  177. {
  178. while (*lpS && *lpS <= ' ')
  179. lpS++;
  180. if (*lpS == '[')
  181. break;
  182. if (*lpS != ';')
  183. {
  184. if (lpKeyName)
  185. {
  186. for (lpT = lpKeyName; *lpT && upper(*lpT) == upper(*lpS); lpT++, lpS++)
  187. ;
  188. while (*lpS == ' ' || *lpS == '\t')
  189. lpS++;
  190. if (!*lpT && *lpS == '=')
  191. {
  192. lpS++;
  193. while (*lpS == ' ' || *lpS == '\t')
  194. lpS++;
  195. while (count < nSize-1 && *lpS && *lpS != ';' && *lpS != '\r' && *lpS != '\n')
  196. {
  197. *lpReturnedString++ = *lpS++;
  198. count++;
  199. }
  200. *lpReturnedString = 0;
  201. return(count);
  202. }
  203. }
  204. else
  205. {
  206. while (*lpS && *lpS != '=' && *lpS != '\r' && *lpS != '\n')
  207. {
  208. if (count >= nSize-3)
  209. {
  210. *lpReturnedString++ = 0;
  211. *lpReturnedString++ = 0;
  212. return(count);
  213. }
  214. *lpReturnedString++ = *lpS++;
  215. count++;
  216. }
  217. *lpReturnedString++ = 0;
  218. count++;
  219. }
  220. }
  221. while (*lpS && *lpS != '\r' && *lpS != '\n')
  222. lpS++;
  223. }
  224. if (!lpKeyName)
  225. {
  226. *lpReturnedString++ = 0;
  227. return(count);
  228. }
  229. getdef:
  230. count = lstrlen(lpDefault);
  231. if (nSize < count)
  232. count = nSize-1;
  233. for (i = 0; i < count; i++)
  234. lpReturnedString[i] = lpDefault[i];
  235. lpReturnedString[i] = 0;
  236. return(count);
  237. }
  238. /*****************************************************************************
  239. * GetBufferProfileInt()
  240. *
  241. * Function reads an unsigned integer from an initialization file buffer
  242. *
  243. * ENTRY:
  244. * lpSectionName - Identifies the section to search.
  245. * lpKeyName - Identifies the "Key" tp search for.
  246. * lpDefault - Default return value if read fails.
  247. * lpBuffer - Pointer to the buffer that contains the initialization
  248. * file.
  249. *
  250. * EXIT:
  251. * UINT - The return value is the integer equivalent of the string
  252. * following the specified key name in the specified initialization
  253. * file. If the key is not found, the return value is the specified
  254. * default value. If the value of the key is less than zero, the
  255. * return value is zero.
  256. *
  257. ****************************************************************************/
  258. UINT GetBufferProfileInt( LPCTSTR lpSectionName, LPCTSTR lpKeyName,
  259. INT nDefault, LPCTSTR lpBuffer )
  260. {
  261. char sz[20];
  262. INT k = 0;
  263. LPSTR lpT;
  264. INT sign = 1;
  265. if (!GetBufferProfileString(lpSectionName,lpKeyName,"",sz,sizeof(sz),lpBuffer))
  266. return(nDefault);
  267. lpT = sz;
  268. while (*lpT && *lpT <= ' ')
  269. lpT++;
  270. if (*lpT == '-')
  271. {
  272. sign = -1;
  273. lpT++;
  274. }
  275. for (; *lpT >= '0' && *lpT <= '9'; lpT++)
  276. k *= 10, k += *lpT - '0';
  277. return(sign * k);
  278. }