From 1b978e76cfd1a08c6083865c91609719f6250cba Mon Sep 17 00:00:00 2001 From: Bert Date: Mon, 7 Feb 2011 23:22:33 +0100 Subject: [PATCH] Basic functionality --- main.cpp | 21 ++++++ oggfile.cpp | 205 ++++++++++++++++++++++++++++++++++++++++++++++++++++ oggfile.h | 12 +-- oggted.h | 2 +- options.h | 2 +- 5 files changed, 234 insertions(+), 8 deletions(-) create mode 100644 oggfile.cpp diff --git a/main.cpp b/main.cpp index 9c6f22e..a27259d 100644 --- a/main.cpp +++ b/main.cpp @@ -24,11 +24,15 @@ #include "oggted.h" #include "fileio.h" +#include "oggfile.h" #include "options.h" const char *command; int main(int argc, char **argv) { + uint fileIdx; + bool firstOutput = true; + command = FileIO::_basename(argv[0]); if (strcmp(command, ".") == 0) command = PROGNAME; @@ -38,6 +42,23 @@ int main(int argc, char **argv) { exit(1); } + for (fileIdx = 0; fileIdx < Options::fileCount; ++fileIdx) { + const char *filename = Options::filenames[fileIdx]; + OggFile file(filename); + + if (Options::listTag) { + if (Options::fileCount > 1) { + if (!firstOutput) + cout << endl; + else + firstOutput = false; + cout << filename << ":" << endl; + } + + file.listTag(); + } + } + return 0; } diff --git a/oggfile.cpp b/oggfile.cpp new file mode 100644 index 0000000..1873676 --- /dev/null +++ b/oggfile.cpp @@ -0,0 +1,205 @@ +/* oggted: oggfile.cpp + * Copyright (c) 2011 Bert Muennich + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include +#include + +#include + +#include "oggfile.h" + +OggFile::OggFile(const char *filename) : file(filename), tag(NULL) { + if (file.isValid()) + tag = file.tag(); +} + +void OggFile::apply(const GenericInfo &info) { + if (!file.isValid() || file.readOnly() || tag == NULL) + return; + + switch(info.id) { + case 'a': + tag->setArtist(*info.value); + break; + case 'A': + tag->setAlbum(*info.value); + break; + case 't': + tag->setTitle(*info.value); + break; + case 'c': + tag->setComment(*info.value); + break; + case 'g': + tag->setGenre(*info.value); + break; + case 'T': + tag->setTrack(info.value->toInt()); + break; + case 'y': + tag->setYear(info.value->toInt()); + break; + } +} + +void OggFile::apply(const FieldInfo &info) { + if (!file.isValid() || file.readOnly() || tag == NULL) + return; + +} + +void OggFile::apply(const MatchInfo &info) { + GenericInfo ginfo; + FieldInfo finfo; + String name, value; + + if (!file.isValid() || file.readOnly() || tag == NULL) + return; + if (info.id == 0 || info.text.length() == 0) + return; + + switch (info.id) { + case 'a': + case 'A': + case 't': + case 'c': + case 'g': + case 'T': + case 'y': + ginfo.id = info.id; + value = info.text; + ginfo.value = &value; + apply(ginfo); + break; + case 'd': + name = "DISCNUMBER"; + value = info.text; + finfo.name = &name; + finfo.value = &value; + apply(finfo); + break; + } +} + +void OggFile::fill(MatchInfo &info) { + string &text = info.text; + ostringstream tmp; + uint track, year; + Ogg::FieldListMap map; + + tmp.fill('0'); + + if (!file.isValid()) + return; + if (info.id == 0) + return; + + switch (info.id) { + case 'a': + text = tag->artist().toCString(USE_UNICODE); + if (text.empty()) + text = "Unknown Artist"; + break; + case 'A': + text = tag->album().toCString(USE_UNICODE); + if (text.empty()) + text = "Unknown Album"; + break; + case 't': + text = tag->title().toCString(USE_UNICODE); + if (text.empty()) + text = "Unknown Title"; + break; + case 'g': + text = tag->genre().toCString(USE_UNICODE); + break; + case 'y': + year = tag->year(); + if (year) { + tmp << year; + text = tmp.str(); + } + break; + case 'T': + tmp.width(2); + track = tag->track(); + if (track) + tmp << track; + else + tmp << 0; + text = tmp.str(); + break; + case 'd': + map = tag->fieldListMap(); + if (map.contains("DISCNUMBER")) + text = map["DISCNUMBER"].front().toCString(USE_UNICODE); + if (text.empty()) + text = "0"; + break; + } +} + +void OggFile::removeFields(const char *id) { + if (id == NULL) + return; + if (!file.isValid() || file.readOnly() || tag == NULL) + return; + + tag->removeField(id); +} + +bool OggFile::save() { + if (!file.isValid() || file.readOnly() || tag == NULL) + return false; + + return file.save(); +} + +void OggFile::showInfo() const { + Vorbis::Properties *properties; + int length; + + if (!file.isValid()) + return; + + if ((properties = file.audioProperties()) == NULL) + return; + + length = properties->length(); + cout << "Ogg Vorbis version " << properties->vorbisVersion() << endl; + printf("bitrate: %d kBit/s, sample rate: %d Hz, length: %02d:%02d:%02d\n", + properties->bitrate(), properties->sampleRate(), + length / 3600, length / 60, length % 60); +} + +void OggFile::listTag() const { + Ogg::FieldListMap map; + Ogg::FieldListMap::Iterator eachKey; + StringList values; + StringList::ConstIterator eachVal; + + if (!file.isValid() || tag == NULL) + return; + + map = tag->fieldListMap(); + for (eachKey = map.begin(); eachKey != map.end(); ++eachKey) { + values = eachKey->second; + for (eachVal = values.begin(); eachVal != values.end(); ++eachVal) + cout << eachKey->first << "=" << *eachVal << endl; + } +} diff --git a/oggfile.h b/oggfile.h index 2936c8c..f3a46eb 100644 --- a/oggfile.h +++ b/oggfile.h @@ -20,20 +20,23 @@ #ifndef __OGGFILE_H__ #define __OGGFILE_H__ +#include +#include + #include "oggted.h" +#include "pattern.h" #include "types.h" class OggFile { public: explicit OggFile(const char*); - ~OggFile(); bool isValid() const { return file.isValid(); } bool isReadOnly() const { return file.readOnly(); } const char* filename() const { return file.name(); } - void apply(GenericInfo&); - void apply(FieldInfo&); + void apply(const GenericInfo&); + void apply(const FieldInfo&); void apply(const MatchInfo&); void fill(MatchInfo&); void removeFields(const char*); @@ -42,9 +45,6 @@ class OggFile { void showInfo() const; void listTag() const; - int filenameToTag(const char*); - int organize(const char*, bool = false, bool = false, struct timeval* = NULL); - private: Ogg::Vorbis::File file; Ogg::XiphComment *tag; diff --git a/oggted.h b/oggted.h index 5b89cf5..3d1ff39 100644 --- a/oggted.h +++ b/oggted.h @@ -25,7 +25,7 @@ #include "config.h" #define PROGNAME "oggted" -#define VERSION "git-20110206" +#define VERSION "git-20110207" using namespace std; using namespace TagLib; diff --git a/options.h b/options.h index a30a559..229ba10 100644 --- a/options.h +++ b/options.h @@ -38,7 +38,7 @@ class Options { public: static bool writeFile; static bool showInfo; - static bool listComment; + static bool listTag; static bool forceOverwrite; static bool preserveTimes; static bool moveFiles;