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.

332 lines
7.0 KiB

  1. //+-------------------------------------------------------------------
  2. //
  3. // File: ctext.cxx
  4. //
  5. // Contents: Implementation for CRegTextFile class
  6. //
  7. // Classes: None.
  8. //
  9. // Functions: CRegTextFile::GetString -- get a string & convert to unicode
  10. // CRegTextFile::GetLong -- get a long from the file
  11. //
  12. // History: 18-Dec-91 Ricksa Created
  13. // 19-Mar-92 Rickhi Skip over comment lines in file
  14. //
  15. //--------------------------------------------------------------------
  16. #include <pch.cxx>
  17. #pragma hdrstop
  18. #include <ctext.hxx>
  19. TCHAR CRegTextFile::s_awcSysDir[MAX_PATH];
  20. int CRegTextFile::s_cSysDir = 0;
  21. //+-------------------------------------------------------------------
  22. //
  23. // Member: CRegTextFile::CRegTextFile, public
  24. //
  25. // Synopsis: Open a file
  26. //
  27. // Effects: File is open
  28. //
  29. // Arguments: [pszFile] -- name of file to open
  30. //
  31. // Requires: Nothing.
  32. //
  33. // Returns: Returns pointer to the file opened.
  34. //
  35. // Signals: CException.
  36. //
  37. // Modifies: None.
  38. //
  39. // Derivation: None.
  40. //
  41. // Algorithm: Attempt to open the file & throw an exception if
  42. // the open request fails.
  43. //
  44. // History: 19-Dec-91 Ricksa Created
  45. //
  46. // Notes:
  47. //
  48. //--------------------------------------------------------------------
  49. CRegTextFile::CRegTextFile(LPSTR pszFile)
  50. {
  51. if (lstrcmpA(pszFile, "") == 0)
  52. {
  53. _fp = stdin;
  54. }
  55. else if ((_fp = fopen(pszFile, "r")) == NULL)
  56. {
  57. printf("Open of file failed %s", pszFile);
  58. }
  59. if (s_cSysDir == 0)
  60. {
  61. // Get the system directory -- we divide by 2 because we want
  62. // cSysDir to be the number of characters *not* the number of
  63. // bytes.
  64. s_cSysDir = GetSystemDirectory(s_awcSysDir, sizeof(s_awcSysDir))
  65. / sizeof(TCHAR);
  66. }
  67. }
  68. //+-------------------------------------------------------------------
  69. //
  70. // Member: CRegTextFile::~CRegTextFile, public
  71. //
  72. // Synopsis: Destroy object and close the file.
  73. //
  74. // Effects: Object is destroyed.
  75. //
  76. // Arguments: None.
  77. //
  78. // Requires: Valid pointer to CRegTextFile.
  79. //
  80. // Returns: Nothing.
  81. //
  82. // Signals: None.
  83. //
  84. // Modifies: Nothing global.
  85. //
  86. // Derivation: None.
  87. //
  88. // Algorithm: Simply closes the file.
  89. //
  90. // History: 19-Dec-91 Ricksa Created
  91. //
  92. // Notes:
  93. //
  94. //--------------------------------------------------------------------
  95. CRegTextFile::~CRegTextFile(void)
  96. {
  97. if (_fp != stdin)
  98. {
  99. fclose(_fp);
  100. }
  101. }
  102. //+-------------------------------------------------------------------
  103. //
  104. // Member: CRegTextFile::GetString, public
  105. //
  106. // Synopsis: Reads an ASCII string from the
  107. //
  108. // Effects: Outputs a buffer filled with UNICODE text.
  109. //
  110. // Arguments: None.
  111. //
  112. // Requires: Pointer to CRegTextFile object.
  113. //
  114. // Returns: Pointer to a buffer filled with Unicode text.
  115. //
  116. // Signals: CException
  117. //
  118. // Algorithm: Read a line of ASCII text and convert it into
  119. // Unicode and return a pointer to the data.
  120. //
  121. // History: 19-Dec-91 Ricksa Created
  122. //
  123. // Notes: This method is not multithreaded and the output
  124. // buffer will be reset on the next call to GetString.
  125. //
  126. //--------------------------------------------------------------------
  127. LPTSTR
  128. CRegTextFile::GetString(void)
  129. {
  130. do
  131. {
  132. if (fgets(_abuf, sizeof(_abuf), _fp) == NULL)
  133. {
  134. printf("Read of file failed");
  135. }
  136. } while (_abuf[0] == COMMENT_MARK);
  137. int len = lstrlenA(_abuf);
  138. int last = len - 1;
  139. // Hack needed because standard libraries think '\r' is part
  140. // of a string.
  141. if (_abuf[last] == 0x0a)
  142. {
  143. _abuf[last] = '\0';
  144. len = last;
  145. }
  146. #ifdef UNICODE
  147. // Convert to wide characters including trailing null
  148. mbstowcs(_awcbuf, _abuf, len + 1);
  149. #else
  150. memcpy(_awcbuf, _abuf, len+1);
  151. #endif
  152. // return new string
  153. return _awcbuf;
  154. }
  155. //+-------------------------------------------------------------------
  156. //
  157. // Member: CRegTextFile::GetLong
  158. //
  159. // Synopsis: Get a string from a file and convert it to an unsigned long.
  160. //
  161. // Arguments: None.
  162. //
  163. // Requires: Pointer to valid CRegTextFile object.
  164. //
  165. // Returns: ULONG read from file.
  166. //
  167. // Signals: CException.
  168. //
  169. // Algorithm: Read string and covert data to a long.
  170. //
  171. // History: 19-Dec-91 Ricksa Created
  172. //
  173. // Notes: This method does no error checking.
  174. //
  175. //--------------------------------------------------------------------
  176. ULONG
  177. CRegTextFile::GetLong(void)
  178. {
  179. do
  180. {
  181. if (fgets(_abuf, sizeof(_abuf), _fp) == NULL)
  182. {
  183. printf("Read of long failed");
  184. }
  185. } while (_abuf[0] == COMMENT_MARK);
  186. return atol(_abuf);
  187. }
  188. //+-------------------------------------------------------------------
  189. //
  190. // Member: CRegTextFile::GetGUID
  191. //
  192. // Synopsis: Get a string from a file and convert it to a GUID.
  193. //
  194. // Arguments: None.
  195. //
  196. // Returns: a pointer to the GUID in the string buffer.
  197. //
  198. // Signals: CException.
  199. //
  200. // Algorithm: Read string and covert data to a GUID.
  201. //
  202. // History: 19-Dec-91 Ricksa Created
  203. //
  204. // Notes: This is not guaranteed to work in a multithreaded
  205. // environment.
  206. //
  207. //--------------------------------------------------------------------
  208. GUID *
  209. CRegTextFile::GetGUID(void)
  210. {
  211. GUID *pguid = (GUID *) _awcbuf;
  212. do
  213. {
  214. if (fgets(_abuf, sizeof(_abuf), _fp) == NULL)
  215. {
  216. printf("Read of GUID failed");
  217. }
  218. } while (_abuf[0] == COMMENT_MARK);
  219. // Convert the string to a GUID
  220. sscanf(_abuf, "%08lX%04X%04X",
  221. &pguid->Data1, &pguid->Data2, &pguid->Data3);
  222. for (int i = 0; i < 8; i++)
  223. {
  224. int tmp;
  225. sscanf(_abuf + 16 + (i * 2), "%02X", &tmp);
  226. pguid->Data4[i] = (char) tmp;
  227. }
  228. return pguid;
  229. }
  230. //+-------------------------------------------------------------------
  231. //
  232. // Member: CRegTextFile::GetPath
  233. //
  234. // Synopsis: Get a path to a file system object
  235. //
  236. // Returns: a pointer to the path in the string buffer.
  237. //
  238. // Algorithm: Read string and add the system directory
  239. //
  240. // History: 26-Mar-92 Ricksa Created
  241. //
  242. // Notes: This is not guaranteed to work in a multithreaded
  243. // environment.
  244. //
  245. //--------------------------------------------------------------------
  246. LPTSTR
  247. CRegTextFile::GetPath(void)
  248. {
  249. GetString();
  250. if (lstrcmpi(_awcbuf, TEXT("END_OF_FILE")) == 0)
  251. {
  252. return _awcbuf;
  253. }
  254. // Temporary buffer to store result
  255. TCHAR awcTmp[MAX_PATH];
  256. // If the first characters in the string are "@:" we
  257. // want to prepend the string with the system directory.
  258. #ifdef UNICODE
  259. if (wcsnicmp(SYS_DIR_STR, _awcbuf, SYS_DIR_STR_LEN) == 0)
  260. #else
  261. if (strnicmp(SYS_DIR_STR, _awcbuf, SYS_DIR_STR_LEN) == 0)
  262. #endif
  263. {
  264. // Copy in the system directory
  265. lstrcpy(awcTmp, s_awcSysDir);
  266. // Copy in the relative path
  267. lstrcat(awcTmp, _awcbuf + SYS_DIR_STR_LEN);
  268. // Copy whole string to output buffer
  269. lstrcpy(_awcbuf, awcTmp);
  270. }
  271. else if ((_awcbuf[1] != ':') && (_awcbuf[3] != ':') && (_awcbuf[1] != '\\'))
  272. {
  273. // Convert relative path to absolute path based in the current
  274. // directory.
  275. GetCurrentDirectory(sizeof(awcTmp), awcTmp);
  276. // Add a slash to end of the current directory
  277. lstrcat(awcTmp, TEXT("\\"));
  278. // Copy in the relative path
  279. lstrcat(awcTmp, _awcbuf);
  280. // Copy whole string to output buffer
  281. lstrcpy(_awcbuf, awcTmp);
  282. }
  283. return _awcbuf;
  284. }