Format taglib sources

This commit is contained in:
Jonas Kvinge
2020-06-13 19:02:42 +02:00
parent 72bff7fa35
commit 4ce099294c
224 changed files with 12905 additions and 15623 deletions

View File

@@ -46,25 +46,22 @@
using namespace Strawberry_TagLib::TagLib;
namespace
{
enum { ApeAPEIndex = 0, ApeID3v1Index = 1 };
namespace {
enum { ApeAPEIndex = 0,
ApeID3v1Index = 1 };
}
class APE::File::FilePrivate
{
public:
FilePrivate() :
APELocation(-1),
APESize(0),
ID3v1Location(-1),
ID3v2Header(0),
ID3v2Location(-1),
ID3v2Size(0),
properties(0) {}
class APE::File::FilePrivate {
public:
FilePrivate() : APELocation(-1),
APESize(0),
ID3v1Location(-1),
ID3v2Header(0),
ID3v2Location(-1),
ID3v2Size(0),
properties(0) {}
~FilePrivate()
{
~FilePrivate() {
delete ID3v2Header;
delete properties;
}
@@ -87,8 +84,7 @@ public:
// static members
////////////////////////////////////////////////////////////////////////////////
bool APE::File::isSupported(IOStream*)
{
bool APE::File::isSupported(IOStream *) {
// An APE file has an ID "MAC " somewhere. An ID3v2 tag may precede.
// FIXME:
@@ -102,69 +98,58 @@ bool APE::File::isSupported(IOStream*)
// public members
////////////////////////////////////////////////////////////////////////////////
APE::File::File(FileName file, bool readProperties, Properties::ReadStyle) :
Strawberry_TagLib::TagLib::File(file),
d(new FilePrivate())
{
if(isOpen())
APE::File::File(FileName file, bool readProperties, Properties::ReadStyle) : Strawberry_TagLib::TagLib::File(file),
d(new FilePrivate()) {
if (isOpen())
read(readProperties);
}
APE::File::File(IOStream *stream, bool readProperties, Properties::ReadStyle) :
Strawberry_TagLib::TagLib::File(stream),
d(new FilePrivate())
{
if(isOpen())
APE::File::File(IOStream *stream, bool readProperties, Properties::ReadStyle) : Strawberry_TagLib::TagLib::File(stream),
d(new FilePrivate()) {
if (isOpen())
read(readProperties);
}
APE::File::~File()
{
APE::File::~File() {
delete d;
}
Strawberry_TagLib::TagLib::Tag *APE::File::tag() const
{
Strawberry_TagLib::TagLib::Tag *APE::File::tag() const {
return &d->tag;
}
PropertyMap APE::File::properties() const
{
PropertyMap APE::File::properties() const {
return d->tag.properties();
}
void APE::File::removeUnsupportedProperties(const StringList &properties)
{
void APE::File::removeUnsupportedProperties(const StringList &properties) {
d->tag.removeUnsupportedProperties(properties);
}
PropertyMap APE::File::setProperties(const PropertyMap &properties)
{
if(ID3v1Tag())
PropertyMap APE::File::setProperties(const PropertyMap &properties) {
if (ID3v1Tag())
ID3v1Tag()->setProperties(properties);
return APETag(true)->setProperties(properties);
}
APE::Properties *APE::File::audioProperties() const
{
APE::Properties *APE::File::audioProperties() const {
return d->properties;
}
bool APE::File::save()
{
if(readOnly()) {
bool APE::File::save() {
if (readOnly()) {
debug("APE::File::save() -- File is read only.");
return false;
}
// Update ID3v1 tag
if(ID3v1Tag() && !ID3v1Tag()->isEmpty()) {
if (ID3v1Tag() && !ID3v1Tag()->isEmpty()) {
// ID3v1 tag is not empty. Update the old one or create a new one.
if(d->ID3v1Location >= 0) {
if (d->ID3v1Location >= 0) {
seek(d->ID3v1Location);
}
else {
@@ -178,7 +163,7 @@ bool APE::File::save()
// ID3v1 tag is empty. Remove the old one.
if(d->ID3v1Location >= 0) {
if (d->ID3v1Location >= 0) {
truncate(d->ID3v1Location);
d->ID3v1Location = -1;
}
@@ -186,12 +171,12 @@ bool APE::File::save()
// Update APE tag
if(APETag() && !APETag()->isEmpty()) {
if (APETag() && !APETag()->isEmpty()) {
// APE tag is not empty. Update the old one or create a new one.
if(d->APELocation < 0) {
if(d->ID3v1Location >= 0)
if (d->APELocation < 0) {
if (d->ID3v1Location >= 0)
d->APELocation = d->ID3v1Location;
else
d->APELocation = length();
@@ -200,7 +185,7 @@ bool APE::File::save()
const ByteVector data = APETag()->render();
insert(data, d->APELocation, d->APESize);
if(d->ID3v1Location >= 0)
if (d->ID3v1Location >= 0)
d->ID3v1Location += (static_cast<long>(data.size()) - d->APESize);
d->APESize = data.size();
@@ -209,10 +194,10 @@ bool APE::File::save()
// APE tag is empty. Remove the old one.
if(d->APELocation >= 0) {
if (d->APELocation >= 0) {
removeBlock(d->APELocation, d->APESize);
if(d->ID3v1Location >= 0)
if (d->ID3v1Location >= 0)
d->ID3v1Location -= d->APESize;
d->APELocation = -1;
@@ -223,35 +208,30 @@ bool APE::File::save()
return true;
}
ID3v1::Tag *APE::File::ID3v1Tag(bool create)
{
ID3v1::Tag *APE::File::ID3v1Tag(bool create) {
return d->tag.access<ID3v1::Tag>(ApeID3v1Index, create);
}
APE::Tag *APE::File::APETag(bool create)
{
APE::Tag *APE::File::APETag(bool create) {
return d->tag.access<APE::Tag>(ApeAPEIndex, create);
}
void APE::File::strip(int tags)
{
if(tags & ID3v1)
void APE::File::strip(int tags) {
if (tags & ID3v1)
d->tag.set(ApeID3v1Index, 0);
if(tags & APE)
if (tags & APE)
d->tag.set(ApeAPEIndex, 0);
if(!ID3v1Tag())
if (!ID3v1Tag())
APETag(true);
}
bool APE::File::hasAPETag() const
{
bool APE::File::hasAPETag() const {
return (d->APELocation >= 0);
}
bool APE::File::hasID3v1Tag() const
{
bool APE::File::hasID3v1Tag() const {
return (d->ID3v1Location >= 0);
}
@@ -259,13 +239,12 @@ bool APE::File::hasID3v1Tag() const
// private members
////////////////////////////////////////////////////////////////////////////////
void APE::File::read(bool readProperties)
{
void APE::File::read(bool readProperties) {
// Look for an ID3v2 tag
d->ID3v2Location = Utils::findID3v2(this);
if(d->ID3v2Location >= 0) {
if (d->ID3v2Location >= 0) {
seek(d->ID3v2Location);
d->ID3v2Header = new ID3v2::Header(readBlock(ID3v2::Header::size()));
d->ID3v2Size = d->ID3v2Header->completeTagSize();
@@ -275,36 +254,36 @@ void APE::File::read(bool readProperties)
d->ID3v1Location = Utils::findID3v1(this);
if(d->ID3v1Location >= 0)
if (d->ID3v1Location >= 0)
d->tag.set(ApeID3v1Index, new ID3v1::Tag(this, d->ID3v1Location));
// Look for an APE tag
d->APELocation = Utils::findAPE(this, d->ID3v1Location);
if(d->APELocation >= 0) {
if (d->APELocation >= 0) {
d->tag.set(ApeAPEIndex, new APE::Tag(this, d->APELocation));
d->APESize = APETag()->footer()->completeTagSize();
d->APELocation = d->APELocation + APE::Footer::size() - d->APESize;
}
if(d->ID3v1Location < 0)
if (d->ID3v1Location < 0)
APETag(true);
// Look for APE audio properties
if(readProperties) {
if (readProperties) {
long streamLength;
if(d->APELocation >= 0)
if (d->APELocation >= 0)
streamLength = d->APELocation;
else if(d->ID3v1Location >= 0)
else if (d->ID3v1Location >= 0)
streamLength = d->ID3v1Location;
else
streamLength = length();
if(d->ID3v2Location >= 0) {
if (d->ID3v2Location >= 0) {
seek(d->ID3v2Location + d->ID3v2Size);
streamLength -= (d->ID3v2Location + d->ID3v2Size);
}

View File

@@ -41,59 +41,62 @@
namespace Strawberry_TagLib {
namespace TagLib {
class Tag;
class Tag;
namespace ID3v1 { class Tag; }
namespace APE { class Tag; }
namespace ID3v1 {
class Tag;
}
namespace APE {
class Tag;
}
//! An implementation of APE metadata
//! An implementation of APE metadata
/*!
/*!
* This is implementation of APE metadata.
*
* This supports ID3v1 and APE (v1 and v2) style comments as well as reading stream
* properties from the file.
*/
namespace APE {
namespace APE {
//! An implementation of TagLib::File with APE specific methods
//! An implementation of TagLib::File with APE specific methods
/*!
/*!
* This implements and provides an interface for APE files to the
* TagLib::Tag and TagLib::AudioProperties interfaces by way of implementing
* the abstract TagLib::File API as well as providing some additional
* information specific to APE files.
*/
class TAGLIB_EXPORT File : public Strawberry_TagLib::TagLib::File
{
public:
/*!
class TAGLIB_EXPORT File : public Strawberry_TagLib::TagLib::File {
public:
/*!
* This set of flags is used for various operations and is suitable for
* being OR-ed together.
*/
enum TagTypes {
//! Empty set. Matches no tag types.
NoTags = 0x0000,
//! Matches ID3v1 tags.
ID3v1 = 0x0001,
//! Matches APE tags.
APE = 0x0002,
//! Matches all tag types.
AllTags = 0xffff
};
enum TagTypes {
//! Empty set. Matches no tag types.
NoTags = 0x0000,
//! Matches ID3v1 tags.
ID3v1 = 0x0001,
//! Matches APE tags.
APE = 0x0002,
//! Matches all tag types.
AllTags = 0xffff
};
/*!
/*!
* Constructs an APE file from \a file. If \a readProperties is true the
* file's audio properties will also be read.
*
* \note In the current implementation, \a propertiesStyle is ignored.
*/
File(FileName file, bool readProperties = true,
Properties::ReadStyle propertiesStyle = Properties::Average);
File(FileName file, bool readProperties = true,
Properties::ReadStyle propertiesStyle = Properties::Average);
/*!
/*!
* Constructs an APE file from \a stream. If \a readProperties is true the
* file's audio properties will also be read.
*
@@ -102,55 +105,55 @@ namespace TagLib {
*
* \note In the current implementation, \a propertiesStyle is ignored.
*/
File(IOStream *stream, bool readProperties = true,
Properties::ReadStyle propertiesStyle = Properties::Average);
File(IOStream *stream, bool readProperties = true,
Properties::ReadStyle propertiesStyle = Properties::Average);
/*!
/*!
* Destroys this instance of the File.
*/
virtual ~File();
virtual ~File();
/*!
/*!
* Returns the Tag for this file. This will be an APE tag, an ID3v1 tag
* or a combination of the two.
*/
virtual Strawberry_TagLib::TagLib::Tag *tag() const;
virtual Strawberry_TagLib::TagLib::Tag *tag() const;
/*!
/*!
* Implements the unified property interface -- export function.
* If the file contains both an APE and an ID3v1 tag, only APE
* will be converted to the PropertyMap.
*/
PropertyMap properties() const;
PropertyMap properties() const;
/*!
/*!
* Removes unsupported properties. Forwards to the actual Tag's
* removeUnsupportedProperties() function.
*/
void removeUnsupportedProperties(const StringList &properties);
void removeUnsupportedProperties(const StringList &properties);
/*!
/*!
* Implements the unified property interface -- import function.
* Creates an APEv2 tag if necessary. A potentially existing ID3v1
* tag will be updated as well.
*/
PropertyMap setProperties(const PropertyMap &);
PropertyMap setProperties(const PropertyMap &);
/*!
/*!
* Returns the APE::Properties for this file. If no audio properties
* were read then this will return a null pointer.
*/
virtual Properties *audioProperties() const;
virtual Properties *audioProperties() const;
/*!
/*!
* Saves the file.
*
* \note According to the official Monkey's Audio SDK, an APE file
* can only have either ID3V1 or APE tags, so a parameter is used here.
*/
virtual bool save();
virtual bool save();
/*!
/*!
* Returns a pointer to the ID3v1 tag of the file.
*
* If \a create is false (the default) this may return a null pointer
@@ -167,9 +170,9 @@ namespace TagLib {
*
* \see hasID3v1Tag()
*/
ID3v1::Tag *ID3v1Tag(bool create = false);
ID3v1::Tag *ID3v1Tag(bool create = false);
/*!
/*!
* Returns a pointer to the APE tag of the file.
*
* If \a create is false (the default) this may return a null pointer
@@ -186,9 +189,9 @@ namespace TagLib {
*
* \see hasAPETag()
*/
APE::Tag *APETag(bool create = false);
APE::Tag *APETag(bool create = false);
/*!
/*!
* This will remove the tags that match the OR-ed together TagTypes from the
* file. By default it removes all tags.
*
@@ -196,42 +199,42 @@ namespace TagLib {
* as their memory will be freed.
* \note In order to make the removal permanent save() still needs to be called
*/
void strip(int tags = AllTags);
void strip(int tags = AllTags);
/*!
/*!
* Returns whether or not the file on disk actually has an APE tag.
*
* \see APETag()
*/
bool hasAPETag() const;
bool hasAPETag() const;
/*!
/*!
* Returns whether or not the file on disk actually has an ID3v1 tag.
*
* \see ID3v1Tag()
*/
bool hasID3v1Tag() const;
bool hasID3v1Tag() const;
/*!
/*!
* Returns whether or not the given \a stream can be opened as an APE
* file.
*
* \note This method is designed to do a quick check. The result may
* not necessarily be correct.
*/
static bool isSupported(IOStream *stream);
static bool isSupported(IOStream *stream);
private:
File(const File &);
File &operator=(const File &);
private:
File(const File &);
File &operator=(const File &);
void read(bool readProperties);
void read(bool readProperties);
class FilePrivate;
FilePrivate *d;
};
}
}
}
class FilePrivate;
FilePrivate *d;
};
} // namespace APE
} // namespace TagLib
} // namespace Strawberry_TagLib
#endif

View File

@@ -35,16 +35,14 @@
using namespace Strawberry_TagLib::TagLib;
using namespace APE;
class APE::Footer::FooterPrivate
{
public:
FooterPrivate() :
version(0),
footerPresent(true),
headerPresent(false),
isHeader(false),
itemCount(0),
tagSize(0) {}
class APE::Footer::FooterPrivate {
public:
FooterPrivate() : version(0),
footerPresent(true),
headerPresent(false),
isHeader(false),
itemCount(0),
tagSize(0) {}
unsigned int version;
@@ -61,13 +59,11 @@ public:
// static members
////////////////////////////////////////////////////////////////////////////////
unsigned int APE::Footer::size()
{
unsigned int APE::Footer::size() {
return 32;
}
ByteVector APE::Footer::fileIdentifier()
{
ByteVector APE::Footer::fileIdentifier() {
return ByteVector("APETAGEX");
}
@@ -75,88 +71,70 @@ ByteVector APE::Footer::fileIdentifier()
// public members
////////////////////////////////////////////////////////////////////////////////
APE::Footer::Footer() :
d(new FooterPrivate())
{
APE::Footer::Footer() : d(new FooterPrivate()) {
}
APE::Footer::Footer(const ByteVector &data) :
d(new FooterPrivate())
{
APE::Footer::Footer(const ByteVector &data) : d(new FooterPrivate()) {
parse(data);
}
APE::Footer::~Footer()
{
APE::Footer::~Footer() {
delete d;
}
unsigned int APE::Footer::version() const
{
unsigned int APE::Footer::version() const {
return d->version;
}
bool APE::Footer::headerPresent() const
{
bool APE::Footer::headerPresent() const {
return d->headerPresent;
}
bool APE::Footer::footerPresent() const
{
bool APE::Footer::footerPresent() const {
return d->footerPresent;
}
bool APE::Footer::isHeader() const
{
bool APE::Footer::isHeader() const {
return d->isHeader;
}
void APE::Footer::setHeaderPresent(bool b) const
{
void APE::Footer::setHeaderPresent(bool b) const {
d->headerPresent = b;
}
unsigned int APE::Footer::itemCount() const
{
unsigned int APE::Footer::itemCount() const {
return d->itemCount;
}
void APE::Footer::setItemCount(unsigned int s)
{
void APE::Footer::setItemCount(unsigned int s) {
d->itemCount = s;
}
unsigned int APE::Footer::tagSize() const
{
unsigned int APE::Footer::tagSize() const {
return d->tagSize;
}
unsigned int APE::Footer::completeTagSize() const
{
if(d->headerPresent)
unsigned int APE::Footer::completeTagSize() const {
if (d->headerPresent)
return d->tagSize + size();
else
return d->tagSize;
}
void APE::Footer::setTagSize(unsigned int s)
{
void APE::Footer::setTagSize(unsigned int s) {
d->tagSize = s;
}
void APE::Footer::setData(const ByteVector &data)
{
void APE::Footer::setData(const ByteVector &data) {
parse(data);
}
ByteVector APE::Footer::renderFooter() const
{
ByteVector APE::Footer::renderFooter() const {
return render(false);
}
ByteVector APE::Footer::renderHeader() const
{
if(!d->headerPresent)
ByteVector APE::Footer::renderHeader() const {
if (!d->headerPresent)
return ByteVector();
else
return render(true);
@@ -166,9 +144,8 @@ ByteVector APE::Footer::renderHeader() const
// protected members
////////////////////////////////////////////////////////////////////////////////
void APE::Footer::parse(const ByteVector &data)
{
if(data.size() < size())
void APE::Footer::parse(const ByteVector &data) {
if (data.size() < size())
return;
// The first eight bytes, data[0..7], are the File Identifier, "APETAGEX".
@@ -192,11 +169,9 @@ void APE::Footer::parse(const ByteVector &data)
d->headerPresent = flags[31];
d->footerPresent = !flags[30];
d->isHeader = flags[29];
}
ByteVector APE::Footer::render(bool isHeader) const
{
ByteVector APE::Footer::render(bool isHeader) const {
ByteVector v;
// add the file identifier -- "APETAGEX"
@@ -221,7 +196,7 @@ ByteVector APE::Footer::render(bool isHeader) const
std::bitset<32> flags;
flags[31] = d->headerPresent;
flags[30] = false; // footer is always present
flags[30] = false; // footer is always present
flags[29] = isHeader;
v.append(ByteVector::fromUInt(flags.to_ulong(), false));

View File

@@ -32,144 +32,143 @@
namespace Strawberry_TagLib {
namespace TagLib {
namespace APE {
namespace APE {
//! An implementation of APE footers
//! An implementation of APE footers
/*!
/*!
* This class implements APE footers (and headers). It attempts to follow, both
* semantically and programmatically, the structure specified in
* the APE v2.0 standard. The API is based on the properties of APE footer and
* headers specified there.
*/
class TAGLIB_EXPORT Footer
{
public:
/*!
class TAGLIB_EXPORT Footer {
public:
/*!
* Constructs an empty APE footer.
*/
Footer();
Footer();
/*!
/*!
* Constructs an APE footer based on \a data. parse() is called
* immediately.
*/
Footer(const ByteVector &data);
Footer(const ByteVector &data);
/*!
/*!
* Destroys the footer.
*/
virtual ~Footer();
virtual ~Footer();
/*!
/*!
* Returns the version number. (Note: This is the 1000 or 2000.)
*/
unsigned int version() const;
unsigned int version() const;
/*!
/*!
* Returns true if a header is present in the tag.
*/
bool headerPresent() const;
bool headerPresent() const;
/*!
/*!
* Returns true if a footer is present in the tag.
*/
bool footerPresent() const;
bool footerPresent() const;
/*!
/*!
* Returns true this is actually the header.
*/
bool isHeader() const;
bool isHeader() const;
/*!
/*!
* Sets whether the header should be rendered or not
*/
void setHeaderPresent(bool b) const;
void setHeaderPresent(bool b) const;
/*!
/*!
* Returns the number of items in the tag.
*/
unsigned int itemCount() const;
unsigned int itemCount() const;
/*!
/*!
* Set the item count to \a s.
* \see itemCount()
*/
void setItemCount(unsigned int s);
void setItemCount(unsigned int s);
/*!
/*!
* Returns the tag size in bytes. This is the size of the frame content and footer.
* The size of the \e entire tag will be this plus the header size, if present.
*
* \see completeTagSize()
*/
unsigned int tagSize() const;
unsigned int tagSize() const;
/*!
/*!
* Returns the tag size, including if present, the header
* size.
*
* \see tagSize()
*/
unsigned int completeTagSize() const;
unsigned int completeTagSize() const;
/*!
/*!
* Set the tag size to \a s.
* \see tagSize()
*/
void setTagSize(unsigned int s);
void setTagSize(unsigned int s);
/*!
/*!
* Returns the size of the footer. Presently this is always 32 bytes.
*/
static unsigned int size();
static unsigned int size();
/*!
/*!
* Returns the string used to identify an APE tag inside of a file.
* Presently this is always "APETAGEX".
*/
static ByteVector fileIdentifier();
static ByteVector fileIdentifier();
/*!
/*!
* Sets the data that will be used as the footer. 32 bytes,
* starting from \a data will be used.
*/
void setData(const ByteVector &data);
void setData(const ByteVector &data);
/*!
/*!
* Renders the footer back to binary format.
*/
ByteVector renderFooter() const;
ByteVector renderFooter() const;
/*!
/*!
* Renders the header corresponding to the footer. If headerPresent is
* set to false, it returns an empty ByteVector.
*/
ByteVector renderHeader() const;
ByteVector renderHeader() const;
protected:
/*!
protected:
/*!
* Called by setData() to parse the footer data. It makes this information
* available through the public API.
*/
void parse(const ByteVector &data);
void parse(const ByteVector &data);
/*!
/*!
* Called by renderFooter and renderHeader
*/
ByteVector render(bool isHeader) const;
ByteVector render(bool isHeader) const;
private:
Footer(const Footer &);
Footer &operator=(const Footer &);
private:
Footer(const Footer &);
Footer &operator=(const Footer &);
class FooterPrivate;
FooterPrivate *d;
};
class FooterPrivate;
FooterPrivate *d;
};
}
}
}
} // namespace APE
} // namespace TagLib
} // namespace Strawberry_TagLib
#endif

View File

@@ -31,12 +31,10 @@
using namespace Strawberry_TagLib::TagLib;
using namespace APE;
class APE::Item::ItemPrivate
{
public:
ItemPrivate() :
type(Text),
readOnly(false) {}
class APE::Item::ItemPrivate {
public:
ItemPrivate() : type(Text),
readOnly(false) {}
Item::ItemTypes type;
String key;
@@ -49,30 +47,22 @@ public:
// public members
////////////////////////////////////////////////////////////////////////////////
APE::Item::Item() :
d(new ItemPrivate())
{
APE::Item::Item() : d(new ItemPrivate()) {
}
APE::Item::Item(const String &key, const String &value) :
d(new ItemPrivate())
{
APE::Item::Item(const String &key, const String &value) : d(new ItemPrivate()) {
d->key = key;
d->text.append(value);
}
APE::Item::Item(const String &key, const StringList &values) :
d(new ItemPrivate())
{
APE::Item::Item(const String &key, const StringList &values) : d(new ItemPrivate()) {
d->key = key;
d->text = values;
}
APE::Item::Item(const String &key, const ByteVector &value, bool binary) :
d(new ItemPrivate())
{
APE::Item::Item(const String &key, const ByteVector &value, bool binary) : d(new ItemPrivate()) {
d->key = key;
if(binary) {
if (binary) {
d->type = Binary;
d->value = value;
}
@@ -81,118 +71,99 @@ APE::Item::Item(const String &key, const ByteVector &value, bool binary) :
}
}
APE::Item::Item(const Item &item) :
d(new ItemPrivate(*item.d))
{
APE::Item::Item(const Item &item) : d(new ItemPrivate(*item.d)) {
}
APE::Item::~Item()
{
APE::Item::~Item() {
delete d;
}
Item &APE::Item::operator=(const Item &item)
{
Item &APE::Item::operator=(const Item &item) {
Item(item).swap(*this);
return *this;
}
void APE::Item::swap(Item &item)
{
void APE::Item::swap(Item &item) {
using std::swap;
swap(d, item.d);
}
void APE::Item::setReadOnly(bool readOnly)
{
void APE::Item::setReadOnly(bool readOnly) {
d->readOnly = readOnly;
}
bool APE::Item::isReadOnly() const
{
bool APE::Item::isReadOnly() const {
return d->readOnly;
}
void APE::Item::setType(APE::Item::ItemTypes val)
{
void APE::Item::setType(APE::Item::ItemTypes val) {
d->type = val;
}
APE::Item::ItemTypes APE::Item::type() const
{
APE::Item::ItemTypes APE::Item::type() const {
return d->type;
}
String APE::Item::key() const
{
String APE::Item::key() const {
return d->key;
}
ByteVector APE::Item::binaryData() const
{
ByteVector APE::Item::binaryData() const {
return d->value;
}
void APE::Item::setBinaryData(const ByteVector &value)
{
void APE::Item::setBinaryData(const ByteVector &value) {
d->type = Binary;
d->value = value;
d->text.clear();
}
ByteVector APE::Item::value() const
{
ByteVector APE::Item::value() const {
// This seems incorrect as it won't be actually rendering the value to keep it
// up to date.
return d->value;
}
void APE::Item::setKey(const String &key)
{
void APE::Item::setKey(const String &key) {
d->key = key;
}
void APE::Item::setValue(const String &value)
{
void APE::Item::setValue(const String &value) {
d->type = Text;
d->text = value;
d->value.clear();
}
void APE::Item::setValues(const StringList &value)
{
void APE::Item::setValues(const StringList &value) {
d->type = Text;
d->text = value;
d->value.clear();
}
void APE::Item::appendValue(const String &value)
{
void APE::Item::appendValue(const String &value) {
d->type = Text;
d->text.append(value);
d->value.clear();
}
void APE::Item::appendValues(const StringList &values)
{
void APE::Item::appendValues(const StringList &values) {
d->type = Text;
d->text.append(values);
d->value.clear();
}
int APE::Item::size() const
{
int APE::Item::size() const {
int result = 8 + d->key.size() + 1;
switch(d->type) {
switch (d->type) {
case Text:
if(!d->text.isEmpty()) {
if (!d->text.isEmpty()) {
StringList::ConstIterator it = d->text.begin();
result += it->data(String::UTF8).size();
it++;
for(; it != d->text.end(); ++it)
for (; it != d->text.end(); ++it)
result += 1 + it->data(String::UTF8).size();
}
break;
@@ -205,31 +176,27 @@ int APE::Item::size() const
return result;
}
StringList APE::Item::toStringList() const
{
StringList APE::Item::toStringList() const {
return d->text;
}
StringList APE::Item::values() const
{
StringList APE::Item::values() const {
return d->text;
}
String APE::Item::toString() const
{
if(d->type == Text && !isEmpty())
String APE::Item::toString() const {
if (d->type == Text && !isEmpty())
return d->text.front();
else
return String();
}
bool APE::Item::isEmpty() const
{
switch(d->type) {
bool APE::Item::isEmpty() const {
switch (d->type) {
case Text:
if(d->text.isEmpty())
if (d->text.isEmpty())
return true;
if(d->text.size() == 1 && d->text.front().isEmpty())
if (d->text.size() == 1 && d->text.front().isEmpty())
return true;
return false;
case Binary:
@@ -240,17 +207,16 @@ bool APE::Item::isEmpty() const
}
}
void APE::Item::parse(const ByteVector &data)
{
void APE::Item::parse(const ByteVector &data) {
// 11 bytes is the minimum size for an APE item
if(data.size() < 11) {
if (data.size() < 11) {
debug("APE::Item::parse() -- no data in item");
return;
}
const unsigned int valueLength = data.toUInt(0, false);
const unsigned int flags = data.toUInt(4, false);
const unsigned int valueLength = data.toUInt(0, false);
const unsigned int flags = data.toUInt(4, false);
// An item key can contain ASCII characters from 0x20 up to 0x7E, not UTF-8.
// We assume that the validity of the given key has been checked.
@@ -262,27 +228,26 @@ void APE::Item::parse(const ByteVector &data)
setReadOnly(flags & 1);
setType(ItemTypes((flags >> 1) & 3));
if(Text == d->type)
if (Text == d->type)
d->text = StringList(ByteVectorList::split(value, '\0'), String::UTF8);
else
d->value = value;
}
ByteVector APE::Item::render() const
{
ByteVector APE::Item::render() const {
ByteVector data;
unsigned int flags = ((d->readOnly) ? 1 : 0) | (d->type << 1);
ByteVector value;
if(isEmpty())
if (isEmpty())
return data;
if(d->type == Text) {
if (d->type == Text) {
StringList::ConstIterator it = d->text.begin();
value.append(it->data(String::UTF8));
it++;
for(; it != d->text.end(); ++it) {
for (; it != d->text.end(); ++it) {
value.append('\0');
value.append(it->data(String::UTF8));
}

View File

@@ -33,194 +33,191 @@
namespace Strawberry_TagLib {
namespace TagLib {
namespace APE {
namespace APE {
//! An implementation of APE-items
//! An implementation of APE-items
/*!
/*!
* This class provides the features of items in the APEv2 standard.
*/
class TAGLIB_EXPORT Item
{
public:
/*!
class TAGLIB_EXPORT Item {
public:
/*!
* Enum of types an Item can have. The value of 3 is reserved.
*/
enum ItemTypes {
//! Item contains text information coded in UTF-8
Text = 0,
//! Item contains binary information
Binary = 1,
//! Item is a locator of external stored information
Locator = 2
};
/*!
enum ItemTypes {
//! Item contains text information coded in UTF-8
Text = 0,
//! Item contains binary information
Binary = 1,
//! Item is a locator of external stored information
Locator = 2
};
/*!
* Constructs an empty item.
*/
Item();
Item();
/*!
/*!
* Constructs a text item with \a key and \a value.
*/
// BIC: Remove this, StringList has a constructor from a single string
Item(const String &key, const String &value);
// BIC: Remove this, StringList has a constructor from a single string
Item(const String &key, const String &value);
/*!
/*!
* Constructs a text item with \a key and \a values.
*/
Item(const String &key, const StringList &values);
Item(const String &key, const StringList &values);
/*!
/*!
* Constructs an item with \a key and \a value.
* If \a binary is true a Binary item will be created, otherwise \a value will be interpreted as text
*/
Item(const String &key, const ByteVector &value, bool binary);
Item(const String &key, const ByteVector &value, bool binary);
/*!
/*!
* Construct an item as a copy of \a item.
*/
Item(const Item &item);
Item(const Item &item);
/*!
/*!
* Destroys the item.
*/
virtual ~Item();
virtual ~Item();
/*!
/*!
* Copies the contents of \a item into this item.
*/
Item &operator=(const Item &item);
Item &operator=(const Item &item);
/*!
/*!
* Exchanges the content of this item by the content of \a item.
*/
void swap(Item &item);
void swap(Item &item);
/*!
/*!
* Returns the key.
*/
String key() const;
String key() const;
/*!
/*!
* Returns the binary value.
* If the item type is not \a Binary, always returns an empty ByteVector.
*/
ByteVector binaryData() const;
ByteVector binaryData() const;
/*!
/*!
* Set the binary value to \a value
* The item's type will also be set to \a Binary
*/
void setBinaryData(const ByteVector &value);
void setBinaryData(const ByteVector &value);
#ifndef DO_NOT_DOCUMENT
/* Remove in next binary incompatible release */
ByteVector value() const;
/* Remove in next binary incompatible release */
ByteVector value() const;
#endif
/*!
/*!
* Sets the key for the item to \a key.
*/
void setKey(const String &key);
void setKey(const String &key);
/*!
/*!
* Sets the text value of the item to \a value and clears any previous contents.
*
* \see toString()
*/
void setValue(const String &value);
void setValue(const String &value);
/*!
/*!
* Sets the text value of the item to the list of values in \a value and clears
* any previous contents.
*
* \see toStringList()
*/
void setValues(const StringList &values);
void setValues(const StringList &values);
/*!
/*!
* Appends \a value to create (or extend) the current list of text values.
*
* \see toString()
*/
void appendValue(const String &value);
void appendValue(const String &value);
/*!
/*!
* Appends \a values to extend the current list of text values.
*
* \see toStringList()
*/
void appendValues(const StringList &values);
void appendValues(const StringList &values);
/*!
/*!
* Returns the size of the full item.
*/
int size() const;
int size() const;
/*!
/*!
* Returns the value as a single string. In case of multiple strings,
* the first is returned. If the data type is not \a Text, always returns
* an empty String.
*/
String toString() const;
String toString() const;
#ifndef DO_NOT_DOCUMENT
/* Remove in next binary incompatible release */
StringList toStringList() const;
/* Remove in next binary incompatible release */
StringList toStringList() const;
#endif
/*!
/*!
* Returns the list of text values. If the data type is not \a Text, always
* returns an empty StringList.
*/
StringList values() const;
StringList values() const;
/*!
/*!
* Render the item to a ByteVector.
*/
ByteVector render() const;
ByteVector render() const;
/*!
/*!
* Parse the item from the ByteVector \a data.
*/
void parse(const ByteVector& data);
void parse(const ByteVector &data);
/*!
/*!
* Set the item to read-only.
*/
void setReadOnly(bool readOnly);
void setReadOnly(bool readOnly);
/*!
/*!
* Return true if the item is read-only.
*/
bool isReadOnly() const;
bool isReadOnly() const;
/*!
/*!
* Sets the type of the item to \a type.
*
* \see ItemTypes
*/
void setType(ItemTypes type);
void setType(ItemTypes type);
/*!
/*!
* Returns the type of the item.
*/
ItemTypes type() const;
ItemTypes type() const;
/*!
/*!
* Returns if the item has any real content.
*/
bool isEmpty() const;
bool isEmpty() const;
private:
class ItemPrivate;
ItemPrivate *d;
};
}
private:
class ItemPrivate;
ItemPrivate *d;
};
} // namespace APE
}
}
} // namespace TagLib
} // namespace Strawberry_TagLib
#endif

View File

@@ -38,17 +38,15 @@
using namespace Strawberry_TagLib::TagLib;
class APE::Properties::PropertiesPrivate
{
public:
PropertiesPrivate() :
length(0),
bitrate(0),
sampleRate(0),
channels(0),
version(0),
bitsPerSample(0),
sampleFrames(0) {}
class APE::Properties::PropertiesPrivate {
public:
PropertiesPrivate() : length(0),
bitrate(0),
sampleRate(0),
channels(0),
version(0),
bitsPerSample(0),
sampleFrames(0) {}
int length;
int bitrate;
@@ -63,67 +61,53 @@ public:
// public members
////////////////////////////////////////////////////////////////////////////////
APE::Properties::Properties(File *, ReadStyle style) :
AudioProperties(style),
d(new PropertiesPrivate())
{
APE::Properties::Properties(File *, ReadStyle style) : AudioProperties(style),
d(new PropertiesPrivate()) {
debug("APE::Properties::Properties() -- This constructor is no longer used.");
}
APE::Properties::Properties(File *file, long streamLength, ReadStyle style) :
AudioProperties(style),
d(new PropertiesPrivate())
{
APE::Properties::Properties(File *file, long streamLength, ReadStyle style) : AudioProperties(style),
d(new PropertiesPrivate()) {
read(file, streamLength);
}
APE::Properties::~Properties()
{
APE::Properties::~Properties() {
delete d;
}
int APE::Properties::length() const
{
int APE::Properties::length() const {
return lengthInSeconds();
}
int APE::Properties::lengthInSeconds() const
{
int APE::Properties::lengthInSeconds() const {
return d->length / 1000;
}
int APE::Properties::lengthInMilliseconds() const
{
int APE::Properties::lengthInMilliseconds() const {
return d->length;
}
int APE::Properties::bitrate() const
{
int APE::Properties::bitrate() const {
return d->bitrate;
}
int APE::Properties::sampleRate() const
{
int APE::Properties::sampleRate() const {
return d->sampleRate;
}
int APE::Properties::channels() const
{
int APE::Properties::channels() const {
return d->channels;
}
int APE::Properties::version() const
{
int APE::Properties::version() const {
return d->version;
}
int APE::Properties::bitsPerSample() const
{
int APE::Properties::bitsPerSample() const {
return d->bitsPerSample;
}
unsigned int APE::Properties::sampleFrames() const
{
unsigned int APE::Properties::sampleFrames() const {
return d->sampleFrames;
}
@@ -131,89 +115,84 @@ unsigned int APE::Properties::sampleFrames() const
// private members
////////////////////////////////////////////////////////////////////////////////
namespace
{
int headerVersion(const ByteVector &header)
{
if(header.size() < 6 || !header.startsWith("MAC "))
return -1;
namespace {
int headerVersion(const ByteVector &header) {
if (header.size() < 6 || !header.startsWith("MAC "))
return -1;
return header.toUShort(4, false);
}
return header.toUShort(4, false);
}
} // namespace
void APE::Properties::read(File *file, long streamLength)
{
void APE::Properties::read(File *file, long streamLength) {
// First, we assume that the file pointer is set at the first descriptor.
long offset = file->tell();
int version = headerVersion(file->readBlock(6));
// Next, we look for the descriptor.
if(version < 0) {
if (version < 0) {
offset = file->find("MAC ", offset);
file->seek(offset);
version = headerVersion(file->readBlock(6));
}
if(version < 0) {
if (version < 0) {
debug("APE::Properties::read() -- APE descriptor not found");
return;
}
d->version = version;
if(d->version >= 3980)
if (d->version >= 3980)
analyzeCurrent(file);
else
analyzeOld(file);
if(d->sampleFrames > 0 && d->sampleRate > 0) {
if (d->sampleFrames > 0 && d->sampleRate > 0) {
const double length = d->sampleFrames * 1000.0 / d->sampleRate;
d->length = static_cast<int>(length + 0.5);
d->length = static_cast<int>(length + 0.5);
d->bitrate = static_cast<int>(streamLength * 8.0 / length + 0.5);
}
}
void APE::Properties::analyzeCurrent(File *file)
{
void APE::Properties::analyzeCurrent(File *file) {
// Read the descriptor
file->seek(2, File::Current);
const ByteVector descriptor = file->readBlock(44);
if(descriptor.size() < 44) {
if (descriptor.size() < 44) {
debug("APE::Properties::analyzeCurrent() -- descriptor is too short.");
return;
}
const unsigned int descriptorBytes = descriptor.toUInt(0, false);
if((descriptorBytes - 52) > 0)
if ((descriptorBytes - 52) > 0)
file->seek(descriptorBytes - 52, File::Current);
// Read the header
const ByteVector header = file->readBlock(24);
if(header.size() < 24) {
if (header.size() < 24) {
debug("APE::Properties::analyzeCurrent() -- MAC header is too short.");
return;
}
// Get the APE info
d->channels = header.toShort(18, false);
d->sampleRate = header.toUInt(20, false);
d->channels = header.toShort(18, false);
d->sampleRate = header.toUInt(20, false);
d->bitsPerSample = header.toShort(16, false);
const unsigned int totalFrames = header.toUInt(12, false);
if(totalFrames == 0)
if (totalFrames == 0)
return;
const unsigned int blocksPerFrame = header.toUInt(4, false);
const unsigned int blocksPerFrame = header.toUInt(4, false);
const unsigned int finalFrameBlocks = header.toUInt(8, false);
d->sampleFrames = (totalFrames - 1) * blocksPerFrame + finalFrameBlocks;
}
void APE::Properties::analyzeOld(File *file)
{
void APE::Properties::analyzeOld(File *file) {
const ByteVector header = file->readBlock(26);
if(header.size() < 26) {
if (header.size() < 26) {
debug("APE::Properties::analyzeOld() -- MAC header is too short.");
return;
}
@@ -221,20 +200,20 @@ void APE::Properties::analyzeOld(File *file)
const unsigned int totalFrames = header.toUInt(18, false);
// Fail on 0 length APE files (catches non-finalized APE files)
if(totalFrames == 0)
if (totalFrames == 0)
return;
const short compressionLevel = header.toShort(0, false);
unsigned int blocksPerFrame;
if(d->version >= 3950)
if (d->version >= 3950)
blocksPerFrame = 73728 * 4;
else if(d->version >= 3900 || (d->version >= 3800 && compressionLevel == 4000))
else if (d->version >= 3900 || (d->version >= 3800 && compressionLevel == 4000))
blocksPerFrame = 73728;
else
blocksPerFrame = 9216;
// Get the APE info
d->channels = header.toShort(4, false);
d->channels = header.toShort(4, false);
d->sampleRate = header.toUInt(6, false);
const unsigned int finalFrameBlocks = header.toUInt(22, false);
@@ -243,7 +222,7 @@ void APE::Properties::analyzeOld(File *file)
// Get the bit depth from the RIFF-fmt chunk.
file->seek(16, File::Current);
const ByteVector fmt = file->readBlock(28);
if(fmt.size() < 28 || !fmt.startsWith("WAVEfmt ")) {
if (fmt.size() < 28 || !fmt.startsWith("WAVEfmt ")) {
debug("APE::Properties::analyzeOld() -- fmt header is too short.");
return;
}

View File

@@ -36,40 +36,39 @@
namespace Strawberry_TagLib {
namespace TagLib {
namespace APE {
namespace APE {
class File;
class File;
//! An implementation of audio property reading for APE
//! An implementation of audio property reading for APE
/*!
/*!
* This reads the data from an APE stream found in the AudioProperties
* API.
*/
class TAGLIB_EXPORT Properties : public AudioProperties
{
public:
/*!
class TAGLIB_EXPORT Properties : public AudioProperties {
public:
/*!
* Create an instance of APE::Properties with the data read from the
* APE::File \a file.
*
* \deprecated
*/
TAGLIB_DEPRECATED Properties(File *file, ReadStyle style = Average);
TAGLIB_DEPRECATED Properties(File *file, ReadStyle style = Average);
/*!
/*!
* Create an instance of APE::Properties with the data read from the
* APE::File \a file.
*/
Properties(File *file, long streamLength, ReadStyle style = Average);
Properties(File *file, long streamLength, ReadStyle style = Average);
/*!
/*!
* Destroys this APE::Properties instance.
*/
virtual ~Properties();
virtual ~Properties();
/*!
/*!
* Returns the length of the file in seconds. The length is rounded down to
* the nearest whole second.
*
@@ -77,69 +76,69 @@ namespace TagLib {
*
* \deprecated
*/
TAGLIB_DEPRECATED virtual int length() const;
TAGLIB_DEPRECATED virtual int length() const;
/*!
/*!
* Returns the length of the file in seconds. The length is rounded down to
* the nearest whole second.
*
* \see lengthInMilliseconds()
*/
// BIC: make virtual
int lengthInSeconds() const;
// BIC: make virtual
int lengthInSeconds() const;
/*!
/*!
* Returns the length of the file in milliseconds.
*
* \see lengthInSeconds()
*/
// BIC: make virtual
int lengthInMilliseconds() const;
// BIC: make virtual
int lengthInMilliseconds() const;
/*!
/*!
* Returns the average bit rate of the file in kb/s.
*/
virtual int bitrate() const;
virtual int bitrate() const;
/*!
/*!
* Returns the sample rate in Hz.
*/
virtual int sampleRate() const;
virtual int sampleRate() const;
/*!
/*!
* Returns the number of audio channels.
*/
virtual int channels() const;
virtual int channels() const;
/*!
/*!
* Returns the number of bits per audio sample.
*/
int bitsPerSample() const;
int bitsPerSample() const;
/*!
/*!
* Returns the total number of audio samples in file.
*/
unsigned int sampleFrames() const;
unsigned int sampleFrames() const;
/*!
/*!
* Returns APE version.
*/
int version() const;
int version() const;
private:
Properties(const Properties &);
Properties &operator=(const Properties &);
private:
Properties(const Properties &);
Properties &operator=(const Properties &);
void read(File *file, long streamLength);
void read(File *file, long streamLength);
void analyzeCurrent(File *file);
void analyzeOld(File *file);
void analyzeCurrent(File *file);
void analyzeOld(File *file);
class PropertiesPrivate;
PropertiesPrivate *d;
};
}
}
}
class PropertiesPrivate;
PropertiesPrivate *d;
};
} // namespace APE
} // namespace TagLib
} // namespace Strawberry_TagLib
#endif

View File

@@ -28,7 +28,7 @@
// it considers specializations with and without class types
// to be different; this define forces Map to use only the
// specialization with the class keyword.
#define WANT_CLASS_INSTANTIATION_OF_MAP (1)
# define WANT_CLASS_INSTANTIATION_OF_MAP (1)
#endif
#include <tfile.h>
@@ -45,39 +45,35 @@
using namespace Strawberry_TagLib::TagLib;
using namespace APE;
namespace
{
const unsigned int MinKeyLength = 2;
const unsigned int MaxKeyLength = 255;
namespace {
const unsigned int MinKeyLength = 2;
const unsigned int MaxKeyLength = 255;
bool isKeyValid(const ByteVector &key)
{
const char *invalidKeys[] = { "ID3", "TAG", "OGGS", "MP+", 0 };
bool isKeyValid(const ByteVector &key) {
// only allow printable ASCII including space (32..126)
const char *invalidKeys[] = { "ID3", "TAG", "OGGS", "MP+", 0 };
for(ByteVector::ConstIterator it = key.begin(); it != key.end(); ++it) {
const int c = static_cast<unsigned char>(*it);
if(c < 32 || c > 126)
return false;
}
// only allow printable ASCII including space (32..126)
const String upperKey = String(key).upper();
for(size_t i = 0; invalidKeys[i] != 0; ++i) {
if(upperKey == invalidKeys[i])
return false;
}
return true;
for (ByteVector::ConstIterator it = key.begin(); it != key.end(); ++it) {
const int c = static_cast<unsigned char>(*it);
if (c < 32 || c > 126)
return false;
}
}
class APE::Tag::TagPrivate
{
public:
TagPrivate() :
file(0),
footerLocation(0) {}
const String upperKey = String(key).upper();
for (size_t i = 0; invalidKeys[i] != 0; ++i) {
if (upperKey == invalidKeys[i])
return false;
}
return true;
}
} // namespace
class APE::Tag::TagPrivate {
public:
TagPrivate() : file(0), footerLocation(0) {}
File *file;
long footerLocation;
@@ -90,150 +86,140 @@ public:
// public methods
////////////////////////////////////////////////////////////////////////////////
APE::Tag::Tag() :
Strawberry_TagLib::TagLib::Tag(),
d(new TagPrivate())
{
}
APE::Tag::Tag() : Strawberry_TagLib::TagLib::Tag(), d(new TagPrivate()) {}
APE::Tag::Tag(Strawberry_TagLib::TagLib::File *file, long footerLocation) : Strawberry_TagLib::TagLib::Tag(), d(new TagPrivate()) {
APE::Tag::Tag(Strawberry_TagLib::TagLib::File *file, long footerLocation) :
Strawberry_TagLib::TagLib::Tag(),
d(new TagPrivate())
{
d->file = file;
d->footerLocation = footerLocation;
read();
}
APE::Tag::~Tag()
{
APE::Tag::~Tag() {
delete d;
}
ByteVector APE::Tag::fileIdentifier()
{
ByteVector APE::Tag::fileIdentifier() {
return ByteVector::fromCString("APETAGEX");
}
String APE::Tag::title() const
{
if(d->itemListMap["TITLE"].isEmpty())
String APE::Tag::title() const {
if (d->itemListMap["TITLE"].isEmpty())
return String();
return d->itemListMap["TITLE"].values().toString();
}
String APE::Tag::artist() const
{
if(d->itemListMap["ARTIST"].isEmpty())
String APE::Tag::artist() const {
if (d->itemListMap["ARTIST"].isEmpty())
return String();
return d->itemListMap["ARTIST"].values().toString();
}
String APE::Tag::album() const
{
if(d->itemListMap["ALBUM"].isEmpty())
String APE::Tag::album() const {
if (d->itemListMap["ALBUM"].isEmpty())
return String();
return d->itemListMap["ALBUM"].values().toString();
}
String APE::Tag::comment() const
{
if(d->itemListMap["COMMENT"].isEmpty())
String APE::Tag::comment() const {
if (d->itemListMap["COMMENT"].isEmpty())
return String();
return d->itemListMap["COMMENT"].values().toString();
}
String APE::Tag::genre() const
{
if(d->itemListMap["GENRE"].isEmpty())
String APE::Tag::genre() const {
if (d->itemListMap["GENRE"].isEmpty())
return String();
return d->itemListMap["GENRE"].values().toString();
}
unsigned int APE::Tag::year() const
{
if(d->itemListMap["YEAR"].isEmpty())
unsigned int APE::Tag::year() const {
if (d->itemListMap["YEAR"].isEmpty())
return 0;
return d->itemListMap["YEAR"].toString().toInt();
}
unsigned int APE::Tag::track() const
{
if(d->itemListMap["TRACK"].isEmpty())
unsigned int APE::Tag::track() const {
if (d->itemListMap["TRACK"].isEmpty())
return 0;
return d->itemListMap["TRACK"].toString().toInt();
}
void APE::Tag::setTitle(const String &s)
{
void APE::Tag::setTitle(const String &s) {
addValue("TITLE", s, true);
}
void APE::Tag::setArtist(const String &s)
{
void APE::Tag::setArtist(const String &s) {
addValue("ARTIST", s, true);
}
void APE::Tag::setAlbum(const String &s)
{
void APE::Tag::setAlbum(const String &s) {
addValue("ALBUM", s, true);
}
void APE::Tag::setComment(const String &s)
{
void APE::Tag::setComment(const String &s) {
addValue("COMMENT", s, true);
}
void APE::Tag::setGenre(const String &s)
{
void APE::Tag::setGenre(const String &s) {
addValue("GENRE", s, true);
}
void APE::Tag::setYear(unsigned int i)
{
if(i == 0)
void APE::Tag::setYear(unsigned int i) {
if (i == 0)
removeItem("YEAR");
else
addValue("YEAR", String::number(i), true);
}
void APE::Tag::setTrack(unsigned int i)
{
if(i == 0)
void APE::Tag::setTrack(unsigned int i) {
if (i == 0)
removeItem("TRACK");
else
addValue("TRACK", String::number(i), true);
}
namespace
{
// conversions of tag keys between what we use in PropertyMap and what's usual
// for APE tags
// usual, APE
const char *keyConversions[][2] = {{"TRACKNUMBER", "TRACK" },
{"DATE", "YEAR" },
{"ALBUMARTIST", "ALBUM ARTIST"},
{"DISCNUMBER", "DISC" },
{"REMIXER", "MIXARTIST" }};
const size_t keyConversionsSize = sizeof(keyConversions) / sizeof(keyConversions[0]);
}
namespace {
// conversions of tag keys between what we use in PropertyMap and what's usual
// for APE tags
// usual, APE
const char *keyConversions[][2] = { { "TRACKNUMBER", "TRACK" },
{ "DATE", "YEAR" },
{ "ALBUMARTIST", "ALBUM ARTIST" },
{ "DISCNUMBER", "DISC" },
{ "REMIXER", "MIXARTIST" } };
const size_t keyConversionsSize = sizeof(keyConversions) / sizeof(keyConversions[0]);
} // namespace
PropertyMap APE::Tag::properties() const
{
PropertyMap APE::Tag::properties() const {
PropertyMap properties;
ItemListMap::ConstIterator it = itemListMap().begin();
for(; it != itemListMap().end(); ++it) {
for (; it != itemListMap().end(); ++it) {
String tagName = it->first.upper();
// if the item is Binary or Locator, or if the key is an invalid string,
// add to unsupportedData
if(it->second.type() != Item::Text || tagName.isEmpty()) {
if (it->second.type() != Item::Text || tagName.isEmpty()) {
properties.unsupportedData().append(it->first);
}
else {
// Some tags need to be handled specially
for(size_t i = 0; i < keyConversionsSize; ++i) {
if(tagName == keyConversions[i][1])
for (size_t i = 0; i < keyConversionsSize; ++i) {
if (tagName == keyConversions[i][1])
tagName = keyConversions[i][0];
}
properties[tagName].append(it->second.toStringList());
@@ -242,20 +228,18 @@ PropertyMap APE::Tag::properties() const
return properties;
}
void APE::Tag::removeUnsupportedProperties(const StringList &properties)
{
void APE::Tag::removeUnsupportedProperties(const StringList &properties) {
StringList::ConstIterator it = properties.begin();
for(; it != properties.end(); ++it)
for (; it != properties.end(); ++it)
removeItem(*it);
}
PropertyMap APE::Tag::setProperties(const PropertyMap &origProps)
{
PropertyMap properties(origProps); // make a local copy that can be modified
PropertyMap APE::Tag::setProperties(const PropertyMap &origProps) {
PropertyMap properties(origProps); // make a local copy that can be modified
// see comment in properties()
for(size_t i = 0; i < keyConversionsSize; ++i)
if(properties.contains(keyConversions[i][0])) {
for (size_t i = 0; i < keyConversionsSize; ++i)
if (properties.contains(keyConversions[i][0])) {
properties.insert(keyConversions[i][1], properties[keyConversions[i][0]]);
properties.erase(keyConversions[i][0]);
}
@@ -263,31 +247,31 @@ PropertyMap APE::Tag::setProperties(const PropertyMap &origProps)
// first check if tags need to be removed completely
StringList toRemove;
ItemListMap::ConstIterator remIt = itemListMap().begin();
for(; remIt != itemListMap().end(); ++remIt) {
for (; remIt != itemListMap().end(); ++remIt) {
String key = remIt->first.upper();
// only remove if a) key is valid, b) type is text, c) key not contained in new properties
if(!key.isEmpty() && remIt->second.type() == APE::Item::Text && !properties.contains(key))
if (!key.isEmpty() && remIt->second.type() == APE::Item::Text && !properties.contains(key))
toRemove.append(remIt->first);
}
for(StringList::ConstIterator removeIt = toRemove.begin(); removeIt != toRemove.end(); removeIt++)
for (StringList::ConstIterator removeIt = toRemove.begin(); removeIt != toRemove.end(); removeIt++)
removeItem(*removeIt);
// now sync in the "forward direction"
PropertyMap::ConstIterator it = properties.begin();
PropertyMap invalid;
for(; it != properties.end(); ++it) {
for (; it != properties.end(); ++it) {
const String &tagName = it->first;
if(!checkKey(tagName))
if (!checkKey(tagName))
invalid.insert(it->first, it->second);
else if(!(itemListMap().contains(tagName)) || !(itemListMap()[tagName].values() == it->second)) {
if(it->second.isEmpty())
else if (!(itemListMap().contains(tagName)) || !(itemListMap()[tagName].values() == it->second)) {
if (it->second.isEmpty())
removeItem(tagName);
else {
StringList::ConstIterator valueIt = it->second.begin();
addValue(tagName, *valueIt, true);
++valueIt;
for(; valueIt != it->second.end(); ++valueIt)
for (; valueIt != it->second.end(); ++valueIt)
addValue(tagName, *valueIt, false);
}
}
@@ -295,35 +279,30 @@ PropertyMap APE::Tag::setProperties(const PropertyMap &origProps)
return invalid;
}
bool APE::Tag::checkKey(const String &key)
{
if(key.size() < MinKeyLength || key.size() > MaxKeyLength)
bool APE::Tag::checkKey(const String &key) {
if (key.size() < MinKeyLength || key.size() > MaxKeyLength)
return false;
return isKeyValid(key.data(String::UTF8));
}
APE::Footer *APE::Tag::footer() const
{
APE::Footer *APE::Tag::footer() const {
return &d->footer;
}
const APE::ItemListMap& APE::Tag::itemListMap() const
{
const APE::ItemListMap &APE::Tag::itemListMap() const {
return d->itemListMap;
}
void APE::Tag::removeItem(const String &key)
{
void APE::Tag::removeItem(const String &key) {
d->itemListMap.erase(key.upper());
}
void APE::Tag::addValue(const String &key, const String &value, bool replace)
{
if(replace)
void APE::Tag::addValue(const String &key, const String &value, bool replace) {
if (replace)
removeItem(key);
if(value.isEmpty())
if (value.isEmpty())
return;
// Text items may contain more than one value.
@@ -331,25 +310,23 @@ void APE::Tag::addValue(const String &key, const String &value, bool replace)
ItemListMap::Iterator it = d->itemListMap.find(key.upper());
if(it != d->itemListMap.end() && it->second.type() == Item::Text)
if (it != d->itemListMap.end() && it->second.type() == Item::Text)
it->second.appendValue(value);
else
setItem(key, Item(key, value));
}
void APE::Tag::setData(const String &key, const ByteVector &value)
{
void APE::Tag::setData(const String &key, const ByteVector &value) {
removeItem(key);
if(value.isEmpty())
if (value.isEmpty())
return;
setItem(key, Item(key, value, true));
}
void APE::Tag::setItem(const String &key, const Item &item)
{
if(!checkKey(key)) {
void APE::Tag::setItem(const String &key, const Item &item) {
if (!checkKey(key)) {
debug("APE::Tag::setItem() - Couldn't set an item due to an invalid key.");
return;
}
@@ -357,8 +334,7 @@ void APE::Tag::setItem(const String &key, const Item &item)
d->itemListMap[key.upper()] = item;
}
bool APE::Tag::isEmpty() const
{
bool APE::Tag::isEmpty() const {
return d->itemListMap.isEmpty();
}
@@ -366,15 +342,14 @@ bool APE::Tag::isEmpty() const
// protected methods
////////////////////////////////////////////////////////////////////////////////
void APE::Tag::read()
{
if(d->file && d->file->isValid()) {
void APE::Tag::read() {
if (d->file && d->file->isValid()) {
d->file->seek(d->footerLocation);
d->footer.setData(d->file->readBlock(Footer::size()));
if(d->footer.tagSize() <= Footer::size() ||
d->footer.tagSize() > static_cast<unsigned long>(d->file->length()))
if (d->footer.tagSize() <= Footer::size() ||
d->footer.tagSize() > static_cast<unsigned long>(d->file->length()))
return;
d->file->seek(d->footerLocation + Footer::size() - d->footer.tagSize());
@@ -382,12 +357,11 @@ void APE::Tag::read()
}
}
ByteVector APE::Tag::render() const
{
ByteVector APE::Tag::render() const {
ByteVector data;
unsigned int itemCount = 0;
for(ItemListMap::ConstIterator it = d->itemListMap.begin(); it != d->itemListMap.end(); ++it) {
for (ItemListMap::ConstIterator it = d->itemListMap.begin(); it != d->itemListMap.end(); ++it) {
data.append(it->second.render());
itemCount++;
}
@@ -399,19 +373,18 @@ ByteVector APE::Tag::render() const
return d->footer.renderHeader() + data + d->footer.renderFooter();
}
void APE::Tag::parse(const ByteVector &data)
{
void APE::Tag::parse(const ByteVector &data) {
// 11 bytes is the minimum size for an APE item
if(data.size() < 11)
if (data.size() < 11)
return;
unsigned int pos = 0;
for(unsigned int i = 0; i < d->footer.itemCount() && pos <= data.size() - 11; i++) {
for (unsigned int i = 0; i < d->footer.itemCount() && pos <= data.size() - 11; i++) {
const int nullPos = data.find('\0', pos + 8);
if(nullPos < 0) {
if (nullPos < 0) {
debug("APE::Tag::parse() - Couldn't find a key/value separator. Stopped parsing.");
return;
}
@@ -419,10 +392,7 @@ void APE::Tag::parse(const ByteVector &data)
const unsigned int keyLength = nullPos - pos - 8;
const unsigned int valLegnth = data.toUInt(pos, false);
if(keyLength >= MinKeyLength
&& keyLength <= MaxKeyLength
&& isKeyValid(data.mid(pos + 8, keyLength)))
{
if (keyLength >= MinKeyLength && keyLength <= MaxKeyLength && isKeyValid(data.mid(pos + 8, keyLength))) {
APE::Item item;
item.parse(data.mid(pos));

View File

@@ -37,74 +37,73 @@
namespace Strawberry_TagLib {
namespace TagLib {
class File;
class File;
//! An implementation of the APE tagging format
//! An implementation of the APE tagging format
namespace APE {
namespace APE {
class Footer;
class Footer;
/*!
/*!
* A mapping between a list of item names, or keys, and the associated item.
*
* \see APE::Tag::itemListMap()
*/
typedef Map<const String, Item> ItemListMap;
typedef Map<const String, Item> ItemListMap;
//! An APE tag implementation
//! An APE tag implementation
class TAGLIB_EXPORT Tag : public Strawberry_TagLib::TagLib::Tag
{
public:
/*!
class TAGLIB_EXPORT Tag : public Strawberry_TagLib::TagLib::Tag {
public:
/*!
* Create an APE tag with default values.
*/
Tag();
Tag();
/*!
/*!
* Create an APE tag and parse the data in \a file with APE footer at
* \a tagOffset.
*/
Tag(Strawberry_TagLib::TagLib::File *file, long footerLocation);
Tag(Strawberry_TagLib::TagLib::File *file, long footerLocation);
/*!
/*!
* Destroys this Tag instance.
*/
virtual ~Tag();
virtual ~Tag();
/*!
/*!
* Renders the in memory values to a ByteVector suitable for writing to
* the file.
*/
ByteVector render() const;
ByteVector render() const;
/*!
/*!
* Returns the string "APETAGEX" suitable for usage in locating the tag in a
* file.
*/
static ByteVector fileIdentifier();
static ByteVector fileIdentifier();
// Reimplementations.
// Reimplementations.
virtual String title() const;
virtual String artist() const;
virtual String album() const;
virtual String comment() const;
virtual String genre() const;
virtual unsigned int year() const;
virtual unsigned int track() const;
virtual String title() const;
virtual String artist() const;
virtual String album() const;
virtual String comment() const;
virtual String genre() const;
virtual unsigned int year() const;
virtual unsigned int track() const;
virtual void setTitle(const String &s);
virtual void setArtist(const String &s);
virtual void setAlbum(const String &s);
virtual void setComment(const String &s);
virtual void setGenre(const String &s);
virtual void setYear(unsigned int i);
virtual void setTrack(unsigned int i);
virtual void setTitle(const String &s);
virtual void setArtist(const String &s);
virtual void setAlbum(const String &s);
virtual void setComment(const String &s);
virtual void setGenre(const String &s);
virtual void setYear(unsigned int i);
virtual void setTrack(unsigned int i);
/*!
/*!
* Implements the unified tag dictionary interface -- export function.
* APE tags are perfectly compatible with the dictionary interface because they
* support both arbitrary tag names and multiple values. Currently only
@@ -118,29 +117,29 @@ namespace TagLib {
* TRACK to TRACKNUMBER, YEAR to DATE, and ALBUM ARTIST to ALBUMARTIST, respectively,
* in order to be compliant with the names used in other formats.
*/
PropertyMap properties() const;
PropertyMap properties() const;
void removeUnsupportedProperties(const StringList &properties);
void removeUnsupportedProperties(const StringList &properties);
/*!
/*!
* Implements the unified tag dictionary interface -- import function. The same
* comments as for the export function apply; additionally note that the APE tag
* specification requires keys to have between 2 and 16 printable ASCII characters
* with the exception of the fixed strings "ID3", "TAG", "OGGS", and "MP+".
*/
PropertyMap setProperties(const PropertyMap &);
PropertyMap setProperties(const PropertyMap &);
/*!
/*!
* Check if the given String is a valid APE tag key.
*/
static bool checkKey(const String&);
static bool checkKey(const String &);
/*!
/*!
* Returns a pointer to the tag's footer.
*/
Footer *footer() const;
Footer *footer() const;
/*!
/*!
* Returns a reference to the item list map. This is an ItemListMap of
* all of the items in the tag.
*
@@ -152,59 +151,58 @@ namespace TagLib {
* \warning You should not modify this data structure directly, instead
* use setItem() and removeItem().
*/
const ItemListMap &itemListMap() const;
const ItemListMap &itemListMap() const;
/*!
/*!
* Removes the \a key item from the tag
*/
void removeItem(const String &key);
void removeItem(const String &key);
/*!
/*!
* Adds to the text item specified by \a key the data \a value. If \a replace
* is true, then all of the other values on the same key will be removed
* first. If a binary item exists for \a key it will be removed first.
*/
void addValue(const String &key, const String &value, bool replace = true);
void addValue(const String &key, const String &value, bool replace = true);
/*!
/*!
* Set the binary data for the key specified by \a item to \a value
* This will convert the item to type \a Binary if it isn't already and
* all of the other values on the same key will be removed.
*/
void setData(const String &key, const ByteVector &value);
void setData(const String &key, const ByteVector &value);
/*!
/*!
* Sets the \a key item to the value of \a item. If an item with the \a key is already
* present, it will be replaced.
*/
void setItem(const String &key, const Item &item);
void setItem(const String &key, const Item &item);
/*!
/*!
* Returns true if the tag does not contain any data.
*/
bool isEmpty() const;
bool isEmpty() const;
protected:
/*!
protected:
/*!
* Reads from the file specified in the constructor.
*/
void read();
void read();
/*!
/*!
* Parses the body of the tag in \a data.
*/
void parse(const ByteVector &data);
void parse(const ByteVector &data);
private:
Tag(const Tag &);
Tag &operator=(const Tag &);
private:
Tag(const Tag &);
Tag &operator=(const Tag &);
class TagPrivate;
TagPrivate *d;
};
}
}
}
class TagPrivate;
TagPrivate *d;
};
} // namespace APE
} // namespace TagLib
} // namespace Strawberry_TagLib
#endif