#include #include #include #include #include using namespace RAT; #include #include #include using namespace std; void DBCommandLoader::LoadCommands( const DS::Meta& meta ) { for( size_t pass = 0; pass < meta.GetPassCount(); pass++ ) { const DS::MetaDB& metaDB = meta.GetMetaDB( pass ); for( size_t command = 0; command < metaDB.GetOverrideCommandCount(); command++ ) { pair fullCommand = metaDB.GetOverrideCommand( command ); LoadCommand( fullCommand.first, fullCommand.second ); } for( size_t file = 0; file < metaDB.GetFileCount(); file++ ) LoadFile( metaDB.GetFile( file ) ); } } void DBCommandLoader::LoadFile( const std::string& contents ) { // Write out a temporary file and load it char path[] = "temp.ratdb.XXXXXX"; mkstemp( path ); ofstream file( path, ios::out ); file << contents; file.close(); DB::Get()->LoadFile( path ); remove( path ); } void DBCommandLoader::LoadCommand( const std::string& tableField, const std::string& value ) { istringstream args( tableField ); string tbl_descriptor, field_descriptor; args >> tbl_descriptor >> field_descriptor; string table; string index; string field; int arrayindex; if (DB::ParseTableName(tbl_descriptor, table, index) && DB::ParseFieldName(field_descriptor, field, arrayindex)) { json::Value parsed; json::Reader jin(value); try { if (!jin.getValue(parsed)) { Log::Die("DBMesssenger: Could not parse JSON input while setting " + table + "[" + index + "]" + " " + field + " to " + value); } } catch (json::parser_error &err) { Log::Die("DBMesssenger: Failed parsing JSON input while setting " + table + "[" + index + "]" + " " + field + ": " + string(err.what())); } if (arrayindex < 0) { // Setting field to a value DB::Get()->Set(table, index, field, parsed); info << "DB: Setting " << table << "[" << index << "]" << " " << field << " to "; } else { // Setting an index in an array to a value DB::Get()->SetValInArray(table, index, field, arrayindex, parsed); info << "DB: Setting " << table << "[" << index << "]" << " " << field << "[" << arrayindex << "] to "; } stringstream str; json::Writer jout(str); jout.putValue(parsed); info << str.str() << newline; } else { if(!DB::ParseTableName(tbl_descriptor, table, index)) Log::Die("DBMesssenger: Malformed table name: " + tbl_descriptor); if(!DB::ParseFieldName(field_descriptor,field, arrayindex)) Log::Die("DBMesssenger: Malformed field name: " + field_descriptor); } }