Skip to content

Commit

Permalink
Merge pull request #53 from kimkulling/issue_50
Browse files Browse the repository at this point in the history
Issue 50
  • Loading branch information
kimkulling committed Oct 24, 2020
2 parents b638003 + 1dc181b commit 32c1a82
Show file tree
Hide file tree
Showing 14 changed files with 382 additions and 62 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Expand Up @@ -94,6 +94,7 @@ SET ( openddl_parser_src
include/openddlparser/OpenDDLStream.h
include/openddlparser/DDLNode.h
include/openddlparser/Value.h
include/openddlparser/TPoolAllocator.h
README.md
)

Expand All @@ -120,7 +121,6 @@ SET ( gtest_src
${GTEST_PATH}/src/gtest-test-part.cc
${GTEST_PATH}/src/gtest-typed-test.cc
${GTEST_PATH}/src/gtest.cc
${GTEST_PATH}/src/gtest_main.cc
)

SET( openddl_parser_unittest_src
Expand All @@ -135,7 +135,9 @@ SET( openddl_parser_unittest_src
test/ValueTest.cpp
test/OpenDDLDefectsTest.cpp
test/OssFuzzTest.cpp
test/main.cpp
)
add_definitions(-DOPENDDL_TEST_DATA="${CMAKE_CURRENT_LIST_DIR}/test/TestData")

SOURCE_GROUP( code FILES ${openddl_parser_unittest_src} )
SOURCE_GROUP( gtest FILES ${gtest_src} )
Expand Down
16 changes: 16 additions & 0 deletions code/OpenDDLParser.cpp
Expand Up @@ -38,6 +38,7 @@ BEGIN_ODDLPARSER_NS
static const char *Version = "0.4.0";

namespace Grammar {

static const char *OpenBracketToken = "{";
static const char *CloseBracketToken = "}";
static const char *OpenPropertyToken = "(";
Expand Down Expand Up @@ -199,12 +200,27 @@ void OpenDDLParser::clear() {
// DDLNode::releaseNodes();
}

bool OpenDDLParser::validate() {
if (m_buffer.empty()) {
return true;
}

if (!isCharacter(m_buffer[0]) && !isNumeric(m_buffer[0])) {
return false;
}

return true;
}

bool OpenDDLParser::parse() {
if (m_buffer.empty()) {
return false;
}

normalizeBuffer(m_buffer);
if (!validate()) {
return false;
}

m_context = new Context;
m_context->m_root = DDLNode::create("root", "", nullptr);
Expand Down
94 changes: 48 additions & 46 deletions demo/main.cpp
Expand Up @@ -20,22 +20,24 @@ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-----------------------------------------------------------------------------------------------*/
#include <iostream>
#include <cassert>
#include <openddlparser/OpenDDLParser.h>
#include <openddlparser/OpenDDLExport.h>
#include <openddlparser/OpenDDLParser.h>
#include <cassert>
#include <iostream>

USE_ODDLPARSER_NS

static const char *FileOption = "--file";
static const char *ExportOption = "--export";
static const char *DumpOption = "--dump";
static const int Error = -1;
static const char *FileOption = "--file";
static const char *ExportOption = "--export";
static const char *DumpOption = "--dump";
static const int Error = -1;

static void showhelp() {
std::cout << "OpenDDL Parser Demo version " << OpenDDLParser::getVersion() << std::endl << std::endl;
std::cout << "OpenDDL Parser Demo version " << OpenDDLParser::getVersion() << std::endl
<< std::endl;
std::cout << "Usage:" << std::endl;
std::cout << "\topenddl_parser_demo --file <filename>" << std::endl << std::endl;
std::cout << "\topenddl_parser_demo --file <filename>" << std::endl
<< std::endl;
std::cout << "Parameter:" << std::endl;
std::cout << "\t--file : The Name of the file to load." << std::endl;
std::cout << "\t--export : The name for the file for export." << std::endl;
Expand All @@ -44,12 +46,12 @@ static void showhelp() {

static const std::string Intention = " ";

static std::string createIntention( unsigned int level ) {
static std::string createIntention(unsigned int level) {
std::string intention;
for (unsigned int i = 0; i<level; ++i) {
for (unsigned int i = 0; i < level; ++i) {
intention += Intention;
}

return intention;
}

Expand All @@ -58,63 +60,63 @@ static void dumpDllNode(DDLNode *node, unsigned int level, IOStreamBase &stream)
return;
}

const std::string intent( createIntention( level ) );
const std::string intent(createIntention(level));
std::cout << intent << "Node " << node->getName() << "\n";
std::cout << intent << "- type " << node->getType() << "\n";
std::cout << intent << "- value " << node->getType() << "\n";
Value *value = node->getValue();
if (nullptr != value) {
value->dump( stream );
value->dump(stream);
}
}

static void dumpDDLNodeTree( DDLNode *root, unsigned int level, IOStreamBase &stream) {
static void dumpDDLNodeTree(DDLNode *root, unsigned int level, IOStreamBase &stream) {
if (nullptr == root) {
return;
}

dumpDllNode( root, level, stream );
dumpDllNode(root, level, stream);

const DDLNode::DllNodeList &children = root->getChildNodeList();
if ( children.empty() ) {
if (children.empty()) {
return;
}

level++;
for ( auto node : children ) {
dumpDDLNodeTree( node, level, stream );
for (auto node : children) {
dumpDDLNodeTree(node, level, stream);
}
}

int main( int argc, char *argv[] ) {
if( argc < 3 ) {
int main(int argc, char *argv[]) {
if (argc < 3) {
showhelp();
return Error;
}

char *filename = nullptr, *exportFilename =nullptr;
bool dump( false ), exportToFile( false );
for ( int i = 1; i < argc; i++) {
char *filename = nullptr, *exportFilename = nullptr;
bool dump(false), exportToFile(false);
for (int i = 1; i < argc; i++) {
if (0 == strncmp(FileOption, argv[i], strlen(FileOption))) {
if ((i + 1) >= argc) {
std::cerr << "No filename specified" << std::endl;
return Error;
}

filename = argv[ i+1 ];
filename = argv[i + 1];
}

if (0 == strncmp(DumpOption, argv[ i ], strlen( DumpOption) ) ) {
if (0 == strncmp(DumpOption, argv[i], strlen(DumpOption))) {
dump = true;
}

if( 0==strncmp(ExportOption, argv[i], strlen(ExportOption ) ) ) {
if (0 == strncmp(ExportOption, argv[i], strlen(ExportOption))) {
if ((i + 1) >= argc) {
std::cerr << "No filename for export specified" << std::endl;
return Error;
} else {
exportToFile = true;
exportFilename = argv[i+1];
exportFilename = argv[i + 1];
}
}
}
Expand All @@ -126,44 +128,44 @@ int main( int argc, char *argv[] ) {
std::cout << "file to import: " << filename << std::endl;
}

FILE *fileStream = fopen( filename, "rb+" );
FILE *fileStream = fopen(filename, "rb+");
if (nullptr == fileStream) {
std::cerr << "Cannot open file " << filename << std::endl;
return Error;
}

// obtain file size:
fseek( fileStream, 0, SEEK_END );
const int size( ftell( fileStream ) );
if ( -1 == size ) {
fseek(fileStream, 0, SEEK_END);
const int size(ftell(fileStream));
if (-1 == size) {
std::cerr << "Error while obtaining file-size of file " << filename << ", aborting operation.\n";
return Error;
}

::rewind( fileStream );
if( size > 0 ) {
char *buffer = new char[ size ];
const size_t readSize( fread( buffer, sizeof( char ), size, fileStream ) );
assert( readSize == static_cast<size_t>( size ) );
::rewind(fileStream);
if (size > 0) {
char *buffer = new char[size];
const size_t readSize(fread(buffer, sizeof(char), size, fileStream));
assert(readSize == static_cast<size_t>(size));
OpenDDLParser theParser;
theParser.setBuffer( buffer, size );
const bool result( theParser.parse() );
if( !result ) {
theParser.setBuffer(buffer, size);
const bool result(theParser.parse());
if (!result) {
std::cerr << "Error while parsing file " << filename << "." << std::endl;
} else {
DDLNode *root = theParser.getRoot();
if ( dump ) {
if (dump) {
IOStreamBase stream;
dumpDDLNodeTree( root, 0, stream );
dumpDDLNodeTree(root, 0, stream);
}
if ( exportToFile ) {
if (exportToFile) {
OpenDDLExport theExporter;
theExporter.exportContext( theParser.getContext(), exportFilename );
theExporter.exportContext(theParser.getContext(), exportFilename);
}
}
delete [] buffer;
delete[] buffer;
}
fclose( fileStream );
fclose(fileStream);

return 0;
}
3 changes: 3 additions & 0 deletions include/openddlparser/OpenDDLParser.h
Expand Up @@ -140,11 +140,14 @@ class DLL_ODDLPARSER_EXPORT OpenDDLParser {
/// @brief Clears all parser data, including buffer and active context.
void clear();

bool validate();

/// @brief Starts the parsing of the OpenDDL-file.
/// @return True in case of success, false in case of an error.
/// @remark In case of errors check log.
bool parse();


bool exportContext(Context *ctx, const std::string &filename);

/// @brief Returns the root node.
Expand Down

0 comments on commit 32c1a82

Please sign in to comment.