public void Load()
{
const string dataSetPattern = @"[\s\t]*(\w+)[\s\t]*(.*)$";
const string dataSourcePattern = @"(\w+):(ABSOLUTE|COUNTER|DERIVE|GAUGE):([+-]?\w+):([+-]?\w+)[,]?\s*";
var dataSetRegex = new Regex(dataSetPattern, RegexOptions.IgnoreCase);
var dataSourceRegex = new Regex(dataSourcePattern, RegexOptions.IgnoreCase);
string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "types.db");
string[] lines = File.ReadAllLines(fileName);
foreach (string line in lines)
{
if (line.StartsWith("#") || line.Trim().Length == 0)
{
continue;
}
Match match = dataSetRegex.Match(line);
if (match.Groups.Count < 3)
{
LogEventInfo logEvent = new LogEventInfo(LogLevel.Error, Logger.Name, String.Format("types.db: invalid data set [{0}]", line));
logEvent.Properties.Add("EventID", ErrorCodes.ERROR_CONFIGURATION_EXCEPTION);
Logger.Log(logEvent);
continue;
}
string dataSetName = match.Groups[1].Value;
MatchCollection matches = dataSourceRegex.Matches(line);
if (matches.Count < 1)
{
LogEventInfo logEvent = new LogEventInfo(LogLevel.Error, Logger.Name, String.Format("types.db: invalid data source [{0}]", line));
logEvent.Properties.Add("EventID", ErrorCodes.ERROR_CONFIGURATION_EXCEPTION);
Logger.Log(logEvent);
continue;
}
var dataSourceList = new List<DataSource>();
foreach (Match m in matches)
{
if (m.Groups.Count != 5)
{
LogEventInfo logEvent = new LogEventInfo(LogLevel.Error, Logger.Name, String.Format("types.db: cannot parse data source [{0}]", line));
logEvent.Properties.Add("EventID", ErrorCodes.ERROR_CONFIGURATION_EXCEPTION);
Logger.Log(logEvent);
dataSourceList.Clear();
break;
}
string dsName = m.Groups[1].Value;
var dstype = (DsType) Enum.Parse(typeof (DsType), m.Groups[2].Value, true);
double min, max;
if (GetDouble(m.Groups[3].Value, out min) != Status.Success)
{
LogEventInfo logEvent = new LogEventInfo(LogLevel.Error, Logger.Name, String.Format("types.db: invalid Min value [{0}]", line));
logEvent.Properties.Add("EventID", ErrorCodes.ERROR_CONFIGURATION_EXCEPTION);
Logger.Log(logEvent);
dataSourceList.Clear();
break;
}
if (GetDouble(m.Groups[4].Value, out max) != Status.Success)
{
LogEventInfo logEvent = new LogEventInfo(LogLevel.Error, Logger.Name, String.Format("types.db: invalid Max value [{0}]", line));
logEvent.Properties.Add("EventID", ErrorCodes.ERROR_CONFIGURATION_EXCEPTION);
Logger.Log(logEvent);
dataSourceList.Clear();
break;
}
var ds = new DataSource(dsName, dstype, min, max);
dataSourceList.Add(ds);
}
if (dataSourceList.Count > 0)
{
_dataSetMap[dataSetName] = dataSourceList;
}
}
}