2011-10-18 10:02:55 -04:00
|
|
|
/*
|
2012-02-11 13:58:26 -05:00
|
|
|
* Copyright (c) 2011 Bryan Schumaker.
|
2011-10-18 10:02:55 -04:00
|
|
|
* Manages the libsaria library save file
|
|
|
|
*/
|
2012-09-12 08:15:31 -04:00
|
|
|
#include <library.h>
|
|
|
|
#include <notify.h>
|
|
|
|
#include <track.h>
|
|
|
|
#include <print.h>
|
|
|
|
#include <ban.h>
|
|
|
|
#include <fs.h>
|
2011-10-18 10:02:55 -04:00
|
|
|
#include "library.h"
|
|
|
|
|
2012-03-24 12:10:18 -04:00
|
|
|
#include <stdlib.h>
|
2012-03-24 09:44:08 -04:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
using namespace std;
|
|
|
|
|
2012-11-23 12:08:18 -05:00
|
|
|
#define LIBRARY_CURRENT_VERSION 3 /* 11/22/2012 */
|
2012-03-24 09:44:08 -04:00
|
|
|
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);
|
|
|
|
|
2012-11-22 14:01:25 -05:00
|
|
|
stream << LIBRARY_CURRENT_VERSION << "\n";
|
2012-03-24 09:44:08 -04:00
|
|
|
stream << path->path << "\n";
|
|
|
|
stream << path->id << " " << path->visible << " ";
|
2012-11-22 14:01:25 -05:00
|
|
|
stream << path->tracks.size() << "\n";
|
2012-03-24 09:44:08 -04:00
|
|
|
|
2012-11-22 14:01:25 -05:00
|
|
|
for (unsigned int i = 0; i < path->tracks.size(); i++)
|
2012-11-24 16:50:53 -05:00
|
|
|
path->tracks[i]->save(&stream);
|
2012-03-24 09:44:08 -04:00
|
|
|
}
|
|
|
|
|
2012-05-15 10:29:26 -04:00
|
|
|
void libsaria::library::save_path(libsaria::library::Path *path)
|
2012-03-24 09:44:08 -04:00
|
|
|
{
|
|
|
|
string filepath = libdir + "/" + id_to_filename(path->id);
|
2012-11-22 11:13:55 -05:00
|
|
|
libsaria::app::save(filepath, do_save_path, path);
|
2012-03-24 09:44:08 -04:00
|
|
|
}
|
|
|
|
|
2012-03-24 12:10:18 -04:00
|
|
|
void read_path(ifstream &stream)
|
|
|
|
{
|
|
|
|
string tmp;
|
|
|
|
stringstream s;
|
|
|
|
unsigned int size;
|
|
|
|
unsigned int version;
|
2012-05-17 13:21:26 -04:00
|
|
|
libsaria::Track *track;
|
2012-03-24 12:10:18 -04:00
|
|
|
struct libsaria::library::Path path, *path_ptr;
|
|
|
|
|
|
|
|
getline(stream, tmp);
|
|
|
|
s << tmp;
|
|
|
|
s >> version;
|
2012-11-23 12:08:18 -05:00
|
|
|
if (version > LIBRARY_CURRENT_VERSION)
|
2012-03-24 12:10:18 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
getline(stream, path.path);
|
|
|
|
|
|
|
|
getline(stream, tmp);
|
|
|
|
s.clear();
|
|
|
|
s.str(tmp);
|
|
|
|
|
2012-11-22 14:01:25 -05:00
|
|
|
s >> path.id >> path.visible;
|
2012-11-23 12:08:18 -05:00
|
|
|
if (version < 3) {
|
2012-11-22 14:01:25 -05:00
|
|
|
unsigned int next_track;
|
|
|
|
s >> next_track;
|
|
|
|
}
|
2012-03-24 12:10:18 -04:00
|
|
|
s >> size;
|
|
|
|
|
|
|
|
path_ptr = push_path(path);
|
|
|
|
for (unsigned int i = 0; i < size; i++) {
|
2012-11-24 16:50:53 -05:00
|
|
|
track = new libsaria::Track(stream, path_ptr, version);
|
|
|
|
path_ptr->tracks.push_back(track);
|
2012-11-24 17:06:49 -05:00
|
|
|
track->id = i;
|
2012-03-24 12:10:18 -04:00
|
|
|
}
|
|
|
|
|
2012-11-22 14:01:25 -05:00
|
|
|
libsaria::library::save_path(path_ptr);
|
2012-08-21 17:55:27 -04:00
|
|
|
libsaria::notify(PATH_UPDATED, path_ptr);
|
2012-11-22 14:01:25 -05:00
|
|
|
if (path.visible)
|
|
|
|
libsaria::library::show_path(path_ptr);
|
2012-03-24 12:10:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int schedule_load()
|
|
|
|
{
|
2012-04-07 09:53:48 -04:00
|
|
|
return libsaria::app::read_numdir(libdir, read_path);
|
2012-03-24 12:10:18 -04:00
|
|
|
}
|
2012-03-31 10:56:35 -04:00
|
|
|
|
|
|
|
void remove_file(libsaria::library::Path *path)
|
|
|
|
{
|
|
|
|
string file = id_to_filename(path->id);
|
|
|
|
libsaria::app::rm(libdir + "/" + file);
|
|
|
|
}
|