-
Notifications
You must be signed in to change notification settings - Fork 1
/
Config.cpp
109 lines (104 loc) · 3.85 KB
/
Config.cpp
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
#include <sstream>
#include <fstream>
#include <stdexcept>
#include "Config.h"
void AppConfigData::ReadOptions( char *mbstring ) {
std::string line;
std::ifstream cfgfile( mbstring );
for( std::string line; std::getline( cfgfile, line ); ) {
std::istringstream iss( line );
std::string id, eq, val;
bool error = false;
if( !( iss >> id ) ) {
error = true;
} else if( id[0] == '#' ) {
continue;
} else if( !( iss >> eq >> val >> std::ws ) || eq != "=" || iss.get() != EOF ) {
error = true;
}
if( error ) {
// do something appropriate: throw, skip, warn, etc.
} else {
Options[id] = val;
}
}
}
void AppConfigData::Save( void ) {
// Save will rewrite the whole file.
// Comments are all lost
// This method needs a redesign
if( ! ReadOnly() ) {
wxString wxsTemp;
std::ofstream filestr;
char mbstring[513];
wcstombs( mbstring, wxsConfigPath.wc_str(), 512 );
filestr.open( mbstring );
if( filestr.is_open() ) {
wxsTemp = _( "Load1 = " ) + _( Options["Load1"].c_str() ) + _( "\n" );
filestr.write( wxsTemp.c_str(), wxsTemp.size() );
wxsTemp = _( "Load2 = " ) + _( Options["Load2"].c_str() ) + _( "\n" );
filestr.write( wxsTemp.c_str(), wxsTemp.size() );
wxsTemp = _( "Latest1 = " ) + _( Options["Latest1"].c_str() ) + _( "\n" );
filestr.write( wxsTemp.c_str(), wxsTemp.size() );
wxsTemp = _( "Latest2 = " ) + _( Options["Latest2"].c_str() ) + _( "\n" );
filestr.write( wxsTemp.c_str(), wxsTemp.size() );
filestr.close();
} else {
if( wxsConfigPath == _( "" ) ) {
wxsTemp = _( "mkdir " );
wxsTemp += wxsConfigDirCandidate;
wcstombs( mbstring, wxsTemp.wc_str(), 512 );
system( mbstring );
wxsConfigPath = wxsConfigPathCandidate;
wcstombs( mbstring, wxsConfigPath.wc_str(), 512 );
std::ofstream output( mbstring, std::ios::out | std::ios::app );
if( output.is_open() ) {
output.close();
}
Save();
}
}
}
}
AppConfigData::AppConfigData( const char *sAppName ) {
bConfigPath = false;
std::ifstream filestr;
wxString wxsTempFile = _( "/etc/" ) + _( sAppName ) + _( ".conf" );
char mbstring[513];
wcstombs( mbstring, wxsTempFile.wc_str(), 512 );
filestr.open( mbstring );
if( filestr.is_open() ) {
wxsConfigPath = wxsTempFile;
bReadOnly = true; // with a .conf file in /etc/ the user cannot change the port, he'll need root assistance
filestr.close();
ReadOptions( mbstring );
} else {
#if defined(__WXMSW__)
wxString wxsHome = _( getenv( "APPDATA" ) );
wxString wxsTempFile = _( "~/" ) + _(sAppName) + _( "/" );
wxsTempFile.Replace( _( "~" ), wxsHome, false );
wxsConfigDirCandidate = wxsTempFile;
wxsTempFile += _( "/" ) + _(sAppName) + _( ".ini" );
#elif defined(__UNIX__)
wxString wxsHome = _( getenv( "HOME" ) );
wxString wxsTempFile = _( "~/.config/" ) + _( sAppName );
wxsTempFile.Replace( _( "~" ), wxsHome, false );
wxsConfigDirCandidate = wxsTempFile;
wxsTempFile += _( "/" ) + _( sAppName ) + _( ".conf" );
#endif
wcstombs( mbstring, wxsTempFile.wc_str(), 512 );
wxsConfigPathCandidate = wxsTempFile;
filestr.open( mbstring );
if( filestr.is_open() ) {
wxsConfigPath = wxsTempFile;
filestr.close();
ReadOptions( mbstring );
}
}
}
wxString AppConfigData::ConfigPath( void ) {
return wxsConfigPath;
}
bool AppConfigData::ReadOnly( void ) {
return bReadOnly;
}