Skip to content

Commit

Permalink
make linter work
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGoatGod committed Sep 21, 2020
1 parent 32baad3 commit cacc180
Show file tree
Hide file tree
Showing 25 changed files with 12,114 additions and 0 deletions.
568 changes: 568 additions & 0 deletions Src/cata_utility.h

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions Src/chkjson/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# JSON check utility
cmake_minimum_required(VERSION 2.8.12)

SET (CHKJSON_SOURCES
${CMAKE_SOURCE_DIR}/src/json.cpp
${CMAKE_SOURCE_DIR}/src/chkjson/chkjson.cpp
)

SET (CHKJSON_HEADERS
${CMAKE_SOURCE_DIR}/src/json.h
)

# test chain
add_custom_target (
check_json
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)

add_custom_command (
TARGET check_json
PRE_BUILD
COMMAND chkjson
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)

INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/src/chkjson )

# Add the actual executable
IF(WIN32)
ADD_EXECUTABLE(chkjson WIN32 EXCLUDE_FROM_ALL ${CHKJSON_SOURCES} ${CHKJSON_HEADERS})
ELSE(WIN32)
ADD_EXECUTABLE(chkjson EXCLUDE_FROM_ALL ${CHKJSON_SOURCES} ${CHKJSON_HEADERS})
ENDIF(WIN32)
173 changes: 173 additions & 0 deletions Src/chkjson/chkjson.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/* Main Loop for cataclysm
* Linux only I guess
* But maybe not
* Who knows
*/
#include <sys/stat.h>
#include <dirent.h>
#include <clocale>
#include <cstdio>
#include <cstring> // for strcmp
#include <stack> // for stack (obviously)
#include <string>
#include <vector>
#include <fstream>
#include <sstream> // for throwing errors
#include <exception>
#include <iterator>
#include <stdexcept>

#include "json.h"

// copypasta: file_finder.cpp
static std::vector<std::string> get_files_from_path( std::string extension, std::string root_path,
bool recursive_search, bool match_extension )
{
std::vector<std::string> files;
const size_t extsz = extension.size();
const char *c_extension = extension.c_str();
// test for empty root path
if( root_path.empty() ) {
root_path = ".";
}

std::stack<std::string> directories, tempstack;
directories.push( root_path );
std::string path;

while( !directories.empty() ) {
path = directories.top();
directories.pop();

DIR *root = opendir( path.c_str() );

if( root ) {
struct dirent *root_file;
struct stat _buff;
DIR *subdir;

while( ( root_file = readdir( root ) ) ) {
// check to see if it is a folder!
if( stat( root_file->d_name, &_buff ) != 0x4 ) {
// ignore '.' and '..' folder names, which are current and parent folder relative paths
if( ( strcmp( root_file->d_name, "." ) != 0 ) && ( strcmp( root_file->d_name, ".." ) != 0 ) ) {
std::string subpath = path + "/" + root_file->d_name;

if( recursive_search ) {
subdir = opendir( subpath.c_str() );
if( subdir ) {
tempstack.push( subpath );
closedir( subdir );
}
}
}
}
// check to see if it is a file with the appropriate extension
std::string tmp = root_file->d_name;
if( tmp.find( c_extension, match_extension ? tmp.size() - extsz : 0 ) != std::string::npos ) {
std::string fullpath = path + "/" + tmp;
files.push_back( fullpath );
}
}
closedir( root );
}
// Directories are added to tempstack in A->Z order, which makes them pop from Z->A. This Makes sure that directories are
// searched in the proper order and that the final output is in the proper order.
while( !tempstack.empty() ) {
directories.push( tempstack.top() );
tempstack.pop();
}
}
return files;
}

// copypasta: init.cpp
static void load_object( JsonObject &jo )
{
std::string type = jo.get_string( "type" );
if( !jo.has_string( "type" ) ) {
jo.throw_error( "JSON object has no type" );
}
}
static void load_all_from_json( JsonIn &jsin )
{
char ch;
jsin.eat_whitespace();
// examine first non-whitespace char
ch = jsin.peek();
if( ch == '{' ) {
// find type and dispatch single object
JsonObject jo = jsin.get_object();
load_object( jo );
jo.finish();
// if there's anything else in the file, it's an error.
jsin.eat_whitespace();
if( jsin.good() ) {
std::stringstream err;
err << "expected single-object file but found '";
err << jsin.peek() << "'";
jsin.error( err.str() );
}
} else if( ch == '[' ) {
jsin.start_array();
// find type and dispatch each object until array close
while( !jsin.end_array() ) {
jsin.eat_whitespace();
ch = jsin.peek();
if( ch != '{' ) {
std::stringstream err;
err << "expected array of objects but found '";
err << ch << "', not '{'";
jsin.error( err.str() );
}
JsonObject jo = jsin.get_object();
load_object( jo );
jo.finish();
}
} else {
// not an object or an array?
std::stringstream err;
err << "expected object or array, but found '" << ch << "'";
jsin.error( err.str() );
}
}

static void load_json_dir( const std::string &dirname )
{
// get a list of all files in the directory
std::vector<std::string> dir =
get_files_from_path( ".json", dirname, true, true );
// iterate over each file
std::vector<std::string>::iterator it;
for( it = dir.begin(); it != dir.end(); it++ ) {
// open the file as a stream
std::ifstream infile( it->c_str(), std::ifstream::in | std::ifstream::binary );
// and stuff it into ram
std::istringstream iss(
std::string(
( std::istreambuf_iterator<char>( infile ) ),
std::istreambuf_iterator<char>()
)
);
infile.close();
// parse it
try {
JsonIn jsin( iss );
load_all_from_json( jsin );
} catch( const JsonError &err ) {
throw std::runtime_error( *( it ) + ": " + err.what() );
}
}
}

int main( int, char ** )
{
setlocale( LC_ALL, "" );
try {
load_json_dir( "data/json" );
} catch( const std::exception &err ) {
printf( "Error: %s\n", err.what() );
return 1;
}
return 0;
}
Loading

0 comments on commit cacc180

Please sign in to comment.