/src/sources/avi/File64.cpp

https://github.com/jeeb/avisynth · C++ · 166 lines · 99 code · 46 blank · 21 comment · 31 complexity · 5386030b719455c95ce76db20f635fe3 MD5 · raw file

  1. // Packaged with Avisynth v1.0 beta.
  2. // http://www.math.berkeley.edu/~benrg/avisynth.html
  3. // VirtualDub - Video processing and capture application
  4. // Copyright (C) 1998-2000 Avery Lee
  5. //
  6. // This program is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation; either version 2 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. #include "stdafx.h"
  20. #include "../../core/Error.h"
  21. #include "File64.h"
  22. // hack...
  23. extern CRITICAL_SECTION g_diskcs;
  24. ////////////
  25. File64::File64() {
  26. }
  27. File64::File64(HANDLE _hFile, HANDLE _hFileUnbuffered)
  28. : hFile(_hFile), hFileUnbuffered(_hFileUnbuffered)
  29. {
  30. i64FilePosition = 0;
  31. }
  32. long File64::_readFile(void *data, long len) {
  33. DWORD dwActual;
  34. if (!ReadFile(hFile, data, len, &dwActual, NULL))
  35. return -1;
  36. i64FilePosition += dwActual;
  37. return (long)dwActual;
  38. }
  39. void File64::_readFile2(void *data, long len) {
  40. long lActual = _readFile(data, len);
  41. if (lActual < 0)
  42. throw MyWin32Error("Failure reading file: %%s.",GetLastError());
  43. if (lActual != len)
  44. throw MyError("Failure reading file: Unexpected end of file");
  45. }
  46. bool File64::_readChunkHeader(FOURCC& pfcc, DWORD& pdwLen) {
  47. DWORD dw[2];
  48. long actual;
  49. actual = _readFile(dw, 8);
  50. if (actual != 8)
  51. return false;
  52. pfcc = dw[0];
  53. pdwLen = dw[1];
  54. return true;
  55. }
  56. void File64::_seekFile(__int64 i64NewPos) {
  57. LONG lHi = (LONG)(i64NewPos>>32);
  58. DWORD dwError;
  59. if (0xFFFFFFFF == SetFilePointer(hFile, (LONG)i64NewPos, &lHi, FILE_BEGIN))
  60. if ((dwError = GetLastError()) != NO_ERROR)
  61. throw MyWin32Error("File64: %%s", dwError);
  62. i64FilePosition = i64NewPos;
  63. }
  64. bool File64::_seekFile2(__int64 i64NewPos) {
  65. LONG lHi = (LONG)(i64NewPos>>32);
  66. DWORD dwError;
  67. // _RPT1(0,"Seeking to %I64d\n", i64NewPos);
  68. if (0xFFFFFFFF == SetFilePointer(hFile, (LONG)i64NewPos, &lHi, FILE_BEGIN))
  69. if ((dwError = GetLastError()) != NO_ERROR)
  70. return false;
  71. i64FilePosition = i64NewPos;
  72. return true;
  73. }
  74. void File64::_skipFile(__int64 bytes) {
  75. LONG lHi = (LONG)(bytes>>32);
  76. DWORD dwError;
  77. LONG lNewLow;
  78. if (0xFFFFFFFF == (lNewLow = SetFilePointer(hFile, (LONG)bytes, &lHi, FILE_CURRENT)))
  79. if ((dwError = GetLastError()) != NO_ERROR)
  80. throw MyWin32Error("File64: %%s", dwError);
  81. i64FilePosition = (unsigned long)lNewLow | (((__int64)(unsigned long)lHi)<<32);
  82. }
  83. bool File64::_skipFile2(__int64 bytes) {
  84. LONG lHi = (LONG)(bytes>>32);
  85. DWORD dwError;
  86. LONG lNewLow;
  87. if (0xFFFFFFFF == (lNewLow = SetFilePointer(hFile, (LONG)bytes, &lHi, FILE_CURRENT)))
  88. if ((dwError = GetLastError()) != NO_ERROR)
  89. return false;
  90. i64FilePosition = (unsigned long)lNewLow | (((__int64)(unsigned long)lHi)<<32);
  91. return true;
  92. }
  93. long File64::_readFileUnbuffered(void *data, long len) {
  94. DWORD dwActual;
  95. EnterCriticalSection(&g_diskcs);
  96. if (!ReadFile(hFileUnbuffered, data, len, &dwActual, NULL)) {
  97. LeaveCriticalSection(&g_diskcs);
  98. return -1;
  99. }
  100. LeaveCriticalSection(&g_diskcs);
  101. return (long)dwActual;
  102. }
  103. void File64::_seekFileUnbuffered(__int64 i64NewPos) {
  104. LONG lHi = (LONG)(i64NewPos>>32);
  105. DWORD dwError;
  106. if (0xFFFFFFFF == SetFilePointer(hFileUnbuffered, (LONG)i64NewPos, &lHi, FILE_BEGIN))
  107. if ((dwError = GetLastError()) != NO_ERROR)
  108. throw MyWin32Error("File64: %%s", dwError);
  109. }
  110. __int64 File64::_posFile() {
  111. return i64FilePosition;
  112. }
  113. __int64 File64::_sizeFile() {
  114. DWORD dwLow, dwHigh;
  115. DWORD dwError;
  116. dwLow = GetFileSize(hFile, &dwHigh);
  117. if (dwLow == 0xFFFFFFFF && (dwError = GetLastError()) != NO_ERROR)
  118. throw MyWin32Error("Cannot determine file size: %%s", dwError);
  119. return ((__int64)dwHigh << 32) | (unsigned long)dwLow;
  120. }