lcpex rewrite - alpha

This commit is contained in:
phanes committed 2023-02-15 18:44:21 -05:00
1 parent bb85754dc0
commit 7ed6e13fa5
82 files changed
+4510 -2592

No files matched your search

+270
View File
@@ -0,0 +1,270 @@
/*
Rex - A configuration management and workflow automation tool that
compiles and runs in minimal environments.
© SILO GROUP LLC, 2023.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "JSON.h"
/**
* @class JSON_Loader_NotReady
* @brief Exception thrown when a member function is called before data is populated.
*
* This class is derived from the standard library class `std::runtime_error` and is thrown
* when a member function is called before the JSON data has been populated.
*/
class JSON_Loader_NotReady : public std::runtime_error
{
public:
/**
* @brief Constructor for the JSON_Loader_NotReady exception.
*
* This constructor creates an instance of the JSON_Loader_NotReady exception and sets its error message.
*/
JSON_Loader_NotReady() : std::runtime_error("JSON_Loader: Tried to access JSON without actually populating JSON.") {}
};
/**
* @class JSON_Loader_FileNotFound
* @brief Exception thrown when the requested file could not be found.
*
* This class is derived from the standard library class `std::runtime_error` and is thrown
* when the file specified in the JSON_Loader constructor could not be found.
*/
class JSON_Loader_FileNotFound : public std::runtime_error
{
public:
/**
* @brief Constructor for the JSON_Loader_FileNotFound exception.
*
* This constructor creates an instance of the JSON_Loader_FileNotFound exception and sets its error message.
*/
JSON_Loader_FileNotFound() : std::runtime_error("JSON_Loader: The requested file could not be found.") {}
};
/**
* @class JSON_Loader_InvalidJSON
* @brief Exception thrown when the provided JSON could not be parsed.
*
* This class is derived from the standard library class `std::runtime_error` and is thrown
* when the JSON data provided to the JSON_Loader could not be parsed.
*/
class JSON_Loader_InvalidJSON : public std::runtime_error
{
public:
/**
* @brief Constructor for the JSON_Loader_InvalidJSON exception.
*
* This constructor creates an instance of the JSON_Loader_InvalidJSON exception and sets its error message.
*/
JSON_Loader_InvalidJSON() : std::runtime_error("JSON_Loader: The JSON provided could not be parsed.") {}
};
/**
* @class JSON_Loader
* @brief The base class for loading and parsing JSON data.
*
* The JSON_Loader class provides the functionalities shared between its derived classes, Suite and Plan.
* It is responsible for initializing the JSON data to an unpopulated state.
*/
/**
* @brief Constructor for the JSON_Loader base class.
*
* This constructor initializes the JSON data to an unpopulated state and sets the logging level.
*
* @param LOG_LEVEL The logging level for the JSON_Loader instance.
*/
JSON_Loader::JSON_Loader(int LOG_LEVEL) : slog(LOG_LEVEL, "_json_")
{
this->populated = false;
this->LOG_LEVEL = LOG_LEVEL;
}
/**
* @brief Loads a JSON-formatted string into the JSON_Loader instance.
*
* This function takes a JSON-formatted string as input, parses it using a Json::Reader, and stores the result in the protected member `json_root`.
* If the parsing is successful, the `populated` flag is set to `true`.
*
* @param input The JSON-formatted string to be loaded into the JSON_Loader instance.
* @throws JSON_Loader_InvalidJSON if the input string could not be parsed.
*/
void JSON_Loader::load_json_string(std::string input)
{
Json::Reader json_reader;
std::ifstream json_file_ifstream(input.c_str(), std::ifstream::binary);
bool parsingSuccessful = json_reader.parse(json_file_ifstream, this->json_root);
if (!parsingSuccessful)
{
this->slog.log(E_FATAL, "Failed to parse adhoc JSON value: " + json_reader.getFormattedErrorMessages());
throw JSON_Loader_InvalidJSON();
}
else
{
this->slog.log(E_DEBUG, "Successfully parsed JSON string with " + std::to_string(this->json_root.size()) + " elements. Value: '" + input + "'.");
}
this->populated = true;
}
/**
* @brief Loads a JSON-formatted file into the JSON_Loader instance.
*
* This function takes a file path as input, parses the file using a Json::Reader, and stores the result in the protected member `json_root`.
* If the parsing is successful, the `populated` flag is set to `true`.
*
* @param filename The file path to the JSON-formatted file to be loaded into the JSON_Loader instance.
* @throws JSON_Loader_FileNotFound if the file specified in `filename` could not be found.
* @throws JSON_Loader_InvalidJSON if the contents of the file could not be parsed.
*/
void JSON_Loader::load_json_file(std::string filename)
{
Json::Reader json_reader;
if (!exists(filename))
{
this->slog.log_task(E_FATAL, "LOAD", "File '" + filename + "' does not exist.");
throw JSON_Loader_FileNotFound();
}
std::ifstream json_file_ifstream(filename, std::ifstream::binary);
bool parsingSuccessful = json_reader.parse(json_file_ifstream, this->json_root);
if (!parsingSuccessful)
{
this->slog.log_task(E_FATAL, "PARSING", "Failed to parse file '" + filename + "': " + json_reader.getFormattedErrorMessages());
throw JSON_Loader_InvalidJSON();
}
else
{
this->slog.log_task(E_DEBUG, "PARSING", "Parsed '" + filename + "' with " + std::to_string(this->json_root.size()) + " element(s).");
}
this->populated = true;
}
/**
* @brief Returns the string representation of the `json_root` member.
*
* This function returns the string representation of the `json_root` member, which contains the parsed JSON data.
*
* @return The string representation of the `json_root` member.
* @throws JSON_Loader_NotReady if the JSON data has not yet been populated.
*/
std::string JSON_Loader::as_string()
{
if (!this->populated)
{
throw JSON_Loader_NotReady();
}
return this->json_root.asString();
}
/**
* @brief Assigns the serialized representation of a specified key to a passed Json::Value reference.
*
* This function takes a reference to a Json::Value object as input, along with a string representing the key to search for.
* If the key is found in the `json_root` member, its value is assigned to the passed Json::Value reference.
*
* @param input A reference to the Json::Value object to receive the value of the specified key.
* @param key The key name to search for in the `json_root` member.
* @return 0 if the key was found and its value was assigned to the passed Json::Value reference, 1 if the key was not found.
* @throws JSON_Loader_NotReady if the JSON data has not yet been populated.
*/
int JSON_Loader::get_serialized(Json::Value &input, std::string key)
{
if (!this->populated)
{
throw JSON_Loader_NotReady();
}
if (this->json_root.isMember(key))
{
input = this->json_root[key];
return 0;
}
this->slog.log_task(E_FATAL, "PARSING", "Failed to find key '" + key + "'.");
return 1;
}
/**
* @brief Assigns the string representation of a specified key to a passed string reference.
*
* This function takes a reference to a string object as input, along with a string representing the key to search for.
* If the key is found in the `json_root` member, its string representation is assigned to the passed string reference.
*
* @param input A reference to the string object to receive the string representation of the specified key.
* @param key The key name to search for in the `json_root` member.
* @return 0 if the key was found and its string representation was assigned to the passed string reference, 1 if the key was not found.
* @throws JSON_Loader_NotReady if the JSON data has not yet been populated.
*/
int JSON_Loader::get_string(std::string &input, std::string key)
{
if (!this->populated)
{
throw JSON_Loader_NotReady();
}
if (this->json_root.isMember(key))
{
input = this->json_root[key].asString();
return 0;
}
this->slog.log_task(E_FATAL, "PARSING", "Failed to find key '" + key + "'.");
return 1;
}
/**
* @brief Assigns the boolean representation of a specified key to a passed bool reference.
*
* This function takes a reference to a bool object as input, along with a string representing the key to search for.
* If the key is found in the `json_root` member, its boolean representation is assigned to the passed bool reference.
*
* @param input A reference to the bool object to receive the boolean representation of the specified key.
* @param key The key name to search for in the `json_root` member.
* @return 0 if the key was found and its boolean representation was assigned to the passed bool reference, 1 if the key was not found.
* @throws JSON_Loader_NotReady if the JSON data has not yet been populated.
*/
int JSON_Loader::get_bool(bool &input, std::string key)
{
if (!this->populated)
{
throw JSON_Loader_NotReady();
}
if (this->json_root.isMember(key))
{
input = this->json_root[key].asBool();
return 0;
}
this->slog.log_task(E_FATAL, "PARSING", "Failed to find key '" + key + "'.");
return 1;
}
+112
View File
@@ -0,0 +1,112 @@
#ifndef REX_JSON_H
#define REX_JSON_H
#include "jsoncpp/json.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <stdexcept>
#include "../misc/helpers.h"
#include "../logger/Logger.h"
/**
* @class JSON_Loader
* @brief Loads and parses JSON data
*
* This class is responsible for loading and parsing JSON data from a file or a string. It uses the `JsonCpp` library to
* handle the parsing and provides functions to access the data in a safe and convenient way.
*
* @param LOG_LEVEL - The log level to use for logging messages.
*/
class JSON_Loader {
protected:
/// The root of the JSON data
Json::Value json_root;
/// Indicates whether the JSON data has been successfully loaded and parsed
bool populated;
public:
/**
* @brief Constructor for the JSON_Loader class
*
* This constructor initializes the JSON_Loader object and sets the log level to use for logging messages.
*
* @param LOG_LEVEL - The log level to use for logging messages.
*/
JSON_Loader(int LOG_LEVEL);
/**
* @brief Loads JSON data from a file
*
* This function loads JSON data from a file and parses it using the `JsonCpp` library.
*
* @param filename - The path to the file to load the JSON data from.
*/
void load_json_file(std::string filename);
/**
* @brief Loads JSON data from a string
*
* This function parses JSON data from a string using the `JsonCpp` library.
*
* @param input - The string containing the JSON data.
*/
void load_json_string(std::string input);
/**
* @brief Returns the JSON data as a string
*
* This function returns the JSON data in string form.
*
* @return The JSON data as a string.
*/
std::string as_string();
/**
* @brief Gets the value of a key in the JSON data
*
* This function retrieves the value of a key in the JSON data and returns it in a `Json::Value` object. If the
* key does not exist, an error is logged and a default `Json::Value` object is returned.
*
* @param input - A reference to a `Json::Value` object to store the result.
* @param key - The key to retrieve the value for.
*
* @return 0 if the key was found and its value was retrieved successfully, 1 otherwise.
*/
int get_serialized(Json::Value &input, std::string key);
/**
* @brief Gets the string value of a key in the JSON data
*
* This function retrieves the string value of a key in the JSON data and returns it in a `std::string` object.
* If the key does not exist or its value is not a string, an error is logged and a default `std::string` object
* is returned.
*
* @param input - A reference to a `std::string` object to store the result.
* @param key - The key to retrieve the string value for.
*
* @return 0 if the key was found and its string value was retrieved successfully, 1 otherwise.
*/
int get_string(std::string &input, std::string key);
/**
* @brief Gets the boolean value of a key in the JSON data
*
* This function retrieves the boolean value of a key in the JSON data and returns it in a `bool` object. If the
* key does not exist or its value is not a boolean, an error is logged and a default `bool` object is returned.
*
* @param input - A reference to a `bool` object to store the result.
* @param key - The key to retrieve the boolean value for.
*
* @return 0 if the key was found and its boolean value was retrieved successfully, 1 otherwise.
*/
int get_bool(bool &input, std::string key);
private:
/// The logger object used for logging messages
Logger slog;
/// The log level to use for logging messages
int LOG_LEVEL;
};
#endif //REX_JSON_H
+330
View File
@@ -0,0 +1,330 @@
/// Json-cpp amalgated forward header (http://jsoncpp.sourceforge.net/).
/// It is intended to be used with #include "json/json-forwards.h"
/// This header provides forward declaration for all JsonCpp types.
// //////////////////////////////////////////////////////////////////////
// Beginning of content of file: LICENSE
// //////////////////////////////////////////////////////////////////////
/*
The JsonCpp library's source code, including accompanying documentation,
tests and demonstration applications, are licensed under the following
conditions...
The author (Baptiste Lepilleur) explicitly disclaims copyright in all
jurisdictions which recognize such a disclaimer. In such jurisdictions,
this software is released into the Public Domain.
In jurisdictions which do not recognize Public Domain property (e.g. Germany as of
2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur, and is
released under the terms of the MIT License (see below).
In jurisdictions which recognize Public Domain property, the user of this
software may choose to accept it either as 1) Public Domain, 2) under the
conditions of the MIT License (see below), or 3) under the terms of dual
Public Domain/MIT License conditions described here, as they choose.
The MIT License is about as close to Public Domain as a license can get, and is
described in clear, concise terms at:
http://en.wikipedia.org/wiki/MIT_License
The full text of the MIT License follows:
========================================================================
Copyright (c) 2007-2010 Baptiste Lepilleur
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 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.
========================================================================
(END LICENSE TEXT)
The MIT license is compatible with both the GPL and commercial
software, affording one all of the rights of Public Domain with the
minor nuisance of being required to keep the above copyright notice
and license text in the source code. Note also that by accepting the
Public Domain "license" you can re-license your copy using whatever
license you like.
*/
// //////////////////////////////////////////////////////////////////////
// End of content of file: LICENSE
// //////////////////////////////////////////////////////////////////////
#ifndef JSON_FORWARD_AMALGATED_H_INCLUDED
# define JSON_FORWARD_AMALGATED_H_INCLUDED
/// If defined, indicates that the source file is amalgated
/// to prevent private header inclusion.
#define JSON_IS_AMALGAMATION
// //////////////////////////////////////////////////////////////////////
// Beginning of content of file: include/json/config.h
// //////////////////////////////////////////////////////////////////////
// Copyright 2007-2010 Baptiste Lepilleur
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
#ifndef JSON_CONFIG_H_INCLUDED
#define JSON_CONFIG_H_INCLUDED
#include <stddef.h>
#include <string> //typedef String
#include <stdint.h> //typedef int64_t, uint64_t
/// If defined, indicates that json library is embedded in CppTL library.
//# define JSON_IN_CPPTL 1
/// If defined, indicates that json may leverage CppTL library
//# define JSON_USE_CPPTL 1
/// If defined, indicates that cpptl vector based map should be used instead of
/// std::map
/// as Value container.
//# define JSON_USE_CPPTL_SMALLMAP 1
// If non-zero, the library uses exceptions to report bad input instead of C
// assertion macros. The default is to use exceptions.
#ifndef JSON_USE_EXCEPTION
#define JSON_USE_EXCEPTION 1
#endif
/// If defined, indicates that the source file is amalgated
/// to prevent private header inclusion.
/// Remarks: it is automatically defined in the generated amalgated header.
// #define JSON_IS_AMALGAMATION
#ifdef JSON_IN_CPPTL
#include <cpptl/config.h>
#ifndef JSON_USE_CPPTL
#define JSON_USE_CPPTL 1
#endif
#endif
#ifdef JSON_IN_CPPTL
#define JSON_API CPPTL_API
#elif defined(JSON_DLL_BUILD)
#if defined(_MSC_VER) || defined(__MINGW32__)
#define JSON_API __declspec(dllexport)
#define JSONCPP_DISABLE_DLL_INTERFACE_WARNING
#endif // if defined(_MSC_VER)
#elif defined(JSON_DLL)
#if defined(_MSC_VER) || defined(__MINGW32__)
#define JSON_API __declspec(dllimport)
#define JSONCPP_DISABLE_DLL_INTERFACE_WARNING
#endif // if defined(_MSC_VER)
#endif // ifdef JSON_IN_CPPTL
#if !defined(JSON_API)
#define JSON_API
#endif
// If JSON_NO_INT64 is defined, then Json only support C++ "int" type for
// integer
// Storages, and 64 bits integer support is disabled.
// #define JSON_NO_INT64 1
#if defined(_MSC_VER) // MSVC
# if _MSC_VER <= 1200 // MSVC 6
// Microsoft Visual Studio 6 only support conversion from __int64 to double
// (no conversion from unsigned __int64).
# define JSON_USE_INT64_DOUBLE_CONVERSION 1
// Disable warning 4786 for VS6 caused by STL (identifier was truncated to '255'
// characters in the debug information)
// All projects I've ever seen with VS6 were using this globally (not bothering
// with pragma push/pop).
# pragma warning(disable : 4786)
# endif // MSVC 6
# if _MSC_VER >= 1500 // MSVC 2008
/// Indicates that the following function is deprecated.
# define JSONCPP_DEPRECATED(message) __declspec(deprecated(message))
# endif
#endif // defined(_MSC_VER)
// In c++11 the override keyword allows you to explicity define that a function
// is intended to override the base-class version. This makes the code more
// managable and fixes a set of common hard-to-find bugs.
#if __cplusplus >= 201103L
# define JSONCPP_OVERRIDE override
# define JSONCPP_NOEXCEPT noexcept
#elif defined(_MSC_VER) && _MSC_VER > 1600 && _MSC_VER < 1900
# define JSONCPP_OVERRIDE override
# define JSONCPP_NOEXCEPT throw()
#elif defined(_MSC_VER) && _MSC_VER >= 1900
# define JSONCPP_OVERRIDE override
# define JSONCPP_NOEXCEPT noexcept
#else
# define JSONCPP_OVERRIDE
# define JSONCPP_NOEXCEPT throw()
#endif
#ifndef JSON_HAS_RVALUE_REFERENCES
#if defined(_MSC_VER) && _MSC_VER >= 1600 // MSVC >= 2010
#define JSON_HAS_RVALUE_REFERENCES 1
#endif // MSVC >= 2010
#ifdef __clang__
#if __has_feature(cxx_rvalue_references)
#define JSON_HAS_RVALUE_REFERENCES 1
#endif // has_feature
#elif defined __GNUC__ // not clang (gcc comes later since clang emulates gcc)
#if defined(__GXX_EXPERIMENTAL_CXX0X__) || (__cplusplus >= 201103L)
#define JSON_HAS_RVALUE_REFERENCES 1
#endif // GXX_EXPERIMENTAL
#endif // __clang__ || __GNUC__
#endif // not defined JSON_HAS_RVALUE_REFERENCES
#ifndef JSON_HAS_RVALUE_REFERENCES
#define JSON_HAS_RVALUE_REFERENCES 0
#endif
#ifdef __clang__
#elif defined __GNUC__ // not clang (gcc comes later since clang emulates gcc)
# if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
# define JSONCPP_DEPRECATED(message) __attribute__ ((deprecated(message)))
# elif (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
# define JSONCPP_DEPRECATED(message) __attribute__((__deprecated__))
# endif // GNUC version
#endif // __clang__ || __GNUC__
#if !defined(JSONCPP_DEPRECATED)
#define JSONCPP_DEPRECATED(message)
#endif // if !defined(JSONCPP_DEPRECATED)
#if __GNUC__ >= 6
# define JSON_USE_INT64_DOUBLE_CONVERSION 1
#endif
#if !defined(JSON_IS_AMALGAMATION)
# include "version.h"
# if JSONCPP_USING_SECURE_MEMORY
# include "allocator.h" //typedef Allocator
# endif
#endif // if !defined(JSON_IS_AMALGAMATION)
namespace Json {
typedef int Int;
typedef unsigned int UInt;
#if defined(JSON_NO_INT64)
typedef int LargestInt;
typedef unsigned int LargestUInt;
#undef JSON_HAS_INT64
#else // if defined(JSON_NO_INT64)
// For Microsoft Visual use specific types as long long is not supported
#if defined(_MSC_VER) // Microsoft Visual Studio
typedef __int64 Int64;
typedef unsigned __int64 UInt64;
#else // if defined(_MSC_VER) // Other platforms, use long long
typedef int64_t Int64;
typedef uint64_t UInt64;
#endif // if defined(_MSC_VER)
typedef Int64 LargestInt;
typedef UInt64 LargestUInt;
#define JSON_HAS_INT64
#endif // if defined(JSON_NO_INT64)
#if JSONCPP_USING_SECURE_MEMORY
#define JSONCPP_STRING std::basic_string<char, std::char_traits<char>, Json::SecureAllocator<char> >
#define JSONCPP_OSTRINGSTREAM std::basic_ostringstream<char, std::char_traits<char>, Json::SecureAllocator<char> >
#define JSONCPP_OSTREAM std::basic_ostream<char, std::char_traits<char>>
#define JSONCPP_ISTRINGSTREAM std::basic_istringstream<char, std::char_traits<char>, Json::SecureAllocator<char> >
#define JSONCPP_ISTREAM std::istream
#else
#define JSONCPP_STRING std::string
#define JSONCPP_OSTRINGSTREAM std::ostringstream
#define JSONCPP_OSTREAM std::ostream
#define JSONCPP_ISTRINGSTREAM std::istringstream
#define JSONCPP_ISTREAM std::istream
#endif // if JSONCPP_USING_SECURE_MEMORY
} // end namespace Json
#endif // JSON_CONFIG_H_INCLUDED
// //////////////////////////////////////////////////////////////////////
// End of content of file: include/json/config.h
// //////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////
// Beginning of content of file: include/json/forwards.h
// //////////////////////////////////////////////////////////////////////
// Copyright 2007-2010 Baptiste Lepilleur
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
#ifndef JSON_FORWARDS_H_INCLUDED
#define JSON_FORWARDS_H_INCLUDED
#if !defined(JSON_IS_AMALGAMATION)
#include "config.h"
#endif // if !defined(JSON_IS_AMALGAMATION)
namespace Json {
// writer.h
class FastWriter;
class StyledWriter;
// reader.h
class Reader;
// features.h
class Features;
// value.h
typedef unsigned int ArrayIndex;
class StaticString;
class Path;
class PathArgument;
class Value;
class ValueIteratorBase;
class ValueIterator;
class ValueConstIterator;
} // namespace Json
#endif // JSON_FORWARDS_H_INCLUDED
// //////////////////////////////////////////////////////////////////////
// End of content of file: include/json/forwards.h
// //////////////////////////////////////////////////////////////////////
#endif //ifndef JSON_FORWARD_AMALGATED_H_INCLUDED
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff