libopenmpt_stream_callbacks_file.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * libopenmpt_stream_callbacks_file.h
  3. * ----------------------------------
  4. * Purpose: libopenmpt public c interface
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #ifndef LIBOPENMPT_STREAM_CALLBACKS_FILE_H
  10. #define LIBOPENMPT_STREAM_CALLBACKS_FILE_H
  11. #include "libopenmpt.h"
  12. #include <limits.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #ifdef _MSC_VER
  17. #include <wchar.h> /* off_t */
  18. #endif
  19. /*! \addtogroup libopenmpt_c
  20. * @{
  21. */
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* This stuff has to be in a header file because of possibly different MSVC CRTs which cause problems for FILE * crossing CRT boundaries. */
  26. static size_t openmpt_stream_file_read_func( void * stream, void * dst, size_t bytes ) {
  27. FILE * f = 0;
  28. size_t retval = 0;
  29. f = (FILE*)stream;
  30. if ( !f ) {
  31. return 0;
  32. }
  33. retval = fread( dst, 1, bytes, f );
  34. if ( retval <= 0 ) {
  35. return 0;
  36. }
  37. return retval;
  38. }
  39. static int openmpt_stream_file_seek_func( void * stream, int64_t offset, int whence ) {
  40. FILE * f = 0;
  41. int fwhence = 0;
  42. f = (FILE*)stream;
  43. if ( !f ) {
  44. return -1;
  45. }
  46. switch ( whence ) {
  47. #if defined(SEEK_SET)
  48. case OPENMPT_STREAM_SEEK_SET:
  49. fwhence = SEEK_SET;
  50. break;
  51. #endif
  52. #if defined(SEEK_CUR)
  53. case OPENMPT_STREAM_SEEK_CUR:
  54. fwhence = SEEK_CUR;
  55. break;
  56. #endif
  57. #if defined(SEEK_END)
  58. case OPENMPT_STREAM_SEEK_END:
  59. fwhence = SEEK_END;
  60. break;
  61. #endif
  62. default:
  63. return -1;
  64. break;
  65. }
  66. #if defined(_MSC_VER)
  67. return _fseeki64( f, offset, fwhence ) ? -1 : 0;
  68. #elif defined(_POSIX_SOURCE) && (_POSIX_SOURCE == 1)
  69. return fseeko( f, offset, fwhence ) ? -1 : 0;
  70. #else
  71. return fseek( f, offset, fwhence ) ? -1 : 0;
  72. #endif
  73. }
  74. static int64_t openmpt_stream_file_tell_func( void * stream ) {
  75. FILE * f = 0;
  76. int64_t retval = 0;
  77. f = (FILE*)stream;
  78. if ( !f ) {
  79. return -1;
  80. }
  81. #if defined(_MSC_VER)
  82. retval = _ftelli64( f );
  83. #elif defined(_POSIX_SOURCE) && (_POSIX_SOURCE == 1)
  84. retval = ftello( f );
  85. #else
  86. retval = ftell( f );
  87. #endif
  88. if ( retval < 0 ) {
  89. return -1;
  90. }
  91. return retval;
  92. }
  93. /*! \brief Provide openmpt_stream_callbacks for standard C FILE objects
  94. *
  95. * Fills openmpt_stream_callbacks suitable for passing a standard C FILE object as a stream parameter to functions doing file input/output.
  96. *
  97. * \remarks The stream argument must be passed as `(void*)(FILE*)file`.
  98. * \sa \ref libopenmpt_c_fileio
  99. * \sa openmpt_stream_callbacks
  100. * \sa openmpt_could_open_probability2
  101. * \sa openmpt_probe_file_header_from_stream
  102. * \sa openmpt_module_create2
  103. */
  104. static openmpt_stream_callbacks openmpt_stream_get_file_callbacks(void) {
  105. openmpt_stream_callbacks retval;
  106. memset( &retval, 0, sizeof( openmpt_stream_callbacks ) );
  107. retval.read = openmpt_stream_file_read_func;
  108. retval.seek = openmpt_stream_file_seek_func;
  109. retval.tell = openmpt_stream_file_tell_func;
  110. return retval;
  111. }
  112. #ifdef __cplusplus
  113. }
  114. #endif
  115. /*!
  116. * @}
  117. */
  118. #endif /* LIBOPENMPT_STREAM_CALLBACKS_FILE_H */