summaryrefslogtreecommitdiff
path: root/binary_config
diff options
context:
space:
mode:
Diffstat (limited to 'binary_config')
-rw-r--r--binary_config/binary_config.cpp119
-rwxr-xr-xbinary_config/compile.sh5
2 files changed, 124 insertions, 0 deletions
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 <iostream>
+#include <fstream>
+#include <cstdio>
+#include <climits>
+#include <cstdint>
+#include <cstring>
+#include <errno.h>
+#include <unistd.h>
+#include <readline/readline.h>
+
+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 );
+}
diff --git a/binary_config/compile.sh b/binary_config/compile.sh
new file mode 100755
index 0000000..229d6e7
--- /dev/null
+++ b/binary_config/compile.sh
@@ -0,0 +1,5 @@
+#!/usr/bin/env bash
+
+echo 'Compiling..'
+/usr/bin/g++ -Wall -pedantic -Werror binary_config.cpp -o binary_config -lreadline
+echo 'Finished compiling'