libsaria: Save the library scan results

I write each track to a file that will be read in on startup (once those
changes are committed)
This commit is contained in:
Bryan Schumaker 2011-09-27 08:23:49 -04:00
parent 517faceb99
commit 1d1ecc8d8c
11 changed files with 188 additions and 0 deletions

42
include/libsaria/files.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef LIBSARIA_FILES_H
#define LIBSARIA_FILES_H
#include <libsaria/idle.h>
#include <libsaria/print.h>
#include <sys/types.h>
#include <fstream>
using namespace std;
class SaveTask : public IdleTask
{
private:
void (* save_func)();
public:
SaveTask(void (*func)());
~SaveTask();
void run_task();
};
class OutFile
{
private:
ofstream out;
bool new_line;
void begin_write();
void end_write(bool);
public:
OutFile(string);
~OutFile();
void write_str(string, bool);
void write_int(int, bool);
void write_ui(unsigned int, bool);
void write_lui(long unsigned int, bool);
void write_ino(ino_t, bool);
};
#endif /* LIBSARIA_FILES_H */

View File

@ -11,5 +11,6 @@ void libsaria_add_library(string);
void libsaria_refresh_library();
void libsaria_library_list(list<Track> &);
void libsaria_play_id(ino_t);
void libsaria_library_save();
#endif /* LIBSARIA_LIBRARY_H */

View File

@ -1,6 +1,8 @@
#ifndef LIBSARIA_TAGS_H
#define LIBSARIA_TAGS_H
#include <libsaria/files.h>
#include <string>
using namespace std;
@ -28,6 +30,7 @@ class TrackTag
TrackTag(const TrackTag &);
TrackTag(string);
~TrackTag();
void save(OutFile &);
unsigned int get_track();
string get_title();

View File

@ -28,3 +28,8 @@ void libsaria_play_id(ino_t inode)
{
library.play_id(inode);
}
void libsaria_library_save()
{
library.save();
}

View File

@ -1,4 +1,5 @@
#include <libsaria/library.h>
#include "library.h"
Library::Library()
@ -24,3 +25,25 @@ void Library::play_id(ino_t inode)
break;
}
}
void Library::save()
{
OutFile out("library.lib");
map<string, LibraryPath>::iterator it;
out.write_lui(path_map.size(), true);
for (it = path_map.begin(); it != path_map.end(); it++)
it->second.save(out);
}
void LibraryPath::save(OutFile &out)
{
map<ino_t, TrackTag>::iterator it;
out.write_str(path, false);
out.write_lui(file_map.size(), true);
for (it = file_map.begin(); it != file_map.end(); it++) {
out.write_ino(it->first, false);
it->second.save(out);
}
}

View File

@ -22,6 +22,7 @@ class Library
void update_path(string);
void list_all(list<Track> &);
void play_id(ino_t);
void save();
};
#endif /* LIBSARIA_LIBRARY_SOURCE_H */

View File

@ -16,6 +16,11 @@ unsigned int LibraryPath::size()
return file_map.size();
}
string LibraryPath::get_path()
{
return path;
}
void LibraryPath::list_all(list<Track> &track_list)
{
map<ino_t, TrackTag>::iterator it;

View File

@ -10,6 +10,7 @@ using namespace std;
#include <libsaria/idle.h>
#include <libsaria/tags.h>
#include <libsaria/track.h>
#include <libsaria/files.h>
class LibraryPath
{
@ -22,10 +23,12 @@ class LibraryPath
~LibraryPath();
void update();
string get_path();
void insert_track(ino_t, TrackTag &);
unsigned int size();
void list_all(list<Track> &track_list);
bool play_id(ino_t);
void save(OutFile &);
};
class ScanTask : public IdleTask

View File

@ -3,6 +3,7 @@
#include <libsaria/idle.h>
#include <libsaria/path.h>
#include <libsaria/tags.h>
#include <libsaria/files.h>
#include <libsaria/library.h>
#include "library.h"
@ -14,6 +15,12 @@ static void queue_dir_scan(LibraryPath *lib_path, string scan_dir, bool end)
libsaria_queue_task(task);
}
static void queue_lib_save()
{
SaveTask *task = new SaveTask(libsaria_library_save);
libsaria_queue_task_front(task);
}
void Library::add_path(string dir)
{
path_map.insert( pair<string, LibraryPath>(dir, LibraryPath(dir)) );
@ -79,5 +86,6 @@ void ScanTask::run_task()
print("Library path size: ");
println(library->size());
libsaria_refresh_library();
queue_lib_save();
}
}

80
libsaria/path/files.cpp Normal file
View File

@ -0,0 +1,80 @@
#include <libsaria/files.h>
#include <libsaria/path.h>
SaveTask::SaveTask(void (*func)())
{
save_func = func;
}
SaveTask::~SaveTask()
{
}
void SaveTask::run_task()
{
save_func();
}
OutFile::OutFile(string path)
{
string out_file = get_saria_dir() + "/" + path;
println("Opening save file: " + out_file);
out.open(out_file.c_str());
new_line = true;
}
OutFile::~OutFile()
{
out.close();
}
void OutFile::begin_write()
{
if (new_line == false)
out << " ";
}
void OutFile::end_write(bool end)
{
if (end == true) {
out << endl;
new_line = true;
} else
new_line = false;
}
void OutFile::write_str(string s, bool end)
{
begin_write();
out << s.size() << " " << s;
end_write(end);
}
void OutFile::write_int(int i, bool end)
{
begin_write();
out << i;
end_write(end);
}
void OutFile::write_ui(unsigned int i, bool end)
{
begin_write();
out << i;
end_write(end);
}
void OutFile::write_lui(long unsigned int i, bool end)
{
begin_write();
out << i;
end_write(end);
}
void OutFile::write_ino(ino_t inode, bool end)
{
begin_write();
out << inode;
end_write(end);
}

View File

@ -64,6 +64,23 @@ TrackTag::~TrackTag()
{
}
void TrackTag::save(OutFile &out)
{
out.write_str(filepath, false);
out.write_str(title, false);
out.write_str(artist, false);
out.write_str(album, false);
out.write_str(comment, false);
out.write_str(genre, false);
out.write_str(lenstr, false);
out.write_ui(year, false);
out.write_ui(track, false);
out.write_int(length, false);
out.write_int(bitrate, false);
out.write_int(sample, false);
out.write_int(channels, true);
}
void TrackTag::make_lenstr()
{
int minutes;