readconf/source/readconf.d

223 lines
5.3 KiB
D
Raw Normal View History

2023-03-23 15:10:56 +00:00
module readconf;
import std.stdio, std.conv, std.path, std.file;
import core.stdc.stdlib : exit;
import std.regex;
import std.meta;
import singlog;
2023-03-25 22:45:41 +00:00
/**
* Read config object
*/
alias rc = Config.file;
2023-03-23 15:10:56 +00:00
class Config
{
private:
static Config config;
string path;
2023-03-25 21:31:25 +00:00
ConfigSection[string] sections;
2023-03-23 15:10:56 +00:00
bool readed = false;
2023-03-25 12:37:46 +00:00
const string pattern = "^( |\\t)*(((\\w(\\w|-)+)(( |\\t)*(=>|=){1}"
~ "( |\\t)*)(?!\\/(\\/|\\*))(([^ >\"'=\\n\\t#;].*?)|(\"(.+)\")"
2023-03-25 22:45:41 +00:00
~ "|('(.+)')){1})|(\\[(\\w(\\w|-)+)\\])|(\\[\\]))( |\\t)*"
2023-03-25 12:37:46 +00:00
~ "(( |\\t)(#|;|\\/\\/|\\/\\*).*)?$";
2023-03-23 15:10:56 +00:00
/**
* Reading the configuration file
*/
2023-03-25 23:03:03 +00:00
bool readConfig()
2023-03-23 15:10:56 +00:00
{
File configuration;
try {
configuration = File(this.path, "r");
} catch (Exception e) {
Log.msg.error("Unable to open the configuration file " ~ this.path);
Log.msg.warning(e);
2023-03-25 23:03:03 +00:00
return false;
2023-03-23 15:10:56 +00:00
}
2023-03-25 13:36:59 +00:00
auto regular = regex(this.pattern, "m");
2023-03-23 15:10:56 +00:00
2023-03-25 21:31:25 +00:00
// if main section
string sectionName = "[]";
2023-03-23 15:10:56 +00:00
while (!configuration.eof())
{
string line = configuration.readln();
auto match = matchFirst(line, regular);
if (match)
{
2023-03-25 21:31:25 +00:00
// if again main section
if (match[20].length)
{
sectionName = match[20];
continue;
}
// if other section
if (match[17].length)
2023-03-23 15:10:56 +00:00
{
2023-03-25 21:31:25 +00:00
sectionName = match[18];
continue;
2023-03-23 15:10:56 +00:00
}
2023-03-25 21:31:25 +00:00
// values
int group = 11;
2023-03-25 22:45:41 +00:00
if (match[group][0] == '\"')
2023-03-25 21:31:25 +00:00
group = 14;
2023-03-25 22:45:41 +00:00
else if (match[group][0] == '\'')
2023-03-25 21:31:25 +00:00
group = 16;
if (sectionName !in this.sections)
this.sections[sectionName] = ConfigSection(sectionName);
this.sections[sectionName].add(ConfigParameter(match[4], match[group]));
}
2023-03-23 15:10:56 +00:00
}
try {
configuration.close();
this.readed = true;
} catch (Exception e) {
Log.msg.error("Unable to close the configuration file " ~ this.path);
Log.msg.warning(e);
2023-03-25 23:03:03 +00:00
this.readed = false;
2023-03-23 15:10:56 +00:00
}
2023-03-25 23:03:03 +00:00
return this.readed;
2023-03-23 15:10:56 +00:00
}
this() {}
public:
/**
* Accessing the Config object
* Returns: Config Object
*/
@property static Config file()
{
if (this.config is null)
this.config = new Config;
return this.config;
}
/**
* Read the configuration file
* Params:
* path = the path to the configuration file
*/
2023-03-25 23:03:03 +00:00
bool read(string path)
2023-03-23 15:10:56 +00:00
{
this.path = path;
if (!path.exists)
{
Log.msg.error("The configuration file does not exist: " ~ path);
2023-03-25 23:03:03 +00:00
return false;
2023-03-23 15:10:56 +00:00
}
2023-03-25 23:03:03 +00:00
return readConfig();
2023-03-23 15:10:56 +00:00
}
2023-03-25 22:45:41 +00:00
/**
* Get the section
* Params:
* section = section name (default main "[]")
*/
@property ConfigSection sectionName(string section = "[]")
2023-03-25 21:31:25 +00:00
{
2023-03-25 23:03:03 +00:00
return section in sections ? sections[section] : ConfigSection();
2023-03-25 21:31:25 +00:00
}
2023-03-25 22:45:41 +00:00
/**
* Section name
*
* Get the section
* Params:
* section = section name (default main "[]")
*/
alias sn = sectionName;
2023-03-25 21:31:25 +00:00
}
struct ConfigSection
{
private string name = "[]";
private ConfigParameter[string] parameters;
/**
* Checking for the presence of a partition
* Returns: true if the parameter is missing, otherwise false
*/
@property bool empty()
2023-03-25 13:36:59 +00:00
{
2023-03-25 21:31:25 +00:00
return this.parameters.length == 0;
}
2023-03-25 13:36:59 +00:00
2023-03-25 21:31:25 +00:00
/**
* Get the parameter value
* Params:
* key = parameter from the configuration file
* Returns: the value of the parameter in the PP structure view
*/
ConfigParameter key(string key)
{
return key in this.parameters ? this.parameters[key] : ConfigParameter();
}
/**
* Get all keys and their values
* Returns: collection of properties structures PP
*/
ConfigParameter[string] keys()
{
return this.parameters;
}
2023-03-25 23:03:03 +00:00
private void add(ConfigParameter parameter)
2023-03-25 21:31:25 +00:00
{
2023-03-25 23:03:03 +00:00
if (parameter.property in parameters)
Log.msg.warning("The parameter exists but will be overwritten");
2023-03-25 21:31:25 +00:00
this.parameters[parameter.property] = parameter;
2023-03-25 13:36:59 +00:00
}
}
/**
* Parameter and its value with the ability to convert to the desired data type
*/
2023-03-25 21:31:25 +00:00
struct ConfigParameter
2023-03-25 13:36:59 +00:00
{
private string property;
private string value;
/**
* Checking for the presence of a parameter
* Returns: true if the parameter is missing, otherwise false
*/
@property bool empty()
{
return this.property.length == 0 || this.value.length == 0;
}
/**
* Get a string representation of the value
* Returns: default string value
*/
@property string toString() const
{
return this.value;
}
alias toString this;
auto opCast(T)() const
{
try {
return this.value.to!T;
} catch (Exception e) {
Log.msg.error("Cannot convert type");
Log.msg.warning(e);
return T.init;
}
}
}