
#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);
}

InFile::InFile(string path)
{
	string in_file = get_saria_dir() + "/" + path;
	in.open(in_file.c_str());
	if (in.good())
		println("Reading save file: " + in_file);
}

InFile::~InFile()
{
	in.close();
}

bool InFile::good()
{
	return in.good();
}

string InFile::read_str()
{
	int len;
	string str = "";

	in >> len;
	if (len == 0)
		return str;
	len++;
	in.get();
	char c[len];
	in.get(c, len);
	return c;
}

int InFile::read_int()
{
	int i;
	in >> i;
	return i;
}

unsigned int InFile::read_ui()
{
	unsigned int i;
	in >> i;
	return i;
}

long unsigned int InFile::read_lui()
{
	long unsigned int i;
	in >> i;
	return i;
}

ino_t InFile::read_ino()
{
	ino_t i;
	in >> i;
	return i;
}
