StreamFileWin32.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "StreamFileWin32.h"
  2. #include <assert.h>
  3. static __int64 Seek64(HANDLE hf, __int64 distance, DWORD MoveMethod)
  4. {
  5. LARGE_INTEGER li;
  6. li.QuadPart = distance;
  7. li.LowPart = SetFilePointer (hf, li.LowPart, &li.HighPart, MoveMethod);
  8. if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR)
  9. {
  10. li.QuadPart = -1;
  11. }
  12. return li.QuadPart;
  13. }
  14. FLAC__StreamEncoderWriteStatus Win32_Write(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
  15. {
  16. assert(bytes <= 4294967295U);
  17. HANDLE file = ((Win32_State *)client_data)->handle;
  18. if(bytes > 0)
  19. {
  20. assert(sizeof(FLAC__byte) == 1);
  21. DWORD bytesWritten = 0, bytesToWrite = (DWORD)bytes;
  22. BOOL result = WriteFile(file, buffer, bytesToWrite, &bytesWritten, NULL);
  23. ((Win32_State *)client_data)->bytesWritten += bytesWritten;
  24. if (!result)
  25. return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
  26. return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
  27. }
  28. else
  29. return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
  30. }
  31. FLAC__StreamEncoderSeekStatus Win32_Seek(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
  32. {
  33. HANDLE file = ((Win32_State *)client_data)->handle;
  34. __int64 result = Seek64(file, absolute_byte_offset, FILE_BEGIN);
  35. if (result == INVALID_SET_FILE_POINTER)
  36. return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
  37. else
  38. {
  39. return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
  40. }
  41. }
  42. FLAC__StreamEncoderTellStatus Win32_Tell(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
  43. {
  44. HANDLE file = ((Win32_State *)client_data)->handle;
  45. __int64 position = Seek64(file, 0, FILE_CURRENT);
  46. if (position == INVALID_SET_FILE_POINTER)
  47. return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
  48. else
  49. {
  50. *absolute_byte_offset=position;
  51. return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
  52. }
  53. }