|
| 1 | +/* Main Loop for cataclysm |
| 2 | + * Linux only I guess |
| 3 | + * But maybe not |
| 4 | + * Who knows |
| 5 | + */ |
| 6 | +#include <sys/stat.h> |
| 7 | +#include <dirent.h> |
| 8 | +#include <clocale> |
| 9 | +#include <cstdio> |
| 10 | +#include <cstring> // for strcmp |
| 11 | +#include <stack> // for stack (obviously) |
| 12 | +#include <string> |
| 13 | +#include <vector> |
| 14 | +#include <fstream> |
| 15 | +#include <sstream> // for throwing errors |
| 16 | +#include <exception> |
| 17 | +#include <iterator> |
| 18 | +#include <stdexcept> |
| 19 | + |
| 20 | +#include "json.h" |
| 21 | + |
| 22 | +// copypasta: file_finder.cpp |
| 23 | +static std::vector<std::string> get_files_from_path( std::string extension, std::string root_path, |
| 24 | + bool recursive_search, bool match_extension ) |
| 25 | +{ |
| 26 | + std::vector<std::string> files; |
| 27 | + const size_t extsz = extension.size(); |
| 28 | + const char *c_extension = extension.c_str(); |
| 29 | + // test for empty root path |
| 30 | + if( root_path.empty() ) { |
| 31 | + root_path = "."; |
| 32 | + } |
| 33 | + |
| 34 | + std::stack<std::string> directories, tempstack; |
| 35 | + directories.push( root_path ); |
| 36 | + std::string path; |
| 37 | + |
| 38 | + while( !directories.empty() ) { |
| 39 | + path = directories.top(); |
| 40 | + directories.pop(); |
| 41 | + |
| 42 | + DIR *root = opendir( path.c_str() ); |
| 43 | + |
| 44 | + if( root ) { |
| 45 | + struct dirent *root_file; |
| 46 | + struct stat _buff; |
| 47 | + DIR *subdir; |
| 48 | + |
| 49 | + while( ( root_file = readdir( root ) ) ) { |
| 50 | + // check to see if it is a folder! |
| 51 | + if( stat( root_file->d_name, &_buff ) != 0x4 ) { |
| 52 | + // ignore '.' and '..' folder names, which are current and parent folder relative paths |
| 53 | + if( ( strcmp( root_file->d_name, "." ) != 0 ) && ( strcmp( root_file->d_name, ".." ) != 0 ) ) { |
| 54 | + std::string subpath = path + "/" + root_file->d_name; |
| 55 | + |
| 56 | + if( recursive_search ) { |
| 57 | + subdir = opendir( subpath.c_str() ); |
| 58 | + if( subdir ) { |
| 59 | + tempstack.push( subpath ); |
| 60 | + closedir( subdir ); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + // check to see if it is a file with the appropriate extension |
| 66 | + std::string tmp = root_file->d_name; |
| 67 | + if( tmp.find( c_extension, match_extension ? tmp.size() - extsz : 0 ) != std::string::npos ) { |
| 68 | + std::string fullpath = path + "/" + tmp; |
| 69 | + files.push_back( fullpath ); |
| 70 | + } |
| 71 | + } |
| 72 | + closedir( root ); |
| 73 | + } |
| 74 | + // Directories are added to tempstack in A->Z order, which makes them pop from Z->A. This Makes sure that directories are |
| 75 | + // searched in the proper order and that the final output is in the proper order. |
| 76 | + while( !tempstack.empty() ) { |
| 77 | + directories.push( tempstack.top() ); |
| 78 | + tempstack.pop(); |
| 79 | + } |
| 80 | + } |
| 81 | + return files; |
| 82 | +} |
| 83 | + |
| 84 | +// copypasta: init.cpp |
| 85 | +static void load_object( JsonObject &jo ) |
| 86 | +{ |
| 87 | + std::string type = jo.get_string( "type" ); |
| 88 | + if( !jo.has_string( "type" ) ) { |
| 89 | + jo.throw_error( "JSON object has no type" ); |
| 90 | + } |
| 91 | +} |
| 92 | +static void load_all_from_json( JsonIn &jsin ) |
| 93 | +{ |
| 94 | + char ch; |
| 95 | + jsin.eat_whitespace(); |
| 96 | + // examine first non-whitespace char |
| 97 | + ch = jsin.peek(); |
| 98 | + if( ch == '{' ) { |
| 99 | + // find type and dispatch single object |
| 100 | + JsonObject jo = jsin.get_object(); |
| 101 | + load_object( jo ); |
| 102 | + jo.finish(); |
| 103 | + // if there's anything else in the file, it's an error. |
| 104 | + jsin.eat_whitespace(); |
| 105 | + if( jsin.good() ) { |
| 106 | + std::stringstream err; |
| 107 | + err << "expected single-object file but found '"; |
| 108 | + err << jsin.peek() << "'"; |
| 109 | + jsin.error( err.str() ); |
| 110 | + } |
| 111 | + } else if( ch == '[' ) { |
| 112 | + jsin.start_array(); |
| 113 | + // find type and dispatch each object until array close |
| 114 | + while( !jsin.end_array() ) { |
| 115 | + jsin.eat_whitespace(); |
| 116 | + ch = jsin.peek(); |
| 117 | + if( ch != '{' ) { |
| 118 | + std::stringstream err; |
| 119 | + err << "expected array of objects but found '"; |
| 120 | + err << ch << "', not '{'"; |
| 121 | + jsin.error( err.str() ); |
| 122 | + } |
| 123 | + JsonObject jo = jsin.get_object(); |
| 124 | + load_object( jo ); |
| 125 | + jo.finish(); |
| 126 | + } |
| 127 | + } else { |
| 128 | + // not an object or an array? |
| 129 | + std::stringstream err; |
| 130 | + err << "expected object or array, but found '" << ch << "'"; |
| 131 | + jsin.error( err.str() ); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +static void load_json_dir( const std::string &dirname ) |
| 136 | +{ |
| 137 | + // get a list of all files in the directory |
| 138 | + std::vector<std::string> dir = |
| 139 | + get_files_from_path( ".json", dirname, true, true ); |
| 140 | + // iterate over each file |
| 141 | + std::vector<std::string>::iterator it; |
| 142 | + for( it = dir.begin(); it != dir.end(); it++ ) { |
| 143 | + // open the file as a stream |
| 144 | + std::ifstream infile( it->c_str(), std::ifstream::in | std::ifstream::binary ); |
| 145 | + // and stuff it into ram |
| 146 | + std::istringstream iss( |
| 147 | + std::string( |
| 148 | + ( std::istreambuf_iterator<char>( infile ) ), |
| 149 | + std::istreambuf_iterator<char>() |
| 150 | + ) |
| 151 | + ); |
| 152 | + infile.close(); |
| 153 | + // parse it |
| 154 | + try { |
| 155 | + JsonIn jsin( iss ); |
| 156 | + load_all_from_json( jsin ); |
| 157 | + } catch( const JsonError &err ) { |
| 158 | + throw std::runtime_error( *( it ) + ": " + err.what() ); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +int main( int, char ** ) |
| 164 | +{ |
| 165 | + setlocale( LC_ALL, "" ); |
| 166 | + try { |
| 167 | + load_json_dir( "data/json" ); |
| 168 | + } catch( const std::exception &err ) { |
| 169 | + printf( "Error: %s\n", err.what() ); |
| 170 | + return 1; |
| 171 | + } |
| 172 | + return 0; |
| 173 | +} |
0 commit comments