99 lines
2.1 KiB
C++
99 lines
2.1 KiB
C++
/*
|
|
* Copyright (c) 2011 Bryan Schumaker.
|
|
* Manages the libsaria library save file
|
|
*/
|
|
#include <library.h>
|
|
#include <notify.h>
|
|
#include <track.h>
|
|
#include <print.h>
|
|
#include <ban.h>
|
|
#include <fs.h>
|
|
#include "library.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
using namespace std;
|
|
|
|
#define LIBRARY_CURRENT_VERSION 3 /* 11/22/2012 */
|
|
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;
|
|
println("Saving library path: %d", path->id);
|
|
|
|
stream << LIBRARY_CURRENT_VERSION << "\n";
|
|
stream << path->path << "\n";
|
|
stream << path->id << " " << path->visible << " ";
|
|
stream << path->tracks.size() << "\n";
|
|
|
|
for (unsigned int i = 0; i < path->tracks.size(); i++)
|
|
path->tracks[i]->save(&stream);
|
|
}
|
|
|
|
void libsaria::library::save_path(libsaria::library::Path *path)
|
|
{
|
|
string filepath = libdir + "/" + id_to_filename(path->id);
|
|
libsaria::app::save(filepath, do_save_path, path);
|
|
}
|
|
|
|
void read_path(ifstream &stream)
|
|
{
|
|
string tmp;
|
|
stringstream s;
|
|
unsigned int size;
|
|
unsigned int version;
|
|
libsaria::Track *track;
|
|
struct libsaria::library::Path path, *path_ptr;
|
|
|
|
getline(stream, tmp);
|
|
s << tmp;
|
|
s >> version;
|
|
if (version > LIBRARY_CURRENT_VERSION)
|
|
return;
|
|
|
|
getline(stream, path.path);
|
|
|
|
getline(stream, tmp);
|
|
s.clear();
|
|
s.str(tmp);
|
|
|
|
s >> path.id >> path.visible;
|
|
if (version < 3) {
|
|
unsigned int next_track;
|
|
s >> next_track;
|
|
}
|
|
s >> size;
|
|
|
|
path_ptr = push_path(path);
|
|
for (unsigned int i = 0; i < size; i++) {
|
|
track = new libsaria::Track(stream, path_ptr, version);
|
|
path_ptr->tracks.push_back(track);
|
|
track->id = i;
|
|
}
|
|
|
|
libsaria::library::save_path(path_ptr);
|
|
libsaria::notify(PATH_UPDATED, path_ptr);
|
|
if (path.visible)
|
|
libsaria::library::show_path(path_ptr);
|
|
}
|
|
|
|
unsigned int schedule_load()
|
|
{
|
|
return libsaria::app::read_numdir(libdir, read_path);
|
|
}
|
|
|
|
void remove_file(libsaria::library::Path *path)
|
|
{
|
|
string file = id_to_filename(path->id);
|
|
libsaria::app::rm(libdir + "/" + file);
|
|
}
|