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


#include "metafile/nml.h"
#include "alloc/ialloc.h"

using namespace GS::NML;


//------------------------------------------------------------------------------
bool File::GetBool(const char *path, bool dflt, bool verbose) const {
    Tag *t = GetTypedTag(path, Variant::VariantBool, verbose);
    return t ? t->GetBool() : dflt;
}

int File::GetInteger(const char *path, int dflt, bool verbose) const {
    Tag *t = GetTypedTag(path, Variant::VariantInteger, verbose);
    return t ? t->GetInteger() : dflt;
}

float File::GetReal(const char *path, float dflt, bool verbose) const {
    Tag *t = GetTypedTag(path, Variant::VariantFloat, verbose);
    return t ? t->GetReal() : dflt;
}

const char *File::GetString(const char *path, const char *dflt, bool verbose) const {
    Tag *t = GetTypedTag(path, Variant::VariantString, verbose);
    return t ? t->GetString() : dflt;
}

//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
void File::Import(const File &src, bool clear_before_import) {
    if (clear_before_import)
        Clear();
    ListForeachPtr(Tag *, tag, src.GetTags())
        AddRoot(tag->Clone());
}

File *File::Clone() const {
    File *clone = new File;
    if (!clone)
        return NULL;

    if (!name.IsEmpty())
        clone->name = name.c_str();

    clone->Import(*this);
    return clone;
}

//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
Tag *File::AddRoot(Tag *t) {
    if (!t)
        return NULL;
    if (!tags.Add(t))
        return NULL;
    return t;
}

Tag *File::AddRoot(const char *name) { return AddRoot(new Tag(name)); }
Tag *File::AddRoot(const char *name, bool v) { return AddRoot(new Tag(name, v)); }
Tag *File::AddRoot(const char *name, int v) { return AddRoot(new Tag(name, v)); }
Tag *File::AddRoot(const char *name, float v) { return AddRoot(new Tag(name, v)); }
Tag *File::AddRoot(const char *name, const char *s) { return AddRoot(new Tag(name, s)); }
Tag *File::AddRoot(const char *name, void *d, size_t s) { return AddRoot(new Tag(name, d, s)); }
bool File::UnlinkRoot(Tag *t) { return tags.Remove(t); }
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
void File::Free() {
    ListDeleteAllPtr(Tag *, tags);
    name.Clear();
}

File::~File() { Free(); }
//------------------------------------------------------------------------------
