Commit 257fbcc2 authored by Alexandre Savard's avatar Alexandre Savard

[#2524] Implemented first yaml parser

parent 4b82f17a
......@@ -37,19 +37,14 @@ class Engine {
public:
Engine();
virtual void open() = 0;
~Engine();
virtual void close() = 0;
void open();
virtual void write() = 0;
void write();
virtual void read() = 0;
private:
yaml_emitter_t emitter;
yaml_event_t event;
};
#endif
/*
* Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
* Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#include "yamlemitter.h"
YamlEmitter::YamlEmitter() {}
YamlEmitter::~YamlEmitter() {}
void YamlEmitter::open() {}
void YamlEmitter::close() {}
void YamlEmitter::read() {}
void YamlEmitter::write() {}
/*
* Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
* Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#ifndef __YAMLEMITTER_H__
#define __YAMLEMITTER_H__
class YamlEmitter {
public:
YamlEmitter();
~YamlEmitter();
void open();
void close();
void read();
void write();
private:
};
#endif
/*
* Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
* Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#include "yamlengine.h"
YamlEngine::YamlEngine() {}
YamlEngine::~YamlEngine() {}
void YamlEngine::open() {}
void YamlEngine::close() {}
void YamlEngine::read() {}
void YamlEngine::write() {}
/*
* Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
* Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#ifndef __YAMLENGINE_H__
#define __YAMLENGINE_H__
#include "yamlparser.h"
#include "yamlemiter.h"
class YamlEngine : Engine {
public:
YamlEngine();
~YamlEngine();
virtual void open();
virtual void close();
virtual void write();
virtual void read();
private:
YamlParser *parser;
YamlEmiter *emiter;
};
#endif
/*
* Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
* Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#include "yamlparser.h"
#include <assert.h>
YamlParser::YamlParser()
{
memset(buffer, 0, BUFFER_SIZE);
open();
}
YamlParser::~YamlParser()
{
close();
}
void YamlParser::open()
{
fd = fopen("test.yaml", "rb");
if(!fd)
throw 20;
if(!yaml_parser_initialize(&parser))
throw 20;
yaml_parser_set_input_file(&parser, fd);
}
void YamlParser::close()
{
yaml_parser_delete(&parser);
if(!fclose(fd))
throw 20;
}
void YamlParser::parse()
{
bool done;
yaml_event_t event;
while(!done) {
if(!yaml_parser_parse(&parse, &event))
throw 20;
done = (event.type == YAML_STREAM_END_EVENT);
if(eventNumber > PARSER_MAXEVENT)
throw 20;
if(!copyEvent(&(events[eventNumber++]), &event))
throw 20;
}
}
int YamlParser::copyEvent(yaml_event_t *event_to, yaml_event_t *event_from)
{
switch (event_from->type) {
case YAML_STREAM_START_EVENT:
return yaml_stream_start_event_initialize(event_to,
event_from->data.stream_start.encoding);
case YAML_STREAM_END_EVENT:
return yaml_stream_end_event_initialize(event_to);
case YAML_DOCUMENT_START_EVENT:
return yaml_document_start_event_initialize(event_to,
event_from->data.document_start.version_directive,
event_from->data.document_start.tag_directives.start,
event_from->data.document_start.tag_directives.end,
event_from->data.document_start.implicit);
case YAML_DOCUMENT_END_EVENT:
return yaml_document_end_event_initialize(event_to,
event_from->data.document_end.implicit);
case YAML_ALIAS_EVENT:
return yaml_alias_event_initialize(event_to,
event_from->data.alias.anchor);
case YAML_SCALAR_EVENT:
return yaml_scalar_event_initialize(event_to,
event_from->data.scalar.anchor,
event_from->data.scalar.tag,
event_from->data.scalar.value,
event_from->data.scalar.length,
event_from->data.scalar.plain_implicit,
event_from->data.scalar.quoted_implicit,
event_from->data.scalar.style);
case YAML_SEQUENCE_START_EVENT:
return yaml_sequence_start_event_initialize(event_to,
event_from->data.sequence_start.anchor,
event_from->data.sequence_start.tag,
event_from->data.sequence_start.implicit,
event_from->data.sequence_start.style);
case YAML_SEQUENCE_END_EVENT:
return yaml_sequence_end_event_initialize(event_to);
case YAML_MAPPING_START_EVENT:
return yaml_mapping_start_event_initialize(event_to,
event_from->data.mapping_start.anchor,
event_from->data.mapping_start.tag,
event_from->data.mapping_start.implicit,
event_from->data.mapping_start.style);
case YAML_MAPPING_END_EVENT:
return yaml_mapping_end_event_initialize(event_to);
default:
assert(1);
}
return 0;
}
......@@ -28,59 +28,58 @@
* as that of the covered work.
*/
#ifndef __YAMLPARSER_H__
#define __YAMLPARSER_H__
#include "engine.h"
#include <yaml.h>
#include <stdio.h>
#define PARSER_BUFFERSIZE 65536
#define PARSER_MAXEVENT 1024
/* Set a generic writer. */
void *ext = ...;
int write_handler(void *ext, char *buffer, int size) {
class YamlParser {
int error = 0;
public:
/*
...
Write `size` bytes.
...
*/
return error ? 0 : 1;
}
YamlParser();
~YamlParsere();
Engine::Engine()
{
open();
}
void open();
Engine::~Engine()
{
close();
}
void close();
void Engine::open()
{
void parse();
yaml_emitter_initialize(&emitter);
private:
yaml_emitter_set_output(&emitter, write_handler, ext);
/**
* Copy yaml parser event in event_to according to their type.
*/
int copyEvent(yaml_event_t *event_to, yaml_event_t *event_from);
yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
if (!yaml_emitter_emit(&emitter, &event))
return;
FILE *fd;
}
/**
* The parser structure.
*/
yaml_parser_t parser;
void Engine::close() {
/**
* The event structure array.
*/
yaml_event_t event[PARSER_MAXEVENT];
/* Create and emit the STREAM-END event. */
yaml_stream_end_event_initialize(&event);
if (!yaml_emitter_emit(&emitter, &event))
return;
/**
*
*/
unsigned char buffer[PARSER_BUFFERSIZE];
/* Destroy the Emitter object. */
yaml_emitter_delete(&emitter);
/**
* Number of event actually parsed
*/
int eventNumber;
}
void Engine::write() {}
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment