libsaria: Rip out old library
The code was a bit messy and didn't make use of namespaces very well. By converting to a set of functions (instead of a class) I can use each function as a function pointer if I want to. I am also able to remove libsaria/library.cpp since this was just a set of wrapper functions to the old static class functions.
This commit is contained in:
parent
1b18069b77
commit
a508b7ff72
|
@ -9,12 +9,16 @@ using namespace std;
|
|||
|
||||
namespace libsaria
|
||||
{
|
||||
void add_library(string);
|
||||
void refresh_library();
|
||||
void library_list(list<Track> &);
|
||||
void play_id(ino_t);
|
||||
void library_save();
|
||||
void library_load();
|
||||
namespace library
|
||||
{
|
||||
void load();
|
||||
void save();
|
||||
void refresh();
|
||||
|
||||
void add_path(string);
|
||||
void play_id(ino_t);
|
||||
void for_each(void (*)(Track &));
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* LIBSARIA_LIBRARY_H */
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#define LIBSARIA_PATH_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
using namespace std;
|
||||
|
||||
#include <sys/types.h>
|
||||
|
@ -13,7 +13,7 @@ struct file
|
|||
ino_t d_ino;
|
||||
};
|
||||
|
||||
void readdir(string, vector<file> &, vector<file> &);
|
||||
void readdir(string, list<file> &, list<file> &);
|
||||
string get_saria_dir();
|
||||
void make_saria_dir();
|
||||
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
|
||||
#include "library/library.h"
|
||||
#include <libsaria/library.h>
|
||||
#include <libsaria/callback.h>
|
||||
#include <libsaria/track.h>
|
||||
|
||||
#include <list>
|
||||
using namespace std;
|
||||
|
||||
static Library library;
|
||||
|
||||
void libsaria::add_library(string dir)
|
||||
{
|
||||
library.add_path(dir);
|
||||
library.update_path(dir);
|
||||
}
|
||||
|
||||
void libsaria::refresh_library()
|
||||
{
|
||||
trigger_callback(LIBRARY_REFRESH);
|
||||
}
|
||||
|
||||
void libsaria::library_list(list<Track> &track_list)
|
||||
{
|
||||
library.list_all(track_list);
|
||||
}
|
||||
|
||||
void libsaria::play_id(ino_t inode)
|
||||
{
|
||||
library.play_id(inode);
|
||||
}
|
||||
|
||||
void libsaria::library_save()
|
||||
{
|
||||
library.save();
|
||||
}
|
||||
|
||||
void libsaria::library_load()
|
||||
{
|
||||
library.load();
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Manages the libsaria library save file
|
||||
*/
|
||||
|
||||
#include <libsaria/library.h>
|
||||
#include "library.h"
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
void library::load()
|
||||
{
|
||||
unsigned int size;
|
||||
string dir;
|
||||
map<string, LibraryPath> *path_map = get_library_map();
|
||||
InFile in("library.lib");
|
||||
|
||||
if (!in.good())
|
||||
return;
|
||||
size = in.read_lui();
|
||||
|
||||
for (unsigned int i = 0; i < size; i++) {
|
||||
dir = in.read_str();
|
||||
path_map->insert(
|
||||
pair<string, LibraryPath>(dir, LibraryPath(in, dir))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void library::save()
|
||||
{
|
||||
OutFile out("library.lib");
|
||||
map<string, LibraryPath> *path_map = get_library_map();
|
||||
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);
|
||||
}
|
||||
|
||||
}; /* namespace: libsaria */
|
|
@ -1,84 +1,71 @@
|
|||
|
||||
#include <map>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
#include <libsaria/callback.h>
|
||||
#include <libsaria/controls.h>
|
||||
#include <libsaria/library.h>
|
||||
#include "library.h"
|
||||
|
||||
Library::Library()
|
||||
static map<string, LibraryPath> path_map;
|
||||
|
||||
map<string, LibraryPath> *get_library_map()
|
||||
{
|
||||
return &path_map;
|
||||
}
|
||||
|
||||
Library::~Library()
|
||||
{
|
||||
}
|
||||
|
||||
void Library::list_all(list<Track> &track_list)
|
||||
LibraryPath *get_library_path(string dir)
|
||||
{
|
||||
map<string, LibraryPath>::iterator it;
|
||||
for (it = path_map.begin(); it != path_map.end(); it++)
|
||||
it->second.list_all(track_list);
|
||||
it = path_map.find(dir);
|
||||
if (it != path_map.end())
|
||||
return &it->second;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void Library::play_id(ino_t inode)
|
||||
{
|
||||
map<string, LibraryPath>::iterator it;
|
||||
for (it = path_map.begin(); it != path_map.end(); it++) {
|
||||
if (it->second.play_id(inode) == true)
|
||||
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 Library::load()
|
||||
{
|
||||
unsigned int size;
|
||||
string dir;
|
||||
InFile in("library.lib");
|
||||
|
||||
if (!in.good())
|
||||
return;
|
||||
size = in.read_lui();
|
||||
|
||||
for (unsigned int i = 0; i < size; i++) {
|
||||
dir = in.read_str();
|
||||
path_map.insert(
|
||||
pair<string, LibraryPath>(dir, LibraryPath(in, dir))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void LibraryPath::save(OutFile &out)
|
||||
void LibraryPath::for_each(void (*ins_func)(Track &))
|
||||
{
|
||||
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);
|
||||
Track track = Track(it->first, &it->second);
|
||||
ins_func(track);
|
||||
}
|
||||
}
|
||||
|
||||
LibraryPath::LibraryPath(InFile &in, string dir)
|
||||
bool LibraryPath::play_id(ino_t id)
|
||||
{
|
||||
unsigned int size;
|
||||
ino_t inode;
|
||||
|
||||
path = dir;
|
||||
size = in.read_lui();
|
||||
println("Library path dir: " + path);
|
||||
print("Library path size: ");
|
||||
println(size);
|
||||
|
||||
for (unsigned int i = 0; i < size; i++) {
|
||||
inode = in.read_ino();
|
||||
file_map[inode] = TrackTag(in);
|
||||
}
|
||||
map<ino_t, TrackTag>::iterator it;
|
||||
it = file_map.find(id);
|
||||
if (it == file_map.end())
|
||||
return false;
|
||||
libsaria::load(it->second.get_filepath());
|
||||
return true;
|
||||
}
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
|
||||
void library::for_each(void (*ins_func)(Track &))
|
||||
{
|
||||
map<string, LibraryPath>::iterator it;
|
||||
for (it = path_map.begin(); it != path_map.end(); it++)
|
||||
it->second.for_each(ins_func);
|
||||
}
|
||||
|
||||
void library::play_id(ino_t id)
|
||||
{
|
||||
map<string, LibraryPath>::iterator it;
|
||||
for (it = path_map.begin(); it != path_map.end(); it++) {
|
||||
if (it->second.play_id(id) == true)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void library::refresh()
|
||||
{
|
||||
trigger_callback(LIBRARY_REFRESH);
|
||||
}
|
||||
|
||||
}; /* namespace: libsaria */
|
||||
|
|
|
@ -3,27 +3,44 @@
|
|||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <list>
|
||||
using namespace std;
|
||||
|
||||
#include <libsaria/track.h>
|
||||
#include "path.h"
|
||||
#include <libsaria/path.h>
|
||||
|
||||
class Library
|
||||
class LibraryPath
|
||||
{
|
||||
private:
|
||||
map<string, LibraryPath> path_map;
|
||||
string path;
|
||||
map <ino_t, TrackTag> file_map;
|
||||
|
||||
public:
|
||||
Library();
|
||||
~Library();
|
||||
LibraryPath(string);
|
||||
LibraryPath(InFile &, string);
|
||||
~LibraryPath();
|
||||
|
||||
void add_path(string);
|
||||
void update_path(string);
|
||||
void list_all(list<Track> &);
|
||||
void play_id(ino_t);
|
||||
void save();
|
||||
void load();
|
||||
void for_each(void (*)(Track &));
|
||||
void insert_track(ino_t, TrackTag &);
|
||||
bool play_id(ino_t);
|
||||
void save(OutFile &);
|
||||
void update();
|
||||
unsigned int size();
|
||||
};
|
||||
|
||||
class ScanTask : public IdleTask
|
||||
{
|
||||
private:
|
||||
LibraryPath *library;
|
||||
string dir;
|
||||
bool end_dir;
|
||||
void tag_file(file);
|
||||
|
||||
public:
|
||||
ScanTask(LibraryPath *, string, bool);
|
||||
~ScanTask();
|
||||
void run_task();
|
||||
};
|
||||
|
||||
map<string, LibraryPath> *get_library_map();
|
||||
LibraryPath *get_library_path(string);
|
||||
|
||||
#endif /* LIBSARIA_LIBRARY_SOURCE_H */
|
||||
|
|
|
@ -1,40 +1,132 @@
|
|||
|
||||
#include <libsaria/controls.h>
|
||||
#include "path.h"
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
#include <libsaria/tags.h>
|
||||
#include <libsaria/library.h>
|
||||
#include "library.h"
|
||||
|
||||
/*
|
||||
* Definitions for the LibraryPath class
|
||||
*/
|
||||
LibraryPath::LibraryPath(string dir)
|
||||
{
|
||||
path = dir;
|
||||
}
|
||||
|
||||
LibraryPath::LibraryPath(InFile &in, string dir)
|
||||
{
|
||||
unsigned int size;
|
||||
ino_t inode;
|
||||
|
||||
path = dir;
|
||||
size = in.read_lui();
|
||||
println("Library path dir: " + path);
|
||||
print("Library path size: ");
|
||||
println(size);
|
||||
|
||||
for (unsigned int i = 0; i < size; i++) {
|
||||
inode = in.read_ino();
|
||||
file_map[inode] = TrackTag(in);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
LibraryPath::~LibraryPath()
|
||||
{
|
||||
}
|
||||
|
||||
void LibraryPath::insert_track(ino_t inode, TrackTag &tag)
|
||||
{
|
||||
file_map[inode] = tag;
|
||||
}
|
||||
|
||||
void LibraryPath::update()
|
||||
{
|
||||
ScanTask *task = new ScanTask(this, path, false);
|
||||
libsaria::queue_task(task);
|
||||
}
|
||||
|
||||
unsigned int LibraryPath::size()
|
||||
{
|
||||
return file_map.size();
|
||||
}
|
||||
|
||||
string LibraryPath::get_path()
|
||||
|
||||
/*
|
||||
* Definitions for the ScanTask class
|
||||
*/
|
||||
ScanTask::ScanTask(LibraryPath *lib_path, string scan_dir, bool end)
|
||||
{
|
||||
return path;
|
||||
library = lib_path;
|
||||
dir = scan_dir;
|
||||
end_dir = end;
|
||||
}
|
||||
|
||||
void LibraryPath::list_all(list<Track> &track_list)
|
||||
ScanTask::~ScanTask()
|
||||
{}
|
||||
|
||||
void ScanTask::tag_file(file filepath)
|
||||
{
|
||||
map<ino_t, TrackTag>::iterator it;
|
||||
for (it = file_map.begin(); it != file_map.end(); it++) {
|
||||
track_list.push_back(Track(it->first, &it->second));
|
||||
try {
|
||||
TrackTag tag(dir + "/" + filepath.name);
|
||||
library->insert_track(filepath.d_ino, tag);
|
||||
} catch (string message) {
|
||||
println(message);
|
||||
}
|
||||
}
|
||||
|
||||
bool LibraryPath::play_id(ino_t inode)
|
||||
/*
|
||||
* 1) Read a directory
|
||||
* 2) Find tags for each music file
|
||||
* 3) Create and queue a new ScanTask for each subdirectory
|
||||
*/
|
||||
void ScanTask::run_task()
|
||||
{
|
||||
map<ino_t, TrackTag>::iterator iter;
|
||||
iter = file_map.find(inode);
|
||||
if (iter == file_map.end())
|
||||
return false;
|
||||
libsaria::load(iter->second.get_filepath());
|
||||
return true;
|
||||
int last, i = 0;
|
||||
list<file> files;
|
||||
list<file> dirs;
|
||||
list<file>::iterator it;
|
||||
ScanTask *scan;
|
||||
SaveTask *save;
|
||||
|
||||
readdir(dir, files, dirs);
|
||||
|
||||
for (it = files.begin(); it != files.end(); it++)
|
||||
tag_file(*it);
|
||||
|
||||
last = dirs.size() - 1;
|
||||
for (it = dirs.begin(); it != dirs.end(); it++) {
|
||||
scan = new ScanTask(library, dir + "/" + (*it).name, i == last);
|
||||
libsaria::queue_task(scan);
|
||||
i++;
|
||||
}
|
||||
|
||||
if (end_dir == true) {
|
||||
print("Library path size: ");
|
||||
println(library->size());
|
||||
save = new SaveTask(libsaria::library::save);
|
||||
libsaria::queue_task_front(save);
|
||||
libsaria::library::refresh();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Other functions related to updating the library
|
||||
*/
|
||||
static void update_path(string dir)
|
||||
{
|
||||
get_library_path(dir)->update();
|
||||
}
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
void library::add_path(string dir)
|
||||
{
|
||||
get_library_map()->insert(
|
||||
pair<string, LibraryPath>(dir, LibraryPath(dir))
|
||||
);
|
||||
update_path(dir);
|
||||
}
|
||||
|
||||
}; /* Namespace: libsaria */
|
||||
|
|
|
@ -1,49 +0,0 @@
|
|||
#ifndef LIBSARIA_LIBRARY_PATH_H
|
||||
#define LIBSARIA_LIBRARY_PATH_H
|
||||
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <map>
|
||||
using namespace std;
|
||||
|
||||
#include <libsaria/path.h>
|
||||
#include <libsaria/idle.h>
|
||||
#include <libsaria/tags.h>
|
||||
#include <libsaria/track.h>
|
||||
#include <libsaria/files.h>
|
||||
|
||||
class LibraryPath
|
||||
{
|
||||
private:
|
||||
string path;
|
||||
map <ino_t, TrackTag> file_map;
|
||||
|
||||
public:
|
||||
LibraryPath(string);
|
||||
LibraryPath(InFile &, string);
|
||||
~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
|
||||
{
|
||||
private:
|
||||
LibraryPath *library;
|
||||
string dir;
|
||||
bool end_dir;
|
||||
void tag_file(file);
|
||||
|
||||
public:
|
||||
ScanTask(LibraryPath *, string, bool);
|
||||
~ScanTask();
|
||||
void run_task();
|
||||
};
|
||||
|
||||
#endif /* LIBSARIA_LIBRARY_PATH_H */
|
|
@ -1,91 +0,0 @@
|
|||
|
||||
#include <libsaria/print.h>
|
||||
#include <libsaria/idle.h>
|
||||
#include <libsaria/path.h>
|
||||
#include <libsaria/tags.h>
|
||||
#include <libsaria/files.h>
|
||||
#include <libsaria/library.h>
|
||||
|
||||
#include "library.h"
|
||||
#include "path.h"
|
||||
|
||||
static void queue_dir_scan(LibraryPath *lib_path, string scan_dir, bool end)
|
||||
{
|
||||
ScanTask *task = new ScanTask(lib_path, scan_dir, 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)) );
|
||||
}
|
||||
|
||||
void Library::update_path(string dir)
|
||||
{
|
||||
map<string, LibraryPath>::iterator it;
|
||||
it = path_map.find(dir);
|
||||
if (it != path_map.end())
|
||||
it->second.update();
|
||||
}
|
||||
|
||||
void LibraryPath::insert_track(ino_t inode, TrackTag & tag)
|
||||
{
|
||||
file_map[inode] = tag;
|
||||
}
|
||||
|
||||
void LibraryPath::update()
|
||||
{
|
||||
queue_dir_scan(this, path, true);
|
||||
}
|
||||
|
||||
ScanTask::ScanTask(LibraryPath *lib_path, string scan_dir, bool end)
|
||||
{
|
||||
library = lib_path;
|
||||
dir = scan_dir;
|
||||
end_dir = end;
|
||||
}
|
||||
|
||||
ScanTask::~ScanTask()
|
||||
{
|
||||
}
|
||||
|
||||
void ScanTask::tag_file(file filepath)
|
||||
{
|
||||
try {
|
||||
TrackTag tag(dir + "/" + filepath.name);
|
||||
library->insert_track(filepath.d_ino, tag);
|
||||
} catch (string message) {
|
||||
println(message);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 1) Read a directory
|
||||
* 2) Create and queue a new UpdateTask for each subdirectory
|
||||
* 3) Find tags for each music file
|
||||
*/
|
||||
void ScanTask::run_task()
|
||||
{
|
||||
vector<file> files;
|
||||
vector<file> dirs;
|
||||
readdir(dir, files, dirs);
|
||||
|
||||
for (unsigned int i = 0; i < files.size(); i++)
|
||||
tag_file(files[i]);
|
||||
|
||||
for (unsigned int i = 0; i < dirs.size(); i++)
|
||||
queue_dir_scan(library, dir + "/" + dirs[i].name, i == dirs.size() - 1);
|
||||
|
||||
if (end_dir == true) {
|
||||
print("Library path size: ");
|
||||
println(library->size());
|
||||
libsaria::refresh_library();
|
||||
queue_lib_save();
|
||||
}
|
||||
}
|
|
@ -12,7 +12,7 @@ void libsaria::init(int argc, char **argv)
|
|||
print("saria dir: ");
|
||||
println(get_saria_dir());
|
||||
make_saria_dir();
|
||||
libsaria::library_load();
|
||||
libsaria::library::load();
|
||||
}
|
||||
|
||||
void libsaria::quit()
|
||||
|
|
|
@ -12,7 +12,7 @@ using namespace std;
|
|||
#define __USE_BSD
|
||||
#endif
|
||||
|
||||
static void sort_entry(struct dirent *dirp, vector<file> &files, vector<file> &dirs)
|
||||
static void sort_entry(struct dirent *dirp, list<file> &files, list<file> &dirs)
|
||||
{
|
||||
struct file entry;
|
||||
|
||||
|
@ -35,7 +35,7 @@ static void sort_entry(struct dirent *dirp, vector<file> &files, vector<file> &d
|
|||
}
|
||||
}
|
||||
|
||||
void readdir(string dir, vector<file> &files, vector<file> &dirs)
|
||||
void readdir(string dir, list<file> &files, list<file> &dirs)
|
||||
{
|
||||
DIR *dp;
|
||||
struct dirent *dirp;
|
||||
|
|
Loading…
Reference in New Issue