First working version with some test executables

This commit is contained in:
T. van der Zwan
2013-07-26 20:38:34 +00:00
commit 10b5b80675
62 changed files with 15621 additions and 0 deletions

31
include/utils/RgbColor.h Normal file
View File

@@ -0,0 +1,31 @@
#pragma once
// STL includes
#include <stdint.h>
#include <iostream>
// Forward class declaration
struct RgbColor;
struct RgbColor
{
uint8_t red;
uint8_t green;
uint8_t blue;
static RgbColor BLACK;
static RgbColor RED;
static RgbColor GREEN;
static RgbColor BLUE;
static RgbColor YELLOW;
static RgbColor WHITE;
};
inline std::ostream& operator<<(std::ostream& os, const RgbColor& color)
{
os << "{" << unsigned(color.red) << "," << unsigned(color.green) << "," << unsigned(color.blue) << "}";
return os;
}

56
include/utils/RgbImage.h Normal file
View File

@@ -0,0 +1,56 @@
#pragma once
#include <cassert>
#include <cstring>
// Local includes
#include "RgbColor.h"
class RgbImage
{
public:
RgbImage(const unsigned width, const unsigned height, const RgbColor background = RgbColor::BLACK);
~RgbImage();
inline unsigned width() const
{
return mWidth;
}
inline unsigned height() const
{
return mHeight;
}
void setPixel(const unsigned x, const unsigned y, const RgbColor color);
const RgbColor& operator()(const unsigned x, const unsigned y) const;
RgbColor& operator()(const unsigned x, const unsigned y);
inline void copy(const RgbImage& other)
{
std::cout << "This image size: [" << width() << "x" << height() << "]. Other image size: [" << other.width() << "x" << other.height() << "]" << std::endl;
assert(other.mWidth == mWidth);
assert(other.mHeight == mHeight);
memcpy(mColors, other.mColors, mWidth*mHeight*sizeof(RgbColor));
}
private:
inline unsigned toIndex(const unsigned x, const unsigned y) const
{
return y*mWidth + x;
}
private:
unsigned mWidth;
unsigned mHeight;
/** The colors of the image */
RgbColor* mColors;
};

View File

@@ -0,0 +1,66 @@
#pragma once
#include <string>
#include <istream>
#include <fstream>
#include <stdexcept>
#include <sstream>
// JSON-Schema includes
#include <utils/jsonschema/JsonSchemaChecker.h>
class JsonFactory
{
public:
static int load(const std::string& schema, const std::istream& config, Json::Value json);
static int load(const std::string& schema, const std::string& config, Json::Value& json)
{
// Load the schema and the config trees
Json::Value schemaTree = readJson(schema);
Json::Value configTree = readJson(config);
// create the validator
JsonSchemaChecker schemaChecker;
schemaChecker.setSchema(schemaTree);
bool valid = schemaChecker.validate(configTree);
for (const std::string& message : schemaChecker.getMessages())
{
std::cout << message << std::endl;
}
if (!valid)
{
std::cerr << "Validation failed for configuration file: " << config.c_str() << std::endl;
return -3;
}
json = configTree;
return 0;
}
static Json::Value readJson(const std::string& filename)
{
// Open the file input stream
std::ifstream ifs(filename.c_str());
return readJson(ifs);
}
static Json::Value readJson(std::istream& stream)
{
// will contains the root value after parsing.
Json::Value jsonTree;
Json::Reader reader;
if (! reader.parse(stream, jsonTree, false))
{
// report to the user the failure and their locations in the document.
std::stringstream sstream;
sstream << "Failed to parse configuration: " << reader.getFormattedErrorMessages().c_str();
throw std::runtime_error(sstream.str());
}
return jsonTree;
}
};

View File

@@ -0,0 +1,75 @@
// Copyright (c) 2012 TNO, The Netherlands.
//
// This file contains information proprietary to TNO.
//
// Any disclosure or use of this information or any reproduction of this document or any part thereof for
// other than the specified purpose for which it is intended is expressly prohibited except as TNO may
// otherwise agree to in writing.
#pragma once
// stl includes
#include <string>
#include <list>
// jsoncpp includes
#include <json/json.h>
/**
* JsonSchemaChecker is a very basic implementation of json schema.
* The json schema definition draft can be found at
* http://tools.ietf.org/html/draft-zyp-json-schema-03
*
* The following keywords are supported:
* - type
* - required
* - properties
* - items
* - enum
* - minimum
* - maximum
*/
class JsonSchemaChecker
{
public:
JsonSchemaChecker();
virtual ~JsonSchemaChecker();
bool setSchema(const Json::Value & schema);
bool validate(const Json::Value & value);
const std::list<std::string> & getMessages() const;
private:
void collectReferences(const Json::Value & schema);
void validate(const Json::Value &value, const Json::Value & schema);
void setMessage(const std::string & message);
void collectDependencies(const Json::Value & value, const Json::Value &schema);
private:
// attribute check functions
void checkType(const Json::Value & value, const Json::Value & schema);
void checkProperties(const Json::Value & value, const Json::Value & schema);
void checkAdditionalProperties(const Json::Value & value, const Json::Value & schema, const Json::Value::Members & ignoredProperties);
void checkDependencies(const Json::Value & value, const Json::Value & schemaLink);
void checkMinimum(const Json::Value & value, const Json::Value & schema);
void checkMaximum(const Json::Value & value, const Json::Value & schema);
void checkItems(const Json::Value & value, const Json::Value & schema);
void checkMinItems(const Json::Value & value, const Json::Value & schema);
void checkMaxItems(const Json::Value & value, const Json::Value & schema);
void checkUniqueItems(const Json::Value & value, const Json::Value & schema);
void checkEnum(const Json::Value & value, const Json::Value & schema);
private:
Json::Value _schema;
std::list<std::string> _currentPath;
std::list<std::string> _messages;
bool _error;
std::map<std::string, const Json::Value *> _references; // ref 2 value
};