summaryrefslogtreecommitdiff
path: root/binary_config/binary_config.cpp
blob: 1acbd3d57d2e2415c589d0ecb092fa9983c6e6a7 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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 );
}