-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaglib.cpp
325 lines (290 loc) · 9.28 KB
/
taglib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/**
* TagLib implementation and php extension
* Mostly for manipulating Tags
* But also reading audioProperties
* Supports MPEG (MP3) and OGG, soon(tm): FLAC
*
* taglib.github.io
* github.com/dayvonjersen/taglib-php
*/
/**
* standard libs
*/
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <sstream>
/**
* .h required for php extensions
*/
#include "php_taglib.h"
/**
* taglib reports errors through std::cerr
* let's expose these messages to PHP
* so they can actually be detected and handled
*
* This idea came from a post I found on Google Groups
* posted August 15, 2002 by Chris Uzdavinis
* https://groups.google.com/d/msg/borland.public.cppbuilder.language/Uua6t3VhELA/vGyq-ituxN8J
*/
static std::stringstream taglib_cerr;
class Stream_Swapper {
public:
Stream_Swapper(std::ostream &orig, std::ostream &replacement)
: buf_(orig.rdbuf()), str_(orig) {
orig.rdbuf(replacement.rdbuf());
}
~Stream_Swapper() { str_.rdbuf(buf_); }
private:
std::streambuf *buf_;
std::ostream &str_;
} swapper(std::cerr, taglib_cerr);
/**
* exceptions yay
*/
#include "zend_exceptions.h"
#include "ext/spl/spl_exceptions.h"
static void php_exception(const char *msg) {
zend_class_entry *exception_ce = zend_exception_get_default();
zend_throw_exception(exception_ce, msg, 0);
}
static bool taglib_error() {
bool retval = false;
/**
* all TagLib errors happen to be prefixed with either
* "TagLib: " - debug() - tdebug.cpp
* "*** " - debugData() - tdebug.cpp
* and std::cerr sometimes contains more than just '\0'
*/
if (taglib_cerr.peek() == 'T' || taglib_cerr.peek() == '*') {
char errorMessage[255];
taglib_cerr.getline(errorMessage, 255);
php_error(E_WARNING, "%s", errorMessage);
retval = true;
}
// zend_replace_error_handling( EH_NORMAL, NULL, NULL TSRMLS_CC);
return retval;
}
/**
* baby duck syndrome - using strings in switch statements
*
* Thanks to Captain Oblivious:
* http://stackoverflow.com/a/16721551
*
* NOTE: This requires C++11.
* NOTE: I've manually editted the Makefile to add the -std=c++11 flag
*/
constexpr unsigned int _charArrForSwitch(const char *str, int index = 0) {
return !str[index] ? 0x1505
: (_charArrForSwitch(str, index + 1) * 0x21) ^ str[index];
}
constexpr unsigned int operator"" _CASE(const char str[], size_t size) {
return _charArrForSwitch(str);
}
/**
* I found this on a StackOverflow question about TagLib
* It works so I'm keeping it for now
* You can set image tags directly from a file.
* http://stackoverflow.com/a/8467869
*/
#include <tfile.h>
class ImageFileTest : public TagLib::File {
public:
ImageFileTest(const char *file) : TagLib::File(file) {}
TagLib::ByteVector data() { return readBlock(length()); }
private:
virtual TagLib::Tag *tag() const { return 0; }
virtual TagLib::AudioProperties *audioProperties() const { return 0; }
virtual bool save() { return false; }
};
/**
* making php class constants requires some boilerplate */
#define _defineclassconstant(name, value) \
zval *_##name##_; \
_##name##_ = (zval *)(pemalloc(sizeof(zval), 1)); \
INIT_PZVAL(_##name##_); \
ZVAL_LONG(_##name##_, value); \
zend_hash_add(&ce->constants_table, #name, sizeof(#name), \
(void *)&_##name##_, sizeof(zval *), NULL);
/**
* Expose Class Constants from TagLib
*/
void taglibbase_register_constants(zend_class_entry *ce) {
/**
* see TagLib::ID3v2::AttachedPictureFrame::Type in attachedpictureframe.h
*
* For use with get/set ID3v2 functions in this extension.
*/
_defineclassconstant(APIC_OTHER, 0x00);
_defineclassconstant(APIC_FILEICON, 0x01);
_defineclassconstant(APIC_OTHERFILEICON, 0x02);
_defineclassconstant(APIC_FRONTCOVER, 0x03);
_defineclassconstant(APIC_BACKCOVER, 0x04);
_defineclassconstant(APIC_LEAFLETPAGE, 0x05);
_defineclassconstant(APIC_MEDIA, 0x06);
_defineclassconstant(APIC_LEADARTIST, 0x07);
_defineclassconstant(APIC_ARTIST, 0x08);
_defineclassconstant(APIC_CONDUCTOR, 0x09);
_defineclassconstant(APIC_BAND, 0x0A);
_defineclassconstant(APIC_COMPOSER, 0x0B);
_defineclassconstant(APIC_LYRICIST, 0x0C);
_defineclassconstant(APIC_RECORDINGLOCATION, 0x0D);
_defineclassconstant(APIC_DURINGRECORDING, 0x0E);
_defineclassconstant(APIC_DURINGPERFORMANCE, 0x0F);
_defineclassconstant(APIC_MOVIESCREENCAPTURE, 0x10);
_defineclassconstant(APIC_COLOUREDFISH, 0x11);
_defineclassconstant(APIC_ILLUSTRATION, 0x12);
_defineclassconstant(APIC_BANDLOGO, 0x13);
_defineclassconstant(APIC_PUBLISHERLOGO, 0x14);
}
/**
* public static function getPictureTypeAsString($type)
*/
PHP_METHOD(TagLib, getPictureTypeAsString) {
zval *type;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &type) == FAILURE) {
RETURN_FALSE;
}
if (Z_TYPE_P(type) != IS_LONG) {
RETURN_FALSE;
}
switch (Z_LVAL_P(type)) {
case 0x00:
RETURN_STRING("Other", 1);
break;
case 0x01:
RETURN_STRING("File Icon", 1);
break;
case 0x02:
RETURN_STRING("Other File Icon", 1);
break;
case 0x03:
RETURN_STRING("Front Cover", 1);
break;
case 0x04:
RETURN_STRING("Back Cover", 1);
break;
case 0x05:
RETURN_STRING("Leaflet Page", 1);
break;
case 0x06:
RETURN_STRING("Media", 1);
break;
case 0x07:
RETURN_STRING("Lead Artist", 1);
break;
case 0x08:
RETURN_STRING("Artist", 1);
break;
case 0x09:
RETURN_STRING("Conductor", 1);
break;
case 0x0A:
RETURN_STRING("Band", 1);
break;
case 0x0B:
RETURN_STRING("Composer", 1);
break;
case 0x0C:
RETURN_STRING("Lyricist", 1);
break;
case 0x0D:
RETURN_STRING("Recording Location", 1);
break;
case 0x0E:
RETURN_STRING("During Recording", 1);
break;
case 0x0F:
RETURN_STRING("During Performance", 1);
break;
case 0x10:
RETURN_STRING("Movie Screencapture", 1);
break;
case 0x11:
RETURN_STRING("<°))))><", 1);
break;
case 0x12:
RETURN_STRING("Illustration", 1);
break;
case 0x13:
RETURN_STRING("Band Logo", 1);
break;
case 0x14:
RETURN_STRING("Publisher Logo", 1);
break;
default:
RETURN_FALSE;
}
}
static zend_function_entry php_taglibbase_methods[] = {
PHP_ME(TagLib, getPictureTypeAsString, NULL,
ZEND_ACC_PUBLIC | ZEND_ACC_STATIC){NULL, NULL, NULL}};
/**
* more .h files will be included in each of the .cpp files
*/
#include "TSRM.h"
#include <tlist.h>
#include <tpropertymap.h>
#include <tstringlist.h>
/**
* just trying to separate out some of this code
*/
#include "taglibmpeg.cpp"
#include "taglibogg.cpp"
#include "taglibflac.cpp"
zend_class_entry *taglibbase_class_entry;
/**
* And let's try to unify them into one extension
* which provides all of the classes
*/
PHP_MINIT_FUNCTION(taglib_minit) {
zend_class_entry base_ce;
zend_class_entry mpeg_ce;
zend_class_entry ogg_ce;
zend_class_entry flac_ce;
INIT_CLASS_ENTRY(base_ce, "TagLib", php_taglibbase_methods);
taglibbase_class_entry = zend_register_internal_class(&base_ce TSRMLS_CC);
taglibbase_register_constants(taglibbase_class_entry);
INIT_CLASS_ENTRY(mpeg_ce, "TagLibMPEG", php_taglibmpeg_methods);
taglibmpeg_class_entry = zend_register_internal_class(&mpeg_ce TSRMLS_CC);
// taglibmpeg_register_constants(taglibmpeg_class_entry);
taglibmpeg_class_entry->create_object = taglibmpegfile_create_handler;
memcpy(&taglibmpegfile_object_handlers, zend_get_std_object_handlers(),
sizeof(zend_object_handlers));
taglibmpegfile_object_handlers.clone_obj = NULL;
INIT_CLASS_ENTRY(ogg_ce, "TagLibOGG", php_taglibogg_methods);
taglibogg_class_entry = zend_register_internal_class(&ogg_ce TSRMLS_CC);
taglibogg_register_constants(taglibogg_class_entry);
taglibogg_class_entry->create_object = tagliboggfile_create_handler;
memcpy(&tagliboggfile_object_handlers, zend_get_std_object_handlers(),
sizeof(zend_object_handlers));
tagliboggfile_object_handlers.clone_obj = NULL;
INIT_CLASS_ENTRY(flac_ce, "TagLibFLAC", php_taglibflac_methods);
taglibflac_class_entry = zend_register_internal_class(&flac_ce TSRMLS_CC);
// taglibflac_register_constants(taglibflac_class_entry);
taglibflac_class_entry->create_object = taglibflacfile_create_handler;
memcpy(&taglibflacfile_object_handlers, zend_get_std_object_handlers(),
sizeof(zend_object_handlers));
taglibflacfile_object_handlers.clone_obj = NULL;
return SUCCESS;
}
static zend_module_dep php_sample_deps[] = {
ZEND_MOD_REQUIRED("standard"){NULL, NULL, NULL}};
zend_module_entry taglib_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER_EX, NULL, php_sample_deps,
// STANDARD_MODULE_HEADER,
#endif
PHP_TAGLIB_EXTNAME, NULL, /* Functions */
PHP_MINIT(taglib_minit), /* MINIT */
NULL, /* MSHUTDOWN */
NULL, /* RINIT */
NULL, /* RSHUTDOWN */
NULL, /* MINFO */
#if ZEND_MODULE_API_NO >= 20010901
PHP_TAGLIB_EXTVER,
#endif
STANDARD_MODULE_PROPERTIES};
#ifdef COMPILE_DL_TAGLIB
ZEND_GET_MODULE(taglib)
#endif