libopenmpt: libopenmpt/libopenmpt_stream_callbacks_fd.h Source File

libopenmpt 0.9.0-pre.12+r25552 cross-platform C++ and C library to decode tracked music files

Loading...
Searching...
No Matches
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#ifndef _MSC_VER
16#include <errno.h>
17#endif
18#ifdef _MSC_VER
19#include <io.h>
20#endif
21#include <limits.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <string.h>
25#ifndef _MSC_VER
26#include <unistd.h>
27#endif
28
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37/* This stuff has to be in a header file because of possibly different MSVC CRTs which cause problems for fd crossing CRT boundaries. */
38
39 static size_t openmpt_stream_fd_read_func( void * stream, void * dst, size_t bytes ) {
40 int fd = 0;
41 #if defined(_MSC_VER)
42 size_t retval = 0;
43 int to_read = 0;
44 int ret_read = 0;
45 #else
46 ssize_t retval = 0;
47 #endif
48 fd = (int)(uintptr_t)stream;
49 if ( fd < 0 ) {
50 return 0;
51 }
52 #if defined(_MSC_VER)
53 retval = 0;
54 while ( bytes > 0 ) {
55 to_read = 0;
56 if ( bytes < (size_t)INT_MAX ) {
57 to_read = (int)bytes;
58 } else {
59 to_read = INT_MAX;
60 }
61 ret_read = _read( fd, dst, to_read );
62 if ( ret_read <= 0 ) {
63 return retval;
64 }
65 bytes -= ret_read;
66 retval += ret_read;
67 }
68 #else
69 do {
70 retval = read( fd, dst, bytes );
71 } while ( ( retval == -1 ) && ( errno == EINTR ) );
72 #endif
73 if ( retval <= 0 ) {
74 return 0;
75 }
76 return retval;
77}
78
90 static openmpt_stream_callbacks openmpt_stream_get_fd_callbacks(void) {
91 openmpt_stream_callbacks retval;
92 memset( &retval, 0, sizeof( openmpt_stream_callbacks ) );
93 retval.read = openmpt_stream_fd_read_func;
94 return retval;
95}
96
97#ifdef __cplusplus
98}
99#endif
100
104
105#endif /* LIBOPENMPT_STREAM_CALLBACKS_FD_H */
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:39
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:90
libopenmpt.h
openmpt_stream_callbacks
Stream callbacks.
Definition libopenmpt.h:300
openmpt_stream_callbacks::read
openmpt_stream_read_func read
Read callback.
Definition libopenmpt.h:306