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.

490 lines
11 KiB

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