[2059] | 1 | //vio_stdio.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009 |
---|
| 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 |
---|
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | * NOTE for everyone want's to change something and send patches: |
---|
| 24 | * read README and HACKING! There a addition information on |
---|
| 25 | * the license of this document you need to read before you send |
---|
| 26 | * any patches. |
---|
| 27 | * |
---|
| 28 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 29 | * or libpulse*: |
---|
| 30 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 31 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 32 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | #include "libroar.h" |
---|
| 36 | |
---|
| 37 | int roar_vio_open_stdio (struct roar_vio_calls * calls, FILE * dst) { |
---|
| 38 | #ifndef ROAR_WITHOUT_VIO_STDIO |
---|
| 39 | if ( calls == NULL || dst == NULL ) |
---|
| 40 | return -1; |
---|
| 41 | |
---|
| 42 | memset(calls, 0, sizeof(struct roar_vio_calls)); |
---|
| 43 | |
---|
| 44 | calls->read = roar_vio_stdio_read; |
---|
| 45 | calls->write = roar_vio_stdio_write; |
---|
| 46 | calls->lseek = roar_vio_stdio_lseek; |
---|
| 47 | calls->sync = roar_vio_stdio_sync; |
---|
| 48 | calls->close = roar_vio_stdio_close; |
---|
| 49 | |
---|
| 50 | calls->inst = dst; |
---|
| 51 | |
---|
| 52 | return 0; |
---|
| 53 | #else |
---|
| 54 | return -1; |
---|
| 55 | #endif |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | FILE * roar_vio_to_stdio (struct roar_vio_calls * calls, int flags) { |
---|
| 59 | #ifdef ROAR_HAVE_FOPENCOOKIE |
---|
| 60 | cookie_io_functions_t foc_funcs; |
---|
| 61 | #endif |
---|
| 62 | |
---|
| 63 | if ( calls == NULL ) |
---|
| 64 | return NULL; |
---|
| 65 | |
---|
| 66 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
| 67 | memset(&foc_funcs, 0, sizeof(cookie_io_functions_t)); |
---|
| 68 | |
---|
| 69 | foc_funcs.close = roar_vio_to_stdio_close; |
---|
| 70 | foc_funcs.read = roar_vio_to_stdio_read; |
---|
| 71 | foc_funcs.write = roar_vio_to_stdio_write; |
---|
| 72 | |
---|
| 73 | return fopencookie((void*) calls, "rw", foc_funcs); |
---|
| 74 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
| 75 | return funopen((void*) calls, roar_vio_to_stdio_read, roar_vio_to_stdio_write, |
---|
| 76 | roar_vio_to_stdio_lseek, roar_vio_to_stdio_close); |
---|
| 77 | #else |
---|
| 78 | return NULL; |
---|
| 79 | #endif |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | #if defined(ROAR_HAVE_FOPENCOOKIE) || defined(ROAR_HAVE_FUNOPEN) |
---|
| 83 | int roar_vio_to_stdio_close (void *__cookie) { |
---|
| 84 | return roar_vio_close((struct roar_vio_calls *) __cookie); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
| 88 | __ssize_t roar_vio_to_stdio_read (void *__cookie, char *__buf, size_t __nbytes) { |
---|
| 89 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
| 90 | int roar_vio_to_stdio_read(void *__cookie, char *__buf, int __nbytes) { |
---|
| 91 | #endif |
---|
| 92 | return roar_vio_read((struct roar_vio_calls *) __cookie, __buf, __nbytes); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
| 96 | __ssize_t roar_vio_to_stdio_write (void *__cookie, __const char *__buf, size_t __n) { |
---|
| 97 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
| 98 | int roar_vio_to_stdio_write(void *__cookie, const char *__buf, int __n) { |
---|
| 99 | #endif |
---|
| 100 | return roar_vio_write((struct roar_vio_calls *) __cookie, (char *) __buf, __n); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
| 104 | int roar_vio_to_stdio_lseek (void *__cookie, _IO_off64_t *__pos, int __w); |
---|
| 105 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
| 106 | fpos_t roar_vio_to_stdio_lseek(void *__cookie, fpos_t __pos, int __w) { |
---|
| 107 | return roar_vio_lseek((struct roar_vio_calls *) __cookie, __pos, __w); |
---|
| 108 | } |
---|
| 109 | #endif |
---|
| 110 | #endif |
---|
| 111 | |
---|
| 112 | // VIOs: |
---|
| 113 | |
---|
| 114 | // stdio: |
---|
| 115 | #ifndef ROAR_WITHOUT_VIO_STDIO |
---|
| 116 | ssize_t roar_vio_stdio_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 117 | return fread(buf, 1, count, (FILE*)(vio->inst)); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | ssize_t roar_vio_stdio_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 121 | return fwrite(buf, 1, count, (FILE*)(vio->inst)); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | off_t roar_vio_stdio_lseek (struct roar_vio_calls * vio, off_t offset, int whence) { |
---|
| 125 | if ( fseek((FILE*)(vio->inst), offset, whence) == -1 ) |
---|
| 126 | return -1; |
---|
| 127 | |
---|
| 128 | return ftell((FILE*)(vio->inst)); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | int roar_vio_stdio_sync (struct roar_vio_calls * vio) { |
---|
| 132 | return fflush((FILE*)(vio->inst)); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | int roar_vio_stdio_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
| 136 | |
---|
| 137 | if ( vio == NULL || cmd == -1 ) |
---|
| 138 | return -1; |
---|
| 139 | |
---|
| 140 | switch (cmd) { |
---|
| 141 | case ROAR_VIO_CTL_GET_FH: |
---|
| 142 | case ROAR_VIO_CTL_GET_READ_FH: |
---|
| 143 | case ROAR_VIO_CTL_GET_WRITE_FH: |
---|
| 144 | *(int*)data = fileno((FILE*)(vio->inst)); |
---|
| 145 | return 0; |
---|
| 146 | break; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | return -1; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | int roar_vio_stdio_close (struct roar_vio_calls * vio) { |
---|
| 153 | return fclose((FILE*)(vio->inst)); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | #endif |
---|
| 157 | |
---|
| 158 | //ll |
---|