Added TagLib (with AUTORS and COPYING files)
[someplayer] / src / taglib / ogg / vorbis / vorbisfile.cpp
1 /***************************************************************************
2     copyright            : (C) 2002 - 2008 by Scott Wheeler
3     email                : wheeler@kde.org
4  ***************************************************************************/
5
6 /***************************************************************************
7  *   This library is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU Lesser General Public License version   *
9  *   2.1 as published by the Free Software Foundation.                     *
10  *                                                                         *
11  *   This library is distributed in the hope that it will be useful, but   *
12  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
14  *   Lesser General Public License for more details.                       *
15  *                                                                         *
16  *   You should have received a copy of the GNU Lesser General Public      *
17  *   License along with this library; if not, write to the Free Software   *
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
19  *   USA                                                                   *
20  *                                                                         *
21  *   Alternatively, this file is available under the Mozilla Public        *
22  *   License Version 1.1.  You may obtain a copy of the License at         *
23  *   http://www.mozilla.org/MPL/                                           *
24  ***************************************************************************/
25
26 #include <bitset>
27
28 #include <tstring.h>
29 #include <tdebug.h>
30
31 #include "vorbisfile.h"
32
33 using namespace TagLib;
34
35 class Vorbis::File::FilePrivate
36 {
37 public:
38   FilePrivate() :
39     comment(0),
40     properties(0) {}
41
42   ~FilePrivate()
43   {
44     delete comment;
45     delete properties;
46   }
47
48   Ogg::XiphComment *comment;
49   Properties *properties;
50 };
51
52 namespace TagLib {
53   /*!
54    * Vorbis headers can be found with one type ID byte and the string "vorbis" in
55    * an Ogg stream.  0x03 indicates the comment header.
56    */
57   static const char vorbisCommentHeaderID[] = { 0x03, 'v', 'o', 'r', 'b', 'i', 's', 0 };
58 }
59
60 ////////////////////////////////////////////////////////////////////////////////
61 // public members
62 ////////////////////////////////////////////////////////////////////////////////
63
64 Vorbis::File::File(FileName file, bool readProperties,
65                    Properties::ReadStyle propertiesStyle) : Ogg::File(file)
66 {
67   d = new FilePrivate;
68   read(readProperties, propertiesStyle);
69 }
70
71 Vorbis::File::~File()
72 {
73   delete d;
74 }
75
76 Ogg::XiphComment *Vorbis::File::tag() const
77 {
78   return d->comment;
79 }
80
81 Vorbis::Properties *Vorbis::File::audioProperties() const
82 {
83   return d->properties;
84 }
85
86 bool Vorbis::File::save()
87 {
88   ByteVector v(vorbisCommentHeaderID);
89
90   if(!d->comment)
91     d->comment = new Ogg::XiphComment;
92   v.append(d->comment->render());
93
94   setPacket(1, v);
95
96   return Ogg::File::save();
97 }
98
99 ////////////////////////////////////////////////////////////////////////////////
100 // private members
101 ////////////////////////////////////////////////////////////////////////////////
102
103 void Vorbis::File::read(bool readProperties, Properties::ReadStyle propertiesStyle)
104 {
105   ByteVector commentHeaderData = packet(1);
106
107   if(commentHeaderData.mid(0, 7) != vorbisCommentHeaderID) {
108     debug("Vorbis::File::read() - Could not find the Vorbis comment header.");
109     setValid(false);
110     return;
111   }
112
113   d->comment = new Ogg::XiphComment(commentHeaderData.mid(7));
114
115   if(readProperties)
116     d->properties = new Properties(this, propertiesStyle);
117 }