1 /* EasyTAG - tag editor for audio files
2  * Copyright (C) 2014 David King <amigadave@amigadave.com>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 51
16  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "wavpack_private.h"
20 
21 #ifdef ENABLE_WAVPACK
22 
23 /* For EOF. */
24 #include <stdio.h>
25 
26 int32_t
wavpack_read_bytes(void * id,void * data,int32_t bcount)27 wavpack_read_bytes (void *id,
28                     void *data,
29                     int32_t bcount)
30 {
31     EtWavpackState *state;
32     gssize bytes_written;
33 
34     state = (EtWavpackState *)id;
35 
36     bytes_written = g_input_stream_read (G_INPUT_STREAM (state->istream), data,
37                                          bcount, NULL, &state->error);
38 
39     if (bytes_written == -1)
40     {
41         return 0;
42     }
43 
44     return bytes_written;
45 }
46 
47 uint32_t
wavpack_get_pos(void * id)48 wavpack_get_pos (void *id)
49 {
50     EtWavpackState *state;
51 
52     state = (EtWavpackState *)id;
53 
54     return g_seekable_tell (state->seekable);
55 }
56 
57 int
wavpack_set_pos_abs(void * id,uint32_t pos)58 wavpack_set_pos_abs (void *id,
59                      uint32_t pos)
60 {
61     EtWavpackState *state;
62 
63     state = (EtWavpackState *)id;
64 
65     if (!g_seekable_seek (state->seekable, pos, G_SEEK_SET, NULL,
66                           &state->error))
67     {
68         return -1;
69     }
70     else
71     {
72         return 0;
73     }
74 }
75 
76 int
wavpack_set_pos_rel(void * id,int32_t delta,int mode)77 wavpack_set_pos_rel (void *id,
78                      int32_t delta,
79                      int mode)
80 {
81     EtWavpackState *state;
82     GSeekType seek_type;
83 
84     state = (EtWavpackState *)id;
85 
86     switch (mode)
87     {
88         case SEEK_SET:
89             seek_type = G_SEEK_SET;
90             break;
91         case SEEK_CUR:
92             seek_type = G_SEEK_CUR;
93             break;
94         case SEEK_END:
95             seek_type = G_SEEK_END;
96             break;
97         default:
98             g_assert_not_reached ();
99             break;
100     }
101 
102     if (!g_seekable_seek (state->seekable, delta, seek_type, NULL,
103                           &state->error))
104     {
105         return -1;
106     }
107     else
108     {
109         return 0;
110     }
111 }
112 
113 int
wavpack_push_back_byte(void * id,int c)114 wavpack_push_back_byte (void *id,
115                         int c)
116 {
117     EtWavpackState *state;
118 
119     state = (EtWavpackState *)id;
120 
121     if (!g_seekable_seek (state->seekable, -1, G_SEEK_CUR, NULL,
122                           &state->error))
123     {
124         return EOF;
125     }
126 
127     /* TODO: Check if c is present in the input stream. */
128     return c;
129 }
130 
131 uint32_t
wavpack_get_length(void * id)132 wavpack_get_length (void *id)
133 {
134     EtWavpackState *state;
135     GFileInfo *info;
136     goffset size;
137 
138     state = (EtWavpackState *)id;
139 
140     info = g_file_input_stream_query_info (state->istream,
141                                            G_FILE_ATTRIBUTE_STANDARD_SIZE,
142                                            NULL, &state->error);
143 
144     if (!info)
145     {
146         return 0;
147     }
148 
149     size = g_file_info_get_size (info);
150     g_object_unref (info);
151 
152     return size;
153 }
154 
155 int
wavpack_can_seek(void * id)156 wavpack_can_seek (void *id)
157 {
158     EtWavpackState *state;
159 
160     state = (EtWavpackState *)id;
161 
162     return g_seekable_can_seek (state->seekable);
163 }
164 
165 int32_t
wavpack_write_bytes(void * id,void * data,int32_t bcount)166 wavpack_write_bytes (void *id,
167                      void *data,
168                      int32_t bcount)
169 {
170     EtWavpackWriteState *state;
171     gssize bytes_written;
172 
173     state = (EtWavpackWriteState *)id;
174 
175     bytes_written = g_output_stream_write (G_OUTPUT_STREAM (state->ostream),
176                                            data, bcount, NULL, &state->error);
177 
178     if (bytes_written == -1)
179     {
180         return 0;
181     }
182 
183     return bytes_written;
184 }
185 
186 #endif /* ENABLE_WAVPACK */
187