Skip to content
This repository has been archived by the owner on Sep 23, 2021. It is now read-only.

Commit

Permalink
Basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Bert committed Feb 7, 2011
1 parent 85814b4 commit 1b978e7
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 8 deletions.
21 changes: 21 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

205 changes: 205 additions & 0 deletions oggfile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/* oggted: oggfile.cpp
* Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
*
* 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 <sstream>
#include <cstdio>

#include <taglib/vorbisproperties.h>

#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;
}
}
12 changes: 6 additions & 6 deletions oggfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,23 @@
#ifndef __OGGFILE_H__
#define __OGGFILE_H__

#include <taglib/vorbisfile.h>
#include <taglib/xiphcomment.h>

#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*);
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion oggted.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "config.h"

#define PROGNAME "oggted"
#define VERSION "git-20110206"
#define VERSION "git-20110207"

using namespace std;
using namespace TagLib;
Expand Down
2 changes: 1 addition & 1 deletion options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1b978e7

Please sign in to comment.