metadata.cpp 24.2 KB
Newer Older
Josh Coalson's avatar
Josh Coalson committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* libFLAC++ - Free Lossless Audio Codec library
 * Copyright (C) 2002  Josh Coalson
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 */

#include "FLAC++/metadata.h"
#include "FLAC/assert.h"
22
23
#include <stdlib.h> // for malloc(), free()
#include <string.h> // for memcpy() etc.
Josh Coalson's avatar
Josh Coalson committed
24
25
26
27

namespace FLAC {
	namespace Metadata {

Josh Coalson's avatar
Josh Coalson committed
28
29
30
31
		// local utility routines

		namespace local {

32
			Prototype *construct_block(::FLAC__StreamMetadata *object)
Josh Coalson's avatar
Josh Coalson committed
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
			{
				Prototype *ret = 0;
				switch(object->type) {
					case FLAC__METADATA_TYPE_STREAMINFO:
						ret = new StreamInfo(object, /*copy=*/false);
						break;
					case FLAC__METADATA_TYPE_PADDING:
						ret = new Padding(object, /*copy=*/false);
						break;
					case FLAC__METADATA_TYPE_APPLICATION:
						ret = new Application(object, /*copy=*/false);
						break;
					case FLAC__METADATA_TYPE_SEEKTABLE:
						ret = new SeekTable(object, /*copy=*/false);
						break;
					case FLAC__METADATA_TYPE_VORBIS_COMMENT:
						ret = new VorbisComment(object, /*copy=*/false);
						break;
					default:
						FLAC__ASSERT(0);
						break;
				}
				return ret;
			}

		};

60
61
		Prototype *clone(const Prototype *object)
		{
Josh Coalson's avatar
Josh Coalson committed
62
63
			FLAC__ASSERT(0 != object);

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
			const StreamInfo *streaminfo = dynamic_cast<const StreamInfo *>(object);
			const Padding *padding = dynamic_cast<const Padding *>(object);
			const Application *application = dynamic_cast<const Application *>(object);
			const SeekTable *seektable = dynamic_cast<const SeekTable *>(object);
			const VorbisComment *vorbiscomment = dynamic_cast<const VorbisComment *>(object);

			if(0 != streaminfo)
				return new StreamInfo(*streaminfo);
			else if(0 != padding)
				return new Padding(*padding);
			else if(0 != application)
				return new Application(*application);
			else if(0 != seektable)
				return new SeekTable(*seektable);
			else if(0 != vorbiscomment)
				return new VorbisComment(*vorbiscomment);
			else {
				FLAC__ASSERT(0);
				return 0;
			}
		}

Josh Coalson's avatar
Josh Coalson committed
86
87
88
89
		//
		// Prototype
		//

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
		Prototype::Prototype(const Prototype &object):
		object_(::FLAC__metadata_object_clone(object.object_)),
		is_reference_(false)
		{
			FLAC__ASSERT(object.is_valid());
		}

		Prototype::Prototype(const ::FLAC__StreamMetadata &object):
		object_(::FLAC__metadata_object_clone(&object)),
		is_reference_(false)
		{
		}

		Prototype::Prototype(const ::FLAC__StreamMetadata *object):
		object_(::FLAC__metadata_object_clone(object)),
		is_reference_(false)
		{
			FLAC__ASSERT(0 != object);
		}

		Prototype::Prototype(::FLAC__StreamMetadata *object, bool copy):
		object_(copy? ::FLAC__metadata_object_clone(object) : object),
		is_reference_(false)
Josh Coalson's avatar
Josh Coalson committed
113
114
115
116
117
		{
			FLAC__ASSERT(0 != object);
		}

		Prototype::~Prototype()
Josh Coalson's avatar
Josh Coalson committed
118
119
120
121
122
		{
			clear();
		}

		void Prototype::clear()
Josh Coalson's avatar
Josh Coalson committed
123
		{
124
			if(0 != object_ && !is_reference_)
Josh Coalson's avatar
Josh Coalson committed
125
				FLAC__metadata_object_delete(object_);
Josh Coalson's avatar
Josh Coalson committed
126
127
128
129
130
			object_ = 0;
		}

		void Prototype::operator=(const Prototype &object)
		{
131
			FLAC__ASSERT(object.is_valid());
Josh Coalson's avatar
Josh Coalson committed
132
			clear();
133
134
			is_reference_ = false;
			object_ = ::FLAC__metadata_object_clone(object.object_);
Josh Coalson's avatar
Josh Coalson committed
135
136
		}

137
		void Prototype::operator=(const ::FLAC__StreamMetadata &object)
Josh Coalson's avatar
Josh Coalson committed
138
139
140
		{
			clear();
			is_reference_ = false;
141
			object_ = ::FLAC__metadata_object_clone(&object);
Josh Coalson's avatar
Josh Coalson committed
142
143
		}

144
		void Prototype::operator=(const ::FLAC__StreamMetadata *object)
Josh Coalson's avatar
Josh Coalson committed
145
146
147
148
		{
			FLAC__ASSERT(0 != object);
			clear();
			is_reference_ = false;
149
			object_ = ::FLAC__metadata_object_clone(object);
Josh Coalson's avatar
Josh Coalson committed
150
151
152
153
154
		}

		bool Prototype::get_is_last() const
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
155
			return (bool)object_->is_last;
Josh Coalson's avatar
Josh Coalson committed
156
157
		}

158
		FLAC__MetadataType Prototype::get_type() const
Josh Coalson's avatar
Josh Coalson committed
159
160
161
162
163
164
165
166
167
168
169
		{
			FLAC__ASSERT(is_valid());
			return object_->type;
		}

		unsigned Prototype::get_length() const
		{
			FLAC__ASSERT(is_valid());
			return object_->length;
		}

170
171
172
173
174
175
		void Prototype::set_is_last(bool value)
		{
			FLAC__ASSERT(is_valid());
			object_->is_last = value;
		}

176

Josh Coalson's avatar
Josh Coalson committed
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
		//
		// StreamInfo
		//

		StreamInfo::StreamInfo():
		Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_STREAMINFO), /*copy=*/false)
		{ }

		StreamInfo::~StreamInfo()
		{ }

		unsigned StreamInfo::get_min_blocksize() const
		{
			FLAC__ASSERT(is_valid());
			return object_->data.stream_info.min_blocksize;
		}

		unsigned StreamInfo::get_max_blocksize() const
		{
			FLAC__ASSERT(is_valid());
			return object_->data.stream_info.max_blocksize;
		}

		unsigned StreamInfo::get_min_framesize() const
		{
			FLAC__ASSERT(is_valid());
			return object_->data.stream_info.min_framesize;
		}

		unsigned StreamInfo::get_max_framesize() const
		{
			FLAC__ASSERT(is_valid());
			return object_->data.stream_info.max_framesize;
		}

		unsigned StreamInfo::get_sample_rate() const
		{
			FLAC__ASSERT(is_valid());
			return object_->data.stream_info.sample_rate;
		}

		unsigned StreamInfo::get_channels() const
		{
			FLAC__ASSERT(is_valid());
			return object_->data.stream_info.channels;
		}

		unsigned StreamInfo::get_bits_per_sample() const
		{
			FLAC__ASSERT(is_valid());
			return object_->data.stream_info.bits_per_sample;
		}

		FLAC__uint64 StreamInfo::get_total_samples() const
		{
			FLAC__ASSERT(is_valid());
			return object_->data.stream_info.total_samples;
		}

		const FLAC__byte *StreamInfo::get_md5sum() const
		{
			FLAC__ASSERT(is_valid());
			return object_->data.stream_info.md5sum;
		}

		void StreamInfo::set_min_blocksize(unsigned value)
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(value >= FLAC__MIN_BLOCK_SIZE);
			FLAC__ASSERT(value <= FLAC__MAX_BLOCK_SIZE);
			object_->data.stream_info.min_blocksize = value;
		}

		void StreamInfo::set_max_blocksize(unsigned value)
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(value >= FLAC__MIN_BLOCK_SIZE);
			FLAC__ASSERT(value <= FLAC__MAX_BLOCK_SIZE);
			object_->data.stream_info.max_blocksize = value;
		}

		void StreamInfo::set_min_framesize(unsigned value)
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(value < (1u < FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN));
			object_->data.stream_info.min_framesize = value;
		}

		void StreamInfo::set_max_framesize(unsigned value)
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(value < (1u < FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN));
			object_->data.stream_info.max_framesize = value;
		}

		void StreamInfo::set_sample_rate(unsigned value)
		{
			FLAC__ASSERT(is_valid());
275
			FLAC__ASSERT(FLAC__format_sample_rate_is_valid(value));
Josh Coalson's avatar
Josh Coalson committed
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
			object_->data.stream_info.sample_rate = value;
		}

		void StreamInfo::set_channels(unsigned value)
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(value > 0);
			FLAC__ASSERT(value <= FLAC__MAX_CHANNELS);
			object_->data.stream_info.channels = value;
		}

		void StreamInfo::set_bits_per_sample(unsigned value)
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(value >= FLAC__MIN_BITS_PER_SAMPLE);
			FLAC__ASSERT(value <= FLAC__MAX_BITS_PER_SAMPLE);
			object_->data.stream_info.bits_per_sample = value;
		}

		void StreamInfo::set_total_samples(FLAC__uint64 value)
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(value < (1u << FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN));
			object_->data.stream_info.total_samples = value;
		}

		void StreamInfo::set_md5sum(const FLAC__byte value[16])
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(0 != value);
			memcpy(object_->data.stream_info.md5sum, value, 16);
		}

309

Josh Coalson's avatar
Josh Coalson committed
310
311
312
313
314
315
316
317
318
319
320
		//
		// Padding
		//

		Padding::Padding():
		Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING), /*copy=*/false)
		{ }

		Padding::~Padding()
		{ }

321
322
323
324
325
326
327
		void Padding::set_length(unsigned length)
		{
			FLAC__ASSERT(is_valid());
			object_->length = length;
		}


Josh Coalson's avatar
Josh Coalson committed
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
		//
		// Application
		//

		Application::Application():
		Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION), /*copy=*/false)
		{ }

		Application::~Application()
		{ }

		const FLAC__byte *Application::get_id() const
		{
			FLAC__ASSERT(is_valid());
			return object_->data.application.id;
		}

		const FLAC__byte *Application::get_data() const
		{
			FLAC__ASSERT(is_valid());
			return object_->data.application.data;
		}

351
		void Application::set_id(const FLAC__byte value[4])
Josh Coalson's avatar
Josh Coalson committed
352
353
354
355
356
357
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(0 != value);
			memcpy(object_->data.application.id, value, 4);
		}

358
359
360
361
362
363
		bool Application::set_data(const FLAC__byte *data, unsigned length)
		{
			FLAC__ASSERT(is_valid());
			return (bool)::FLAC__metadata_object_application_set_data(object_, (FLAC__byte*)data, length, true);
		}

Josh Coalson's avatar
Josh Coalson committed
364
365
366
		bool Application::set_data(FLAC__byte *data, unsigned length, bool copy)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
367
			return (bool)::FLAC__metadata_object_application_set_data(object_, data, length, copy);
Josh Coalson's avatar
Josh Coalson committed
368
369
		}

370

Josh Coalson's avatar
Josh Coalson committed
371
372
373
374
375
376
377
378
379
380
381
		//
		// SeekTable
		//

		SeekTable::SeekTable():
		Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE), /*copy=*/false)
		{ }

		SeekTable::~SeekTable()
		{ }

382
383
384
385
386
387
		unsigned SeekTable::get_num_points() const
		{
			FLAC__ASSERT(is_valid());
			return object_->data.seek_table.num_points;
		}

388
		::FLAC__StreamMetadata_SeekPoint SeekTable::get_point(unsigned index) const
389
390
391
392
393
394
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(index < object_->data.seek_table.num_points);
			return object_->data.seek_table.points[index];
		}

395
		void SeekTable::set_point(unsigned index, const ::FLAC__StreamMetadata_SeekPoint &point)
396
397
398
399
400
401
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(index < object_->data.seek_table.num_points);
			::FLAC__metadata_object_seektable_set_point(object_, index, point);
		}

402
		bool SeekTable::insert_point(unsigned index, const ::FLAC__StreamMetadata_SeekPoint &point)
403
404
405
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(index <= object_->data.seek_table.num_points);
Josh Coalson's avatar
Josh Coalson committed
406
			return (bool)::FLAC__metadata_object_seektable_insert_point(object_, index, point);
407
408
409
410
411
412
		}

		bool SeekTable::delete_point(unsigned index)
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(index < object_->data.seek_table.num_points);
Josh Coalson's avatar
Josh Coalson committed
413
			return (bool)::FLAC__metadata_object_seektable_delete_point(object_, index);
414
415
		}

416
417
418
419
420
421
		bool SeekTable::is_legal() const
		{
			FLAC__ASSERT(is_valid());
			return (bool)::FLAC__metadata_object_seektable_is_legal(object_);
		}

422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445

		//
		// VorbisComment::Entry
		//

		VorbisComment::Entry::Entry()
		{
			zero();
		}

		VorbisComment::Entry::Entry(const char *field, unsigned field_length)
		{
			zero();
			construct(field, field_length);
		}

		VorbisComment::Entry::Entry(const char *field_name, const char *field_value, unsigned field_value_length)
		{
			zero();
			construct(field_name, field_value, field_value_length);
		}

		VorbisComment::Entry::Entry(const Entry &entry)
		{
446
			FLAC__ASSERT(entry.is_valid());
447
			zero();
448
			construct((const char *)entry.entry_.entry, entry.entry_.length);
449
450
451
452
		}

		void VorbisComment::Entry::operator=(const Entry &entry)
		{
453
			FLAC__ASSERT(entry.is_valid());
454
			clear();
455
			construct((const char *)entry.entry_.entry, entry.entry_.length);
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
		}

		VorbisComment::Entry::~Entry()
		{
			clear();
		}

		bool VorbisComment::Entry::is_valid() const
		{
			return is_valid_;
		}

		unsigned VorbisComment::Entry::get_field_length() const
		{
			FLAC__ASSERT(is_valid());
			return entry_.length;
		}

		unsigned VorbisComment::Entry::get_field_name_length() const
		{
			FLAC__ASSERT(is_valid());
			return field_name_length_;
		}

		unsigned VorbisComment::Entry::get_field_value_length() const
		{
			FLAC__ASSERT(is_valid());
			return field_value_length_;
		}

486
		::FLAC__StreamMetadata_VorbisComment_Entry VorbisComment::Entry::get_entry() const
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
		{
			FLAC__ASSERT(is_valid());
			return entry_;
		}

		const char *VorbisComment::Entry::get_field() const
		{
			FLAC__ASSERT(is_valid());
			return (const char *)entry_.entry;
		}

		const char *VorbisComment::Entry::get_field_name() const
		{
			FLAC__ASSERT(is_valid());
			return field_name_;
		}

		const char *VorbisComment::Entry::get_field_value() const
		{
			FLAC__ASSERT(is_valid());
			return field_value_;
		}

		bool VorbisComment::Entry::set_field(const char *field, unsigned field_length)
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(0 != field);

			clear_entry();

			if(0 == (entry_.entry = (FLAC__byte*)malloc(field_length))) {
				is_valid_ = false;
			}
			else {
				entry_.length = field_length;
				memcpy(entry_.entry, field, field_length);
523
				(void) parse_field();
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
			}

			return is_valid_;
		}

		bool VorbisComment::Entry::set_field_name(const char *field_name)
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(0 != field_name);

			clear_field_name();

			if(0 == (field_name_ = strdup(field_name))) {
				is_valid_ = false;
			}
			else {
				field_name_length_ = strlen(field_name_);
				compose_field();
			}

			return is_valid_;
		}

		bool VorbisComment::Entry::set_field_value(const char *field_value, unsigned field_value_length)
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(0 != field_value);

			clear_field_value();

			if(0 == (field_value_ = (char *)malloc(field_value_length))) {
				is_valid_ = false;
			}
			else {
				field_value_length_ = field_value_length;
				memcpy(field_value_, field_value, field_value_length);
				compose_field();
			}

			return is_valid_;
		}

		void VorbisComment::Entry::zero()
		{
568
			is_valid_ = true;
569
570
571
572
573
574
575
576
577
578
579
580
581
			entry_.length = 0;
			entry_.entry = 0;
			field_name_ = 0;
			field_name_length_ = 0;
			field_value_ = 0;
			field_value_length_ = 0;
		}

		void VorbisComment::Entry::clear()
		{
			clear_entry();
			clear_field_name();
			clear_field_value();
582
			is_valid_ = true;
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
		}

		void VorbisComment::Entry::clear_entry()
		{
			if(0 != entry_.entry) {
				free(entry_.entry);
				entry_.entry = 0;
				entry_.length = 0;
			}
		}

		void VorbisComment::Entry::clear_field_name()
		{
			if(0 != field_name_) {
				free(field_name_);
				field_name_ = 0;
				field_name_length_ = 0;
			}
		}

		void VorbisComment::Entry::clear_field_value()
		{
			if(0 != field_value_) {
				free(field_value_);
				field_value_ = 0;
				field_value_length_ = 0;
			}
		}

		void VorbisComment::Entry::construct(const char *field, unsigned field_length)
		{
614
			if(set_field(field, field_length))
615
616
617
618
619
				parse_field();
		}

		void VorbisComment::Entry::construct(const char *field_name, const char *field_value, unsigned field_value_length)
		{
620
621
			if(set_field_name(field_name) && set_field_value(field_value, field_value_length))
				compose_field();
622
623
624
625
626
627
628
		}

		void VorbisComment::Entry::compose_field()
		{
			clear_entry();

			if(0 == (entry_.entry = (FLAC__byte*)malloc(field_name_length_ + 1 + field_value_length_))) {
629
				is_valid_ = false;
630
631
632
633
634
635
			}
			else {
				memcpy(entry_.entry, field_name_, field_name_length_);
				entry_.length += field_name_length_;
				memcpy(entry_.entry + entry_.length, "=", 1);
				entry_.length += 1;
636
				memcpy(entry_.entry + entry_.length, field_value_, field_value_length_);
637
638
639
640
641
642
643
644
645
646
647
648
				entry_.length += field_value_length_;
				is_valid_ = true;
			}
		}

		void VorbisComment::Entry::parse_field()
		{
			clear_field_name();
			clear_field_value();

			const char *p = (const char *)memchr(entry_.entry, '=', entry_.length);

649
650
			if(0 == p)
				p = (const char *)entry_.entry + entry_.length;
651

652
653
654
			field_name_length_ = p - (const char *)entry_.entry;
			if(0 == (field_name_ = (char *)malloc(field_name_length_ + 1))) { // +1 for the trailing \0
				is_valid_ = false;
655
656
657
658
659
				return;
			}
			memcpy(field_name_, entry_.entry, field_name_length_);
			field_name_[field_name_length_] = '\0';

660
661
662
663
664
665
666
667
668
669
670
671
672
673
			if(entry_.length - field_name_length_ == 0) {
				field_value_length_ = 0;
				if(0 == (field_value_ = (char *)malloc(0))) {
					is_valid_ = false;
					return;
				}
			}
			else {
				field_value_length_ = entry_.length - field_name_length_ - 1;
				if(0 == (field_value_ = (char *)malloc(field_value_length_))) {
					is_valid_ = false;
					return;
				}
				memcpy(field_value_, ++p, field_value_length_);
674
675
676
677
678
679
			}

			is_valid_ = true;
		}


Josh Coalson's avatar
Josh Coalson committed
680
681
682
683
684
685
686
687
688
689
690
		//
		// VorbisComment
		//

		VorbisComment::VorbisComment():
		Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT), /*copy=*/false)
		{ }

		VorbisComment::~VorbisComment()
		{ }

691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
		unsigned VorbisComment::get_num_comments() const
		{
			FLAC__ASSERT(is_valid());
			return object_->data.vorbis_comment.num_comments;
		}

		VorbisComment::Entry VorbisComment::get_vendor_string() const
		{
			FLAC__ASSERT(is_valid());
			return Entry((const char *)object_->data.vorbis_comment.vendor_string.entry, object_->data.vorbis_comment.vendor_string.length);
		}

		VorbisComment::Entry VorbisComment::get_comment(unsigned index) const
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(index < object_->data.vorbis_comment.num_comments);
707
			return Entry((const char *)object_->data.vorbis_comment.comments[index].entry, object_->data.vorbis_comment.comments[index].length);
708
709
710
711
712
		}

		bool VorbisComment::set_vendor_string(const VorbisComment::Entry &entry)
		{
			FLAC__ASSERT(is_valid());
713
714
715
716
717
			// vendor_string is a special kind of entry
			::FLAC__StreamMetadata_VorbisComment_Entry vendor_string;
			vendor_string.length = entry.get_field_name_length();
			vendor_string.entry = (FLAC__byte*)entry.get_field_name(); // we can cheat on const-ness because we make a copy below:
			return (bool)::FLAC__metadata_object_vorbiscomment_set_vendor_string(object_, vendor_string, /*copy=*/true);
718
719
720
721
722
723
		}

		bool VorbisComment::set_comment(unsigned index, const VorbisComment::Entry &entry)
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(index < object_->data.vorbis_comment.num_comments);
Josh Coalson's avatar
Josh Coalson committed
724
			return (bool)::FLAC__metadata_object_vorbiscomment_set_comment(object_, index, entry.get_entry(), /*copy=*/true);
725
726
727
728
729
730
		}

		bool VorbisComment::insert_comment(unsigned index, const VorbisComment::Entry &entry)
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(index <= object_->data.vorbis_comment.num_comments);
Josh Coalson's avatar
Josh Coalson committed
731
			return (bool)::FLAC__metadata_object_vorbiscomment_insert_comment(object_, index, entry.get_entry(), /*copy=*/true);
732
733
734
735
736
737
		}

		bool VorbisComment::delete_comment(unsigned index)
		{
			FLAC__ASSERT(is_valid());
			FLAC__ASSERT(index < object_->data.vorbis_comment.num_comments);
Josh Coalson's avatar
Josh Coalson committed
738
			return (bool)::FLAC__metadata_object_vorbiscomment_delete_comment(object_, index);
739
740
		}

Josh Coalson's avatar
Josh Coalson committed
741
742
743
744
745
746
747
748
749
750
751

		// ============================================================
		//
		//  Level 0
		//
		// ============================================================

		bool get_streaminfo(const char *filename, StreamInfo &streaminfo)
		{
			FLAC__ASSERT(0 != filename);

752
			::FLAC__StreamMetadata s;
Josh Coalson's avatar
Josh Coalson committed
753

754
			if(::FLAC__metadata_get_streaminfo(filename, &s)) {
Josh Coalson's avatar
Josh Coalson committed
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
				streaminfo = s;
				return true;
			}
			else
				return false;
		}


		// ============================================================
		//
		//  Level 1
		//
		// ============================================================

		SimpleIterator::SimpleIterator():
		iterator_(::FLAC__metadata_simple_iterator_new())
		{ }

		SimpleIterator::~SimpleIterator()
		{
			clear();
		}

		void SimpleIterator::clear()
		{
			if(0 != iterator_)
				FLAC__metadata_simple_iterator_delete(iterator_);
			iterator_ = 0;
		}

785
		bool SimpleIterator::init(const char *filename, bool read_only, bool preserve_file_stats)
Josh Coalson's avatar
Josh Coalson committed
786
787
788
		{
			FLAC__ASSERT(0 != filename);
			FLAC__ASSERT(is_valid());
789
			return (bool)::FLAC__metadata_simple_iterator_init(iterator_, filename, read_only, preserve_file_stats);
Josh Coalson's avatar
Josh Coalson committed
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
		}

		bool SimpleIterator::is_valid() const
		{
			return 0 != iterator_;
		}

		SimpleIterator::Status SimpleIterator::status()
		{
			FLAC__ASSERT(is_valid());
			return Status(::FLAC__metadata_simple_iterator_status(iterator_));
		}

		bool SimpleIterator::is_writable() const
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
806
			return (bool)::FLAC__metadata_simple_iterator_is_writable(iterator_);
Josh Coalson's avatar
Josh Coalson committed
807
808
809
810
811
		}

		bool SimpleIterator::next()
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
812
			return (bool)::FLAC__metadata_simple_iterator_next(iterator_);
Josh Coalson's avatar
Josh Coalson committed
813
814
815
816
817
		}

		bool SimpleIterator::prev()
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
818
			return (bool)::FLAC__metadata_simple_iterator_prev(iterator_);
Josh Coalson's avatar
Josh Coalson committed
819
820
		}

821
		::FLAC__MetadataType SimpleIterator::get_block_type() const
Josh Coalson's avatar
Josh Coalson committed
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__metadata_simple_iterator_get_block_type(iterator_);
		}

		Prototype *SimpleIterator::get_block()
		{
			FLAC__ASSERT(is_valid());
			return local::construct_block(::FLAC__metadata_simple_iterator_get_block(iterator_));
		}

		bool SimpleIterator::set_block(Prototype *block, bool use_padding)
		{
			FLAC__ASSERT(0 != block);
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
837
			return (bool)::FLAC__metadata_simple_iterator_set_block(iterator_, block->object_, use_padding);
Josh Coalson's avatar
Josh Coalson committed
838
839
840
841
842
843
		}

		bool SimpleIterator::insert_block_after(Prototype *block, bool use_padding)
		{
			FLAC__ASSERT(0 != block);
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
844
			return (bool)::FLAC__metadata_simple_iterator_insert_block_after(iterator_, block->object_, use_padding);
Josh Coalson's avatar
Josh Coalson committed
845
846
847
848
849
		}

		bool SimpleIterator::delete_block(bool use_padding)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
850
			return (bool)::FLAC__metadata_simple_iterator_delete_block(iterator_, use_padding);
Josh Coalson's avatar
Josh Coalson committed
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
		}


		// ============================================================
		//
		//  Level 2
		//
		// ============================================================

		Chain::Chain():
		chain_(::FLAC__metadata_chain_new())
		{ }

		Chain::~Chain()
		{
			clear();
		}

		void Chain::clear()
		{
			if(0 != chain_)
				FLAC__metadata_chain_delete(chain_);
			chain_ = 0;
		}

		bool Chain::is_valid() const
		{
			return 0 != chain_;
		}

		Chain::Status Chain::status()
		{
			FLAC__ASSERT(is_valid());
			return Status(::FLAC__metadata_chain_status(chain_));
		}

		bool Chain::read(const char *filename)
		{
			FLAC__ASSERT(0 != filename);
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
891
			return (bool)::FLAC__metadata_chain_read(chain_, filename);
Josh Coalson's avatar
Josh Coalson committed
892
893
894
895
896
		}

		bool Chain::write(bool use_padding, bool preserve_file_stats)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
897
			return (bool)::FLAC__metadata_chain_write(chain_, use_padding, preserve_file_stats);
Josh Coalson's avatar
Josh Coalson committed
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
		}

		void Chain::merge_padding()
		{
			FLAC__ASSERT(is_valid());
			::FLAC__metadata_chain_merge_padding(chain_);
		}

		void Chain::sort_padding()
		{
			FLAC__ASSERT(is_valid());
			::FLAC__metadata_chain_sort_padding(chain_);
		}


		Iterator::Iterator():
		iterator_(::FLAC__metadata_iterator_new())
		{ }

		Iterator::~Iterator()
		{
			clear();
		}

		void Iterator::clear()
		{
			if(0 != iterator_)
				FLAC__metadata_iterator_delete(iterator_);
			iterator_ = 0;
		}

		bool Iterator::is_valid() const
		{
			return 0 != iterator_;
		}

934
		void Iterator::init(Chain &chain)
Josh Coalson's avatar
Josh Coalson committed
935
936
		{
			FLAC__ASSERT(is_valid());
937
938
			FLAC__ASSERT(chain.is_valid());
			::FLAC__metadata_iterator_init(iterator_, chain.chain_);
Josh Coalson's avatar
Josh Coalson committed
939
940
941
942
943
		}

		bool Iterator::next()
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
944
			return (bool)::FLAC__metadata_iterator_next(iterator_);
Josh Coalson's avatar
Josh Coalson committed
945
946
947
948
949
		}

		bool Iterator::prev()
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
950
			return (bool)::FLAC__metadata_iterator_prev(iterator_);
Josh Coalson's avatar
Josh Coalson committed
951
952
		}

Josh Coalson's avatar
Josh Coalson committed
953
		::FLAC__MetadataType Iterator::get_block_type() const
Josh Coalson's avatar
Josh Coalson committed
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__metadata_iterator_get_block_type(iterator_);
		}

		Prototype *Iterator::get_block()
		{
			FLAC__ASSERT(is_valid());
			Prototype *block = local::construct_block(::FLAC__metadata_iterator_get_block(iterator_));
			if(0 != block)
				block->set_reference(true);
			return block;
		}

		bool Iterator::set_block(Prototype *block)
		{
			FLAC__ASSERT(0 != block);
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
972
			bool ret = (bool)::FLAC__metadata_iterator_set_block(iterator_, block->object_);
Josh Coalson's avatar
Josh Coalson committed
973
974
975
976
977
978
979
980
981
982
			if(ret) {
				block->set_reference(true);
				delete block;
			}
			return ret;
		}

		bool Iterator::delete_block(bool replace_with_padding)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
983
			return (bool)::FLAC__metadata_iterator_delete_block(iterator_, replace_with_padding);
Josh Coalson's avatar
Josh Coalson committed
984
985
986
987
988
989
		}

		bool Iterator::insert_block_before(Prototype *block)
		{
			FLAC__ASSERT(0 != block);
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
990
			bool ret = (bool)::FLAC__metadata_iterator_insert_block_before(iterator_, block->object_);
Josh Coalson's avatar
Josh Coalson committed
991
992
993
994
995
996
997
998
999
1000
1001
			if(ret) {
				block->set_reference(true);
				delete block;
			}
			return ret;
		}

		bool Iterator::insert_block_after(Prototype *block)
		{
			FLAC__ASSERT(0 != block);
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
1002
			bool ret = (bool)::FLAC__metadata_iterator_insert_block_after(iterator_, block->object_);
Josh Coalson's avatar
Josh Coalson committed
1003
1004
1005
1006
1007
1008
1009
			if(ret) {
				block->set_reference(true);
				delete block;
			}
			return ret;
		}

Josh Coalson's avatar
Josh Coalson committed
1010
1011
	};
};