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.

404 lines
13 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: C W I N F . H
  7. //
  8. // Contents: Declaration of class CWInfFile and other related classes
  9. //
  10. // Notes:
  11. //
  12. // Author: kumarp 04/12/97 17:17:27
  13. //
  14. //----------------------------------------------------------------------------
  15. #pragma once
  16. #include "kkstl.h"
  17. //----------------------------------------------------------------------------
  18. // forward declarations and useful typedefs
  19. //----------------------------------------------------------------------------
  20. typedef unsigned __int64 QWORD;
  21. class CWInfContext;
  22. typedef CWInfContext &RCWInfContext;
  23. class CWInfFile;
  24. typedef CWInfFile *PCWInfFile, &RCWInfFile;
  25. class CWInfSection;
  26. typedef CWInfSection *PCWInfSection, &RCWInfSection;
  27. class CWInfKey;
  28. typedef CWInfKey *PCWInfKey, &RCWInfKey;
  29. class CWInfLine;
  30. typedef CWInfLine *PCWInfLine, &RCWInfLine;
  31. // access mode for CWInfFile
  32. enum WInfAccessMode { IAM_Read, IAM_Write };
  33. // search mode for CWInfFile
  34. enum WInfSearchMode { ISM_FromCurrentPosition, ISM_FromBeginning };
  35. typedef list<PCWInfLine> WifLinePtrList;
  36. typedef WifLinePtrList::iterator WifLinePtrListIter;
  37. // ----------------------------------------------------------------------
  38. // Class CWInfContext
  39. //
  40. // Inheritance:
  41. // none
  42. //
  43. // Purpose:
  44. // Stores context within a CWInfFile during reading or writing
  45. //
  46. // Hungarian: wix
  47. // ----------------------------------------------------------------------
  48. class CWInfContext
  49. {
  50. friend class CWInfFile;
  51. public:
  52. CWInfContext() { posSection = 0; posLine = 0; }
  53. private:
  54. WifLinePtrListIter posSection;
  55. WifLinePtrListIter posLine;
  56. };
  57. // ----------------------------------------------------------------------
  58. // Class CWInfContext
  59. //
  60. // Inheritance:
  61. // none
  62. //
  63. // Purpose:
  64. // Allows simultaneous reading from and writing to an INF/INI style file
  65. //
  66. // Hungarian: wif
  67. // ----------------------------------------------------------------------
  68. class CWInfFile
  69. {
  70. public:
  71. CWInfFile();
  72. ~CWInfFile();
  73. BOOL Init();
  74. virtual BOOL Create(IN PCWSTR pszFileName);
  75. virtual BOOL Create(IN FILE *fp);
  76. virtual BOOL Open(IN PCWSTR pszFileName);
  77. virtual BOOL Open(IN FILE *fp);
  78. virtual BOOL Close();
  79. virtual BOOL SaveAs(IN PCWSTR pszFileName);
  80. virtual BOOL SaveAsEx(IN PCWSTR pszFileName); // used by SysPrep
  81. virtual BOOL Flush();
  82. virtual BOOL FlushEx(); // used by SysPrep
  83. virtual PCWSTR FileName() const { return m_strFileName.c_str(); }
  84. virtual const CWInfContext CurrentReadContext() const { return m_ReadContext; }
  85. virtual void SetReadContext(IN RCWInfContext cwic)
  86. { m_ReadContext = cwic; }
  87. virtual const CWInfContext CurrentWriteContext() const { return m_WriteContext; }
  88. virtual void SetWriteContext(IN RCWInfContext cwic)
  89. { m_WriteContext = cwic; }
  90. //Functions for reading
  91. virtual PCWInfSection FindSection(IN PCWSTR pszSectionName,
  92. IN WInfSearchMode wsmMode=ISM_FromBeginning);
  93. virtual void SetCurrentReadSection(IN PCWInfSection pwisSection);
  94. virtual PCWInfSection CurrentReadSection() const;
  95. virtual PCWInfKey FindKey(IN PCWSTR pszKeyName,
  96. IN WInfSearchMode wsmMode=ISM_FromCurrentPosition);
  97. virtual PCWInfKey FirstKey();
  98. virtual PCWInfKey NextKey();
  99. //these functions return the FALSE if value not found
  100. //or if it is in a wrong format
  101. virtual BOOL GetStringArrayValue(IN PCWSTR pszKeyName, OUT TStringArray &saStrings);
  102. virtual BOOL GetStringListValue(IN PCWSTR pszKeyName, OUT TStringList &slList);
  103. virtual BOOL GetStringValue(IN PCWSTR pszKeyName, OUT tstring &strValue);
  104. virtual BOOL GetIntValue(IN PCWSTR pszKeyName, OUT DWORD *pdwValue);
  105. virtual BOOL GetQwordValue(IN PCWSTR pszKeyName, OUT QWORD *pqwValue);
  106. virtual BOOL GetBoolValue(IN PCWSTR pszKeyName, OUT BOOL *pfValue);
  107. //these functions return the default value if value not found
  108. //or if it is in a wrong format
  109. virtual PCWSTR GetStringValue(IN PCWSTR pszKeyName, IN PCWSTR pszDefault);
  110. virtual DWORD GetIntValue(IN PCWSTR pszKeyName, IN DWORD dwDefault);
  111. virtual QWORD GetQwordValue(IN PCWSTR pszKeyName, IN QWORD qwDefault);
  112. virtual BOOL GetBoolValue(IN PCWSTR pszKeyName, IN BOOL fDefault);
  113. //Functions for writing
  114. virtual void GotoEnd();
  115. virtual PCWInfSection AddSectionIfNotPresent(IN PCWSTR pszSectionName);
  116. virtual PCWInfSection AddSection(IN PCWSTR pszSectionName);
  117. virtual void GotoEndOfSection(PCWInfSection section);
  118. virtual PCWInfSection CurrentWriteSection() const;
  119. void RemoveSection(IN PCWSTR szSectionName);
  120. void RemoveSections(IN TStringList& slSections);
  121. virtual PCWInfKey AddKey(IN PCWSTR pszKeyName);
  122. virtual void AddKey(IN PCWSTR pszKeyName, IN PCWSTR Value);
  123. virtual void AddKey(IN PCWSTR pszKeyName, IN DWORD Value);
  124. virtual void AddQwordKey(IN PCWSTR pszKeyName, IN QWORD qwValue);
  125. virtual void AddHexKey(IN PCWSTR pszKeyName, IN DWORD Value);
  126. virtual void AddBoolKey(IN PCWSTR pszKeyName, IN BOOL Value);
  127. virtual void AddKeyV(IN PCWSTR pszKeyName, IN PCWSTR Format, IN ...);
  128. virtual void AddKey(IN PCWSTR pszKeyName, IN PCWSTR Format, IN va_list arglist);
  129. virtual void AddKey(IN PCWSTR pszKeyName, IN const TStringList &slValues);
  130. virtual void AddComment(IN PCWSTR pszComment);
  131. virtual void AddRawLine(IN PCWSTR szText);
  132. virtual void AddRawLines(IN PCWSTR* pszLines, IN DWORD cLines);
  133. protected:
  134. WifLinePtrList *m_plSections, *m_plLines;
  135. CWInfContext m_WriteContext, m_ReadContext;
  136. BOOL AddLine(IN const PCWInfLine ilLine);
  137. virtual void ParseLine(IN PCWSTR pszLine);
  138. private:
  139. tstring m_strFileName;
  140. FILE* m_fp;
  141. };
  142. // ----------------------------------------------------------------------
  143. // Type of a line in a CWInfFile
  144. //
  145. // Hungarian: ilt
  146. // ----------------------------------------------------------------------
  147. enum InfLineTypeEnum { INF_UNKNOWN, INF_SECTION, INF_KEY, INF_COMMENT, INF_BLANK, INF_RAW };
  148. typedef enum InfLineTypeEnum InfLineType;
  149. // ----------------------------------------------------------------------
  150. // Class CWInfLine
  151. //
  152. // Inheritance:
  153. // none
  154. //
  155. // Purpose:
  156. // Represents a line in a CWInfFile
  157. //
  158. // Hungarian: wil
  159. // ----------------------------------------------------------------------
  160. class CWInfLine
  161. {
  162. public:
  163. CWInfLine(InfLineType type) { m_Type = type; }
  164. virtual void GetText(tstring &text) const = 0;
  165. virtual void GetTextEx(tstring &text) const = 0; // used by SysPrep
  166. InfLineType Type() const { return m_Type; }
  167. virtual ~CWInfLine(){};
  168. protected:
  169. InfLineType m_Type;
  170. };
  171. // ----------------------------------------------------------------------
  172. // Class CWInfSection
  173. //
  174. // Inheritance:
  175. // CWInfLine
  176. //
  177. // Purpose:
  178. // Represents a section in a CWInfFile
  179. //
  180. // Hungarian: wis
  181. // ----------------------------------------------------------------------
  182. class CWInfSection : public CWInfLine
  183. {
  184. friend class CWInfFile;
  185. public:
  186. virtual void GetText(tstring &text) const;
  187. virtual void GetTextEx(tstring &text) const; // used by SysPrep
  188. virtual PCWSTR Name() const { return m_Name.c_str(); }
  189. //Functions for reading
  190. virtual PCWInfKey FindKey(IN PCWSTR pszKeyName,
  191. IN WInfSearchMode wsmMode=ISM_FromCurrentPosition);
  192. virtual PCWInfKey FirstKey();
  193. virtual PCWInfKey NextKey();
  194. //these functions return the FALSE if value not found
  195. //or if it is in a wrong format
  196. virtual BOOL GetStringArrayValue(IN PCWSTR pszKeyName, OUT TStringArray &saStrings);
  197. virtual BOOL GetStringListValue(IN PCWSTR pszKeyName, OUT TStringList &slList);
  198. virtual BOOL GetStringValue(IN PCWSTR pszKeyName, OUT tstring &strValue);
  199. virtual BOOL GetIntValue(IN PCWSTR pszKeyName, OUT DWORD *pdwValue);
  200. virtual BOOL GetQwordValue(IN PCWSTR pszKeyName, OUT QWORD *pqwValue);
  201. virtual BOOL GetBoolValue(IN PCWSTR pszKeyName, OUT BOOL *pfValue);
  202. //these functions return the default value if value not found
  203. //or if it is in a wrong format
  204. virtual PCWSTR GetStringValue(IN PCWSTR pszKeyName, IN PCWSTR pszDefault);
  205. virtual DWORD GetIntValue(IN PCWSTR pszKeyName, IN DWORD dwDefault);
  206. virtual QWORD GetQwordValue(IN PCWSTR pszKeyName, IN QWORD qwDefault);
  207. virtual BOOL GetBoolValue(IN PCWSTR pszKeyName, IN BOOL fDefault);
  208. //Functions for writing
  209. virtual void GotoEnd();
  210. virtual PCWInfKey AddKey(IN PCWSTR pszKeyName);
  211. virtual void AddKey(IN PCWSTR pszKeyName, IN PCWSTR Value);
  212. virtual void AddKey(IN PCWSTR pszKeyName, IN DWORD Value);
  213. virtual void AddQwordKey(IN PCWSTR pszKeyName, IN QWORD qwValue);
  214. virtual void AddHexKey(IN PCWSTR pszKeyName, IN DWORD Value);
  215. virtual void AddBoolKey(IN PCWSTR pszKeyName, IN BOOL Value);
  216. virtual void AddKeyV(IN PCWSTR pszKeyName, IN PCWSTR Format, IN ...);
  217. virtual void AddKey(IN PCWSTR pszKeyName, IN const TStringList &slValues);
  218. virtual void AddComment(IN PCWSTR pszComment);
  219. virtual void AddRawLine(IN PCWSTR szLine);
  220. protected:
  221. tstring m_Name;
  222. WifLinePtrListIter m_posLine, m_posSection;
  223. CWInfFile *m_Parent;
  224. CWInfSection(IN PCWSTR pszSectionName, IN PCWInfFile parent);
  225. ~CWInfSection();
  226. };
  227. // ----------------------------------------------------------------------
  228. // Class CWInfKey
  229. //
  230. // Inheritance:
  231. // CWInfLine
  232. //
  233. // Purpose:
  234. // Represents a key=value line in a CWInfFile
  235. //
  236. // Hungarian: wik
  237. // ----------------------------------------------------------------------
  238. class CWInfKey : public CWInfLine
  239. {
  240. friend class CWInfFile;
  241. public:
  242. CWInfKey(IN PCWSTR pszKeyName);
  243. ~CWInfKey();
  244. static void Init();
  245. static void UnInit();
  246. virtual void GetText(tstring &text) const;
  247. virtual void GetTextEx(tstring &text) const; // used by SysPrep
  248. PCWSTR Name() const { return m_Name.c_str(); }
  249. //Read values
  250. //these functions return the FALSE if value not found
  251. //or if it is in a wrong format
  252. virtual BOOL GetStringArrayValue(OUT TStringArray &saStrings) const;
  253. virtual BOOL GetStringListValue(OUT TStringList& slList) const;
  254. virtual BOOL GetStringValue(OUT tstring& strValue) const;
  255. virtual BOOL GetIntValue(OUT DWORD *pdwValue) const;
  256. virtual BOOL GetQwordValue(OUT QWORD *pqwValue) const;
  257. virtual BOOL GetBoolValue(OUT BOOL *pfValue) const;
  258. //these functions return the default value if value not found
  259. //or if it is in a wrong format
  260. virtual PCWSTR GetStringValue(IN PCWSTR pszDefault) const;
  261. virtual DWORD GetIntValue(IN DWORD dwDefault) const;
  262. virtual QWORD GetQwordValue(IN QWORD qwDefault) const;
  263. virtual BOOL GetBoolValue(IN BOOL fDefault) const;
  264. //Write values
  265. virtual void SetValues(IN PCWSTR Format, va_list arglist);
  266. virtual void SetValues(IN PCWSTR Format, IN ...);
  267. virtual void SetValue(IN PCWSTR Value);
  268. virtual void SetValue(IN DWORD Value);
  269. virtual void SetQwordValue(IN QWORD Value);
  270. virtual void SetHexValue(IN DWORD Value);
  271. virtual void SetBoolValue(IN BOOL Value);
  272. virtual void SetValue(IN const TStringList &slValues);
  273. protected:
  274. static WCHAR *m_Buffer;
  275. private:
  276. tstring m_Name, m_Value;
  277. BOOL m_fIsAListAndAlreadyProcessed; // the value is a MULTI_SZ, will be
  278. // written out as a comma-separated
  279. // list, and has already been checked
  280. // to see if it has special chars and
  281. // needs to be surrounded by quotes.
  282. };
  283. // ----------------------------------------------------------------------
  284. // Class CWInfComment
  285. //
  286. // Inheritance:
  287. // CWInfComment
  288. //
  289. // Purpose:
  290. // Represents a comment line in a CWInfFile
  291. //
  292. // Hungarian: wic
  293. // ----------------------------------------------------------------------
  294. class CWInfComment : public CWInfLine
  295. {
  296. public:
  297. CWInfComment(IN PCWSTR pszComment);
  298. ~CWInfComment();
  299. virtual void GetText(tstring &text) const;
  300. virtual void GetTextEx(tstring &text) const; // used by SysPrep
  301. protected:
  302. private:
  303. tstring m_strCommentText;
  304. };
  305. // ----------------------------------------------------------------------
  306. // Class CWInfRaw
  307. //
  308. // Inheritance:
  309. // CWInfRaw
  310. //
  311. // Purpose:
  312. // Represents a raw line in a CWInfFile
  313. //
  314. // Hungarian: wir
  315. // ----------------------------------------------------------------------
  316. class CWInfRaw : public CWInfLine
  317. {
  318. public:
  319. CWInfRaw(IN PCWSTR szText);
  320. ~CWInfRaw();
  321. virtual void GetText(tstring &text) const;
  322. virtual void GetTextEx(tstring &text) const; // used by SysPrep
  323. protected:
  324. private:
  325. tstring m_strText;
  326. };
  327. // ----------------------------------------------------------------------