/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


		#include "metafile/nml_string.h"
		#include "metafile/nml.h"
		#include "filesystem/io_memory.h"
		#include "memory/nauto_ptr.h"
		#include "nstring/nstring.h"
		#include "log/log.h"


namespace GS {
	namespace NML {

//-----------------------------------------------------------------------------
bool		TagToString(const Tag &tag, String &str)
{
	IO::Memory memory_fs;

	AutoPtr <IO::Handle> h(memory_fs.Open("file", IO::ModeWrite));
	if	(h.IsNull() || !Parser::SaveTag(*h, tag))
		return false;
	h = NULL;

	Array <char> data;
	if	(!memory_fs.FileLoad("file", data))
		return false;

	str.Set(data.Start(), data.End());
	return true;
}
bool		TagFromString(const String &str, Tag &tag)
{
	return Parser::ParseTag(tag, str.c_str(), &str.c_str()[str.Len()]);
}
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
bool		FileToString(const File &file, String &str)
{
	IO::Memory memory_fs;

	AutoPtr <IO::Handle> h(memory_fs.Open("file", IO::ModeWrite));
	if	(h.IsNull() || !Parser::Save(*h, file))
		return false;
	h = NULL;

	Array <char> data;
	if	(!memory_fs.FileLoad("file", data))
		return false;

	str.Set(data.Start(), data.End());
	return true;
}
bool		FileFromString(const String &str, File &file)
{
	return Parser::LoadFromMemory(str.c_str(), str.Len(), file);
}
//-----------------------------------------------------------------------------

	} // NML
} // GS
