readconf/source/readconf.d

230 lines
5.6 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:
2023-03-25 23:35:08 +00:00
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, // "[]"
}
2023-03-23 15:10:56 +00:00
static Config config;
string path;
bool readed = false;
2023-03-25 23:35:08 +00:00
ConfigSection[string] sections;
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) {
2023-03-25 23:35:08 +00:00
Log.msg.warning("Unable to open the configuration file " ~ this.path);
Log.msg.error(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 23:35:08 +00:00
// reading from the main section
2023-03-25 21:31:25 +00:00
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
2023-03-25 23:35:08 +00:00
if (match[GROUP_SECTION_MAIN].length)
2023-03-25 21:31:25 +00:00
{
2023-03-25 23:35:08 +00:00
sectionName = match[GROUP_SECTION_MAIN];
2023-03-25 21:31:25 +00:00
continue;
}
2023-03-25 23:35:08 +00:00
if (match[GROUP_SECTION_OTHER_OUTER].length)
2023-03-23 15:10:56 +00:00
{
2023-03-25 23:35:08 +00:00
sectionName = match[GROUP_SECTION_OTHER_INNER];
2023-03-25 21:31:25 +00:00
continue;
2023-03-23 15:10:56 +00:00
}
2023-03-25 23:35:08 +00:00
int group = GROUP_VALUE_1;
2023-03-25 22:45:41 +00:00
if (match[group][0] == '\"')
2023-03-25 23:35:08 +00:00
group = GROUP_VALUE_2;
2023-03-25 22:45:41 +00:00
else if (match[group][0] == '\'')
2023-03-25 23:35:08 +00:00
group = GROUP_VALUE_3;
2023-03-25 21:31:25 +00:00
if (sectionName !in this.sections)
this.sections[sectionName] = ConfigSection(sectionName);
2023-03-25 23:35:08 +00:00
this.sections[sectionName].add(ConfigParameter(match[GROUP_PROPERTY], match[group]));
2023-03-25 21:31:25 +00:00
}
2023-03-23 15:10:56 +00:00
}
try {
configuration.close();
this.readed = true;
} catch (Exception e) {
2023-03-25 23:35:08 +00:00
Log.msg.warning("Unable to close the configuration file " ~ this.path);
Log.msg.error(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)
2023-03-25 23:35:08 +00:00
throw new Exception("The configuration file does not exist: " ~ path);
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) {
2023-03-25 23:35:08 +00:00
Log.msg.warning("Cannot convert type");
Log.msg.error(e);
2023-03-25 13:36:59 +00:00
return T.init;
}
}
}