/*
 * 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 */
