readded confs, some basic exception handling, and reworking conf now

This commit is contained in:
Chris Punches
2017-06-11 15:14:45 -04:00
parent 82d768e3c1
commit b36e6acabd
11 changed files with 142 additions and 74 deletions

View File

@@ -1,17 +1,32 @@
//
// Created by phanes on 4/22/17.
//
#include "JSON_Loader.h"
#include "helpers.h"
#include <stdexcept>
class JSON_Loader_NotReady: public std::runtime_error
{
public:
JSON_Loader_NotReady(): std::runtime_error("JSON_Loader: Tried to access JSON without actually populating JSON.") {}
};
class JSON_Loader_FileNotFound: public std::runtime_error
{
public:
JSON_Loader_FileNotFound(): std::runtime_error("JSON_Loader: The requested file could not be found.") {}
};
class JSON_Loader_InvalidJSON: public std::runtime_error
{
public:
JSON_Loader_InvalidJSON(): std::runtime_error("JSON_Loader: The JSON provided could not be parsed.") {}
};
JSON_Loader::JSON_Loader()
{
// empty
this->populated = false;
}
// loads json from a file into a deserializable type and sets to private member json_root
int JSON_Loader::load_json_file( std::string filename, bool verbose )
void JSON_Loader::load_json_file( std::string filename, bool verbose )
{
// reads from a file into a Json::Value type.
Json::Reader json_reader;
@@ -22,9 +37,8 @@ int JSON_Loader::load_json_file( std::string filename, bool verbose )
// first, check if the file exists
if (! exists( filename ) )
{
std::cerr << "File '" << filename << "' does not exist.";
// exit with failure signal
return 1;
std::cerr << "File '" << filename << "' does not exist." << std::endl;
throw JSON_Loader_FileNotFound();
}
// create the ifstream file handle
@@ -36,8 +50,8 @@ int JSON_Loader::load_json_file( std::string filename, bool verbose )
if (! parsingSuccessful )
{
std::cerr << "Failed to parse '" << filename << "':\n\t" << json_reader.getFormattedErrorMessages();
// exit with failure signal
return 1;
throw JSON_Loader_InvalidJSON();
} else {
// if in verbose mode, give the user an "it worked" message
if (verbose)
@@ -45,12 +59,12 @@ int JSON_Loader::load_json_file( std::string filename, bool verbose )
std::cout << "Parsed '" << filename << "' with " << this->json_root.size() << " element(s)." << std::endl;
}
}
// exit successfully
return 0;
// exit successfully and set flag that this can be used now.
this->populated = true;
}
// loads json from std::string into a serialized type and sets to private member json_root
int JSON_Loader::load_json_string( std::string input, bool verbose )
void JSON_Loader::load_json_string( std::string input, bool verbose )
{
// reads from a string into a Json::Value type.
Json::Reader json_reader;
@@ -67,30 +81,34 @@ int JSON_Loader::load_json_string( std::string input, bool verbose )
if (! parsingSuccessful )
{
std::cerr << "Failed to parse adhoc JSON value." << std::endl << input << std::endl << std::endl << json_reader.getFormattedErrorMessages();
// exit with failure signal
return 1;
throw JSON_Loader_InvalidJSON();
} else {
// if in verbose mode, give the user an "it worked" message
if ( verbose )
{
std::cout << "Successfully parsed JSON string with " << this->json_root.size() << " elements. Value:" << std::endl;
std::cout << input << std::endl;
std::cout << input << std::endl << std::endl;
}
}
// exit successfully
return 0;
this->populated = true;
}
// returns the serialized representation of json_root
Json::Value JSON_Loader::as_serialized()
{
if ( ! this->populated ) { throw JSON_Loader_NotReady(); }
return this->json_root;
}
// returns the string representation of json_root
std::string JSON_Loader::as_string()
{
if ( ! this->populated ) { throw JSON_Loader_NotReady(); }
return this->json_root.asString();
}
@@ -98,29 +116,25 @@ std::string JSON_Loader::as_string()
// the Jason::Value object to assign the fetched value to
// verbosity flag
// exit or not on failure
int JSON_Loader::get_key( Json::Value &input, std::string key, bool verbose, bool safety)
int JSON_Loader::get_key( Json::Value &input, std::string key, bool verbose)
{
// throw if the class is not ready to be used.
if ( ! this->populated ) { throw JSON_Loader_NotReady(); }
if ( this->json_root.isMember( key ) )
{
// key was found so return it
// key was found so return it to the passed input ref
input = this->json_root[ key ];
return 0;
} else {
// key was not found
if ( verbose )
{
// verbose mode tells the user what key we were looking for.
std::cerr << "Failed to find key '" << key << "'." << std::endl;
return 1;
}
if ( ! safety )
{
// if we're not ignoring fatal errors
std::cout << "Exiting." << std::endl;
// exit code for failure
// this should probably be a 'throw'....
exit(1);
}
}
// key was not found
// shouldn't the be handled further up?
if ( verbose )
{
// verbose mode tells the user what key we were looking for.
std::cerr << "Failed to find key '" << key << "'." << std::endl;
}
// exit code for failure
return 1;
}