1 /*******************************************************************************
2 * Goggles Audio Player Library *
3 ********************************************************************************
4 * Copyright (C) 2010-2021 by Sander Jansen. All Rights Reserved *
5 * --- *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see http://www.gnu.org/licenses. *
18 ********************************************************************************/
19 #include "ap_defs.h"
20 #include "ap_input_plugin.h"
21
22 using namespace ap;
23
24 namespace ap {
25
26 extern InputPlugin * ap_file_plugin(IOContext * context);
27 extern InputPlugin * ap_http_plugin(IOContext * context);
28
29 /// Open plugin for given url
open(IOContext * ctx,const FXString & url)30 InputPlugin* InputPlugin::open(IOContext * ctx,const FXString & url) {
31 FXString scheme = FXURL::scheme(url);
32
33 if (__likely(scheme.empty() || scheme=="file")) {
34 InputPlugin * file = ap_file_plugin(ctx);
35 if (!file->open(url)){
36 delete file;
37 return nullptr;
38 }
39 return file;
40 }
41 else if (scheme=="http" || scheme=="https") {
42 InputPlugin * http = ap_http_plugin(ctx);
43 if (!http->open(url)){
44 delete http;
45 return nullptr;
46 }
47 return http;
48 }
49 else {
50 return nullptr;
51 }
52 }
53
54
55
56
read_uint24_be(FXuint & value)57 FXbool InputPlugin::read_uint24_be(FXuint & value) {
58 FXuchar v[3];
59 if (read(&v,3)==3) {
60 #if FOX_BIGENDIAN == 0
61 value = (v[0]<<16) | (v[1]<<8) | v[2];
62 #else
63 value = v[0] | (v[1]<<8) | (v[2]<<16);
64 #endif
65 return true;
66 }
67 return false;
68 }
69
70
read_uint32_le(FXuint & value)71 FXbool InputPlugin::read_uint32_le(FXuint & value) {
72 if (read(&value,4)==4) {
73 #if FOX_BIGENDIAN == 1
74 value = swap32(value);
75 #endif
76 return true;
77 }
78 return false;
79 }
80
81
read_uint32_be(FXuint & value)82 FXbool InputPlugin::read_uint32_be(FXuint & value) {
83 if (read(&value,4)==4) {
84 #if FOX_BIGENDIAN == 0
85 value = swap32(value);
86 #endif
87 return true;
88 }
89 return false;
90 }
91
92
read_uint64_be(FXulong & value)93 FXbool InputPlugin::read_uint64_be(FXulong & value) {
94 if (read(&value,8)==8) {
95 #if FOX_BIGENDIAN == 0
96 value = swap64(value);
97 #endif
98 return true;
99 }
100 return false;
101 }
102
103
read_int64_be(FXlong & value)104 FXbool InputPlugin::read_int64_be(FXlong & value) {
105 if (read(&value,8)==8) {
106 #if FOX_BIGENDIAN == 0
107 value = swap64(value);
108 #endif
109 return true;
110 }
111 return false;
112 }
113
114
read_int32_be(FXint & value)115 FXbool InputPlugin::read_int32_be(FXint & value) {
116 if (read(&value,4)==4) {
117 #if FOX_BIGENDIAN == 0
118 value = swap32(value);
119 #endif
120 return true;
121 }
122 return false;
123 }
124
125
read_uint16_be(FXushort & value)126 FXbool InputPlugin::read_uint16_be(FXushort & value) {
127 if (read(&value,2)==2) {
128 #if FOX_BIGENDIAN == 0
129 value = swap16(value);
130 #endif
131 return true;
132 }
133 return false;
134 }
135
136
read_int16_be(FXshort & value)137 FXbool InputPlugin::read_int16_be(FXshort & value) {
138 if (read(&value,2)==2) {
139 #if FOX_BIGENDIAN == 0
140 value = swap16(value);
141 #endif
142 return true;
143 }
144 return false;
145 }
146
147
read_float_be(FXfloat & value)148 FXbool InputPlugin::read_float_be(FXfloat & value) {
149 FXuchar v[4];
150 if (read(&v,4)==4) {
151 #if FOX_BIGENDIAN == 0
152 reinterpret_cast<FXuchar*>(&value)[0] = v[3];
153 reinterpret_cast<FXuchar*>(&value)[1] = v[2];
154 reinterpret_cast<FXuchar*>(&value)[2] = v[1];
155 reinterpret_cast<FXuchar*>(&value)[3] = v[0];
156 #else
157 value = 0;
158 #endif
159 return true;
160 }
161 return false;
162 }
163
164
read_double_be(FXdouble & value)165 FXbool InputPlugin::read_double_be(FXdouble & value) {
166 FXuchar v[8];
167 if (read(&v,8)==8) {
168 #if FOX_BIGENDIAN == 0
169 reinterpret_cast<FXuchar*>(&value)[0] = v[7];
170 reinterpret_cast<FXuchar*>(&value)[1] = v[6];
171 reinterpret_cast<FXuchar*>(&value)[2] = v[5];
172 reinterpret_cast<FXuchar*>(&value)[3] = v[4];
173 reinterpret_cast<FXuchar*>(&value)[4] = v[3];
174 reinterpret_cast<FXuchar*>(&value)[5] = v[2];
175 reinterpret_cast<FXuchar*>(&value)[6] = v[1];
176 reinterpret_cast<FXuchar*>(&value)[7] = v[0];
177 #else
178 value = 0;
179 #endif
180 return true;
181 }
182 return false;
183 }
184 }
185