1 /*
2 * mp3plot - Bitrate analysis tool
3 *
4 * Copyright (C) 2007, 2009 Toni Corvera
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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 // $Id: tags.cc 1101 2009-04-05 17:33:40Z $
22
23 #include "tags.h"
24 #include "../utils/str_utils.h"
25 #include "../mp3_config.h" // btoh16
26
27 #include <iostream>
28 #include <cstring> // memcmp, memcpy
29
30 using std::string;
31 using std::ifstream;
32 using std::cout;
33
34 namespace str = net_outlyer::str;
35
36 namespace mp3plot {
37
38 namespace mp3 {
39
40 using net_outlyer::btoh16;
41
42 // -- ID3v2 Header --
43
read(ifstream & in)44 void id3v2tag::read(ifstream & in) throw (e_read_failure) {
45 in.read(CHARR(id), 3);
46 check(in, "header");
47
48 if (0 != ::memcmp("ID3", CHARR(id), 3)) {
49 // Tried to read an ID3v2 tag from a file without it
50 throw e_bad_filetype();
51 }
52 // POST ]-> It looks like a valid file
53
54 in.read(CHARR(version), 2);
55 check(in, "header");
56 in.read(CHARR(&flags), 1);
57 check(in, "header");
58 //in.read(CHARR(&tag_size), 4);
59 //if (!in.good()) throw 1;
60 {
61 tag_size = 0;
62 for (int i=0; i<4; ++i) {
63 uint8_t byte;
64 in.read(CHARR(&byte), 1);
65 // FIXME: probably incorrect for more than 2 bytes!
66 tag_size = (tag_size << 7) + (byte & 0x7f);
67 }
68 }
69 }
70
operator <<(std::ostream & os,const id3v2tag & t)71 std::ostream & operator<<(std::ostream & os, const id3v2tag & t) {
72 string sid;
73
74 {
75 char buffer[4];
76 ::memcpy(buffer, t.id, 3);
77 buffer[3] = 0;
78 sid = buffer;
79 }
80
81 os << "Tag Identifier / File Magic: " << sid << "\n";
82 os << "Tag version: " << str::to_hex(btoh16(* t.version),2,true) << "\n";
83 os << "ID3V2 Flags: " << str::to_hex(t.flags,1,true) << "\n";
84 os << "Tag size: " << t.tag_size << std::endl;
85
86 return os;
87 }
88
89 // -- ID3v1 Header --
90
read(ifstream & in)91 void id3v1tag::read(ifstream & in) throw (e_read_failure) {
92 }
93
94 } // namespace mp3
95
96 } // namespace mp3plot
97
98 // vim:set ts=4 et ai:
99
100