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.

456 lines
18 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. /* File: inicm.c */
  4. /**************************************************************************
  5. ** Install: .INI file commands.
  6. **************************************************************************/
  7. extern HWND hwndFrame;
  8. /*
  9. ** Purpose:
  10. ** To Create a section in the given ini file. If the file does not
  11. ** already exist it will be created. If the section already exists it
  12. ** may or may not be erased (including key/value pairs) based upon the
  13. ** value of the cmo parameter (see below).
  14. ** Arguments:
  15. ** szFile: a non-Null zero terminated string containing the file name
  16. ** for the ini file. This must either be a fully qualified path
  17. ** or the string "WIN.INI" to signify the Windows' file.
  18. ** szSect: a zero terminated string containing the name of the section
  19. ** to be created.
  20. ** cmo: valid command options:
  21. ** cmoNone: no effect
  22. ** cmoOverwrite: Causes the entire section (including key/value
  23. ** pairs) to be erased before creating the section again (without
  24. ** any key/value pairs).
  25. ** cmoVital: causes the Vital command handler to be called
  26. ** if the function fails for any reason.
  27. ** Returns:
  28. ** fTrue if the function succeeds, fFalse otherwise.
  29. **
  30. ****************************************************************************/
  31. BOOL APIENTRY FCreateIniSection(SZ szFile, SZ szSect, CMO cmo)
  32. {
  33. EERC eerc;
  34. BOOL fVital = cmo & cmoVital;
  35. ChkArg(szFile != (SZ)NULL && *szFile != '\0', 1, fFalse);
  36. ChkArg(szSect != (SZ)NULL && *szSect != '\0', 2, fFalse);
  37. PreCondition(FValidPath(szFile) ||
  38. CrcStringCompareI(szFile, "WIN.INI") == crcEqual, fFalse);
  39. if (cmo & cmoOverwrite &&
  40. !FRemoveIniSection(szFile, szSect, cmo))
  41. return(!fVital);
  42. if (CrcStringCompareI(szFile, "WIN.INI") == crcEqual)
  43. {
  44. while (!WriteProfileString(szSect, "", NULL))
  45. if ((eerc = EercErrorHandler(hwndFrame, grcWriteIniValueErr, fVital,
  46. szFile, szSect, "")) != eercRetry)
  47. return(eerc == eercIgnore);
  48. SendMessage((HWND)(-1), WM_WININICHANGE, 0, (LPARAM)szSect);
  49. }
  50. else while (!WritePrivateProfileString(szSect, "", NULL, szFile))
  51. if ((eerc = EercErrorHandler(hwndFrame, grcWriteIniValueErr, fVital,
  52. szFile, szSect, "")) != eercRetry)
  53. return(eerc == eercIgnore);
  54. return(fTrue);
  55. }
  56. /*
  57. ** Purpose:
  58. ** To replace an existing section in an ini file with a new section.
  59. ** Replacing the section will also remove any of the key/value pairs
  60. ** contained in the section being replaced. This function will fail if
  61. ** the section doesn't already exist (unlike FRemoveIniSection).
  62. ** Arguments:
  63. ** szFile: a non-NULL zero terminated string containing the file name
  64. ** for the ini file. This must either be a fully qualified path
  65. ** or the string "WIN.INI" to signify the Windows' file.
  66. ** szSect: a non-NULL zero terminated string containing the name of
  67. ** the section to be replaced.
  68. ** szNewSect: a non-NULL zero terminated string containing the name of
  69. ** the section that will replace the section in szSect.
  70. ** cmo: valid command options:
  71. ** cmoNone: no effect
  72. ** cmoVital: causes the Vital command handler to be called
  73. ** if the function fails for any reason.
  74. ** Notes: This function is unable to distinguish between a section with no
  75. ** keys and a section that does not exist.
  76. ** Returns:
  77. ** fTrue if the function succeeds, fFalse otherwise.
  78. **
  79. ****************************************************************************/
  80. BOOL APIENTRY FReplaceIniSection(SZ szFile, SZ szSect, SZ szNewSect,
  81. CMO cmo)
  82. {
  83. EERC eerc;
  84. CHP rgch[10];
  85. BOOL fVital = cmo & cmoVital;
  86. ChkArg(szFile != (SZ)NULL, 1, fFalse);
  87. ChkArg(szSect != (SZ)NULL, 2, fFalse);
  88. ChkArg(szNewSect != (SZ)NULL, 3, fFalse);
  89. PreCondition(FValidPath(szFile) ||
  90. CrcStringCompareI(szFile, "WIN.INI") == crcEqual, fFalse);
  91. if (CrcStringCompareI(szFile, "WIN.INI") == crcEqual)
  92. {
  93. while (GetProfileString(szSect, NULL, "a", rgch, 10) == 1)
  94. if ((eerc = EercErrorHandler(hwndFrame, grcReplaceIniValueErr,
  95. fVital, szFile, szSect, "")) != eercRetry)
  96. return(eerc == eercIgnore);
  97. }
  98. else while (GetPrivateProfileString(szSect, NULL, "a", rgch,10,szFile) == 1)
  99. if ((eerc = EercErrorHandler(hwndFrame, grcReplaceIniValueErr, fVital,
  100. szFile, szSect, "")) != eercRetry)
  101. return(eerc == eercIgnore);
  102. if (!FRemoveIniSection(szFile, szSect, cmo) ||
  103. !FCreateIniSection(szFile, szNewSect, cmo))
  104. return(!fVital);
  105. return(fTrue);
  106. }
  107. /*
  108. ** Purpose:
  109. ** To remove a section (including all key/value pairs in the section)
  110. ** from an ini file. No attempt is made to determine if the section
  111. ** already exists or not.
  112. ** Arguments:
  113. ** szFile: a non-NULL zero terminated string containing the file name
  114. ** for the ini file. This must either be a fully qualified path
  115. ** or the string "WIN.INI" to signify the Windows' file.
  116. ** szSect: a non-NULL zero terminated string containing the name of
  117. ** the section to be removed.
  118. ** cmo: valid command options:
  119. ** cmoNone: no effect
  120. ** cmoVital: causes the Vital command handler to be called
  121. ** if the function fails for any reason.
  122. ** Returns:
  123. ** fTrue if the function succeeds, fFalse otherwise.
  124. **
  125. ****************************************************************************/
  126. BOOL APIENTRY FRemoveIniSection(SZ szFile, SZ szSect, CMO cmo)
  127. {
  128. EERC eerc;
  129. BOOL fVital = cmo & cmoVital;
  130. ChkArg(szFile != (SZ)NULL, 1, fFalse);
  131. ChkArg(szSect != (SZ)NULL, 2, fFalse);
  132. PreCondition(FValidPath(szFile) ||
  133. CrcStringCompareI(szFile, "WIN.INI") == crcEqual, fFalse);
  134. if (CrcStringCompareI(szFile, "WIN.INI") == crcEqual)
  135. {
  136. while (!WriteProfileString(szSect, NULL, NULL))
  137. if ((eerc = EercErrorHandler(hwndFrame, grcWriteIniValueErr, fVital,
  138. szFile, szSect, "")) != eercRetry)
  139. return(eerc == eercIgnore);
  140. SendMessage((HWND)(-1), WM_WININICHANGE, 0, (LPARAM)szSect);
  141. }
  142. else while (!WritePrivateProfileString(szSect, NULL, NULL, szFile))
  143. if ((eerc = EercErrorHandler(hwndFrame, grcWriteIniValueErr, fVital,
  144. szFile, szSect, "")) != eercRetry)
  145. return(eerc == eercIgnore);
  146. return(fTrue);
  147. }
  148. /*
  149. ** Purpose:
  150. ** To Create a Key (with no value) in the given ini file and section.
  151. ** If the file does not already exist it will be created. If the section
  152. ** does not already exist it will also be created.
  153. ** Arguments:
  154. ** szFile: a non-NULL zero terminated string containing the file name
  155. ** for the ini file. This must either be a fully qualified path
  156. ** or the string "WIN.INI" to signify the Windows' file.
  157. ** szSect: a non-NULL zero terminated string containing the name of the
  158. ** section in which the key will be created.
  159. ** szKey: non-NULL, non-empty name of the key to be created in the
  160. ** section szSect.
  161. ** cmo: valid command options:
  162. ** cmoNone: no effect
  163. ** cmoOverwrite: causes the key (and value if it exists) to be
  164. ** removed if it already exists before creating the key.
  165. ** cmoVital: causes the Vital command handler to be called
  166. ** if the function fails for any reason.
  167. ** Returns:
  168. ** fTrue if the function succeeds, fFalse otherwise.
  169. **
  170. ****************************************************************************/
  171. BOOL APIENTRY FCreateIniKeyNoValue(SZ szFile, SZ szSect, SZ szKey,
  172. CMO cmo)
  173. {
  174. EERC eerc;
  175. CHP rgch[2];
  176. BOOL fVital = cmo & cmoVital;
  177. ChkArg(szFile != (SZ)NULL, 1, fFalse);
  178. ChkArg(szSect != (SZ)NULL, 2, fFalse);
  179. ChkArg(szKey != (SZ)NULL && *szKey != '\0', 3, fFalse);
  180. PreCondition(FValidPath(szFile) ||
  181. CrcStringCompareI(szFile, "WIN.INI") == crcEqual, fFalse);
  182. if (CrcStringCompareI(szFile, "WIN.INI") == crcEqual)
  183. {
  184. if (!(cmo & cmoOverwrite) &&
  185. GetProfileString(szSect, szKey, "", rgch, 2))
  186. return(fTrue);
  187. while (!WriteProfileString(szSect, szKey, ""))
  188. if ((eerc = EercErrorHandler(hwndFrame, grcWriteIniValueErr, fVital,
  189. szFile, szSect, szKey)) != eercRetry)
  190. return(eerc == eercIgnore);
  191. SendMessage((HWND)(-1), WM_WININICHANGE, 0, (LPARAM)szSect);
  192. }
  193. else
  194. {
  195. if (!(cmo & cmoOverwrite) &&
  196. GetPrivateProfileString(szSect, szKey, "", rgch, 2, szFile))
  197. return(fTrue);
  198. while (!WritePrivateProfileString(szSect, szKey, "", szFile))
  199. if ((eerc = EercErrorHandler(hwndFrame, grcWriteIniValueErr, fVital,
  200. szFile, szSect, szKey)) != eercRetry)
  201. return(eerc == eercIgnore);
  202. }
  203. return(fTrue);
  204. }
  205. /*
  206. ** Purpose:
  207. ** To Create a Key/value pair in the given ini file and section.
  208. ** If the file does not already exist it will be created. If the section
  209. ** does not already exist it will also be created.
  210. ** Arguments:
  211. ** szFile: a non-NULL zero terminated string containing the file name
  212. ** for the ini file. This must either be a fully qualified path
  213. ** or the string "WIN.INI" to signify the Windows' file.
  214. ** szSect: a non-NULL zero terminated string containing the name of the
  215. ** section in which the key/value pair will be created.
  216. ** szKey: non-NULL, non-empty name of the key to be created in the
  217. ** section szSect.
  218. ** szValue: the name of the value to be created in conjunction with the
  219. ** key szKey in the section szSect.
  220. ** cmo: valid command options:
  221. ** cmoNone: no effect
  222. ** cmoOverwrite: causes the key/value pair to be removed if it
  223. ** already exists before creating the key.
  224. ** cmoVital: causes the Vital command handler to be called
  225. ** if the function fails for any reason.
  226. ** Returns:
  227. ** fTrue if the function succeeds, fFalse otherwise.
  228. **
  229. ****************************************************************************/
  230. BOOL APIENTRY FCreateIniKeyValue(SZ szFile, SZ szSect, SZ szKey,
  231. SZ szValue, CMO cmo)
  232. {
  233. EERC eerc;
  234. CHP rgch[2];
  235. BOOL fVital = cmo & cmoVital;
  236. ChkArg(szFile != (SZ)NULL, 1, fFalse);
  237. ChkArg(szSect != (SZ)NULL, 2, fFalse);
  238. ChkArg(szKey != (SZ)NULL && *szKey != '\0', 3, fFalse);
  239. ChkArg(szValue != (SZ)NULL, 4, fFalse);
  240. PreCondition(FValidPath(szFile) ||
  241. CrcStringCompareI(szFile, "WIN.INI") == crcEqual, fFalse);
  242. if (CrcStringCompareI(szFile, "WIN.INI") == crcEqual)
  243. {
  244. if (!(cmo & cmoOverwrite) &&
  245. GetProfileString(szSect, szKey, "", rgch, 2))
  246. return(fTrue);
  247. while (!WriteProfileString(szSect, szKey, szValue))
  248. if ((eerc = EercErrorHandler(hwndFrame, grcWriteIniValueErr, fVital,
  249. szFile, szSect, szKey)) != eercRetry)
  250. return(eerc == eercIgnore);
  251. SendMessage((HWND)(-1), WM_WININICHANGE, 0, (LPARAM)szSect);
  252. }
  253. else
  254. {
  255. if (!(cmo & cmoOverwrite) &&
  256. GetPrivateProfileString(szSect, szKey, "", rgch, 2, szFile))
  257. return(fTrue);
  258. while (!WritePrivateProfileString(szSect, szKey, szValue, szFile))
  259. if ((eerc = EercErrorHandler(hwndFrame, grcWriteIniValueErr, fVital,
  260. szFile, szSect, szKey)) != eercRetry)
  261. return(eerc == eercIgnore);
  262. }
  263. return(fTrue);
  264. }
  265. /*
  266. ** Purpose:
  267. ** To replace an existing key/value pair in an ini file with a new pair.
  268. ** Replacing the key/value pair will also remove any of the key/value pairs
  269. ** contained in the section being replaced.
  270. ** Arguments:
  271. ** szFile: a non-NULL zero terminated string containing the file name
  272. ** for the ini file. This must either be a fully qualified path
  273. ** or the string "WIN.INI" to signify the Windows' file.
  274. ** szSect: a zero terminated string containing the name of the section
  275. ** in which the key/value pair is to be replaced.
  276. ** szKey: non-NULL, non-empty name of the key to
  277. ** be replaced in the section szSect.
  278. ** szValue: a zero terminated string containing the name of the value
  279. ** to be replaced in conjunction with the key szKey in the
  280. ** section szSect.
  281. ** cmo: valid command options:
  282. ** cmoNone: no effect
  283. ** cmoVital: causes the Vital command handler to be called
  284. ** if the function fails for any reason.
  285. ** Returns:
  286. ** fTrue if the function succeeds, fFalse otherwise.
  287. **
  288. ****************************************************************************/
  289. BOOL APIENTRY FReplaceIniKeyValue(SZ szFile, SZ szSect, SZ szKey,
  290. SZ szValue, CMO cmo)
  291. {
  292. BOOL fVital = cmo & cmoVital;
  293. ChkArg(szFile != (SZ)NULL, 1, fFalse);
  294. ChkArg(szSect != (SZ)NULL, 2, fFalse);
  295. ChkArg(szKey != (SZ)NULL && *szKey != '\0', 3, fFalse);
  296. ChkArg(szValue != (SZ)NULL, 4, fFalse);
  297. PreCondition(FValidPath(szFile) ||
  298. CrcStringCompareI(szFile, "WIN.INI") == crcEqual, fFalse);
  299. if (!FCreateIniKeyValue(szFile, szSect, szKey, szValue,
  300. (CMO)(cmoOverwrite | cmo)))
  301. return(!fVital);
  302. return(fTrue);
  303. }
  304. /*
  305. ** Purpose:
  306. ** To append a value to an existing key/value pair in an ini file.
  307. ** Arguments:
  308. ** szFile: a non-NULL zero terminated string containing the file name
  309. ** for the ini file. This must either be a fully qualified path
  310. ** or the string "WIN.INI" to signify the Windows' file.
  311. ** szSect: a zero terminated string containing the name of the section
  312. ** in which the key/value pair is to be appended.
  313. ** szKey: non-NULL, non-empty name of the key
  314. ** whose value will be appended in the section szSect.
  315. ** szValue: a zero terminated string containing the value
  316. ** to be appended in conjunction with the key szKey in the
  317. ** section szSect.
  318. ** cmo: valid command options:
  319. ** cmoNone: no effect
  320. ** cmoVital: causes the Vital command handler to be called
  321. ** if the function fails for any reason.
  322. ** Returns:
  323. ** fTrue if the function succeeds, fFalse otherwise.
  324. **
  325. ****************************************************************************/
  326. BOOL APIENTRY FAppendIniKeyValue(SZ szFile, SZ szSect, SZ szKey,
  327. SZ szValue, CMO cmo)
  328. {
  329. CHP rgch[256];
  330. BOOL fVital = cmo & cmoVital;
  331. ChkArg(szFile != (SZ)NULL, 1, fFalse);
  332. ChkArg(szSect != (SZ)NULL, 2, fFalse);
  333. ChkArg(szKey != (SZ)NULL && *szKey != '\0', 3, fFalse);
  334. ChkArg(szValue != (SZ)NULL, 4, fFalse);
  335. PreCondition(FValidPath(szFile) ||
  336. CrcStringCompareI(szFile, "WIN.INI") == crcEqual, fFalse);
  337. if (CrcStringCompareI(szFile, "WIN.INI") == crcEqual)
  338. GetProfileString(szSect, szKey, "", rgch, 255);
  339. else
  340. GetPrivateProfileString(szSect, szKey, "", rgch, 255, szFile);
  341. if (rgch[0] != '\0')
  342. {
  343. if (strlen(rgch) + strlen(szValue) + 1 >= 255)
  344. {
  345. EvalAssert(EercErrorHandler(hwndFrame, grcIniValueTooLongErr,
  346. fVital, 0, 0, 0) == eercAbort);
  347. return(!fVital);
  348. }
  349. EvalAssert(SzStrCat((LPSTR)rgch, " ") == (LPSTR)rgch);
  350. }
  351. EvalAssert(SzStrCat((LPSTR)rgch, szValue) == (LPSTR)rgch);
  352. if (!FCreateIniKeyValue(szFile, szSect, szKey, rgch,
  353. (CMO)(cmoOverwrite | cmo)))
  354. return(!(cmo & cmoVital));
  355. return(fTrue);
  356. }
  357. /*
  358. ** Purpose:
  359. ** To remove a key/value pair from a section in an ini file.
  360. ** No attempt is made to determine if the key already exists.
  361. ** Arguments:
  362. ** szFile: a non-NULL zero terminated string containing the file name
  363. ** for the ini file. This must either be a fully qualified path
  364. ** or the string "WIN.INI" to signify the Windows' file.
  365. ** szSect: a zero terminated string containing the name of the section
  366. ** to be from which the key/value pair is to be removed.
  367. ** szKey: non-NULL, non-empty key of the key/value pair to be
  368. ** removed from the section szSect.
  369. ** cmo: valid command options:
  370. ** cmoNone: no effect
  371. ** cmoVital: causes the Vital command handler to be called
  372. ** if the function fails for any reason.
  373. ** Returns:
  374. ** fTrue if the function succeeds, fFalse otherwise.
  375. **
  376. ****************************************************************************/
  377. BOOL APIENTRY FRemoveIniKey(SZ szFile, SZ szSect, SZ szKey, CMO cmo)
  378. {
  379. EERC eerc;
  380. BOOL fVital = cmo & cmoVital;
  381. ChkArg(szFile != (SZ)NULL, 1, fFalse);
  382. ChkArg(szSect != (SZ)NULL, 2, fFalse);
  383. ChkArg(szKey != (SZ)NULL && *szKey != '\0', 3, fFalse);
  384. PreCondition(FValidPath(szFile) ||
  385. CrcStringCompareI(szFile, "WIN.INI") == crcEqual, fFalse);
  386. if (CrcStringCompareI(szFile, "WIN.INI") == crcEqual)
  387. {
  388. while (!WriteProfileString(szSect, szKey, NULL))
  389. if ((eerc = EercErrorHandler(hwndFrame, grcWriteIniValueErr, fVital,
  390. szFile, szSect, szKey)) != eercRetry)
  391. return(eerc == eercIgnore);
  392. SendMessage((HWND)(-1), WM_WININICHANGE, 0, (LPARAM)szSect);
  393. }
  394. else while (!WritePrivateProfileString(szSect, szKey, NULL, szFile))
  395. if ((eerc = EercErrorHandler(hwndFrame, grcWriteIniValueErr, fVital,
  396. szFile, szSect, szKey)) != eercRetry)
  397. return(eerc == eercIgnore);
  398. return(fTrue);
  399. }