Leaked source code of windows server 2003
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.

531 lines
13 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. LPWSTR pszBuffer = new WCHAR[wStringLength + 1];
  124. m_pFile->Read(pszBuffer,wStringLength*sizeof(WCHAR));
  125. pszBuffer[wStringLength] = (WCHAR)'\0';
  126. // if (wStringLength > sizeof(szBuffer))
  127. // ThrowFileFormatException();
  128. // szBuffer[wStringLength] = (WCHAR)'\0';
  129. //wStringLength *= sizeof(WCHAR);
  130. // if (m_pFile->Read(reinterpret_cast<void *>(pszBuffer), wStringLength) != wStringLength)
  131. // ThrowFileFormatException();
  132. szString = pszBuffer;
  133. delete [] pszBuffer;
  134. }
  135. /*
  136. * WriteHeader - Write the header for the current version (currently
  137. * Version 5.00).
  138. *
  139. * History: a-jsari 10/31/97 Initial version
  140. */
  141. void CMSInfoFile::WriteHeader(CDataSource *)
  142. {
  143. time_t tNow;
  144. WriteUnsignedInt(VERSION_500_MAGIC_NUMBER); // File magic number.
  145. WriteUnsignedInt(0x500); // Version number
  146. time(&tNow);
  147. WriteLong((LONG)tNow); // Current time.
  148. #ifdef _WIN64
  149. WriteLong((LONG) (tNow>>32));
  150. #endif
  151. WriteString(""); // Network machine
  152. WriteString(""); // Network user name.
  153. /* msiFile.WriteString("");
  154. msiFile.WriteUnsignedInt(1);
  155. msiFile.WriteUnsignedInt(0);
  156. msiFile.WriteString("");
  157. msiFile.WriteUnsignedInt(0);
  158. msiFile.WriteByte(0x00);
  159. msiFile.WriteUnsignedInt(0);
  160. msiFile.WriteUnsignedInt(CMSInfo5Category::CHILD);*/
  161. }
  162. /*
  163. * WriteChildMark - Write the special integer which specifies that the
  164. * following folder will be the child of the previous folder.
  165. *
  166. * History: a-jsari 11/5/97 Initial version.
  167. */
  168. void CMSInfoFile::WriteChildMark()
  169. {
  170. WriteUnsignedInt(CMSInfo5Category::CHILD);
  171. }
  172. /*
  173. * WriteEndMark - Write the special integer which specifies that the
  174. * end of data has been reached.
  175. *
  176. * History: a-jsari 11/5/97 Initial version.
  177. */
  178. void CMSInfoFile::WriteEndMark()
  179. {
  180. WriteUnsignedInt(CMSInfo5Category::END);
  181. }
  182. /*
  183. * WriteNextMark - Write the special integer which specifies that the
  184. * following folder will be the next folder in the list.
  185. *
  186. * History: a-jsari 11/5/97 Initial version.
  187. */
  188. void CMSInfoFile::WriteNextMark()
  189. {
  190. WriteUnsignedInt(CMSInfo5Category::NEXT);
  191. }
  192. /*
  193. * WriteParentMark - Write the special mark specifying a parent node, with
  194. * the number of times the reading function should go up.
  195. *
  196. * History: a-jsari 11/5/97 Initial version.
  197. */
  198. void CMSInfoFile::WriteParentMark(unsigned cIterations)
  199. {
  200. WriteUnsignedInt(CMSInfo5Category::PARENT | cIterations);
  201. }
  202. /*
  203. * WriteByte - Write a byte to our internal file.
  204. *
  205. * History: a-jsari 10/22/97 Initial version
  206. */
  207. void CMSInfoFile::WriteByte(BYTE bValue)
  208. {
  209. m_pFile->Write(reinterpret_cast<void *>(&bValue), sizeof(bValue));
  210. }
  211. /*
  212. * WriteString - Write szValue as a string of wide characters, prefixed by
  213. * the string length.
  214. *
  215. * History: a-jsari 10/22/97 Initial version
  216. */
  217. void CMSInfoFile::WriteString(CString szValue)
  218. {
  219. LPWSTR pszString;
  220. USES_CONVERSION;
  221. WriteUnsignedInt(szValue.GetLength());
  222. pszString = T2W(const_cast<LPTSTR>((LPCTSTR)szValue));
  223. m_pFile->Write(reinterpret_cast<void *>(pszString),
  224. szValue.GetLength() * sizeof(WCHAR));
  225. }
  226. /*
  227. * WriteLong - Write a long value to our internal file.
  228. *
  229. * History: a-jsari 10/22/97 Initial version
  230. */
  231. void CMSInfoFile::WriteLong(long lValue)
  232. {
  233. m_pFile->Write(reinterpret_cast<void *>(&lValue), sizeof(lValue));
  234. }
  235. /*
  236. * WriteUnsignedInt - Write an unsigned integer value to our internal file.
  237. *
  238. * History: a-jsari 10/22/97 Initial version
  239. */
  240. void CMSInfoFile::WriteUnsignedInt(unsigned uValue)
  241. {
  242. // unsigned* utest = (unsigned*) (reinterpret_cast<void *>(&uValue));
  243. // UINT utest2 = (UINT) *utest;
  244. m_pFile->Write(reinterpret_cast<void *>(&uValue), sizeof(uValue));
  245. }
  246. /*
  247. * WriteUnsignedLong - Write an unsigned long value to our internal file.
  248. *
  249. * History: a-jsari 12/1/97 Initial version
  250. */
  251. void CMSInfoFile::WriteUnsignedLong(unsigned long dwValue)
  252. {
  253. long lValue;
  254. ::memcpy(&lValue, &dwValue, sizeof(dwValue));
  255. WriteLong(lValue);
  256. }
  257. /*
  258. * ReadTcharFromCFile - Read a TCHAR value from the file specified.
  259. *
  260. * History: a-jsari 12/26/97 Initial version
  261. */
  262. void CMSInfoFile::ReadTcharFromCFile(CFile *pFile, TCHAR &tcValue)
  263. {
  264. ASSERT(pFile != NULL);
  265. if (pFile->Read(reinterpret_cast<void *>(&tcValue), sizeof(tcValue)) != sizeof(tcValue))
  266. ThrowFileFormatException();
  267. }
  268. /*
  269. * ReadUnsignedFromCFile - Read an unsigned value from the file specified.
  270. *
  271. * History: a-jsari 10/20/97 Initial version
  272. */
  273. void CMSInfoFile::ReadUnsignedFromCFile(CFile *pFile, unsigned &uValue)
  274. {
  275. ASSERT(pFile);
  276. if (pFile->Read(reinterpret_cast<void *>(&uValue), sizeof(uValue)) != sizeof(uValue))
  277. ThrowFileFormatException();
  278. }
  279. /*
  280. * ReadLongFromCFile - Read a long from the file specified.
  281. *
  282. * History: a-jsari 10/20/97 Initial version.
  283. */
  284. void CMSInfoFile::ReadLongFromCFile(CFile *pFile, long &lValue)
  285. {
  286. ASSERT(pFile);
  287. if (pFile->Read(reinterpret_cast<void *>(&lValue), sizeof(lValue)) != sizeof(lValue))
  288. {
  289. ThrowFileFormatException();
  290. }
  291. }
  292. /*
  293. * CMSInfoTextFile - Constructor
  294. *
  295. * History: a-jsari 11/13/97 Initial version
  296. */
  297. CMSInfoTextFile::CMSInfoTextFile(LPCTSTR szFileName, UINT nFlags)
  298. {
  299. try
  300. {
  301. m_pFile = new CFile(szFileName, nFlags);
  302. }
  303. catch (CFileException * e)
  304. {
  305. e->ReportError();
  306. throw;
  307. }
  308. }
  309. /*
  310. * CMSInfoTextFile - Constructor
  311. *
  312. * History: a-jsari 12/26/97 Initial version
  313. */
  314. CMSInfoTextFile::CMSInfoTextFile(CFile *pFile)
  315. :CMSInfoFile(pFile)
  316. {
  317. }
  318. /*
  319. * WriteHeader - Write the special header for the text file.
  320. *
  321. * History: a-jsari 10/31/97 Initial version
  322. */
  323. void CMSInfoTextFile::WriteHeader(CDataSource *pSource)
  324. {
  325. AFX_MANAGE_STATE(::AfxGetStaticModuleState());
  326. // mark file as unicode
  327. WCHAR wHeader = 0xFEFF;
  328. m_pFile->Write( &wHeader, 2);
  329. // FIX: Make this point to the right time.
  330. CTime tNow = CTime::GetCurrentTime();
  331. CString strTimeFormat;
  332. // strTimeFormat.LoadString(IDS_TIME_FORMAT);
  333. CString strHeaderText = tNow.Format(strTimeFormat);
  334. WriteString(strHeaderText);
  335. // WriteString(pSource->MachineName());
  336. }
  337. /*
  338. * WriteTitle - Write the title of a folder.
  339. *
  340. * History: a-jsari 11/5/97 Initial version
  341. */
  342. void CMSInfoTextFile::WriteTitle(CString szName)
  343. {
  344. CString szWriteString = _T("[");
  345. szWriteString += szName + _T("]\n\n");
  346. WriteString(szWriteString);
  347. }
  348. /*
  349. * WriteLong - Write a long value in the text file.
  350. *
  351. * History: a-jsari 10/23/97 Initial version
  352. */
  353. void CMSInfoTextFile::WriteLong(long lValue)
  354. {
  355. CString szTextValue;
  356. szTextValue.Format(_T("%ld"), lValue);
  357. WriteString(szTextValue);
  358. }
  359. /*
  360. * WriteUnsignedInt - Write an unsigned value in the text file.
  361. *
  362. * History: a-jsari 10/23/97 Initial version
  363. */
  364. void CMSInfoTextFile::WriteUnsignedInt(unsigned uValue)
  365. {
  366. CString szTextValue;
  367. szTextValue.Format(_T("%ud"), uValue);
  368. WriteString(szTextValue);
  369. }
  370. /*
  371. * WriteString - Write a string to a text file.
  372. *
  373. * History: a-jsari 10/23/97 Initial version
  374. */
  375. void CMSInfoTextFile::WriteString(CString szValue)
  376. {
  377. if (szValue.GetLength() == 0)
  378. return;
  379. //a-stephl dynamic_cast<CFile *>(m_pFile)->Write((LPCTSTR)szValue, szValue.GetLength() * sizeof(TCHAR));
  380. m_pFile->Write((LPCTSTR)szValue, szValue.GetLength() * sizeof(TCHAR));
  381. }
  382. /*
  383. * WriteString - Write a string to a memory file.
  384. *
  385. * History: a-jsari 1/5/98 Initial version
  386. */
  387. void CMSInfoMemoryFile::WriteString(CString szValue)
  388. {
  389. if (szValue.GetLength() == 0)
  390. return;
  391. m_pFile->Write((LPCTSTR)szValue, szValue.GetLength() * sizeof(TCHAR));
  392. }
  393. #if 0
  394. /*
  395. * ReadUnsignedInt -
  396. *
  397. * History: a-jsari 10/21/97 Initial version
  398. */
  399. void CMSInfoReverseEndianFile::ReadUnsignedInt(unsigned &uValue)
  400. {
  401. CMSInfoReverseEndianFile::ReadUnsignedFromCFile(m_pFile, uValue);
  402. }
  403. /*
  404. * ReadLong -
  405. *
  406. * History: a-jsari 10/21/97 Initial version
  407. */
  408. void CMSInfoReverseEndianFile::ReadLong(long &lValue)
  409. {
  410. CMSInfoReverseEndianFile::ReadLongFromCFile(m_pFile, lValue);
  411. }
  412. /*
  413. * ReadString -
  414. *
  415. * History: a-jsari 10/21/97 Initial version
  416. */
  417. void CMSInfoReverseEndianFile::ReadString(CString &szValue)
  418. {
  419. unsigned uStringLength;
  420. WCHAR szBuffer[DefaultReadBufferSize];
  421. LPWSTR pszBuffer = szBuffer;
  422. ReadUnsignedInt(uStringLength);
  423. for (unsigned i = uStringLength ; i > 0 ; --i) {
  424. szBuffer[i] = 0;
  425. for (unsigned j = sizeof(WCHAR) ; j > 0 ; --j) {
  426. BYTE bRead;
  427. ReadByte(bRead);
  428. szBuffer[i] >>= 8;
  429. szBuffer[i] |= bRead;
  430. }
  431. }
  432. }
  433. /*
  434. * ReadIntegerFromCFile - Template class to read an arbitrarily sized int
  435. * from a CFile pointer.
  436. *
  437. * History: a-jsari 10/21/97 Initial version
  438. */
  439. template <class T> void ReadIntegerFromCFile(CFile *pFile, T &tValue)
  440. {
  441. union ReverseBuffer { BYTE bytes[sizeof(T)]; T tVal; };
  442. union ReverseBuffer rbReverse;
  443. union ReverseBuffer rbSwap;
  444. if (pFile->Read(reinterpret_cast<void *>(&tValue), sizeof(T)) != sizeof(T))
  445. ThrowFileFormatException();
  446. unsigned j = 0;
  447. for (unsigned i = sizeof(union ReverseBuffer) ; i > 0 ; --i, ++j) {
  448. rbSwap.bytes[i] = rbReverse.bytes[j];
  449. }
  450. tValue = rbReverse.tVal;
  451. }
  452. /*
  453. * ReadUnsignedFromCFile -
  454. *
  455. * History: a-jsari 10/21/97 Initial version
  456. */
  457. void CMSInfoReverseEndianFile::ReadUnsignedFromCFile(CFile *pFile, unsigned &uValue)
  458. {
  459. ReadIntegerFromCFile<unsigned>(pFile, uValue);
  460. }
  461. /*
  462. * ReadLongFromCFile -
  463. *
  464. * History: a-jsari 10/21/97 Initial version
  465. */
  466. void CMSInfoReverseEndianFile::ReadLongFromCFile(CFile *pFile, long &lValue)
  467. {
  468. ReadIntegerFromCFile<long>(pFile, lValue);
  469. }
  470. #endif