dev #1

Merged
alexander merged 9 commits from dev into master 2023-03-26 08:38:23 +00:00
2 changed files with 31 additions and 24 deletions
Showing only changes of commit 7351a94b9f - Show all commits

View File

@ -35,5 +35,5 @@
"dependencies": { "dependencies": {
"singlog": "~>0.1.0" "singlog": "~>0.1.0"
}, },
"version": "0.1.4" "version": "0.1.7"
} }

View File

@ -14,10 +14,21 @@ alias rc = Config.file;
class Config class Config
{ {
private: private:
enum {
GROUP_PROPERTY = 4,
GROUP_VALUE_1 = 11, // string
GROUP_VALUE_2 = 14, // "strin"
GROUP_VALUE_3 = 16, // 'string'
GROUP_SECTION_OTHER_OUTER = 17, // "[string]"
GROUP_SECTION_OTHER_INNER = 18, // "[string]"
GROUP_SECTION_MAIN = 20, // "[]"
}
static Config config; static Config config;
string path; string path;
ConfigSection[string] sections;
bool readed = false; bool readed = false;
ConfigSection[string] sections;
const string pattern = "^( |\\t)*(((\\w(\\w|-)+)(( |\\t)*(=>|=){1}" const string pattern = "^( |\\t)*(((\\w(\\w|-)+)(( |\\t)*(=>|=){1}"
~ "( |\\t)*)(?!\\/(\\/|\\*))(([^ >\"'=\\n\\t#;].*?)|(\"(.+)\")" ~ "( |\\t)*)(?!\\/(\\/|\\*))(([^ >\"'=\\n\\t#;].*?)|(\"(.+)\")"
~ "|('(.+)')){1})|(\\[(\\w(\\w|-)+)\\])|(\\[\\]))( |\\t)*" ~ "|('(.+)')){1})|(\\[(\\w(\\w|-)+)\\])|(\\[\\]))( |\\t)*"
@ -33,14 +44,14 @@ private:
try { try {
configuration = File(this.path, "r"); configuration = File(this.path, "r");
} catch (Exception e) { } catch (Exception e) {
Log.msg.error("Unable to open the configuration file " ~ this.path); Log.msg.warning("Unable to open the configuration file " ~ this.path);
Log.msg.warning(e); Log.msg.error(e);
return false; return false;
} }
auto regular = regex(this.pattern, "m"); auto regular = regex(this.pattern, "m");
// if main section // reading from the main section
string sectionName = "[]"; string sectionName = "[]";
while (!configuration.eof()) while (!configuration.eof())
@ -50,28 +61,28 @@ private:
if (match) if (match)
{ {
// if again main section // if again main section
if (match[20].length) if (match[GROUP_SECTION_MAIN].length)
{ {
sectionName = match[20]; sectionName = match[GROUP_SECTION_MAIN];
continue; continue;
} }
// if other section if (match[GROUP_SECTION_OTHER_OUTER].length)
if (match[17].length)
{ {
sectionName = match[18]; sectionName = match[GROUP_SECTION_OTHER_INNER];
continue; continue;
} }
// values
int group = 11; int group = GROUP_VALUE_1;
if (match[group][0] == '\"') if (match[group][0] == '\"')
group = 14; group = GROUP_VALUE_2;
else if (match[group][0] == '\'') else if (match[group][0] == '\'')
group = 16; group = GROUP_VALUE_3;
if (sectionName !in this.sections) if (sectionName !in this.sections)
this.sections[sectionName] = ConfigSection(sectionName); this.sections[sectionName] = ConfigSection(sectionName);
this.sections[sectionName].add(ConfigParameter(match[4], match[group])); this.sections[sectionName].add(ConfigParameter(match[GROUP_PROPERTY], match[group]));
} }
} }
@ -79,8 +90,8 @@ private:
configuration.close(); configuration.close();
this.readed = true; this.readed = true;
} catch (Exception e) { } catch (Exception e) {
Log.msg.error("Unable to close the configuration file " ~ this.path); Log.msg.warning("Unable to close the configuration file " ~ this.path);
Log.msg.warning(e); Log.msg.error(e);
this.readed = false; this.readed = false;
} }
@ -111,11 +122,7 @@ public:
{ {
this.path = path; this.path = path;
if (!path.exists) if (!path.exists)
{ throw new Exception("The configuration file does not exist: " ~ path);
Log.msg.error("The configuration file does not exist: " ~ path);
return false;
}
return readConfig(); return readConfig();
} }
@ -214,8 +221,8 @@ struct ConfigParameter
try { try {
return this.value.to!T; return this.value.to!T;
} catch (Exception e) { } catch (Exception e) {
Log.msg.error("Cannot convert type"); Log.msg.warning("Cannot convert type");
Log.msg.warning(e); Log.msg.error(e);
return T.init; return T.init;
} }
} }