[2059] | 1 | //vio_stdio.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5823] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2013 |
---|
[2059] | 5 | * |
---|
| 6 | * This file is part of libroar a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * libroar is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[2059] | 23 | * |
---|
| 24 | * NOTE for everyone want's to change something and send patches: |
---|
| 25 | * read README and HACKING! There a addition information on |
---|
| 26 | * the license of this document you need to read before you send |
---|
| 27 | * any patches. |
---|
| 28 | * |
---|
| 29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 30 | * or libpulse*: |
---|
| 31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 34 | */ |
---|
| 35 | |
---|
| 36 | #include "libroar.h" |
---|
| 37 | |
---|
| 38 | int roar_vio_open_stdio (struct roar_vio_calls * calls, FILE * dst) { |
---|
| 39 | #ifndef ROAR_WITHOUT_VIO_STDIO |
---|
| 40 | if ( calls == NULL || dst == NULL ) |
---|
| 41 | return -1; |
---|
| 42 | |
---|
| 43 | memset(calls, 0, sizeof(struct roar_vio_calls)); |
---|
[5388] | 44 | calls->flags = ROAR_VIO_FLAGS_NONE; |
---|
| 45 | calls->refc = 1; |
---|
[2059] | 46 | |
---|
| 47 | calls->read = roar_vio_stdio_read; |
---|
| 48 | calls->write = roar_vio_stdio_write; |
---|
| 49 | calls->lseek = roar_vio_stdio_lseek; |
---|
| 50 | calls->sync = roar_vio_stdio_sync; |
---|
| 51 | calls->close = roar_vio_stdio_close; |
---|
| 52 | |
---|
| 53 | calls->inst = dst; |
---|
| 54 | |
---|
| 55 | return 0; |
---|
| 56 | #else |
---|
| 57 | return -1; |
---|
| 58 | #endif |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | FILE * roar_vio_to_stdio (struct roar_vio_calls * calls, int flags) { |
---|
| 62 | #ifdef ROAR_HAVE_FOPENCOOKIE |
---|
| 63 | cookie_io_functions_t foc_funcs; |
---|
| 64 | #endif |
---|
| 65 | |
---|
[5270] | 66 | // (void)flags is the the individual sections because later added APIs |
---|
| 67 | // may require those flags. |
---|
| 68 | |
---|
[2059] | 69 | if ( calls == NULL ) |
---|
| 70 | return NULL; |
---|
| 71 | |
---|
| 72 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
[5270] | 73 | (void)flags; |
---|
| 74 | |
---|
[2059] | 75 | memset(&foc_funcs, 0, sizeof(cookie_io_functions_t)); |
---|
| 76 | |
---|
| 77 | foc_funcs.close = roar_vio_to_stdio_close; |
---|
| 78 | foc_funcs.read = roar_vio_to_stdio_read; |
---|
| 79 | foc_funcs.write = roar_vio_to_stdio_write; |
---|
| 80 | |
---|
| 81 | return fopencookie((void*) calls, "rw", foc_funcs); |
---|
| 82 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
[5270] | 83 | (void)flags; |
---|
| 84 | |
---|
[2059] | 85 | return funopen((void*) calls, roar_vio_to_stdio_read, roar_vio_to_stdio_write, |
---|
| 86 | roar_vio_to_stdio_lseek, roar_vio_to_stdio_close); |
---|
| 87 | #else |
---|
| 88 | return NULL; |
---|
| 89 | #endif |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | #if defined(ROAR_HAVE_FOPENCOOKIE) || defined(ROAR_HAVE_FUNOPEN) |
---|
| 93 | int roar_vio_to_stdio_close (void *__cookie) { |
---|
| 94 | return roar_vio_close((struct roar_vio_calls *) __cookie); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
[5088] | 98 | ssize_t roar_vio_to_stdio_read (void *__cookie, char *__buf, size_t __nbytes) { |
---|
[2059] | 99 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
| 100 | int roar_vio_to_stdio_read(void *__cookie, char *__buf, int __nbytes) { |
---|
| 101 | #endif |
---|
| 102 | return roar_vio_read((struct roar_vio_calls *) __cookie, __buf, __nbytes); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
[5088] | 106 | ssize_t roar_vio_to_stdio_write (void *__cookie, __const char *__buf, size_t __n) { |
---|
[2059] | 107 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
| 108 | int roar_vio_to_stdio_write(void *__cookie, const char *__buf, int __n) { |
---|
| 109 | #endif |
---|
[4845] | 110 | ROAR_DBG("roar_vio_to_stdio_write(*) = ?"); |
---|
[2059] | 111 | return roar_vio_write((struct roar_vio_calls *) __cookie, (char *) __buf, __n); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
| 115 | int roar_vio_to_stdio_lseek (void *__cookie, _IO_off64_t *__pos, int __w); |
---|
| 116 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
| 117 | fpos_t roar_vio_to_stdio_lseek(void *__cookie, fpos_t __pos, int __w) { |
---|
| 118 | return roar_vio_lseek((struct roar_vio_calls *) __cookie, __pos, __w); |
---|
| 119 | } |
---|
| 120 | #endif |
---|
| 121 | #endif |
---|
| 122 | |
---|
| 123 | // VIOs: |
---|
| 124 | |
---|
| 125 | // stdio: |
---|
| 126 | #ifndef ROAR_WITHOUT_VIO_STDIO |
---|
| 127 | ssize_t roar_vio_stdio_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 128 | return fread(buf, 1, count, (FILE*)(vio->inst)); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | ssize_t roar_vio_stdio_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 132 | return fwrite(buf, 1, count, (FILE*)(vio->inst)); |
---|
| 133 | } |
---|
| 134 | |
---|
[5278] | 135 | roar_off_t roar_vio_stdio_lseek (struct roar_vio_calls * vio, roar_off_t offset, int whence) { |
---|
[5502] | 136 | // ftell() is broken on win. It sometimes returnes the binary offset and sometimes |
---|
| 137 | // the text mode offset (why? why are both diffrent?). |
---|
| 138 | #if defined(ROAR_HAVE_FSEEK) && defined(ROAR_HAVE_FTELL) && !defined(ROAR_TARGET_WIN32) |
---|
[2059] | 139 | if ( fseek((FILE*)(vio->inst), offset, whence) == -1 ) |
---|
| 140 | return -1; |
---|
| 141 | |
---|
| 142 | return ftell((FILE*)(vio->inst)); |
---|
[3900] | 143 | #else |
---|
[5278] | 144 | return (roar_off_t)-1; |
---|
[3900] | 145 | #endif |
---|
[2059] | 146 | } |
---|
| 147 | |
---|
| 148 | int roar_vio_stdio_sync (struct roar_vio_calls * vio) { |
---|
| 149 | return fflush((FILE*)(vio->inst)); |
---|
| 150 | } |
---|
| 151 | |
---|
[5278] | 152 | int roar_vio_stdio_ctl (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) { |
---|
[2059] | 153 | |
---|
| 154 | if ( vio == NULL || cmd == -1 ) |
---|
| 155 | return -1; |
---|
| 156 | |
---|
| 157 | switch (cmd) { |
---|
[3795] | 158 | case ROAR_VIO_CTL_GET_NAME: |
---|
| 159 | if ( data == NULL ) |
---|
| 160 | return -1; |
---|
| 161 | |
---|
| 162 | *(char**)data = "stdio"; |
---|
| 163 | return 0; |
---|
| 164 | break; |
---|
[3900] | 165 | #ifdef ROAR_HAVE_FILENO |
---|
[2059] | 166 | case ROAR_VIO_CTL_GET_FH: |
---|
| 167 | case ROAR_VIO_CTL_GET_READ_FH: |
---|
| 168 | case ROAR_VIO_CTL_GET_WRITE_FH: |
---|
[3900] | 169 | case ROAR_VIO_CTL_GET_SELECT_FH: |
---|
| 170 | case ROAR_VIO_CTL_GET_SELECT_READ_FH: |
---|
| 171 | case ROAR_VIO_CTL_GET_SELECT_WRITE_FH: |
---|
[2059] | 172 | *(int*)data = fileno((FILE*)(vio->inst)); |
---|
| 173 | return 0; |
---|
| 174 | break; |
---|
[3900] | 175 | #endif |
---|
[2059] | 176 | } |
---|
| 177 | |
---|
| 178 | return -1; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | int roar_vio_stdio_close (struct roar_vio_calls * vio) { |
---|
| 182 | return fclose((FILE*)(vio->inst)); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | #endif |
---|
| 186 | |
---|
| 187 | //ll |
---|