libsaria: Save library

- Change WriteTask() to take an extra void pointer argument
- Pass library path pointer through WriteTask
- Store tracks to file named after library id.
- Remove newline from tags

Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
Bryan Schumaker 2012-03-24 09:44:08 -04:00
parent ab687a49bc
commit d0a1b9fdf8
12 changed files with 105 additions and 31 deletions

View File

@ -18,9 +18,10 @@ namespace libsaria
{
void init(string);
void mkdir(string);
void mkdir();
void save(string, void (*)(ofstream &), DataState *);
void save(string, void (*)(ofstream &, void *), DataState *, void *);
void read_now(string, void (*)(ifstream &));
} /* Namespace: app */

View File

@ -17,6 +17,7 @@ namespace libsaria
struct Path {
bool visible;
DataState data_state;
unsigned int id;
unsigned int next_track;
string path;

View File

@ -19,10 +19,11 @@ class WriteTask : public IdleTask
{
private:
string filepath;
void (*func)(ofstream &);
void (*func)(ofstream &, void *);
DataState *state;
void *data;
public:
WriteTask(string, void (*)(ofstream &), DataState *);
WriteTask(string, void (*)(ofstream &, void *), DataState *, void *);
~WriteTask();
void run_task();
};

View File

@ -40,6 +40,7 @@ namespace libsaria
public:
Track(string, library::Path *);
~Track();
void save(ofstream &);
/* Track();
Track(string, sid_t);
Track(InFile &);

View File

@ -64,6 +64,12 @@ namespace libsaria
make_dir(appdir.c_str());
}
void app::mkdir(string dir)
{
string d = appdir + "/" + dir;
make_dir(d.c_str());
}
void app::init(string name)
{
string home = getenv("HOME");
@ -74,9 +80,10 @@ namespace libsaria
mkdir();
}
void app::save(string file, void (*func)(ofstream &), DataState *state)
void app::save(string file, void (*func)(ofstream &, void *),
DataState *state, void *data)
{
new WriteTask(appdir + "/" + file, func, state);
new WriteTask(appdir + "/" + file, func, state, data);
}
void app::read_now(string file, void (*func)(ifstream &))

View File

@ -31,11 +31,13 @@ void IOTask::run_task()
}
WriteTask::WriteTask(string file, void (*fn)(ofstream &), DataState *s)
WriteTask::WriteTask(string file, void (*fn)(ofstream &, void *),
DataState *s, void *d)
{
filepath = file;
func = fn;
state = s;
data = d;
queue();
}
@ -50,6 +52,6 @@ void WriteTask::run_task()
return;
ofstream stream;
stream.open(filepath.c_str());
func(stream);
func(stream, data);
stream.close();
}

View File

@ -2,12 +2,46 @@
* Copyright (c) 2011 Bryan Schumaker.
* Manages the libsaria library save file
*/
#include <libsaria/task.h>
#include <libsaria/library.h>
#include <libsaria/libpath.h>
#include <libsaria/track.h>
#include <libsaria/print.h>
#include <libsaria/fs.h>
#include "library.h"
#include <fstream>
#include <sstream>
using namespace std;
static string libdir = "library";
static string id_to_filename(unsigned int id)
{
stringstream s;
s << id;
return s.str();
}
static void do_save_path(ofstream &stream, void *data)
{
libsaria::library::Path *path = (libsaria::library::Path *)data;
list<libsaria::Track>::iterator it;
println("Saving library path: %d", path->id);
stream << 1 << "\n"; /* Save file version: 1 */
stream << path->path << "\n";
stream << path->id << " " << path->visible << " ";
stream << path->next_track << " " << path->tracks.size() << "\n";
for (it = path->tracks.begin(); it != path->tracks.end(); it++)
(*it).save(stream);
}
void save_path(libsaria::library::Path *path)
{
string filepath = libdir + "/" + id_to_filename(path->id);
libsaria::app::save(filepath, do_save_path, &path->data_state, path);
}
/*static void do_load()
{
unsigned int size;

View File

@ -3,6 +3,7 @@
#include <libsaria/library.h>
#include <libsaria/stack.h>
#include <libsaria/track.h>
#include <libsaria/fs.h>
#include "library.h"
//#include <map>
@ -49,6 +50,7 @@ namespace libsaria
list<libsaria::library::Driver *>::iterator it;
path.visible = true;
path.data_state = CLEAN;
path.id = next_id++;
path.next_track = 0;
path.path = dir;
@ -80,6 +82,7 @@ namespace libsaria
void library::init()
{
//load();
app::mkdir("library");
stack::push(&lib_playlist);
}

View File

@ -7,6 +7,7 @@
extern list<libsaria::library::Driver *>driver_list;
extern libsaria::Set lib_playlist;
void update_path(struct libsaria::library::Path *);
void save_path(struct libsaria::library::Path *);
/*#include <string>
using namespace std;
*/

View File

@ -60,6 +60,7 @@ void ScanTask::run_task()
try {
path->tracks.push_back(libsaria::Track(*it, path));
tracks.push_back(&path->tracks.back());
path->data_state = DIRTY;
} catch (string message) {
println(message);
}
@ -84,15 +85,20 @@ void ReaddirTask::run_task()
{
list<string> file_list;
list<string>::iterator it;
unsigned int i;
ScanTask *task;
libsaria::list_dir(path->path, file_list);
println("Found: %d files to scan", file_list.size());
i = 0;
while (file_list.size() > 0) {
task = new ScanTask(path, file_list);
task->queue();
if ((i++ % 50) == 0)
save_path(path);
}
save_path(path);
}
void update_path(struct libsaria::library::Path *path)

View File

@ -11,7 +11,7 @@ using namespace std;
static DataState state = CLEAN;
static map<string, int> preferences;
static void save(ofstream &stream)
static void save(ofstream &stream, void *data)
{
map<string, int>::iterator it;
println("Saving preferences!");
@ -46,7 +46,7 @@ static void set_preference(string key, int p)
{
preferences[key] = p;
state = DIRTY;
libsaria::app::save("prefs", save, &state);
libsaria::app::save("prefs", save, &state, NULL);
}
namespace libsaria

View File

@ -9,7 +9,9 @@
#include <taglib/tag.h>
#include <taglib/fileref.h>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;
static libsaria::Track *cur = NULL;
@ -30,6 +32,11 @@ static void make_lenstr(unsigned int length, string &res)
res = stream.str();
}
static inline void escape_text(string &text)
{
replace(text.begin(), text.end(), '\n', ' ');
}
namespace libsaria
{
@ -52,6 +59,13 @@ namespace libsaria
year = tag->year();
track = tag->track();
/* Strip out newline characters */
escape_text(title);
escape_text(artist);
escape_text(album);
escape_text(comment);
escape_text(genre);
/* Extract audio properties */
prop = ref.audioProperties();
length = prop->length();
@ -64,7 +78,6 @@ namespace libsaria
{
filepath = file;
path = lib_path;
id = lib_path->next_track++;
count = 0;
last_day = 0;
last_month = 0;
@ -72,6 +85,7 @@ namespace libsaria
read_tags();
id = lib_path->next_track++;
artist_lc = lowercase(artist);
album_lc = lowercase(album);
make_lenstr(length, lenstr);
@ -164,26 +178,29 @@ namespace libsaria
libsaria::index::add_track(this);
//libsaria::library::list_track(this);
}
void Track::save(OutFile &out)
*/
void Track::save(ofstream &out)
{
out << songid;
out << filepath;
out << title;
out << artist;
out << album;
out << comment;
out << genre;
out << lenstr;
out << year;
out << track;
out << length;
out << bitrate;
out << sample;
out << channels;
out << "\n";
out << filepath << "\n";
out << title << "\n";
out << artist << "\n";
out << album << "\n";
out << comment << "\n";
out << genre << "\n";
out << lenstr << "\n";
out << id << " ";
out << year << " ";
out << track << " ";
out << count << " ";
out << last_day << " ";
out << last_month << " ";
out << last_year << " ";
out << length << " ";
out << bitrate << " ";
out << sample << " ";
out << channels << "\n";
}
/*
void Track::make_lenstr()
{
int minutes;