StdioFileEx.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // StdioFileEx.h: interface for the CStdioFileEx class.
  2. //
  3. // Version 1.1 23 August 2003. Incorporated fixes from Dennis Jeryd.
  4. // Version 1.3 19 February 2005. Incorporated fixes Howard J Oh and some of my own.
  5. //
  6. // Copyright David Pritchard 2003-2004. [email protected]
  7. //
  8. // You can use this class freely, but please keep my ego happy
  9. // by leaving this comment in place.
  10. //
  11. //////////////////////////////////////////////////////////////////////
  12. #if !defined(AFX_STDIOFILEEX_H__41AFE3CA_25E0_482F_8B00_C40775BCDB81__INCLUDED_)
  13. #define AFX_STDIOFILEEX_H__41AFE3CA_25E0_482F_8B00_C40775BCDB81__INCLUDED_
  14. #if _MSC_VER > 1000
  15. #pragma once
  16. #endif // _MSC_VER > 1000
  17. #define nUNICODE_BOM 0xFEFF // Unicode "byte order mark" which goes at start of file
  18. #define sNEWLINE _T("\r\n") // New line characters
  19. #define sDEFAULT_UNICODE_FILLER_CHAR "#" // Filler char used when no conversion from Unicode to local code page is possible
  20. class CStdioFileEx: public CStdioFile
  21. {
  22. public:
  23. CStdioFileEx();
  24. CStdioFileEx( LPCTSTR lpszFileName, UINT nOpenFlags );
  25. virtual BOOL Open( LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL );
  26. virtual BOOL ReadString(CString& rString);
  27. virtual void WriteString( LPCTSTR lpsz );
  28. bool IsFileUnicodeText() { return m_bIsUnicodeText; }
  29. unsigned long GetCharCount();
  30. // Additional flag to allow Unicode text writing
  31. static const UINT modeWriteUnicode;
  32. // static utility functions
  33. // --------------------------------------------------------------------------------------------
  34. //
  35. // CStdioFileEx::GetUnicodeStringFromMultiByteString()
  36. //
  37. // --------------------------------------------------------------------------------------------
  38. // Returns: int - num. of chars written (0 means error)
  39. // Parameters: char * szMultiByteString (IN) Multi-byte input string
  40. // wchar_t* szUnicodeString (OUT) Unicode outputstring
  41. // int& nUnicodeBufferSize (IN/OUT) Size of Unicode output buffer(IN)
  42. // Actual bytes written to buffer (OUT)
  43. // UINT nCodePage (IN) Code page used to perform conversion
  44. // Default = -1 (Get local code page).
  45. //
  46. // Purpose: Gets a Unicode string from a MultiByte string.
  47. // Notes: None.
  48. // Exceptions: None.
  49. //
  50. static int GetUnicodeStringFromMultiByteString(IN char * szMultiByteString, OUT wchar_t* szUnicodeString, IN OUT int& nUnicodeBufferSize, IN UINT nCodePage=-1);
  51. // --------------------------------------------------------------------------------------------
  52. //
  53. // CStdioFileEx::GetMultiByteStringFromUnicodeString()
  54. //
  55. // --------------------------------------------------------------------------------------------
  56. // Returns: int - number of characters written. 0 means error
  57. // Parameters: wchar_t * szUnicodeString (IN) Unicode input string
  58. // char* szMultiByteString (OUT) Multibyte output string
  59. // short nMultiByteBufferSize (IN) Multibyte buffer size
  60. // UINT nCodePage (IN) Code page used to perform conversion
  61. // Default = -1 (Get local code page).
  62. //
  63. // Purpose: Gets a MultiByte string from a Unicode string.
  64. // Notes: .
  65. // Exceptions: None.
  66. //
  67. static int GetMultiByteStringFromUnicodeString(wchar_t * szUnicodeString,char* szMultiByteString,
  68. short nMultiByteBufferSize,UINT nCodePage=-1);
  69. // --------------------------------------------------------------------------------------------
  70. //
  71. // CStdioFileEx::IsFileUnicode()
  72. //
  73. // --------------------------------------------------------------------------------------------
  74. // Returns: bool
  75. // Parameters: const CString& sFilePath
  76. //
  77. // Purpose: Determines whether a file is Unicode by reading the first character and detecting
  78. // whether it's the Unicode byte marker.
  79. // Notes: None.
  80. // Exceptions: None.
  81. //
  82. static bool IsFileUnicode(const CString& sFilePath);
  83. protected:
  84. UINT ProcessFlags(const CString& sFilePath, UINT& nOpenFlags);
  85. bool m_bIsUnicodeText;
  86. UINT m_nFlags;
  87. };
  88. #endif // !defined(AFX_STDIOFILEEX_H__41AFE3CA_25E0_482F_8B00_C40775BCDB81__INCLUDED_)