libopenmpt: libopenmpt/libopenmpt_stream_callbacks_fd.h Source File

libopenmpt 0.4.53-pre.0+r25032 cross-platform C++ and C library to decode tracked music files

libopenmpt_stream_callbacks_fd.h
Go to the documentation of this file.
1 /*
2 * libopenmpt_stream_callbacks_fd.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_FD_H
11 #define LIBOPENMPT_STREAM_CALLBACKS_FD_H
12
13 #include "libopenmpt.h"
14
15 #ifdef _MSC_VER
16 #include <io.h>
17 #endif
18 #include <limits.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <string.h>
22 #ifndef _MSC_VER
23 #include <unistd.h>
24 #endif
25
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 /* This stuff has to be in a header file because of possibly different MSVC CRTs which cause problems for fd crossing CRT boundaries. */
35
36 static size_t openmpt_stream_fd_read_func( void * stream, void * dst, size_t bytes ) {
37 int fd = 0;
38 #if defined(_MSC_VER)
39 size_t retval = 0;
40 int to_read = 0;
41 int ret_read = 0;
42 #else
43 ssize_t retval = 0;
44 #endif
45 fd = (int)(uintptr_t)stream;
46 if ( fd < 0 ) {
47 return 0;
48 }
49 #if defined(_MSC_VER)
50 retval = 0;
51 while ( bytes > 0 ) {
52 to_read = 0;
53 if ( bytes < (size_t)INT_MAX ) {
54 to_read = (int)bytes;
55 } else {
56 to_read = INT_MAX;
57 }
58 ret_read = _read( fd, dst, to_read );
59 if ( ret_read <= 0 ) {
60 return retval;
61 }
62 bytes -= ret_read;
63 retval += ret_read;
64 }
65 #else
66 retval = read( fd, dst, bytes );
67 #endif
68 if ( retval <= 0 ) {
69 return 0;
70 }
71 return retval;
72 }
73
85 static openmpt_stream_callbacks openmpt_stream_get_fd_callbacks(void) {
86 openmpt_stream_callbacks retval;
87 memset( &retval, 0, sizeof( openmpt_stream_callbacks ) );
88 retval.read = openmpt_stream_fd_read_func;
89 return retval;
90 }
91
92 #ifdef __cplusplus
93 }
94 #endif
95
100 #endif /* LIBOPENMPT_STREAM_CALLBACKS_FD_H */
101
openmpt_stream_get_fd_callbacks
static openmpt_stream_callbacks openmpt_stream_get_fd_callbacks(void)
Provide openmpt_stream_callbacks for standard POSIX file descriptors.
Definition: libopenmpt_stream_callbacks_fd.h:85
libopenmpt.h
openmpt_stream_callbacks
Stream callbacks.
Definition: libopenmpt.h:300
openmpt_stream_fd_read_func
static size_t openmpt_stream_fd_read_func(void *stream, void *dst, size_t bytes)
Definition: libopenmpt_stream_callbacks_fd.h:36
openmpt_stream_callbacks::read
openmpt_stream_read_func read
Read callback.
Definition: libopenmpt.h:306