49 lines
1.6 KiB
C#
49 lines
1.6 KiB
C#
using System.IO;
|
|
using System.Text;
|
|
using IniParser;
|
|
using IniParser.Model;
|
|
|
|
namespace Elwig.Helpers {
|
|
public class Config {
|
|
|
|
private readonly string FileName;
|
|
public string DatabaseFile = App.DataPath + "database.sqlite3";
|
|
public string? DatabaseLog = null;
|
|
|
|
public Config(string filename) {
|
|
FileName = filename;
|
|
Read();
|
|
}
|
|
|
|
public void Read() {
|
|
var parser = new FileIniDataParser();
|
|
IniData? ini = null;
|
|
try {
|
|
ini = parser.ReadFile(FileName, Encoding.UTF8);
|
|
} catch {}
|
|
|
|
if (ini == null || !ini.TryGetKey("database.file", out string db)) {
|
|
DatabaseFile = App.DataPath + "database.sqlite3";
|
|
} else if (db.Length > 1 && (db[1] == ':' || db[0] == '/' || db[0] == '\\')) {
|
|
DatabaseFile = db;
|
|
} else {
|
|
DatabaseFile = App.DataPath + db;
|
|
}
|
|
|
|
if (ini == null || !ini.TryGetKey("database.log", out string log)) {
|
|
DatabaseLog = null;
|
|
} else if (log.Length > 1 && (log[1] == ':' || log[0] == '/' || log[0] == '\\')) {
|
|
DatabaseLog = log;
|
|
} else {
|
|
DatabaseLog = App.DataPath + log;
|
|
}
|
|
}
|
|
|
|
public void Write() {
|
|
using var file = new StreamWriter(FileName, false, Encoding.UTF8);
|
|
file.Write($"\r\n[database]\r\nfile = {DatabaseFile}\r\n");
|
|
if (DatabaseLog != null) file.Write($"log = {DatabaseLog}\r\n");
|
|
}
|
|
}
|
|
}
|