23 #include <sys/types.h>
27 #include <sys/statvfs.h>
37 if ( matchType & regexMatcher::units ) {
39 long long intval = 0LL;
43 if ( matchType & regexMatcher::floating ) {
45 dblval = splitted.first;
46 unit = splitted.second;
50 intval = splitted.first;
51 unit = splitted.second;
54 if ( unit ==
"b" || unit ==
"B" ) {
56 }
else if ( unit ==
"kb" || unit ==
"kB" || unit ==
"Kb" || unit ==
"KB" ) {
58 }
else if ( unit ==
"mb" || unit ==
"Mb" || unit ==
"MB" ) {
60 }
else if ( unit ==
"gb" || unit ==
"Gb" || unit ==
"GB" ) {
66 value *= ( ( matchType & regexMatcher::floating ) ? dblval : intval );
68 value = atoll ( str.c_str() );
75 value = ! ( str ==
"false" || str ==
"False" || str ==
"FALSE" || str ==
"0" );
81 if ( str ==
"fixed" ) {
82 value = swapPolicy::fixed;
83 }
else if ( str ==
"autoextendable" ) {
84 value = swapPolicy::autoextendable;
85 }
else if ( str ==
"interactive" ) {
86 value = swapPolicy::interactive;
88 value = swapPolicy::autoextendable;
92 configuration::configuration() : memoryManager (
"memoryManager",
"cyclicManagedMemory",
regexMatcher::text ),
95 swapfiles (
"swapfiles",
"rambrainswap-%d-%d",
regexMatcher::swapfilename ),
111 ifstream meminfo (
"/proc/meminfo", ifstream::in );
113 for (
int c = 0; c < 3; ++c ) {
114 meminfo.getline ( line, 1024 );
119 char *begin = NULL, *end = NULL;
122 if ( *pos <= '9' && *pos >=
'0' ) {
123 if ( begin == NULL ) {
127 if ( begin != NULL ) {
133 * ( end + 1 ) = 0x00;
134 bytes_avail = atol ( begin ) * 1024;
142 ret = readlink (
"/proc/self/exe", exe,
sizeof ( exe ) - 1 );
145 struct statvfs stats;
146 statvfs ( exe, &stats );
163 vector<configLineBase *> readLines;
166 for (
int i = 0; i < 3; ++i ) {
179 for (
int i = 0; i < 3; ++i ) {
184 bool readAConfig =
false;
188 infomsgf (
"Rambrain is initialized using custom config file: %s\n",
customConfigPath.c_str() );
193 infomsgf (
"Rambrain is initialized using user's config file: %s\n",
localConfigPath.c_str() );
198 infomsgf (
"Rambrain is initialized using system wide config file: %s\n",
globalConfigPath.c_str() );
202 if ( !readAConfig ) {
203 infomsg (
"Rambrain is initialized using default settings." );
212 bool ret =
true, defaultDone =
false, specificDone =
false;
214 vector<configLineBase *> thisReadLines ( readLines );
216 while ( stream.good() && ! ( defaultDone && specificDone ) ) {
217 getline ( stream, line );
219 if ( line.empty() ) {
223 unsigned int current = stream.tellg();
226 ret &=
parseConfigBlock ( stream, specificDone ? readLines : thisReadLines );
235 stream.seekg ( current );
240 readLines.insert ( readLines.end(), thisReadLines.begin(), thisReadLines.end() );
241 unique ( readLines.begin(), readLines.end() );
251 while ( stream.good() ) {
252 getline ( stream, line );
254 if ( line.empty() ) {
258 first = line.substr ( 0, 1 );
259 if ( first ==
"[" ) {
261 }
else if ( first ==
"#" ) {
264 bool matched =
false;
267 size_t comment = line.find_first_of (
'#' );
268 if ( comment != line.npos ) {
269 line = line.substr ( 0, comment );
275 if ( match.first == cl->
name ) {
278 if ( find ( readLines.begin(), readLines.end(), cl ) == readLines.end() ) {
279 readLines.push_back ( cl );
287 warnmsgf (
"Could not parse config line: %s\n", line.c_str() );
300 ret = readlink (
"/proc/self/exe", exe,
sizeof ( exe ) - 1 );
306 string fullName ( exe );
308 unsigned int found = fullName.find_last_of (
"/\\" );
309 return fullName.substr ( found + 1 );
314 struct passwd *pw = getpwuid ( getuid() );
320 str.erase ( str.begin(), std::find_if ( str.begin(), str.end(), std::not1 ( std::ptr_fun<int, int> ( std::isspace ) ) ) );
321 str.erase ( std::find_if ( str.rbegin(), str.rend(), std::not1 ( std::ptr_fun<int, int> ( std::isspace ) ) ).base(), str.end() );
virtual void setValue(const string &str)
Reset the value from a string.
swapPolicy
An enumeration to regulate how the swap should define when it approaches it's set boundary...
configLine< swapPolicy > policy
bool reopenStreams()
(Re)Open streams to config files
configLine< bool > enableDMA
bool matchConfigBlock(const string &str, const string &blockname="default") const
Checks if a string matches the header of a configuration block.
pair< long long int, string > splitIntegerValueUnit(const string &str) const
Split a string containing value and possibly unit into both parts.
void stripLeadingTrailingWhitespace(string &str) const
Strips any leading and trailing whitespace from a given string.
bool parseConfigBlock(istream &stream, vector< configLineBase * > &readLines)
Parse an identified configuration block and extract all matching variables.
Class for config key value pairs represented by a line in a config file.
pair< string, string > matchKeyEqualsValue(const string &str, int valueType=alphanumtext) const
Checks if a string matches something like key = value.
configLine< string > swapfiles
bool parseConfigFile(istream &stream, vector< configLineBase * > &readLines)
Parse config file.
string getHomeDir() const
Look up the home directory of the current user.
const global_bytesize gig
const global_bytesize kib
const string globalConfigPath
string substituteHomeDir(const string &source, const string &homedir) const
Replace all occurences of a ~ by the absolute home directory.
Class to handle regex matching used for parsing configuration files.
virtual void setValue(const string &str)=0
Reset the value from a string.
configLine< global_bytesize > memory
configLine< string > swap
bool readConfig()
Read in and parse config.
string getApplicationName() const
Extract the current binary's name out of the /proc file system.
configLine< string > memoryManager
pair< double, string > splitDoubleValueUnit(const string &str) const
Split a string containing value and possibly unit into both parts.
configReader()
Init a new reader.
configLine< global_bytesize > swapMemory
vector< configLineBase * > configOptions
Base class for config lines.
const global_bytesize mib