Debian Codegraph @

src/engine/sfWrapper.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "sfWrapper.h"
21 #include "../fileutils.h"
22 #include "../ta-log.h"
23 #include "sndfile.h"
24
25 sf_count_t _vioGetSize(void* user) {
26 return ((SFWrapper*)user)->ioGetSize();
27 }
28
29 sf_count_t _vioSeek(sf_count_t offset, int whence, void* user) {
30 return ((SFWrapper*)user)->ioSeek(offset,whence);
31 }
32
33 sf_count_t _vioRead(void* ptr, sf_count_t count, void* user) {
34 return ((SFWrapper*)user)->ioRead(ptr,count);
35 }
36
37 sf_count_t _vioWrite(const void* ptr, sf_count_t count, void* user) {
38 return ((SFWrapper*)user)->ioWrite(ptr,count);
39 }
40
41 sf_count_t _vioTell(void* user) {
42 return ((SFWrapper*)user)->ioTell();
43 }
44
45 sf_count_t SFWrapper::ioGetSize() {
46 sf_count_t ret=(sf_count_t)len;
47 if (fileMode==SFM_WRITE || fileMode==SFM_RDWR) {
48 ssize_t lastTell=ftell(f);
49 fseek(f,0,SEEK_END);
50 ret=(sf_count_t)ftell(f);
51 fseek(f,lastTell,SEEK_SET);
52 }
53 return ret;
54 }
55
56 sf_count_t SFWrapper::ioSeek(sf_count_t offset, int whence) {
57 return fseek(f,offset,whence);
58 }
59
60 sf_count_t SFWrapper::ioRead(void* ptr, sf_count_t count) {
61 return fread(ptr,1,count,f);
62 }
63
64 sf_count_t SFWrapper::ioWrite(const void* ptr, sf_count_t count) {
65 return fwrite(ptr,1,count,f);
66 }
67
68 sf_count_t SFWrapper::ioTell() {
69 return ftell(f);
70 }
71
72 int SFWrapper::doClose() {
73 int ret=sf_close(sf);
74 fclose(f);
75 return ret;
76 }
77
78 SNDFILE* SFWrapper::doOpen(const char* path, int mode, SF_INFO* sfinfo) {
79 vio.get_filelen=_vioGetSize;
80 vio.read=_vioRead;
81 vio.seek=_vioSeek;
82 vio.tell=_vioTell;
83 vio.write=_vioWrite;
84 logV("SFWrapper: opening %s",path);
85
86 const char* modeC="rb";
87 if (mode==SFM_WRITE) {
88 modeC="wb";
89 }
90 if (mode==SFM_RDWR) {
91 modeC="rb+";
92 }
93
94 f=ps_fopen(path,modeC);
95 if (f==NULL) {
96 logE("SFWrapper: failed to open (%s)",strerror(errno));
97 return NULL;
98 }
99
100 if (fseek(f,0,SEEK_END)==-1) {
101 logE("SFWrapper: failed to seek to end (%s)",strerror(errno));
102 fclose(f);
103 f=NULL;
104 return NULL;
105 }
106
107 len=ftell(f);
108 if (len==(SIZE_MAX>>1)) {
109 logE("SFWrapper: failed to tell (%s)",strerror(errno));
110 len=0;
111 fclose(f);
112 f=NULL;
113 return NULL;
114 }
115
116 if (fseek(f,0,SEEK_SET)==-1) {
117 logE("SFWrapper: failed to seek to beginning (%s)",strerror(errno));
118 len=0;
119 fclose(f);
120 f=NULL;
121 return NULL;
122 }
123
124 sf=sf_open_virtual(&vio,mode,sfinfo,this);
125 if (sf!=NULL) fileMode=mode;
126 if (sf==NULL) {
127 logE("SFWrapper: WHY IS IT NULL?!");
128 }
129 return sf;
130 }