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.

306 lines
11 KiB

  1. //
  2. // MODULE: FILEREAD.H
  3. //
  4. // PURPOSE: file reading classes
  5. //
  6. // COMPANY: Saltmine Creative, Inc. (206)-284-7511 [email protected]
  7. //
  8. // AUTHOR: Oleg Kalosha
  9. //
  10. // ORIGINAL DATE: 7-29-98
  11. //
  12. // NOTES:
  13. //
  14. // Version Date By Comments
  15. //--------------------------------------------------------------------
  16. // V3.0 08-04-98 OK
  17. // V3.1 01-08-99 JM improving abstraction so CHMs can be worked into this.
  18. //
  19. #ifndef __FILEREAD_H_
  20. #define __FILEREAD_H_
  21. #include "BaseException.h"
  22. #include "stateless.h"
  23. #include <sstream>
  24. #include <vector>
  25. using namespace std;
  26. namespace std {
  27. typedef basic_string<TCHAR> tstring;
  28. typedef basic_stringbuf<TCHAR> tstringbuf;
  29. typedef basic_istream<TCHAR> tistream;
  30. typedef basic_ostream<TCHAR> tostream;
  31. typedef basic_iostream<TCHAR> tiostream;
  32. typedef basic_istringstream<TCHAR> tistringstream;
  33. typedef basic_ostringstream<TCHAR> tostringstream;
  34. typedef basic_stringstream<TCHAR> tstringstream;
  35. };
  36. ////////////////////////////////////////////////////////////////////////////////////
  37. // CFileReaderException
  38. ////////////////////////////////////////////////////////////////////////////////////
  39. class CPhysicalFileReader;
  40. class CFileReader;
  41. class CFileReaderException : public CBaseException
  42. {
  43. public:
  44. enum eErr {eErrOpen,
  45. eErrClose,
  46. eErrRead,
  47. eErrAllocateToRead,
  48. eErrGetSize,
  49. eErrGetDateTime,
  50. eErrParse
  51. } m_eErr;
  52. protected:
  53. CPhysicalFileReader* m_pFileReader;
  54. public:
  55. // source_file is LPCSTR rather than LPCTSTR because __FILE__ is char[35]
  56. CFileReaderException(CPhysicalFileReader* reader, eErr err, LPCSTR source_file, int line);
  57. CFileReaderException(CFileReader* reader, eErr err, LPCSTR source_file, int line);
  58. virtual ~CFileReaderException();
  59. public:
  60. virtual void CloseFile();
  61. void LogEvent() const; // Function used to write CFileReader exceptions to the event log.
  62. };
  63. ////////////////////////////////////////////////////////////////////////////////////
  64. // CAbstractFileReader
  65. // This abstract class manages a file, which is initially read into a memory buffer, then
  66. // copied into a stream.
  67. // It can be renewed from stream without reading file.
  68. // It checks file for existance.
  69. // This class is abstract, in that it doesn't consider whether the file is in normal
  70. // file storage or in a CHM. It must be specialized to handle those two cases. Since
  71. // it must be specialized to one or the other, this class should never be directly instantiated.
  72. ////////////////////////////////////////////////////////////////////////////////////
  73. class CAbstractFileReader : public CStateless
  74. {
  75. private:
  76. bool m_bIsValid; // file data is consistent - no errors arose during reading and parsing
  77. bool m_bIsRead; // file has been read
  78. public:
  79. enum EFileTime {eFileTimeCreated, eFileTimeModified, eFileTimeAccessed};
  80. // static utilities
  81. static CString GetJustPath(const CString& full_path);
  82. static CString GetJustName(const CString& full_path);
  83. static CString GetJustNameWithoutExtension(const CString& full_path);
  84. static CString GetJustExtension(const CString& full_path);
  85. static bool GetFileTime(const CString& full_path, EFileTime type, time_t& out);
  86. public:
  87. CAbstractFileReader();
  88. ~CAbstractFileReader();
  89. public:
  90. virtual CString GetPathName() const =0;
  91. virtual CString GetJustPath() const =0;
  92. virtual CString GetJustName() const =0;
  93. virtual CString GetJustNameWithoutExtension() const =0;
  94. virtual CString GetJustExtension() const =0;
  95. virtual bool GetFileTime(EFileTime type, time_t& out) const =0;
  96. public:
  97. // I (Oleg) designed these functions to be the only way to perform file access.
  98. // That is, you cannot call (say) Open or ReadData.
  99. // The locking is designed accordingly.
  100. // In inherited classes there might be function to access results
  101. // of reading and parsing - in this case user is responsible for properly
  102. // locking the results while they are being used
  103. // These functions are NOT intended to be virtual and overridden!
  104. bool Exists();
  105. bool Read();
  106. bool IsRead() {return m_bIsRead;}
  107. bool IsValid() {return m_bIsValid;}
  108. protected:
  109. virtual void Open()=0;
  110. virtual void ReadData(LPTSTR * ppBuf) =0;
  111. virtual void StreamData(LPTSTR * ppBuf)=0;
  112. virtual void Parse()=0;
  113. virtual bool UseDefault()=0;
  114. virtual void Close()=0; // unlike CPhysicalFileReader::Close(), this throws exception if
  115. // it cannot close the file
  116. };
  117. ////////////////////////////////////////////////////////////////////////////////////
  118. // CPhysicalFileReader
  119. // This is an abstract class. Classes that provide physical access to a file should inherit
  120. // from this class.
  121. // A pointer to this class can be used by CFileReader to get a physical instantiation of file
  122. // access. The idea is that CPhysicalFileReader will have one descendant
  123. // (CNormalFileReader) to access files in normal directories and another
  124. // (CCHMFileReader) to access files drawn from a CHM.
  125. // CHMs don't arise in the Online Troubleshooter, but they do in the Local Troubleshooter.
  126. ////////////////////////////////////////////////////////////////////////////////////
  127. class CPhysicalFileReader
  128. {
  129. public:
  130. CPhysicalFileReader();
  131. virtual ~CPhysicalFileReader();
  132. static CPhysicalFileReader * makeReader( const CString& strFileName );
  133. protected:
  134. friend class CFileReader;
  135. friend class CFileReaderException;
  136. //
  137. // only CFileReader class is meant to access these functions
  138. virtual void Open()=0;
  139. virtual void ReadData(LPTSTR * ppBuf) =0;
  140. virtual bool CloseHandle()=0; // doesn't throw exception, therefore may be used by exception class.
  141. //
  142. public:
  143. virtual CString GetPathName() const =0;
  144. virtual CString GetJustPath() const =0;
  145. virtual CString GetJustName() const =0;
  146. virtual CString GetJustNameWithoutExtension() const =0;
  147. virtual CString GetJustExtension() const =0;
  148. virtual bool GetFileTime(CAbstractFileReader::EFileTime type, time_t& out) const =0;
  149. virtual CString GetNameToLog() const =0;
  150. };
  151. ////////////////////////////////////////////////////////////////////////////////////
  152. // CNormalFileReader
  153. // This class manages a file from ordinary storage.
  154. // Do not use this for files within a CHM
  155. ////////////////////////////////////////////////////////////////////////////////////
  156. class CNormalFileReader : public CPhysicalFileReader
  157. {
  158. private:
  159. CString m_strPath; // full path and name
  160. HANDLE m_hFile; // handle corresponding to m_strPath (if open)
  161. public:
  162. CNormalFileReader(LPCTSTR path);
  163. ~CNormalFileReader();
  164. protected:
  165. //
  166. // only CFileReader class is meant to access these functions
  167. virtual bool CloseHandle(); // doesn't throw exception, therefore may be used by exception class.
  168. virtual void Open();
  169. virtual void ReadData(LPTSTR * ppBuf);
  170. //
  171. public:
  172. // return full file path and its components
  173. CString GetPathName() const {return m_strPath;}
  174. CString GetJustPath() const;
  175. CString GetJustName() const;
  176. CString GetJustNameWithoutExtension() const;
  177. CString GetJustExtension() const;
  178. bool GetFileTime(CAbstractFileReader::EFileTime type, time_t& out) const;
  179. CString GetNameToLog() const;
  180. };
  181. ////////////////////////////////////////////////////////////////////////////////////
  182. // CFileReader
  183. // This class manages a file from ordinary storage, which is initially read into a memory buffer, then
  184. // copied into a stream.
  185. // It can be renewed from stream without reading file.
  186. // It checks file for existance.
  187. // Do not use this for files within a CHM
  188. ////////////////////////////////////////////////////////////////////////////////////
  189. class CFileReader : public CAbstractFileReader
  190. {
  191. private:
  192. CPhysicalFileReader *m_pPhysicalFileReader;
  193. bool m_bDeletePhysicalFileReader;
  194. public:
  195. CFileReader(CPhysicalFileReader * pPhysicalFileReader, bool bDeletePhysicalFileReader =true);
  196. ~CFileReader();
  197. public:
  198. // This function exists only so that CFileReaderException can reinterpret a CFileReader as
  199. // a CPhysicalFileReader
  200. CPhysicalFileReader * GetPhysicalFileReader() {return m_pPhysicalFileReader;}
  201. public:
  202. // return full file path and its components
  203. CString GetPathName() const {return m_pPhysicalFileReader->GetPathName();}
  204. CString GetJustPath() const {return m_pPhysicalFileReader->GetJustPath();}
  205. CString GetJustName() const {return m_pPhysicalFileReader->GetJustName();}
  206. CString GetJustNameWithoutExtension() const {return m_pPhysicalFileReader->GetJustNameWithoutExtension();}
  207. CString GetJustExtension() const {return m_pPhysicalFileReader->GetJustExtension();}
  208. bool GetFileTime(EFileTime type, time_t& out) const {return m_pPhysicalFileReader->GetFileTime(type, out);}
  209. public:
  210. tstring& GetContent(tstring&); // Data access in form of tstring
  211. CString& GetContent(CString&); // Data access in form of CString
  212. protected:
  213. virtual void Open() {m_pPhysicalFileReader->Open();}
  214. virtual void ReadData(LPTSTR * ppBuf) {m_pPhysicalFileReader->ReadData(ppBuf);}
  215. virtual void StreamData(LPTSTR * ppBuf);
  216. virtual void Parse(); // is empty for this class
  217. virtual bool UseDefault(); // is empty for this class
  218. virtual void Close();
  219. protected:
  220. tistringstream m_StreamData;
  221. };
  222. ////////////////////////////////////////////////////////////////////////////////////
  223. // CTextFileReader
  224. // Specialize CFileReader to a text file
  225. ////////////////////////////////////////////////////////////////////////////////////
  226. class CTextFileReader : public CFileReader
  227. {
  228. protected:
  229. static bool IsAmongSeparators(TCHAR separatorCandidate, const vector<TCHAR>& separator_arr);
  230. CString m_strDefaultContents; // default contents to use if there is no such file.
  231. public:
  232. // static utilities
  233. static void GetWords(const CString& text, vector<CString>& out, const vector<TCHAR>& separators); // extract words from string
  234. static long GetPos(tistream&);
  235. static bool SetPos(tistream&, long pos);
  236. static bool GetLine(tistream&, CString&);
  237. static bool Find(tistream&, const CString&, bool from_stream_begin =true);
  238. static bool NextLine(tistream&);
  239. static bool PrevLine(tistream&);
  240. static void SetAtLineBegin(tistream&);
  241. public:
  242. CTextFileReader(CPhysicalFileReader * pPhysicalFileReader, LPCTSTR szDefaultContents = NULL, bool bDeletePhysicalFileReader =true);
  243. ~CTextFileReader();
  244. #ifdef __DEBUG_CUSTOM
  245. public:
  246. #else
  247. protected:
  248. #endif
  249. long GetPos();
  250. bool SetPos(long pos);
  251. #ifdef __DEBUG_CUSTOM
  252. public:
  253. #else
  254. protected:
  255. #endif
  256. bool GetLine(CString&);
  257. bool Find(const CString&, bool from_stream_begin =true);
  258. bool NextLine();
  259. bool PrevLine();
  260. void SetAtLineBegin();
  261. protected:
  262. bool UseDefault(); // Note: not virtual. No further inheritance intended.
  263. };
  264. #endif __FILEREAD_H_