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.

189 lines
4.3 KiB

  1. //
  2. // MODULE: DSCREAD.CPP
  3. //
  4. // PURPOSE: dsc reading classes
  5. //
  6. // COMPANY: Saltmine Creative, Inc. (206)-284-7511 [email protected]
  7. //
  8. // AUTHOR: Oleg Kalosha
  9. //
  10. // ORIGINAL DATE: 8-19-98
  11. //
  12. // NOTES:
  13. //
  14. // Version Date By Comments
  15. //--------------------------------------------------------------------
  16. // V3.0 08-04-98 OK
  17. //
  18. #include "stdafx.h"
  19. #include "dscread.h"
  20. #include "fileread.h"
  21. #include "event.h"
  22. #include "baseexception.h"
  23. #ifdef LOCAL_TROUBLESHOOTER
  24. #include "CHMfileReader.h"
  25. #endif
  26. ////////////////////////////////////////////////////////////////////////////////////
  27. // CDSCReaderException
  28. ////////////////////////////////////////////////////////////////////////////////////
  29. // source_file is LPCSTR rather than LPCTSTR because __FILE__ is char[35]
  30. CDSCReaderException::CDSCReaderException(CDSCReader* reader, eErr err, LPCSTR source_file, int line)
  31. : CBaseException(source_file, line),
  32. m_pDSCReader(reader),
  33. m_eErr(err)
  34. {
  35. }
  36. CDSCReaderException::~CDSCReaderException()
  37. {
  38. }
  39. void CDSCReaderException::Clear()
  40. {
  41. m_pDSCReader->Clear();
  42. }
  43. ////////////////////////////////////////////////////////////////////////////////////
  44. // CDSCReader
  45. // This handles just the reading of BNTS. CBN packages it up for public consumption.
  46. ////////////////////////////////////////////////////////////////////////////////////
  47. CDSCReader::CDSCReader(CPhysicalFileReader* pPhysicalFileReader)
  48. : CStateless(),
  49. m_pPhysicalFileReader(pPhysicalFileReader),
  50. m_strPath(pPhysicalFileReader->GetPathName()),
  51. m_strName(pPhysicalFileReader->GetJustName()),
  52. m_bIsRead(false),
  53. m_bDeleteFile(false)
  54. {
  55. // Arbitrary default for m_stimeLastWrite
  56. m_stimeLastWrite.wYear = 0;
  57. m_stimeLastWrite.wMonth = 0;
  58. m_stimeLastWrite.wDayOfWeek =0;
  59. m_stimeLastWrite.wDay = 1;
  60. m_stimeLastWrite.wHour = 0;
  61. m_stimeLastWrite.wMinute = 0;
  62. m_stimeLastWrite.wSecond = 0;
  63. m_stimeLastWrite.wMilliseconds = 0;
  64. }
  65. CDSCReader::~CDSCReader()
  66. {
  67. delete m_pPhysicalFileReader;
  68. }
  69. bool CDSCReader::IsValid() const
  70. {
  71. bool ret = false;
  72. LOCKOBJECT();
  73. ret = m_Network.BValidNet() ? true : false;
  74. UNLOCKOBJECT();
  75. return ret;
  76. }
  77. bool CDSCReader::IsRead() const
  78. {
  79. bool ret = false;
  80. LOCKOBJECT();
  81. ret = m_bIsRead;
  82. UNLOCKOBJECT();
  83. return ret;
  84. }
  85. bool CDSCReader::Read()
  86. {
  87. bool ret = false;
  88. #ifdef LOCAL_TROUBLESHOOTER
  89. CHMfileHandler( m_strPath );
  90. #endif
  91. LOCKOBJECT();
  92. if (m_bIsRead)
  93. Clear();
  94. if (m_Network.BReadModel(m_strPath, NULL))
  95. {
  96. m_bIsRead = true;
  97. ret = true;
  98. }
  99. UNLOCKOBJECT();
  100. if (m_bDeleteFile)
  101. ::DeleteFile( m_strPath );
  102. return ret;
  103. }
  104. void CDSCReader::Clear()
  105. {
  106. LOCKOBJECT();
  107. m_Network.Clear();
  108. m_bIsRead = false;
  109. UNLOCKOBJECT();
  110. }
  111. #ifdef LOCAL_TROUBLESHOOTER
  112. // Function called from the ctor to handle the checking and optionally writing out
  113. // of a CHM file to a temporary file.
  114. bool CDSCReader::CHMfileHandler( LPCTSTR path )
  115. {
  116. bool bRetVal= false;
  117. if (CCHMFileReader::IsCHMfile( m_strPath ))
  118. {
  119. CString strContent;
  120. CFileReader file_reader(m_pPhysicalFileReader, false/*don't delete physical reader*/);
  121. // read file from inside CHM
  122. if (!file_reader.Read())
  123. return bRetVal;
  124. file_reader.GetContent(strContent);
  125. // Build the temporary file name.
  126. TCHAR szTempDir[ _MAX_DIR ];
  127. ::GetTempPath( sizeof( szTempDir ), szTempDir );
  128. CString strTmpFName= szTempDir;
  129. strTmpFName+= file_reader.GetJustNameWithoutExtension();
  130. strTmpFName+= _T(".");
  131. strTmpFName+= file_reader.GetJustExtension();
  132. // Open the temporary file and write out the contents of the CHM file.
  133. HANDLE hTmpFile= ::CreateFile( strTmpFName,
  134. GENERIC_WRITE,
  135. 0, // No Sharing.
  136. NULL,
  137. CREATE_ALWAYS,
  138. FILE_ATTRIBUTE_TEMPORARY,
  139. NULL );
  140. if (INVALID_HANDLE_VALUE != hTmpFile)
  141. {
  142. DWORD dwBytesWritten;
  143. if (!::WriteFile( hTmpFile, (LPCTSTR)strContent, strContent.GetLength(), &dwBytesWritten, NULL))
  144. {
  145. // >>> Need to consider what we should do in this case.
  146. }
  147. else
  148. {
  149. bRetVal= true;
  150. }
  151. ::CloseHandle( hTmpFile );
  152. }
  153. if (bRetVal)
  154. {
  155. // Reassign the path to the temporary file.
  156. m_strPath= strTmpFName;
  157. // Set the delete flag to true.
  158. m_bDeleteFile= true;
  159. }
  160. }
  161. return( bRetVal );
  162. }
  163. #endif