From ab607fc39b6dfc766f7481c33e5f1cf35a2f55d9 Mon Sep 17 00:00:00 2001 From: stderr64 Date: Thu, 24 Oct 2024 23:04:43 +0300 Subject: Recreated repository --- binary_config/binary_config.cpp | 119 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 binary_config/binary_config.cpp (limited to 'binary_config/binary_config.cpp') diff --git a/binary_config/binary_config.cpp b/binary_config/binary_config.cpp new file mode 100644 index 0000000..1acbd3d --- /dev/null +++ b/binary_config/binary_config.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct config_data{ + unsigned char testvalue1[255]; + unsigned char testvalue2[255]; +}; + +struct config_data g_config = {}; + +bool write_test_config( std::fstream *cfg_file, struct config_data *cdata ){ + if ( cfg_file == NULL || !cfg_file->is_open() ) + return false; + if ( cdata == NULL ) + return false; + char *tmp_copy = (char*)calloc( sizeof(*cdata), 1 ); + if ( tmp_copy == NULL ) + return false; + std::cout << "Allocated memory for writing config: " << (sizeof(*cdata) * 1) << " bytes\n"; + memcpy( (void*)tmp_copy, (const void*)cdata, sizeof(*cdata) ); + cfg_file->write( tmp_copy, sizeof(*cdata) ); + cfg_file->flush(); + free( tmp_copy ); + return true; +} + +void print_config( std::fstream *cfg, struct config_data *cdt ){ + if ( !cfg->is_open() ) + return; + if ( cdt == NULL ) + return; + size_t cfg_size = 0; + cfg->seekg( 0, std::ios::end ); + cfg_size = (size_t)cfg->tellg(); + cfg->seekg( 0, std::ios::beg ); + if ( cfg_size < sizeof(*cdt) || cfg_size > sizeof(*cdt) ){ + std::cerr << "Error: config file isn't valid\n"; + return; + } + char *cbytes = (char*)calloc( cfg_size, sizeof(char) ); + if ( cbytes == NULL ) + return; + std::cout << "Allocated memory for reading config: " << (cfg_size * sizeof(char)) << " bytes\n"; + cfg->read( cbytes, cfg_size ); + memcpy( (void*)cdt, (const void*)cbytes, cfg_size ); + cfg_size = 0; + free( cbytes ); + std::cout << "testvalue1 = " << cdt->testvalue1 << "\ntestvalue2 = " << cdt->testvalue2 << "\n"; + return; +} + +int main( int argc, char *args[] ){ + if ( argc <= 1 || args[1] == NULL ){ + std::cout << "Usage: " << args[0] << " [config file]\n"; + exit( EXIT_SUCCESS ); + } + memset( (void*)&g_config, 0, sizeof(struct config_data) ); + std::cout << "Opening test config file " << args[1] << "\n"; + std::fstream *ocfg = new std::fstream( args[1], std::ios::in | std::ios::out | std::ios::binary | std::ios::app ); + if ( !ocfg->good() ){ + std::cerr << "Failed to open given config file (" << strerror(errno) << ")\n"; + exit( EXIT_FAILURE ); + } + std::cout << "Opened test config file " << args[1] << "\n"; + char *tv1 = readline( "Test value 1: " ); + if ( tv1 == NULL ){ + std::cerr << "Error: no value given\n"; + ocfg->close(); + exit( EXIT_FAILURE ); + } + if ( (size_t)strlen(tv1) > 255 ){ + std::cerr << "Error: test value 1 is too long\n"; + free( tv1 ); + ocfg->close(); + exit( EXIT_FAILURE ); + } + memcpy( (void*)&g_config.testvalue1, (const void*)tv1, strlen(tv1) ); + free( tv1 ); + char *tv2 = readline( "Test value 2: " ); + if ( tv2 == NULL ){ + std::cerr << "Error: no value given\n"; + ocfg->close(); + exit( EXIT_FAILURE ); + } + if ( (size_t)strlen(tv2) > 255 ){ + std::cerr << "Error: test value 2 is too long\n"; + ocfg->close(); + free( tv2 ); + exit( EXIT_FAILURE ); + } + memcpy( (void*)&g_config.testvalue2, (const void*)tv2, strlen(tv2) ); + free( tv2 ); + std::cout << "Writing test config file\n"; + if ( !write_test_config(ocfg, &g_config) ){ + std::cerr << "Error: failed to write test config\n"; + ocfg->close(); + exit( EXIT_FAILURE ); + } + std::cout << "Test config written\n"; + std::cout << "Clearing config from memory before reading it back\n"; + memset( (void*)&g_config, 0, sizeof(g_config) ); + std::cout << "Config cleared from memory\n"; + std::cout << "Printing out the test config by reading it from written file\n"; + print_config( ocfg, &g_config ); + std::cout << "Clearing config from memory\n"; + memset( (void*)&g_config, 0, sizeof(g_config) ); + std::cout << "Config cleared from memory\n"; + std::cout << "Closing config file\n"; + ocfg->close(); + std::cout << "Config closed, exiting\n"; + exit( EXIT_SUCCESS ); +} -- cgit v1.2.3-86-g962b