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.

230 lines
4.8 KiB

  1. ////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // File: fileio.cpp
  4. //
  5. // History: 06-Apr-01 markder Created.
  6. //
  7. // Desc: This file contains classes to encapsulate MBCS and UNICODE files.
  8. //
  9. ////////////////////////////////////////////////////////////////////////////////////
  10. #include "StdAfx.h"
  11. #include "FileIO.h"
  12. CTextFile::CTextFile( LPCTSTR lpszFileName, UINT nOpenFlags ) :
  13. CFile(lpszFileName, nOpenFlags)
  14. {
  15. }
  16. CANSITextFile::CANSITextFile(LPCTSTR lpszFileName, UINT dwCodePage, UINT nOpenFlags) :
  17. CTextFile(lpszFileName, (nOpenFlags & (~CFile::typeText)) | CFile::typeBinary)
  18. {
  19. m_dwCodePage = dwCodePage;
  20. }
  21. CUTF8TextFile::CUTF8TextFile(LPCTSTR lpszFileName, UINT nOpenFlags) :
  22. CTextFile(lpszFileName, (nOpenFlags & (~CFile::typeText)) | CFile::typeBinary)
  23. {
  24. UCHAR EncodingMarker[3];
  25. EncodingMarker[0] = (UCHAR) 0xEF;
  26. EncodingMarker[1] = (UCHAR) 0xBB;
  27. EncodingMarker[2] = (UCHAR) 0xBF;
  28. Write(&EncodingMarker, 3);
  29. }
  30. CUTF16TextFile::CUTF16TextFile(LPCTSTR lpszFileName, UINT nOpenFlags) :
  31. CTextFile(lpszFileName, (nOpenFlags & (~CFile::typeText)) | CFile::typeBinary)
  32. {
  33. UCHAR EncodingMarker[2];
  34. EncodingMarker[0] = (UCHAR) 0xFF;
  35. EncodingMarker[1] = (UCHAR) 0xFE;
  36. Write(&EncodingMarker, 2);
  37. }
  38. void CANSITextFile::WriteString(LPCTSTR lpsz)
  39. {
  40. LPVOID lpBuf = NULL;
  41. LPVOID lpAllocatedBuffer = NULL;
  42. LONG nBufSize = 0;
  43. LONG nBytesToWrite = 0;
  44. CString csStringToWrite(lpsz);
  45. csStringToWrite.Replace(_T("\012"), _T("\015\012"));
  46. #ifdef UNICODE
  47. nBufSize = WideCharToMultiByte(
  48. m_dwCodePage,
  49. 0,
  50. csStringToWrite,
  51. -1,
  52. NULL,
  53. 0,
  54. NULL,
  55. NULL
  56. );
  57. lpAllocatedBuffer = malloc(nBufSize);
  58. if (lpAllocatedBuffer == NULL) {
  59. AfxThrowMemoryException();
  60. }
  61. lpBuf = lpAllocatedBuffer;
  62. nBytesToWrite = WideCharToMultiByte(
  63. m_dwCodePage,
  64. 0,
  65. csStringToWrite,
  66. -1,
  67. (LPSTR) lpBuf,
  68. nBufSize,
  69. NULL,
  70. NULL
  71. );
  72. //
  73. // Take off NULL terminator
  74. //
  75. nBytesToWrite--;
  76. #else
  77. lpBuf = (LPVOID) (LPCSTR) csStringToWrite;
  78. nBytesToWrite = strlen(csStringToWrite); // we use strlen to get total bytes
  79. #endif
  80. if (nBytesToWrite == 0) {
  81. goto eh;
  82. }
  83. Write(lpBuf, nBytesToWrite);
  84. eh:
  85. if (lpAllocatedBuffer) {
  86. free(lpAllocatedBuffer);
  87. }
  88. }
  89. void CUTF8TextFile::WriteString(LPCTSTR lpsz)
  90. {
  91. LPVOID lpBuf = NULL;
  92. LPVOID lpAllocatedBuffer = NULL;
  93. LONG nBufSize = 0;
  94. LONG nBytesToWrite = 0;
  95. CString csStringToWrite(lpsz);
  96. csStringToWrite.Replace(_T("\012"), _T("\015\012"));
  97. #ifdef UNICODE
  98. nBufSize = WideCharToMultiByte(
  99. CP_UTF8,
  100. 0,
  101. csStringToWrite,
  102. -1,
  103. NULL,
  104. 0,
  105. NULL,
  106. NULL
  107. );
  108. lpAllocatedBuffer = malloc(nBufSize);
  109. if (lpAllocatedBuffer == NULL) {
  110. AfxThrowMemoryException();
  111. }
  112. lpBuf = lpAllocatedBuffer;
  113. nBytesToWrite = WideCharToMultiByte(
  114. CP_UTF8,
  115. 0,
  116. csStringToWrite,
  117. -1,
  118. (LPSTR) lpBuf,
  119. nBufSize,
  120. NULL,
  121. NULL
  122. );
  123. //
  124. // Take off NULL terminator
  125. //
  126. nBytesToWrite--;
  127. #else
  128. lpBuf = (LPVOID) (LPCSTR) csStringToWrite;
  129. nBytesToWrite = strlen(csStringToWrite); // we use strlen to get total bytes
  130. #endif
  131. if (nBytesToWrite == 0) {
  132. goto eh;
  133. }
  134. Write(lpBuf, nBytesToWrite);
  135. eh:
  136. if (lpAllocatedBuffer) {
  137. free(lpAllocatedBuffer);
  138. }
  139. }
  140. void CUTF16TextFile::WriteString(LPCTSTR lpsz)
  141. {
  142. LPVOID lpBuf = NULL;
  143. LPVOID lpAllocatedBuffer = NULL;
  144. LONG nBufSize = 0;
  145. LONG nBytesToWrite = 0;
  146. CString csStringToWrite(lpsz);
  147. csStringToWrite.Replace(_T("\012"), _T("\015\012"));
  148. #ifdef UNICODE
  149. lpBuf = (LPVOID) (LPCWSTR) csStringToWrite;
  150. nBytesToWrite = csStringToWrite.GetLength() * sizeof(WCHAR);
  151. #else
  152. nBufSize = MultiByteToWideChar(
  153. CP_ACP,
  154. 0,
  155. csStringToWrite,
  156. -1,
  157. NULL,
  158. 0);
  159. lpAllocatedBuffer = malloc(nBufSize);
  160. if (lpAllocatedBuffer == NULL) {
  161. AfxThrowMemoryException();
  162. }
  163. lpBuf = lpAllocatedBuffer;
  164. nBytesToWrite = MultiByteToWideChar(
  165. CP_ACP,
  166. 0,
  167. csStringToWrite,
  168. -1,
  169. lpBuf,
  170. nBufSize);
  171. //
  172. // Take off NULL terminator
  173. //
  174. nBytesToWrite -= sizeof(WCHAR);
  175. #endif
  176. if (nBytesToWrite == 0) {
  177. goto eh;
  178. }
  179. Write(lpBuf, nBytesToWrite);
  180. eh:
  181. if (lpAllocatedBuffer) {
  182. free(lpAllocatedBuffer);
  183. }
  184. }