/* * Copyright (C) 2008 DLFSS * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ using System; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; namespace ODBC2MySQL_.Config_ { class ConfigReader { ConfigReader() { lineNumber = 0; rgxIdentifier = new Regex(@"^([a-z0-9]+){1}\.{0,1}([a-z0-9]+){0,1}\.{0,1}([a-z0-9]+){0,1}\.{0,1}([a-z0-9]+){0,1}\s*=\s*([a-z0-9]{1}[^#]+[^#\s\r\n]{1})${1}", RegexOptions.IgnoreCase); ReadToArray(); } //private const string CONFIG_FILE = "odbc2mysql.cfg"; //private const string CONFIG_VERSION = "07282008"; private const string confFile = "ODBC2MySQL.cfg"; private const string confVersion = "20080808"; private static Dictionary confValue = new Dictionary(); private uint lineNumber; private Regex rgxIdentifier; public void ReadToArray() { StreamReader streamReader = new StreamReader(confFile); string lineReaded = ""; while(!streamReader.EndOfStream) { lineNumber++; lineReaded = streamReader.ReadLine(); Match match = rgxIdentifier.Match(lineReaded); //Program.log.debug(match.Groups[5] + "\r\n"); //continue; if (match.Success) { if (match.Groups[4].Value != "") { if (!confValue.ContainsKey(match.Groups[1].Value)) confValue.Add(match.Groups[1].Value, new Dictionary()); if (!((Dictionary)confValue[match.Groups[1].Value]).ContainsKey(match.Groups[2].Value)) ((Dictionary)confValue[match.Groups[1].Value]).Add(match.Groups[2].Value, new Dictionary()); if (!((Dictionary)((Dictionary)confValue[match.Groups[1].Value])[match.Groups[2].Value]).ContainsKey(match.Groups[3].Value) ) ((Dictionary)((Dictionary)confValue[match.Groups[1].Value])[match.Groups[2].Value]).Add(match.Groups[3].Value, new Dictionary()); if (!((Dictionary)((Dictionary)((Dictionary)confValue[match.Groups[1].Value])[match.Groups[2].Value])[match.Groups[3].Value]).ContainsKey(match.Groups[4].Value)) ((Dictionary)((Dictionary)((Dictionary)confValue[match.Groups[1].Value])[match.Groups[2].Value])[match.Groups[3].Value]).Add(match.Groups[4].Value, match.Groups[5].Value); continue; } if (match.Groups[3].Value != "") { if (!confValue.ContainsKey(match.Groups[1].Value)) confValue.Add(match.Groups[1].Value, new Dictionary()); if (!((Dictionary)confValue[match.Groups[1].Value]).ContainsKey(match.Groups[2].Value)) ((Dictionary)confValue[match.Groups[1].Value]).Add(match.Groups[2].Value, new Dictionary()); if (!((Dictionary)((Dictionary)confValue[match.Groups[1].Value])[match.Groups[2].Value]).ContainsKey(match.Groups[3].Value)) ((Dictionary)((Dictionary)confValue[match.Groups[1].Value])[match.Groups[2].Value]).Add(match.Groups[3].Value, match.Groups[5].Value); continue; } if (match.Groups[2].Value != "") { if (!confValue.ContainsKey(match.Groups[1].Value)) confValue.Add(match.Groups[1].Value, new Dictionary()); if (!((Dictionary)confValue[match.Groups[1].Value]).ContainsKey(match.Groups[2].Value)) ((Dictionary)confValue[match.Groups[1].Value]).Add(match.Groups[2].Value, match.Groups[5].Value); continue; } if (match.Groups[1].Value != "") { if (!confValue.ContainsKey(match.Groups[1].Value)) confValue.Add(match.Groups[1].Value, match.Groups[5].Value); continue; } //Program.log.debug((string)((Dictionary)((Dictionary)((Dictionary)confValue[match.Groups[0].Value])[match.Groups[1].Value])[match.Groups[2].Value])[match.Groups[3].Value]); } } } public static int GetIntValue(params string[] args) { if (args.Length == 4) { if (confValue.ContainsKey(args[0]) && ((Dictionary)confValue[args[0]]).ContainsKey(args[1]) && ((Dictionary)((Dictionary)confValue[args[0]])[args[1]]).ContainsKey(args[2]) && ((Dictionary)((Dictionary)((Dictionary)confValue[args[0]])[args[1]])[args[2]]).ContainsKey(args[3])) return Convert.ToInt32(((Dictionary)((Dictionary)((Dictionary)confValue[args[0]])[args[1]])[args[2]])[args[3]]); else Program.log.error("Configuration unable to find value for key: " + args[0] + "." + args[1] + "." + args[2] + "." + args[3] + "\r\n"); return -1; } if (args.Length == 3) { if (confValue.ContainsKey(args[0]) && ((Dictionary)confValue[args[0]]).ContainsKey(args[1]) && ((Dictionary)((Dictionary)confValue[args[0]])[args[1]]).ContainsKey(args[2])) return Convert.ToInt32(((Dictionary)((Dictionary)confValue[args[0]])[args[1]])[args[2]]); else Program.log.error("Configuration unable to find value for key: " + args[0] + "." + args[1] + "." + args[2] + "\r\n"); return -1; } if (args.Length == 2) { if (confValue.ContainsKey(args[0]) && ((Dictionary)confValue[args[0]]).ContainsKey(args[1])) return Convert.ToInt32(((Dictionary)confValue[args[0]])[args[1]]); else Program.log.error("Configuration unable to find value for key: " + args[0] + "." + args[1] + "\r\n"); return -1; } if (args.Length == 1) { if (confValue.ContainsKey(args[0])) return Convert.ToInt32(confValue[args[0]]); else Program.log.error("Configuration unable to find value for key: " + args[0] + "\r\n"); return -1; } return -1; } public static string GetStringValue(params string[] args) { if (args.Length == 4) { if (confValue.ContainsKey(args[0]) && ((Dictionary)confValue[args[0]]).ContainsKey(args[1]) && ((Dictionary)((Dictionary)confValue[args[0]])[args[1]]).ContainsKey(args[2]) && ((Dictionary)((Dictionary)((Dictionary)confValue[args[0]])[args[1]])[args[2]]).ContainsKey(args[3])) return (string)((Dictionary)((Dictionary)((Dictionary)confValue[args[0]])[args[1]])[args[2]])[args[3]]; else Program.log.error("Configuration unable to find value for key: " + args[0] + "." + args[1] + "."+args[2]+"."+args[3]+"\r\n"); return null; } if (args.Length == 3) { if (confValue.ContainsKey(args[0]) && ((Dictionary)confValue[args[0]]).ContainsKey(args[1]) && ((Dictionary)((Dictionary)confValue[args[0]])[args[1]]).ContainsKey(args[2])) return (string)((Dictionary)((Dictionary)confValue[args[0]])[args[1]])[args[2]]; else Program.log.error("Configuration unable to find value for key: " + args[0] + "." + args[1] + "."+args[2]+"\r\n"); return null; } if (args.Length == 2) { if (confValue.ContainsKey(args[0]) && ((Dictionary)confValue[args[0]]).ContainsKey(args[1])) return (string)((Dictionary)confValue[args[0]])[args[1]]; else Program.log.error("Configuration unable to find value for key: " + args[0] + "." + args[1] + "\r\n"); return null; } if (args.Length == 1) { if (confValue.ContainsKey(args[0])) return (string)confValue[args[0]]; else Program.log.error("Configuration unable to find value for key: " + args[0] + "\r\n"); return null; } return null; } public static List GetIdentifierList(params string[] args) { List temp = new List(); if (args.Length == 3) { if (confValue.ContainsKey(args[0]) && ((Dictionary)confValue[args[0]]).ContainsKey(args[1]) && ((Dictionary)((Dictionary)confValue[args[0]])[args[1]]).ContainsKey(args[2])) { Dictionary.Enumerator itr = ((Dictionary)((Dictionary)((Dictionary)confValue[args[0]])[args[1]])[args[2]]).GetEnumerator(); while (itr.MoveNext()) temp.Add(itr.Current.Key); } else Program.log.error("Configuration unable to find value for key: " + args[0] + "." + args[1] + "." + args[2] + "\r\n"); return temp; } if (args.Length == 2) { if ( confValue.ContainsKey(args[0]) && ((Dictionary)confValue[args[0]]).ContainsKey(args[1]) ) { Dictionary.Enumerator itr = ((Dictionary)((Dictionary)confValue[args[0]])[args[1]]).GetEnumerator(); while (itr.MoveNext()) temp.Add(itr.Current.Key); } else Program.log.error("Configuration unable to find value for key: " + args[0] + "." + args[1] + "\r\n"); return temp; } if (args.Length == 1) { if (confValue.ContainsKey(args[0])) { Dictionary.Enumerator itr = ((Dictionary)confValue[args[0]]).GetEnumerator(); while (itr.MoveNext()) temp.Add(itr.Current.Key); } else Program.log.error("Configuration unable to find value for key: " + args[0] + "." + args[1] + "\r\n"); return temp; } return null; } public static bool Initialize() { if (!File.Exists("ODBC2MySQL.cfg")) { Program.log.error("Unable to find the config file: ODBC2MySQL.cfg\r\n"); return false; } ConfigReader configReader = new ConfigReader(); return true; } } }