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.

160 lines
3.5 KiB

  1. //
  2. // Class for reading memory mapped files with a standard
  3. // stream interface like the fX C runtime. In NT 404
  4. // this class is at least 50% faster than the fopen routines.
  5. //
  6. #include <pch.cxx>
  7. #pragma hdrstop
  8. #include "mmfile.hxx"
  9. #define _CR 13
  10. #define _LF 10
  11. CMMFile::CMMFile(WCHAR const *szFile,BOOL fWrite)
  12. {
  13. ULONG ulSizeHigh;
  14. DWORD dwAccess,dwProtect,dwFileAccess,dwCreate;
  15. _hfFile = 0;
  16. _hMapping = 0;
  17. _pvView = 0;
  18. _fWrite = fWrite;
  19. _ulFileSize = 0;
  20. _ulChunk = MMF_CHUNK;
  21. if (fWrite)
  22. {
  23. dwFileAccess = GENERIC_READ | GENERIC_WRITE;
  24. dwCreate = OPEN_ALWAYS;
  25. dwProtect = PAGE_READWRITE;
  26. dwAccess = FILE_MAP_ALL_ACCESS;
  27. }
  28. else
  29. {
  30. dwFileAccess = GENERIC_READ;
  31. dwCreate = OPEN_EXISTING;
  32. dwProtect = PAGE_READONLY;
  33. dwAccess = FILE_MAP_READ;
  34. }
  35. if (INVALID_HANDLE_VALUE !=
  36. (_hfFile = CreateFile(szFile,dwFileAccess,FILE_SHARE_READ,0,dwCreate,FILE_ATTRIBUTE_NORMAL,0)))
  37. {
  38. if (_ulFileSize = GetFileSize(_hfFile,&ulSizeHigh))
  39. {
  40. if (_hMapping = CreateFileMapping((HANDLE) _hfFile,NULL,dwProtect,0,0,NULL))
  41. _pvView = (BYTE *) MapViewOfFile(_hMapping,dwAccess,0,0,0);
  42. if (!(_hMapping && _pvView))
  43. {
  44. if (_pvView)
  45. UnmapViewOfFile(_pvView);
  46. _pvView = 0;
  47. if (_hMapping)
  48. CloseHandle(_hMapping);
  49. _hMapping = 0;
  50. if (_hfFile)
  51. CloseHandle(_hfFile);
  52. _hfFile = 0;
  53. }
  54. }
  55. }
  56. if ( INVALID_HANDLE_VALUE == _hfFile )
  57. _hfFile = 0;
  58. _pcCurrent = (char *) _pvView;
  59. } //CMMFile
  60. CMMFile::~CMMFile(void)
  61. {
  62. if (_pvView)
  63. UnmapViewOfFile(_pvView);
  64. if (_hMapping)
  65. CloseHandle(_hMapping);
  66. if (_hfFile)
  67. CloseHandle(_hfFile);
  68. _hfFile = 0;
  69. } //~CMMFile
  70. //
  71. // clone c runtime fgets()
  72. //
  73. BOOL CMMFile::GetS(char *sz,int iMaxLen)
  74. {
  75. BOOL fRet;
  76. int i,ic;
  77. if (iMaxLen)
  78. {
  79. *sz = 0;
  80. iMaxLen--;
  81. }
  82. if (isEOF())
  83. fRet = FALSE;
  84. else
  85. {
  86. fRet = TRUE;
  87. for (i=0; (i < iMaxLen) &&
  88. ((sz[i] = (char) GetChar()) != MMF_EOF) &&
  89. (sz[i] != _CR) &&
  90. (sz[i] != _LF);
  91. i++)
  92. ;
  93. if (sz[i] == MMF_EOF)
  94. sz[i] = 0;
  95. else
  96. {
  97. if ((ic = GetChar()) != _LF)
  98. UnGetChar(ic);
  99. if (sz[i] == _CR)
  100. sz[i] = _LF;
  101. sz[i+1] = 0;
  102. }
  103. }
  104. return(fRet);
  105. } //GetS
  106. ULONG CMMFile::Grow(ULONG ulNewSize)
  107. {
  108. ULONG ulOffset;
  109. ulNewSize = _RoundUpToChunk(ulNewSize);
  110. if (_ulFileSize < ulNewSize)
  111. {
  112. if (_pvView)
  113. {
  114. ulOffset = (ULONG) (_pcCurrent - (char *) _pvView);
  115. UnmapViewOfFile(_pvView);
  116. _pvView = NULL;
  117. }
  118. else ulOffset = 0L;
  119. if (_hMapping)
  120. {
  121. CloseHandle(_hMapping);
  122. _hMapping = 0;
  123. }
  124. _ulFileSize = ulNewSize;
  125. if (_hMapping = CreateFileMapping(_hfFile,NULL,PAGE_READWRITE,0,_ulFileSize,NULL))
  126. _pvView = MapViewOfFile(_hMapping,FILE_MAP_ALL_ACCESS,0,0,0);
  127. _pcCurrent = (char *) _pvView + ulOffset;
  128. }
  129. return(_ulFileSize);
  130. } //Grow
  131. int CMMFile::PutJustS(char *sz)
  132. {
  133. ULONG ulLen = strlen(sz);
  134. Grow(Tell() + ulLen);
  135. memcpy(_pcCurrent,sz,ulLen);
  136. _pcCurrent += ulLen;
  137. return((int) ulLen);
  138. } //PutS