libopenmpt  0.4.10+release
cross-platform C++ and C library to decode tracked music files
libopenmpt_stream_callbacks_file.h
Go to the documentation of this file.
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 
10 #ifndef LIBOPENMPT_STREAM_CALLBACKS_FILE_H
11 #define LIBOPENMPT_STREAM_CALLBACKS_FILE_H
12 
13 #include "libopenmpt.h"
14 
15 #include <limits.h>
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <string.h>
19 #ifdef _MSC_VER
20 #include <wchar.h> /* off_t */
21 #endif
22 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /* This stuff has to be in a header file because of possibly different MSVC CRTs which cause problems for FILE * crossing CRT boundaries. */
32 
33 static size_t openmpt_stream_file_read_func( void * stream, void * dst, size_t bytes ) {
34  FILE * f = 0;
35  size_t retval = 0;
36  f = (FILE*)stream;
37  if ( !f ) {
38  return 0;
39  }
40  retval = fread( dst, 1, bytes, f );
41  if ( retval <= 0 ) {
42  return 0;
43  }
44  return retval;
45 }
46 
47 static int openmpt_stream_file_seek_func( void * stream, int64_t offset, int whence ) {
48  FILE * f = 0;
49  int fwhence = 0;
50  f = (FILE*)stream;
51  if ( !f ) {
52  return -1;
53  }
54  switch ( whence ) {
55 #if defined(SEEK_SET)
57  fwhence = SEEK_SET;
58  break;
59 #endif
60 #if defined(SEEK_CUR)
62  fwhence = SEEK_CUR;
63  break;
64 #endif
65 #if defined(SEEK_END)
67  fwhence = SEEK_END;
68  break;
69 #endif
70  default:
71  return -1;
72  break;
73  }
74  #if defined(_MSC_VER)
75  return _fseeki64( f, offset, fwhence ) ? -1 : 0;
76  #elif defined(_POSIX_SOURCE) && (_POSIX_SOURCE == 1)
77  return fseeko( f, offset, fwhence ) ? -1 : 0;
78  #else
79  return fseek( f, offset, fwhence ) ? -1 : 0;
80  #endif
81 }
82 
83 static int64_t openmpt_stream_file_tell_func( void * stream ) {
84  FILE * f = 0;
85  int64_t retval = 0;
86  f = (FILE*)stream;
87  if ( !f ) {
88  return -1;
89  }
90  #if defined(_MSC_VER)
91  retval = _ftelli64( f );
92  #elif defined(_POSIX_SOURCE) && (_POSIX_SOURCE == 1)
93  retval = ftello( f );
94  #else
95  retval = ftell( f );
96  #endif
97  if ( retval < 0 ) {
98  return -1;
99  }
100  return retval;
101 }
102 
116  memset( &retval, 0, sizeof( openmpt_stream_callbacks ) );
120  return retval;
121 }
122 
123 #ifdef __cplusplus
124 }
125 #endif
126 
131 #endif /* LIBOPENMPT_STREAM_CALLBACKS_FILE_H */
132 
static int openmpt_stream_file_seek_func(void *stream, int64_t offset, int whence)
Definition: libopenmpt_stream_callbacks_file.h:47
#define OPENMPT_STREAM_SEEK_CUR
Definition: libopenmpt.h:255
#define OPENMPT_STREAM_SEEK_SET
Definition: libopenmpt.h:253
openmpt_stream_tell_func tell
Tell callback.
Definition: libopenmpt.h:320
#define OPENMPT_STREAM_SEEK_END
Definition: libopenmpt.h:257
Stream callbacks.
Definition: libopenmpt.h:300
openmpt_stream_read_func read
Read callback.
Definition: libopenmpt.h:306
static size_t openmpt_stream_file_read_func(void *stream, void *dst, size_t bytes)
Definition: libopenmpt_stream_callbacks_file.h:33
static int64_t openmpt_stream_file_tell_func(void *stream)
Definition: libopenmpt_stream_callbacks_file.h:83
openmpt_stream_seek_func seek
Seek callback.
Definition: libopenmpt.h:313
static openmpt_stream_callbacks openmpt_stream_get_file_callbacks(void)
Provide openmpt_stream_callbacks for standard C FILE objects.
Definition: libopenmpt_stream_callbacks_file.h:114