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.

221 lines
6.1 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1996, Microsoft Corporation.
  4. //
  5. // File: fmapio.cxx
  6. //
  7. // Contents: A class to read lines from a unicode or an ascii file.
  8. //
  9. // History: 96/Jan/3 DwightKr Created
  10. // Aug 20 1996 SrikantS Moved from web
  11. //
  12. //----------------------------------------------------------------------------
  13. #include <pch.cxx>
  14. #pragma hdrstop
  15. //+---------------------------------------------------------------------------
  16. //
  17. // Member: CFileMapView::CFileMapView - public
  18. //
  19. // Synopsis: Maps a file in its entirety
  20. //
  21. // Arguments: [wcsFileName] - full path of file to map
  22. //
  23. // History: 96/Jan/03 DwightKr created
  24. //
  25. //----------------------------------------------------------------------------
  26. CFileMapView::CFileMapView( WCHAR const * wcsFileName ) :
  27. _hFile(INVALID_HANDLE_VALUE),
  28. _hMap(0),
  29. _pbBuffer(0),
  30. _cbBuffer(0),
  31. _IsUnicode(FALSE)
  32. {
  33. //
  34. // Open the file
  35. //
  36. _hFile = CreateFile( wcsFileName,
  37. GENERIC_READ,
  38. FILE_SHARE_READ,
  39. 0,
  40. OPEN_EXISTING,
  41. 0,
  42. 0 );
  43. if ( INVALID_HANDLE_VALUE == _hFile )
  44. {
  45. qutilDebugOut(( DEB_IWARN, "Unable to open %ws for mapping\n", wcsFileName ));
  46. THROW( CException() );
  47. }
  48. _cbBuffer = GetFileSize( _hFile, 0 );
  49. END_CONSTRUCTION(CFileMapView);
  50. }
  51. //+---------------------------------------------------------------------------
  52. //
  53. // Member: CFileMapView::~CFileMapView - public
  54. //
  55. // Synopsis: Release handles & unmap file
  56. //
  57. // History: 96/Jan/03 DwightKr created
  58. //
  59. //----------------------------------------------------------------------------
  60. CFileMapView::~CFileMapView()
  61. {
  62. if ( 0 != _pbBuffer )
  63. {
  64. UnmapViewOfFile( _pbBuffer );
  65. }
  66. if ( 0 != _hMap )
  67. {
  68. CloseHandle( _hMap );
  69. }
  70. if ( INVALID_HANDLE_VALUE != _hFile )
  71. {
  72. CloseHandle( _hFile );
  73. }
  74. }
  75. //+---------------------------------------------------------------------------
  76. //
  77. // Member: CFileMapView::Init - public
  78. //
  79. // Synopsis: Maps the file
  80. //
  81. // History: 96/Jan/03 DwightKr created
  82. //
  83. //----------------------------------------------------------------------------
  84. void CFileMapView::Init()
  85. {
  86. //
  87. // Create a map of the file
  88. //
  89. _hMap = CreateFileMapping( _hFile,
  90. 0,
  91. PAGE_READONLY,
  92. 0,
  93. _cbBuffer,
  94. 0 );
  95. if ( 0 == _hMap )
  96. {
  97. qutilDebugOut(( DEB_IWARN, "CreateFileMapping failed\n" ));
  98. THROW( CException() );
  99. }
  100. _pbBuffer = (BYTE *) MapViewOfFile( _hMap,
  101. FILE_MAP_READ,
  102. 0,
  103. 0,
  104. _cbBuffer );
  105. if ( 0 == _pbBuffer )
  106. {
  107. qutilDebugOut(( DEB_IWARN, "MapViewOfFile failed\n" ));
  108. THROW( CException() );
  109. }
  110. _IsUnicode = (GetBufferSize() > 3) && // At least one unicode character
  111. (_pbBuffer[0] == 0xFF) && // Begins with OxFF 0xFE
  112. (_pbBuffer[1] == 0xFE) &&
  113. ((GetBufferSize() & 1) == 0); // Must be an even # of bytes
  114. }
  115. //+---------------------------------------------------------------------------
  116. //
  117. // Member: CFileBuffer::CFileBuffer - public
  118. //
  119. // Synopsis: Constructor
  120. //
  121. // Arguments: [fileMap] - a mapped file
  122. //
  123. // History: 96/May/06 DwightKr created
  124. //
  125. //----------------------------------------------------------------------------
  126. CFileBuffer::CFileBuffer( CFileMapView & fileMap,
  127. UINT codePage )
  128. {
  129. //
  130. // We need to return unicode data from methods of this class. If the file
  131. // contains ASCII data, convert it to unicode here.
  132. //
  133. if ( !fileMap.IsUnicode() )
  134. {
  135. _cwcFileBuffer = MultiByteToXArrayWideChar( fileMap.GetBuffer(),
  136. fileMap.GetBufferSize(),
  137. codePage,
  138. _pwBuffer );
  139. _wcsNextLine = _pwBuffer.Get();
  140. }
  141. else
  142. {
  143. //
  144. // We have a unicode file. Skip past the leading 0xFF - 0xFE bytes
  145. //
  146. _wcsNextLine = (WCHAR *) (fileMap.GetBuffer() + 2);
  147. _cwcFileBuffer = (fileMap.GetBufferSize() - 2) / sizeof(WCHAR);
  148. }
  149. END_CONSTRUCTION(CFileBuffer);
  150. }
  151. //+---------------------------------------------------------------------------
  152. //
  153. // Member: CFileBuffer::fgetsw - public
  154. //
  155. // Synopsis: Gets the next line from the file
  156. //
  157. // Arguments: [wcsLine] - buffer to return next line into
  158. // [cwcLine] - size of the buffer in characters
  159. //
  160. // History: 96/May/06 DwightKr created
  161. //
  162. //----------------------------------------------------------------------------
  163. ULONG CFileBuffer::fgetsw( XGrowable<WCHAR> & xLine )
  164. {
  165. ULONG cwcCopied = 0;
  166. //
  167. // Copy characters upto either cwcLine, a CR, or the end of
  168. // the string.
  169. //
  170. while ( (_cwcFileBuffer > 0) )
  171. {
  172. xLine.SetSize( cwcCopied + 1 );
  173. xLine[ cwcCopied] = *_wcsNextLine;
  174. cwcCopied++;
  175. _wcsNextLine++;
  176. _cwcFileBuffer--;
  177. //
  178. // If we just copied over a CR, then break out of the
  179. // loop; we've found the end of a line.
  180. //
  181. if ( L'\n' == xLine[ cwcCopied - 1] )
  182. {
  183. break;
  184. }
  185. }
  186. xLine.SetSize( cwcCopied + 1 );
  187. xLine[ cwcCopied ] = 0; // Null terminate
  188. return cwcCopied;
  189. }