1 | //vio_string.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010 |
---|
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, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
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_putc (struct roar_vio_calls * vio, char c) { |
---|
39 | return roar_vio_write(vio, &c, 1); |
---|
40 | } |
---|
41 | |
---|
42 | int roar_vio_getc (struct roar_vio_calls * vio) { |
---|
43 | unsigned char c; |
---|
44 | |
---|
45 | if ( roar_vio_read(vio, &c, 1) != 1 ) |
---|
46 | return EOF; |
---|
47 | |
---|
48 | return c; |
---|
49 | } |
---|
50 | |
---|
51 | int roar_vio_printf(struct roar_vio_calls * vio, const char *format, ...) { |
---|
52 | va_list ap; |
---|
53 | int ret; |
---|
54 | char buf[8192]; |
---|
55 | |
---|
56 | va_start(ap, format); |
---|
57 | ret = vsnprintf(buf, 8192, format, ap); |
---|
58 | va_end(ap); |
---|
59 | |
---|
60 | return roar_vio_write(vio, buf, ret); |
---|
61 | } |
---|
62 | |
---|
63 | char * roar_vio_fgets (struct roar_vio_calls * vio, char * s, size_t size) { |
---|
64 | size_t have = 0; |
---|
65 | size_t need = size; |
---|
66 | ssize_t ret; |
---|
67 | char cur; |
---|
68 | char buf[1024]; |
---|
69 | off_t offs; |
---|
70 | char * eol; |
---|
71 | |
---|
72 | if ( size == 0 ) |
---|
73 | return s; |
---|
74 | |
---|
75 | if ( vio == NULL || s == NULL ) |
---|
76 | return NULL; |
---|
77 | |
---|
78 | // space for the \0 |
---|
79 | size -= 1; |
---|
80 | |
---|
81 | if ( (offs = roar_vio_lseek(vio, 0, SEEK_CUR)) == (off_t)-1 ) { |
---|
82 | // need to use the one byte at a time methode |
---|
83 | while ( have < size ) { |
---|
84 | if ( roar_vio_read(vio, &cur, 1) != 1 ) |
---|
85 | break; |
---|
86 | |
---|
87 | s[have] = cur; |
---|
88 | have++; |
---|
89 | |
---|
90 | if ( cur == '\n' ) |
---|
91 | break; |
---|
92 | } |
---|
93 | } else { |
---|
94 | // can use the optimized version |
---|
95 | eol = NULL; |
---|
96 | |
---|
97 | memset(s, '+', size); |
---|
98 | |
---|
99 | while ( have < size && eol == NULL ) { |
---|
100 | ret = roar_vio_read(vio, buf, need > 1023 ? 1023 : need); |
---|
101 | |
---|
102 | if ( ret == -1 || ret == 0 ) |
---|
103 | break; |
---|
104 | |
---|
105 | ROAR_DBG("roar_vio_fgets(*): have=%u", (unsigned int) have); |
---|
106 | |
---|
107 | buf[1023] = 0; |
---|
108 | if ( (eol = strstr(buf, "\n")) == NULL ) { |
---|
109 | memcpy(s, buf, ret); |
---|
110 | s += ret; |
---|
111 | have += ret; |
---|
112 | need -= ret; |
---|
113 | } else { |
---|
114 | offs = eol - buf - ret + 1; |
---|
115 | if ( roar_vio_lseek(vio, offs, SEEK_CUR) == -1 ) |
---|
116 | return NULL; |
---|
117 | |
---|
118 | ROAR_DBG("roar_vio_fgets(*): have=%u", (unsigned int) have); |
---|
119 | ROAR_DBG("roar_vio_fgets(*): eol - buf=%lli", (long long) (eol - buf)); |
---|
120 | |
---|
121 | ret = (size_t)(eol - buf); |
---|
122 | ret++; |
---|
123 | ROAR_DBG("roar_vio_fgets(*): ret=%lli", (long long) ret); |
---|
124 | |
---|
125 | memcpy(s, buf, ret); |
---|
126 | have += ret; |
---|
127 | |
---|
128 | ROAR_DBG("roar_vio_fgets(*): have=%u", (unsigned int) have); |
---|
129 | |
---|
130 | break; |
---|
131 | } |
---|
132 | ROAR_DBG("roar_vio_fgets(*): have=%u", (unsigned int) have); |
---|
133 | } |
---|
134 | |
---|
135 | } |
---|
136 | |
---|
137 | ROAR_DBG("roar_vio_fgets(*): have=%u", (unsigned int) have); |
---|
138 | |
---|
139 | if ( !have ) |
---|
140 | return NULL; |
---|
141 | |
---|
142 | s[have] = 0; |
---|
143 | |
---|
144 | return s; |
---|
145 | } |
---|
146 | |
---|
147 | //ll |
---|