SimpleParser.h

// SimpleParser.h
//
// Author David Barrett-Lennard
// (C)opyright Cedanet Pty Ltd 2013

#pragma once
#ifndef Ceda_cxUtils_SimpleParser_H
#define Ceda_cxUtils_SimpleParser_H

#include "cxUtils.h"
#include "xstring.h"
#include "IException.h"
#include "SubString.h"

namespace ceda
{

///////////////////////////////////////////////////////////////////////////////////////////////////
// EToken

enum EToken
{
    TOKEN_UNDEF,
    TOKEN_END,
    TOKEN_INTEGER,
    TOKEN_FLOAT,
    TOKEN_DOUBLE_QUOTED_STRING,
    TOKEN_IDENTIFIER,
    TOKEN_COMMENT,
    TOKEN_EQUALS,
    TOKEN_OTHER
};

///////////////////////////////////////////////////////////////////////////////////////////////////
// ParserException

struct ParserException : public IException
{
    ParserException(const xstring& description, const xchar* pos) : 
        m_description(description),
        m_pos(pos)
    {
    }

    ParserException(const xstring& description, SubString s) : 
        m_description(description),
        m_pos(s.ptr())
    {
    }

    virtual ConstStringZ what() const 
    { 
        return m_description.c_str(); 
    }

    void Write(xostream& os) const
    {
        os << m_description;
    }

    xstring m_description;
    const xchar* m_pos;
};

///////////////////////////////////////////////////////////////////////////////////////////////////
// SimpleParser

class cxUtils_API SimpleParser
{
public:
    void Init(SubString s);
    void ReadNextToken();
    void ParseGivenToken(SubString s);
    xstring ParseString();
    bool ParseBool();
    int32 ParseInt32();
    int64 ParseInt64();
    float32 ParseFloat32();
    float64 ParseFloat64();
    bool ParseSetting(const xstring& name, xstring& value);
    bool ParsePathSetting(const xstring& name, xstring& value);
    bool ParseSetting(const xstring& name, bool& value);
    bool ParseSetting(const xstring& name, int32& value);
    bool ParseSetting(const xstring& name, int64& value);
    bool ParseSetting(const xstring& name, float32& value);
    bool ParseSetting(const xstring& name, float64& value);
protected:
    SubString file_;
    SubString token_;
    EToken tokenType_;
};

} // namespace ceda

#endif // include guard