libopenmpt: libopenmpt/libopenmpt_stream_callbacks_file.h Source File

libopenmpt 0.8.5-pre.0+r24758 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 <stdint.h>
16#include <stdio.h>
17#include <string.h>
18
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27/* This stuff has to be in a header file because of possibly different MSVC CRTs which cause problems for FILE * crossing CRT boundaries. */
28
29 static size_t openmpt_stream_file_read_func( void * stream, void * dst, size_t bytes ) {
30 FILE * f = 0;
31 size_t retval = 0;
32 f = (FILE*)stream;
33 if ( !f ) {
34 return 0;
35 }
36 retval = fread( dst, 1, bytes, f );
37 if ( retval <= 0 ) {
38 return 0;
39 }
40 return retval;
41}
42
43 static int openmpt_stream_file_seek_func( void * stream, int64_t offset, int whence ) {
44 FILE * f = 0;
45 int fwhence = 0;
46 f = (FILE*)stream;
47 if ( !f ) {
48 return -1;
49 }
50 switch ( whence ) {
51 case OPENMPT_STREAM_SEEK_SET:
52 fwhence = SEEK_SET;
53 break;
54 case OPENMPT_STREAM_SEEK_CUR:
55 fwhence = SEEK_CUR;
56 break;
57 case OPENMPT_STREAM_SEEK_END:
58 fwhence = SEEK_END;
59 break;
60 default:
61 return -1;
62 break;
63 }
64 if ( (long)offset != offset ) {
65 return -1;
66 }
67 return fseek( f, (long)offset, fwhence ) ? -1 : 0;
68}
69
70 static int64_t openmpt_stream_file_tell_func( void * stream ) {
71 FILE * f = 0;
72 long result = 0;
73 int64_t retval = 0;
74 f = (FILE*)stream;
75 if ( !f ) {
76 return -1;
77 }
78 result = ftell( f );
79 if ( (int64_t)result != result ) {
80 return -1;
81 }
82 retval = (int64_t)result;
83 if ( retval < 0 ) {
84 return -1;
85 }
86 return retval;
87}
88
109 LIBOPENMPT_DEPRECATED static LIBOPENMPT_C_INLINE openmpt_stream_callbacks openmpt_stream_get_file_callbacks(void) {
110 openmpt_stream_callbacks retval;
111 memset( &retval, 0, sizeof( openmpt_stream_callbacks ) );
112 retval.read = openmpt_stream_file_read_func;
113 retval.seek = openmpt_stream_file_seek_func;
114 retval.tell = openmpt_stream_file_tell_func;
115 return retval;
116}
117
132 static openmpt_stream_callbacks openmpt_stream_get_file_callbacks2(void) {
133 openmpt_stream_callbacks retval;
134 memset( &retval, 0, sizeof( openmpt_stream_callbacks ) );
135 retval.read = openmpt_stream_file_read_func;
136 retval.seek = openmpt_stream_file_seek_func;
137 retval.tell = openmpt_stream_file_tell_func;
138 return retval;
139}
140
141#ifdef __cplusplus
142}
143#endif
144
149#endif /* LIBOPENMPT_STREAM_CALLBACKS_FILE_H */
openmpt_stream_get_file_callbacks2
static openmpt_stream_callbacks openmpt_stream_get_file_callbacks2(void)
Provide openmpt_stream_callbacks for standard C FILE objects.
Definition: libopenmpt_stream_callbacks_file.h:132
OPENMPT_STREAM_SEEK_SET
#define OPENMPT_STREAM_SEEK_SET
Definition: libopenmpt.h:253
OPENMPT_STREAM_SEEK_CUR
#define OPENMPT_STREAM_SEEK_CUR
Definition: libopenmpt.h:255
openmpt_stream_file_seek_func
static int openmpt_stream_file_seek_func(void *stream, int64_t offset, int whence)
Definition: libopenmpt_stream_callbacks_file.h:43
openmpt_stream_file_read_func
static size_t openmpt_stream_file_read_func(void *stream, void *dst, size_t bytes)
Definition: libopenmpt_stream_callbacks_file.h:29
OPENMPT_STREAM_SEEK_END
#define OPENMPT_STREAM_SEEK_END
Definition: libopenmpt.h:257
openmpt_stream_get_file_callbacks
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:109
openmpt_stream_file_tell_func
static int64_t openmpt_stream_file_tell_func(void *stream)
Definition: libopenmpt_stream_callbacks_file.h:70
libopenmpt.h
LIBOPENMPT_DEPRECATED
#define LIBOPENMPT_DEPRECATED
Definition: libopenmpt_config.h:315
LIBOPENMPT_C_INLINE
#define LIBOPENMPT_C_INLINE
Definition: libopenmpt_config.h:339
openmpt_stream_callbacks
Stream callbacks.
Definition: libopenmpt.h:300
openmpt_stream_callbacks::read
openmpt_stream_read_func read
Read callback.
Definition: libopenmpt.h:306
openmpt_stream_callbacks::seek
openmpt_stream_seek_func seek
Seek callback.
Definition: libopenmpt.h:313
openmpt_stream_callbacks::tell
openmpt_stream_tell_func tell
Tell callback.
Definition: libopenmpt.h:320