[DEV] basic init

This commit is contained in:
Edouard DUPIN 2013-07-30 17:58:48 +02:00
commit 30d2d9dbbe
18 changed files with 705 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.pyc

0
ejson/Array.cpp Normal file
View File

0
ejson/Array.h Normal file
View File

0
ejson/Boolean.cpp Normal file
View File

0
ejson/Boolean.h Normal file
View File

0
ejson/Null.cpp Normal file
View File

0
ejson/Null.h Normal file
View File

0
ejson/Number.cpp Normal file
View File

0
ejson/Number.h Normal file
View File

0
ejson/Object.cpp Normal file
View File

0
ejson/Object.h Normal file
View File

133
ejson/Value.cpp Normal file
View File

@ -0,0 +1,133 @@
/**
* @author Edouard DUPIN
*
* @copyright 2011, Edouard DUPIN, all right reserved
*
* @license BSD v3 (see license file)
*/
#include <ejson/Value.h>
#include <ejson/debug.h>
#undef __class__
#define __class__ "Value"
etk::CCout& ejson::operator <<(etk::CCout& _os, const ejson::filePos& _obj)
{
_os << "(l=";
_os << _obj.GetLine();
_os << ",c=";
_os << _obj.GetCol();
_os << ")";
return _os;
}
ejson::Value::Value(const etk::UString& _value) :
m_pos(0,0),
m_value(_value)
{
// nothing to do.
}
void ejson::Value::AddIndent(etk::UString& _data, int32_t _indent) const
{
for (int32_t iii=0; iii<_indent; iii++) {
_data+="\t";
}
}
void ejson::Value::DrawElementParsed(const etk::UniChar& _val, const ejson::filePos& _filePos) const
{
if (_val=='\n') {
JSON_DEBUG(_filePos << " Parse '\\n'");
} else if (_val=='\t') {
JSON_DEBUG(_filePos << " Parse '\\t'");
} else {
JSON_DEBUG(_filePos << " Parse '" << _val << "'");
}
}
int32_t ejson::Value::CountWhiteChar(const etk::UString& _data, int32_t _pos, ejson::filePos& _filePos) const
{
_filePos.Clear();
int32_t white=0;
for (int32_t iii=_pos; iii<_data.Size(); iii++) {
_filePos.Check(_data[iii]);
if(true == _data[iii].IsWhiteChar()) {
white++;
} else {
break;
}
}
--_filePos;
return white;
}
void ejson::Value::Clear(void)
{
m_value="";
m_pos.Clear();
}
bool ejson::Value::IParse(const etk::UString& _data, int32_t& _pos, exml::filePos& _filePos, ejson::Document& _doc)
{
JSON_PARSE_ELEMENT("start parse : 'Value' named='" << m_value << "'");
for (int32_t iii=_pos; iii<_data.Size(); iii++) {
_filePos.Check(_data[iii]);
#ifdef ENABLE_DISPLAY_PARSED_ELEMENT
DrawElementParsed(_data[iii], _filePos);
#endif
exml::filePos tmpPos;
if( _data[iii]==' '
|| _data[iii]=='\t'
|| _data[iii]=='\n'
|| _data[iii]=='\r') {
// white space ==> nothing to do ...
} else if (_data[iii]=='{') {
// find an object:
} else if (_data[iii]=='"') {
// find a string
} else if (_data[iii]=='[') {
// find a list:
} else if( _data[iii]=='n'
&& iii+3<_data.Size()
&& _data[iii+1]=='u'
&& _data[iii+2]=='l'
&& _data[iii+3]=='l') {
// maybe find null:
} else if( _data[iii]=='t'
&& iii+3<_data.Size()
&& _data[iii+1]=='r'
&& _data[iii+2]=='u'
&& _data[iii+3]=='u') {
// maybe find true:
} else if( _data[iii]=='f'
&& iii+3<_data.Size()
&& _data[iii+1]=='a'
&& _data[iii+2]=='l'
&& _data[iii+3]=='s'
&& _data[iii+4]=='e') {
// maybe find false:
} else if( _data[iii]=>'0'
&& _data[iii]=<'9') {
// maybe find number:
} else {
// find an error ....
CREATE_ERROR(_doc, _data, _pos, _filePos, "Find '>' with no element in the element...");
// move the curent index
_pos += iii+1;
return false;
}
}
return false;
}

252
ejson/Value.h Normal file
View File

@ -0,0 +1,252 @@
/**
* @author Edouard DUPIN
*
* @copyright 2011, Edouard DUPIN, all right reserved
*
* @license BSD v3 (see license file)
*/
#ifndef __ETK_JSON_VALUE_H__
#define __ETK_JSON_VALUE_H__
#include <etk/types.h>
#include <etk/UString.h>
#include <etk/math/Vector2D.h>
namespace exml
{
#if 1
#define EXML_PARSE_ELEMENT EXML_VERBOSE
#else
#define EXML_PARSE_ELEMENT EXML_DEBUG
#endif
#if 1
#define EXML_PARSE_ATTRIBUTE EXML_VERBOSE
#else
#define EXML_PARSE_ATTRIBUTE EXML_DEBUG
#endif
class Document;
class Attribute;
class Comment;
class Declaration;
class Element;
class Text;
typedef enum {
typeUnknow, //!< might be an error ...
typeValue, //!< XXXXXX:*
typeDocument, //!< all the file main access
typeArray, //!< [...]
typeNull, //!< the 'null'
typeNumber, //!< the 216516.211651e+5454
typeObject, //!< the { ... }
} nodeType_te;
class filePos
{
private:
int32_t m_col;
int32_t m_line;
public:
filePos(void) : m_col(0),m_line(0) { };
filePos(int32_t _line, int32_t _col) : m_col(_col),m_line(_line) { };
~filePos(void) { };
filePos& operator ++(void) { m_col++; return *this; };
filePos& operator --(void) { m_col--; if(m_col<0) { m_col=0;} return *this; };
const filePos& operator +=(const filePos& _obj)
{
if (_obj.m_line==0) {
m_col += _obj.m_col;
} else {
m_col = _obj.m_col;
m_line += _obj.m_line;
}
return *this;
};
const filePos& operator +=(int32_t _col)
{
m_col += _col;
return *this;
};
const filePos& operator= (const filePos& _obj )
{
m_col = _obj.m_col;
m_line = _obj.m_line;
return *this;
}
void NewLine(void) { m_col=0; m_line++; };
bool Check(const etk::UniChar& _val)
{
m_col++;
if (_val=='\n') {
NewLine();
return true;
}
return false;
}
void Set(int32_t _line, int32_t _col)
{
m_col = _col;
m_line = _line;
}
void Clear(void)
{
m_col = 0;
m_line = 0;
}
int32_t GetCol(void) const { return m_col; };
int32_t GetLine(void) const { return m_line; };
};
etk::CCout& operator <<(etk::CCout& _os, const filePos& _obj);
class Value
{
public:
/**
* @brief basic element of a xml structure
*/
Value(void) : m_pos(0,0) { };
/**
* @brief basic element of a xml structure
* @param[in] value of the value
*/
Value(const etk::UString& _value);
/**
* @brief destructor
*/
virtual ~Value(void) { };
public:
/**
* @brief Parse the Current node [pure VIRUAL]
* @param[in] _data data string to parse.
* @param[in,out] _pos position in the string to start parse, return the position end of parsing.
* @param[in] _caseSensitive Request a parsion of element that is not case sensitive (all element is in low case)
* @param[in,out] file parsing position (line x col x)
* @return false if an error occured.
*/
virtual bool IParse(const etk::UString& _data, int32_t& _pos, exml::filePos& _filePos, ejson::Document& _doc) = 0;
/**
* @brief Generate a string with the tree of the xml
* @param[in,out] _data string where to add the elements
* @param[in] current indentation of the file
* @return false if an error occured.
*/
virtual bool IGenerate(etk::UString& _data, int32_t _indent) const { return true; };
protected:
etk::UString m_value; //!< value of the node (for element this is the name, for text it is the inside text ...)
public:
/**
* @brief Set the value of the node.
* @param[in] _value New value of the node.
*/
virtual void SetValue(etk::UString _value) { m_value = _value; };
/**
* @brief Get the current element Value.
* @return the reference of the string value.
*/
virtual const etk::UString& GetValue(void) const { return m_value; };
public:
/**
* @brief Get the node type.
* @return the type of the Node.
*/
virtual nodeType_te GetType(void) const { return typeValue; };
protected:
/**
* @brief Add indentation of the string input.
* @param[in,out] _data String where the indentation is done.
* @param[in] _indent Number of tab to add at the string.
*/
void AddIndent(etk::UString& _data, int32_t _indent) const;
/**
* @brief Display the cuurent element that is curently parse.
* @param[in] _val Char that is parsed.
* @param[in] _filePos Position of the char in the file.
*/
void DrawElementParsed(const etk::UniChar& _val, const exml::filePos& _filePos) const;
/**
* @brief count the number of white char in the string from the specify position (stop at the first element that is not a white char)
* @param[in] _data Data to parse.
* @param[in] _pos Start position in the string.
* @param[out] _filePos new poistion of te file to add.
* @return number of white element.
*/
int32_t CountWhiteChar(const etk::UString& _data, int32_t _pos, exml::filePos& _filePos) const;
public:
/**
* @brief Cast the element in a Document if it is possible.
* @return pointer on the class or NULL.
*/
virtual ejson::Document* ToDocument(void) { return NULL; };
virtual const ejson::Document* ToDocument(void) const { return NULL; };
/**
* @brief Cast the element in a Array if it is possible.
* @return pointer on the class or NULL.
*/
virtual ejson::Array* ToArray(void) { return NULL; };
virtual const ejson::Array* ToArray(void) const{ return NULL; };
/**
* @brief Cast the element in a Object if it is possible.
* @return pointer on the class or NULL.
*/
virtual ejson::Object* ToObject(void) { return NULL; };
virtual const ejson::Object* ToObject(void) const{ return NULL; };
/**
* @brief Cast the element in a Boolean if it is possible.
* @return pointer on the class or NULL.
*/
virtual ejson::Boolean* ToBoolean(void) { return NULL; };
virtual const ejson::Boolean* ToBoolean(void) const{ return NULL; };
/**
* @brief Cast the element in a Null if it is possible.
* @return pointer on the class or NULL.
*/
virtual ejson::Null* ToNull(void) { return NULL; };
virtual const ejson::Null* ToNull(void) const{ return NULL; };
/**
* @brief Cast the element in a Number if it is possible.
* @return pointer on the class or NULL.
*/
virtual ejson::Number* ToNumber(void) { return NULL; };
virtual const ejson::Number* ToNumber(void) const{ return NULL; };
/**
* @brief Check if the node is a ejson::Document
* @return true if the node is a ejson::Document
*/
bool IsDocument(void) const { return GetType()==ejson::typeDocument; };
/**
* @brief Check if the node is a ejson::Array
* @return true if the node is a ejson::Array
*/
bool IsArray(void) const { return GetType()==ejson::typeArray; };
/**
* @brief Check if the node is a ejson::Object
* @return true if the node is a ejson::Object
*/
bool IsObject(void) const { return GetType()==ejson::typeObject; };
/**
* @brief Check if the node is a ejson::Boolean
* @return true if the node is a ejson::Boolean
*/
bool IsBoolean(void) const { return GetType()==ejson::typeBoolean; };
/**
* @brief Check if the node is a ejson::Null
* @return true if the node is a ejson::Null
*/
bool IsNull(void) const { return GetType()==ejson::typeNull; };
/**
* @brief Check if the node is a ejson::Number
* @return true if the node is a ejson::Number
*/
bool IsNumber(void) const { return GetType()==ejson::typeNumber; };
/**
* @brief Clear the Node
*/
virtual void Clear(void);
};
};
#endif

11
ejson/debug.cpp Normal file
View File

@ -0,0 +1,11 @@
/**
* @author Edouard DUPIN
*
* @copyright 2011, Edouard DUPIN, all right reserved
*
* @license BSD v3 (see license file)
*/
#include <ejson/debug.h>
const char * g_ejsonLibName = "ejson ";

27
ejson/debug.h Normal file
View File

@ -0,0 +1,27 @@
/**
* @author Edouard DUPIN
*
* @copyright 2011, Edouard DUPIN, all right reserved
*
* @license BSD v3 (see license file)
*/
#ifndef __EJSON_DEBUG_H__
#define __EJSON_DEBUG_H__
#include <etk/types.h>
#include <etk/Debug.h>
extern const char * g_ejsonLibName;
#define JSON_CRITICAL(data) ETK_CRITICAL(g_ejsonLibName, data)
#define JSON_WARNING(data) ETK_WARNING(g_ejsonLibName, data)
#define JSON_ERROR(data) ETK_ERROR(g_ejsonLibName, data)
#define JSON_INFO(data) ETK_INFO(g_ejsonLibName, data)
#define JSON_DEBUG(data) ETK_DEBUG(g_ejsonLibName, data)
#define JSON_VERBOSE(data) ETK_VERBOSE(g_ejsonLibName, data)
#define JSON_ASSERT(cond, data) ETK_ASSERT(g_ejsonLibName, cond, data)
#define JSON_CHECK_INOUT(cond) ETK_CHECK_INOUT(g_ejsonLibName, cond)
#define JSON_TODO(cond) ETK_TODO(g_ejsonLibName, cond)
#endif

162
ejson/ejson.cpp Normal file
View File

@ -0,0 +1,162 @@
/**
* @author Edouard DUPIN
*
* @copyright 2011, Edouard DUPIN, all right reserved
*
* @license BSD v3 (see license file)
*/
#include <ejson/Document.h>
#include <ejson/debug.h>
#include <etk/os/FSNode.h>
#undef __class__
#define __class__ "Document"
ejson::Document::Document(void) :
m_writeErrorWhenDetexted(true),
m_comment(""),
m_Line(""),
m_filePos(0,0)
{
}
bool ejson::Document::IGenerate(etk::UString& _data, int32_t _indent) const
{
for (int32_t iii=0; iii<m_listSub.Size(); iii++) {
if (NULL!=m_listSub[iii]) {
m_listSub[iii]->IGenerate(_data, _indent);
}
}
return true;
}
bool ejson::Document::Parse(const etk::UString& _data)
{
JSON_VERBOSE("Start parsing document (type: string) size=" << _data.Size());
Clear();
ejson::filePos filePos(1,0);
int32_t parsePos = 0;
return SubParse(_data, parsePos, m_caseSensitive, filePos, *this, true);
}
bool ejson::Document::Generate(etk::UString& _data)
{
_data = "";
return IGenerate(_data,0);
}
bool ejson::Document::Load(const etk::UString& _file)
{
// Start loading the XML :
JSON_VERBOSE("open file (xml) \"" << _file << "\"");
Clear();
etk::FSNode tmpFile(_file);
if (false == tmpFile.Exist()) {
JSON_ERROR("File Does not exist : " << _file);
return false;
}
int64_t fileSize = tmpFile.FileSize();
if (0==fileSize) {
JSON_ERROR("This file is empty : " << _file);
return false;
}
if (false == tmpFile.FileOpenRead()) {
JSON_ERROR("Can not open (r) the file : " << _file);
return false;
}
// allocate data
char * fileBuffer = new char[fileSize+5];
if (NULL == fileBuffer) {
JSON_ERROR("Error Memory allocation size=" << fileSize);
return false;
}
// TODO : change this ... get the charset from the Declaration element ...
memset(fileBuffer, 0, (fileSize+5)*sizeof(char));
// load data from the file :
tmpFile.FileRead(fileBuffer, 1, fileSize);
// close the file:
tmpFile.FileClose();
// convert in UTF8 :
etk::UString tmpDataUnicode(fileBuffer, unicode::EDN_CHARSET_UTF8);
// remove temporary buffer:
delete(fileBuffer);
// parse the data :
bool ret = Parse(tmpDataUnicode);
//Display();
return ret;
}
bool ejson::Document::Store(const etk::UString& _file)
{
etk::UString createData;
if (false == Generate(createData)) {
JSON_ERROR("Error while creating the XML : " << _file);
return false;
}
etk::FSNode tmpFile(_file);
if (false == tmpFile.FileOpenWrite()) {
JSON_ERROR("Can not open (w) the file : " << _file);
return false;
}
etk::Char endTable = createData.c_str();
if (tmpFile.FileWrite(endTable, sizeof(char), endTable.Size()) != endTable.Size()) {
JSON_ERROR("Error while writing output XML file : " << _file);
tmpFile.FileClose();
return false;
}
tmpFile.FileClose();
return true;
}
void ejson::Document::Display(void)
{
etk::UString tmpp;
IGenerate(tmpp, 0);
JSON_INFO("Generated JSON : \n" << tmpp);
}
etk::UString CreatePosPointer(const etk::UString& _line, int32_t _pos)
{
etk::UString out;
int32_t iii;
for (iii=0; iii<_pos && iii<_line.Size(); iii++) {
if (_line[iii] == '\t') {
out += "\t";
} else {
out += " ";
}
}
for (; iii<_pos; iii++) {
out += " ";
}
out += "^";
return out;
}
void ejson::Document::DisplayError(void)
{
if (m_comment.Size()==0) {
JSON_ERROR("No error detected ???");
return;
}
JSON_ERROR(m_filePos << " " << m_comment << "\n"
<< m_Line << "\n"
<< CreatePosPointer(m_Line, m_filePos.GetCol()) );
#ifdef ENABLE_CRITICAL_WHEN_ERROR
JSON_CRITICAL("detect error");
#endif
}
void ejson::Document::CreateError(const etk::UString& _data, int32_t _pos, const ejson::filePos& _filePos, const etk::UString& _comment)
{
m_comment = _comment;
m_Line = _data.ExtractLine(_pos);
m_filePos = _filePos;
if (true==m_writeErrorWhenDetexted) {
DisplayError();
}
}

92
ejson/ejson.h Normal file
View File

@ -0,0 +1,92 @@
/**
* @author Edouard DUPIN
*
* @copyright 2011, Edouard DUPIN, all right reserved
*
* @license BSD v3 (see license file)
*/
#ifndef __ETK_JSON_H__
#define __ETK_JSON_H__
#include <ejson/Object.h>
#include <etk/unicode.h>
#include <etk/Vector.h>
namespace ejson
{
class Document : public ejson::Object
{
public:
/**
* @brief Constructor
*/
Document(void);
/**
* @brief Destructor
*/
virtual ~Document(void) { };
public:
/**
* @brief Parse a string that contain an XML
* @param[in] _data Data to parse
* @return false : An error occured
* @return true : Parsing is OK
*/
bool Parse(const etk::UString& _data);
/**
* @brief Generate a string that contain the created XML
* @param[out] _data Data where the xml is stored
* @return false : An error occured
* @return true : Parsing is OK
*/
bool Generate(etk::UString& _data);
/**
* @brief Load the file that might contain the xml
* @param[in] _file Filename of the xml (compatible with etk FSNode naming)
* @return false : An error occured
* @return true : Parsing is OK
*/
bool Load(const etk::UString& _file);
/**
* @brief Store the Xml in the file
* @param[in] _file Filename of the xml (compatible with etk FSNode naming)
* @return false : An error occured
* @return true : Parsing is OK
*/
bool Store(const etk::UString& _file);
/**
* @brief Display the Document on console
*/
void Display(void);
private:
bool m_writeErrorWhenDetexted;
etk::UString m_comment;
etk::UString m_Line;
ejson::filePos m_filePos;
public:
void DisplayErrorWhenDetected(void) { m_writeErrorWhenDetexted=true; };
void NotDisplayErrorWhenDetected(void) { m_writeErrorWhenDetexted=false; };
void CreateError(const etk::UString& _data, int32_t _pos, const ejson::filePos& _filePos, const etk::UString& _comment);
void DisplayError(void);
public: // herited function:
virtual nodeType_te GetType(void) const { return typeDocument; };
bool IParse(const etk::UString& _data, int32_t& _pos, exml::filePos& _filePos, ejson::Document& _doc);
bool IGenerate(etk::UString& _data, int32_t _indent) const;
virtual ejson::Document* ToDocument(void) { return this; };
virtual const ejson::Document* ToDocument(void) const { return this; };
};
};
#define EJSON_CREATE_ERROR(doc,data,pos,filePos,comment) \
do { \
JSON_ERROR(comment); \
(doc).CreateError((data),(pos),(filePos),(comment)); \
} while (0)
//__LINE__, __class__, __func__
#endif

27
lutin_ejson.py Normal file
View File

@ -0,0 +1,27 @@
#!/usr/bin/python
import lutinModule
import lutinTools
def Create(target):
myModule = lutinModule.module(__file__, 'ejson', 'LIBRARY')
myModule.AddModuleDepend(['etk'])
myModule.AddSrcFile([
'ejson/debug.cpp',
'ejson/Node.cpp',
'ejson/ejson.cpp',
'ejson/Array.cpp',
'ejson/Boolean.cpp',
'ejson/Node.cpp',
'ejson/Null.cpp',
'ejson/Number.cpp',
'ejson/Object.cpp',
'ejson/Value.cpp'])
myModule.AddExportPath(lutinTools.GetCurrentPath(__file__))
# add the currrent module at the
return myModule