using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; public class Config { public const string CONFIGFILE = "CONFIG"; private const char separator = '='; private const string comment = "!"; private const char connchar = '§'; public string key_prefix { get; set; } private int _cores = 0; private bool _singlecore = false; private string _defaultpath = System.IO.Path.Combine(Environment.CurrentDirectory, CONFIGFILE); public int Cores => _cores; public bool Singlecore => _singlecore; public List KeyValues { get; private set; } public string FileConfig { get; set; } public Config() { //_cores = Environment.ProcessorCount; //if (_cores > 1) _singlecore = false; if (File.Exists(_defaultpath)) { FileConfig = _defaultpath; key_prefix = ""; Read(); } } public Config(string fileconfig, string prx = "") : this() { if (File.Exists(fileconfig) && fileconfig != _defaultpath) { FileConfig = fileconfig; key_prefix = prx; Read(); } } public void Read() { KeyValues = AnalizeConfig(this.FileConfig, this.key_prefix); } public List AnalizeConfig(string configfile, string prefix = "") { List ret = new List(); if (configfile == null || configfile == string.Empty) return ret; if (!File.Exists(configfile)) return null; this.FileConfig = configfile; this.key_prefix = prefix; var _content = File.ReadAllLines(configfile); if (_content.Length > 0) { foreach (var element in _content) { //devo escludere i commenti e le //righe vuote /// se la linea comincia con ||| non leggo piu string linea = element.Trim(); if (linea.StartsWith("|||")) break; else { if (linea != string.Empty && !linea.StartsWith(comment)) { var elements = linea.Split(separator); string k = string.Empty; string v = string.Empty; if (elements.Length > 0) { k = elements[0]; } if (elements.Length > 1) { v = elements[1]; } v = v.Replace(connchar, separator); ret.Add(new KeyValue(prefix + k, v)); } } } } return ret; } }