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.

524 lines
12 KiB

  1. // FileIO.cpp Implementation of MSInfoFile classes.
  2. //
  3. // Copyright (c) 1998-1999 Microsoft Corporation
  4. #include "stdafx.h"
  5. #include "FileIO.h"
  6. //#include "DataSrc.h"
  7. #include "category.h"
  8. #include "MSInfo5Category.h"
  9. //#include "Resource.h"
  10. CFileFormatException CMSInfoFile::xptFileFormat;
  11. const unsigned CMSInfoFile::DefaultReadBufferSize = 512; // 256;
  12. /*
  13. * CMSInfoFile - Construct an MSInfoFile, setting the CFile to the pointer passed
  14. *
  15. * History: a-jsari 10/20/97 Initial version
  16. */
  17. void CMSInfoFile::ReadCategoryHeader()
  18. {
  19. LONG l;
  20. ASSERT(this->m_pFile != NULL);
  21. ReadLong(l); // Save time.
  22. m_tsSaveTime = (ULONG) l;
  23. #ifdef _WIN64
  24. ReadLong(l); // Save time.
  25. m_tsSaveTime |= ((time_t) l) << 32;
  26. #endif
  27. CString szDummy;
  28. ReadString(szDummy); // Network machine name
  29. ReadString(szDummy); // Network user name
  30. }
  31. CMSInfoFile::CMSInfoFile(CFile *pFile)
  32. {
  33. if (pFile != NULL)
  34. m_pFile = pFile;
  35. }
  36. /*
  37. * CMSInfoFile - Construct an MSInfoFile, opening the CFile
  38. *
  39. * History: a-jsari 11/13/97 Initial version
  40. */
  41. CMSInfoFile::CMSInfoFile(LPCTSTR szFileName, UINT nFlags)
  42. :m_strFileName(szFileName)
  43. {
  44. m_pFile = new CFile(szFileName, nFlags);
  45. if (m_pFile == NULL) ::AfxThrowMemoryException();
  46. }
  47. /*
  48. * ~CMSInfoFile - Destroy an MSInfoFile, closing the CFile pointer
  49. *
  50. * History: a-jsari 10/20/97 Initial version
  51. */
  52. CMSInfoFile::~CMSInfoFile()
  53. {
  54. if (m_pFile)
  55. {
  56. //m_pFile->Close();
  57. delete m_pFile;
  58. }
  59. }
  60. /*
  61. * ReadUnsignedInt - Read an int from a file with the same byte-order
  62. * as our current implementation.
  63. *
  64. * History: a-jsari 10/21/97 Initial version
  65. */
  66. void CMSInfoFile::ReadUnsignedInt(unsigned &uValue)
  67. {
  68. ReadUnsignedFromCFile(m_pFile, uValue);
  69. }
  70. /*
  71. * ReadUnsignedLong - Read a long from a file with the same byte-order
  72. * as our current implementation.
  73. *
  74. * History: a-jsari 12/1/97 Initial version
  75. */
  76. void CMSInfoFile::ReadUnsignedLong(unsigned long &dwValue)
  77. {
  78. long lValue;
  79. ReadLongFromCFile(m_pFile, lValue);
  80. ::memcpy(&dwValue, &lValue, sizeof(unsigned long));
  81. }
  82. /*
  83. * ReadLong - Read a long from a file written with our current byte-order
  84. *
  85. * History: a-jsari 10/21/97 Initial version
  86. */
  87. void CMSInfoFile::ReadLong(long &lValue)
  88. {
  89. ReadLongFromCFile(m_pFile, lValue);
  90. }
  91. /*
  92. * ReadSignedInt - Read a signed integer value.
  93. *
  94. * History: a-jsari 10/20/97 Initial version
  95. */
  96. void CMSInfoFile::ReadSignedInt(int &wValue)
  97. {
  98. unsigned uValue;
  99. ReadUnsignedInt(uValue);
  100. ::memcpy(&wValue, &uValue, sizeof(int));
  101. }
  102. /*
  103. * ReadTchar - Read a tchar.
  104. *
  105. * History: a-jsari 12/26/97 Initial version.
  106. */
  107. void CMSInfoFile::ReadTchar(TCHAR &tcValue)
  108. {
  109. ReadTcharFromCFile(m_pFile, tcValue);
  110. }
  111. /*
  112. * ReadString - Read a string.
  113. *
  114. * History: a-jsari 10/20/97 Initial version.
  115. */
  116. void CMSInfoFile::ReadString(CString &szString)
  117. {
  118. unsigned wStringLength;
  119. WCHAR szBuffer[DefaultReadBufferSize]; // Maximum string length = sizeof(szBuffer)
  120. LPWSTR pszBuffer = szBuffer;
  121. ASSERT(m_pFile);
  122. ReadUnsignedInt(wStringLength);
  123. if (wStringLength > sizeof(szBuffer))
  124. ThrowFileFormatException();
  125. szBuffer[wStringLength] = (WCHAR)'\0';
  126. wStringLength *= sizeof(WCHAR);
  127. if (m_pFile->Read(reinterpret_cast<void *>(pszBuffer), wStringLength) != wStringLength)
  128. ThrowFileFormatException();
  129. szString = pszBuffer;
  130. }
  131. /*
  132. * WriteHeader - Write the header for the current version (currently
  133. * Version 5.00).
  134. *
  135. * History: a-jsari 10/31/97 Initial version
  136. */
  137. void CMSInfoFile::WriteHeader(CDataSource *)
  138. {
  139. time_t tNow;
  140. WriteUnsignedInt(VERSION_500_MAGIC_NUMBER); // File magic number.
  141. WriteUnsignedInt(0x500); // Version number
  142. time(&tNow);
  143. WriteLong((LONG)tNow); // Current time.
  144. #ifdef _WIN64
  145. WriteLong((LONG) (tNow>>32));
  146. #endif
  147. WriteString(""); // Network machine
  148. WriteString(""); // Network user name.
  149. /* msiFile.WriteString("");
  150. msiFile.WriteUnsignedInt(1);
  151. msiFile.WriteUnsignedInt(0);
  152. msiFile.WriteString("");
  153. msiFile.WriteUnsignedInt(0);
  154. msiFile.WriteByte(0x00);
  155. msiFile.WriteUnsignedInt(0);
  156. msiFile.WriteUnsignedInt(CMSInfo5Category::CHILD);*/
  157. }
  158. /*
  159. * WriteChildMark - Write the special integer which specifies that the
  160. * following folder will be the child of the previous folder.
  161. *
  162. * History: a-jsari 11/5/97 Initial version.
  163. */
  164. void CMSInfoFile::WriteChildMark()
  165. {
  166. WriteUnsignedInt(CMSInfo5Category::CHILD);
  167. }
  168. /*
  169. * WriteEndMark - Write the special integer which specifies that the
  170. * end of data has been reached.
  171. *
  172. * History: a-jsari 11/5/97 Initial version.
  173. */
  174. void CMSInfoFile::WriteEndMark()
  175. {
  176. WriteUnsignedInt(CMSInfo5Category::END);
  177. }
  178. /*
  179. * WriteNextMark - Write the special integer which specifies that the
  180. * following folder will be the next folder in the list.
  181. *
  182. * History: a-jsari 11/5/97 Initial version.
  183. */
  184. void CMSInfoFile::WriteNextMark()
  185. {
  186. WriteUnsignedInt(CMSInfo5Category::NEXT);
  187. }
  188. /*
  189. * WriteParentMark - Write the special mark specifying a parent node, with
  190. * the number of times the reading function should go up.
  191. *
  192. * History: a-jsari 11/5/97 Initial version.
  193. */
  194. void CMSInfoFile::WriteParentMark(unsigned cIterations)
  195. {
  196. WriteUnsignedInt(CMSInfo5Category::PARENT | cIterations);
  197. }
  198. /*
  199. * WriteByte - Write a byte to our internal file.
  200. *
  201. * History: a-jsari 10/22/97 Initial version
  202. */
  203. void CMSInfoFile::WriteByte(BYTE bValue)
  204. {
  205. m_pFile->Write(reinterpret_cast<void *>(&bValue), sizeof(bValue));
  206. }
  207. /*
  208. * WriteString - Write szValue as a string of wide characters, prefixed by
  209. * the string length.
  210. *
  211. * History: a-jsari 10/22/97 Initial version
  212. */
  213. void CMSInfoFile::WriteString(CString szValue)
  214. {
  215. LPWSTR pszString;
  216. USES_CONVERSION;
  217. WriteUnsignedInt(szValue.GetLength());
  218. pszString = T2W(const_cast<LPTSTR>((LPCTSTR)szValue));
  219. m_pFile->Write(reinterpret_cast<void *>(pszString),
  220. szValue.GetLength() * sizeof(WCHAR));
  221. }
  222. /*
  223. * WriteLong - Write a long value to our internal file.
  224. *
  225. * History: a-jsari 10/22/97 Initial version
  226. */
  227. void CMSInfoFile::WriteLong(long lValue)
  228. {
  229. m_pFile->Write(reinterpret_cast<void *>(&lValue), sizeof(lValue));
  230. }
  231. /*
  232. * WriteUnsignedInt - Write an unsigned integer value to our internal file.
  233. *
  234. * History: a-jsari 10/22/97 Initial version
  235. */
  236. void CMSInfoFile::WriteUnsignedInt(unsigned uValue)
  237. {
  238. // unsigned* utest = (unsigned*) (reinterpret_cast<void *>(&uValue));
  239. // UINT utest2 = (UINT) *utest;
  240. m_pFile->Write(reinterpret_cast<void *>(&uValue), sizeof(uValue));
  241. }
  242. /*
  243. * WriteUnsignedLong - Write an unsigned long value to our internal file.
  244. *
  245. * History: a-jsari 12/1/97 Initial version
  246. */
  247. void CMSInfoFile::WriteUnsignedLong(unsigned long dwValue)
  248. {
  249. long lValue;
  250. ::memcpy(&lValue, &dwValue, sizeof(dwValue));
  251. WriteLong(lValue);
  252. }
  253. /*
  254. * ReadTcharFromCFile - Read a TCHAR value from the file specified.
  255. *
  256. * History: a-jsari 12/26/97 Initial version
  257. */
  258. void CMSInfoFile::ReadTcharFromCFile(CFile *pFile, TCHAR &tcValue)
  259. {
  260. ASSERT(pFile != NULL);
  261. if (pFile->Read(reinterpret_cast<void *>(&tcValue), sizeof(tcValue)) != sizeof(tcValue))
  262. ThrowFileFormatException();
  263. }
  264. /*
  265. * ReadUnsignedFromCFile - Read an unsigned value from the file specified.
  266. *
  267. * History: a-jsari 10/20/97 Initial version
  268. */
  269. void CMSInfoFile::ReadUnsignedFromCFile(CFile *pFile, unsigned &uValue)
  270. {
  271. ASSERT(pFile);
  272. if (pFile->Read(reinterpret_cast<void *>(&uValue), sizeof(uValue)) != sizeof(uValue))
  273. ThrowFileFormatException();
  274. }
  275. /*
  276. * ReadLongFromCFile - Read a long from the file specified.
  277. *
  278. * History: a-jsari 10/20/97 Initial version.
  279. */
  280. void CMSInfoFile::ReadLongFromCFile(CFile *pFile, long &lValue)
  281. {
  282. ASSERT(pFile);
  283. if (pFile->Read(reinterpret_cast<void *>(&lValue), sizeof(lValue)) != sizeof(lValue))
  284. {
  285. ThrowFileFormatException();
  286. }
  287. }
  288. /*
  289. * CMSInfoTextFile - Constructor
  290. *
  291. * History: a-jsari 11/13/97 Initial version
  292. */
  293. CMSInfoTextFile::CMSInfoTextFile(LPCTSTR szFileName, UINT nFlags)
  294. {
  295. try
  296. {
  297. m_pFile = new CFile(szFileName, nFlags);
  298. }
  299. catch (CFileException * e)
  300. {
  301. e->ReportError();
  302. throw;
  303. }
  304. }
  305. /*
  306. * CMSInfoTextFile - Constructor
  307. *
  308. * History: a-jsari 12/26/97 Initial version
  309. */
  310. CMSInfoTextFile::CMSInfoTextFile(CFile *pFile)
  311. :CMSInfoFile(pFile)
  312. {
  313. }
  314. /*
  315. * WriteHeader - Write the special header for the text file.
  316. *
  317. * History: a-jsari 10/31/97 Initial version
  318. */
  319. void CMSInfoTextFile::WriteHeader(CDataSource *pSource)
  320. {
  321. AFX_MANAGE_STATE(::AfxGetStaticModuleState());
  322. // mark file as unicode
  323. WCHAR wHeader = 0xFEFF;
  324. m_pFile->Write( &wHeader, 2);
  325. // FIX: Make this point to the right time.
  326. CTime tNow = CTime::GetCurrentTime();
  327. CString strTimeFormat;
  328. // strTimeFormat.LoadString(IDS_TIME_FORMAT);
  329. CString strHeaderText = tNow.Format(strTimeFormat);
  330. WriteString(strHeaderText);
  331. // WriteString(pSource->MachineName());
  332. }
  333. /*
  334. * WriteTitle - Write the title of a folder.
  335. *
  336. * History: a-jsari 11/5/97 Initial version
  337. */
  338. void CMSInfoTextFile::WriteTitle(CString szName)
  339. {
  340. CString szWriteString = _T("[");
  341. szWriteString += szName + _T("]\n\n");
  342. WriteString(szWriteString);
  343. }
  344. /*
  345. * WriteLong - Write a long value in the text file.
  346. *
  347. * History: a-jsari 10/23/97 Initial version
  348. */
  349. void CMSInfoTextFile::WriteLong(long lValue)
  350. {
  351. CString szTextValue;
  352. szTextValue.Format(_T("%ld"), lValue);
  353. WriteString(szTextValue);
  354. }
  355. /*
  356. * WriteUnsignedInt - Write an unsigned value in the text file.
  357. *
  358. * History: a-jsari 10/23/97 Initial version
  359. */
  360. void CMSInfoTextFile::WriteUnsignedInt(unsigned uValue)
  361. {
  362. CString szTextValue;
  363. szTextValue.Format(_T("%ud"), uValue);
  364. WriteString(szTextValue);
  365. }
  366. /*
  367. * WriteString - Write a string to a text file.
  368. *
  369. * History: a-jsari 10/23/97 Initial version
  370. */
  371. void CMSInfoTextFile::WriteString(CString szValue)
  372. {
  373. if (szValue.GetLength() == 0)
  374. return;
  375. //a-stephl dynamic_cast<CFile *>(m_pFile)->Write((LPCTSTR)szValue, szValue.GetLength() * sizeof(TCHAR));
  376. m_pFile->Write((LPCTSTR)szValue, szValue.GetLength() * sizeof(TCHAR));
  377. }
  378. /*
  379. * WriteString - Write a string to a memory file.
  380. *
  381. * History: a-jsari 1/5/98 Initial version
  382. */
  383. void CMSInfoMemoryFile::WriteString(CString szValue)
  384. {
  385. if (szValue.GetLength() == 0)
  386. return;
  387. m_pFile->Write((LPCTSTR)szValue, szValue.GetLength() * sizeof(TCHAR));
  388. }
  389. #if 0
  390. /*
  391. * ReadUnsignedInt -
  392. *
  393. * History: a-jsari 10/21/97 Initial version
  394. */
  395. void CMSInfoReverseEndianFile::ReadUnsignedInt(unsigned &uValue)
  396. {
  397. CMSInfoReverseEndianFile::ReadUnsignedFromCFile(m_pFile, uValue);
  398. }
  399. /*
  400. * ReadLong -
  401. *
  402. * History: a-jsari 10/21/97 Initial version
  403. */
  404. void CMSInfoReverseEndianFile::ReadLong(long &lValue)
  405. {
  406. CMSInfoReverseEndianFile::ReadLongFromCFile(m_pFile, lValue);
  407. }
  408. /*
  409. * ReadString -
  410. *
  411. * History: a-jsari 10/21/97 Initial version
  412. */
  413. void CMSInfoReverseEndianFile::ReadString(CString &szValue)
  414. {
  415. unsigned uStringLength;
  416. WCHAR szBuffer[DefaultReadBufferSize];
  417. LPWSTR pszBuffer = szBuffer;
  418. ReadUnsignedInt(uStringLength);
  419. for (unsigned i = uStringLength ; i > 0 ; --i) {
  420. szBuffer[i] = 0;
  421. for (unsigned j = sizeof(WCHAR) ; j > 0 ; --j) {
  422. BYTE bRead;
  423. ReadByte(bRead);
  424. szBuffer[i] >>= 8;
  425. szBuffer[i] |= bRead;
  426. }
  427. }
  428. }
  429. /*
  430. * ReadIntegerFromCFile - Template class to read an arbitrarily sized int
  431. * from a CFile pointer.
  432. *
  433. * History: a-jsari 10/21/97 Initial version
  434. */
  435. template <class T> void ReadIntegerFromCFile(CFile *pFile, T &tValue)
  436. {
  437. union ReverseBuffer { BYTE bytes[sizeof(T)]; T tVal; };
  438. union ReverseBuffer rbReverse;
  439. union ReverseBuffer rbSwap;
  440. if (pFile->Read(reinterpret_cast<void *>(&tValue), sizeof(T)) != sizeof(T))
  441. ThrowFileFormatException();
  442. unsigned j = 0;
  443. for (unsigned i = sizeof(union ReverseBuffer) ; i > 0 ; --i, ++j) {
  444. rbSwap.bytes[i] = rbReverse.bytes[j];
  445. }
  446. tValue = rbReverse.tVal;
  447. }
  448. /*
  449. * ReadUnsignedFromCFile -
  450. *
  451. * History: a-jsari 10/21/97 Initial version
  452. */
  453. void CMSInfoReverseEndianFile::ReadUnsignedFromCFile(CFile *pFile, unsigned &uValue)
  454. {
  455. ReadIntegerFromCFile<unsigned>(pFile, uValue);
  456. }
  457. /*
  458. * ReadLongFromCFile -
  459. *
  460. * History: a-jsari 10/21/97 Initial version
  461. */
  462. void CMSInfoReverseEndianFile::ReadLongFromCFile(CFile *pFile, long &lValue)
  463. {
  464. ReadIntegerFromCFile<long>(pFile, lValue);
  465. }
  466. #endif