Exemple #1
0
        /// <summary>
        /// Parses the ledcontrol.ini file.
        /// </summary>
        /// <param name="LedControlIniFile">The ledcontrol.ini FileInfo object.</param>
        /// <param name="ThrowExceptions">if set to <c>true</c> [throw exceptions].</param>
        /// <exception cref="System.Exception">
        /// File {0} does not contain data.
        /// or
        /// Could not find {0} section in file {1}.
        /// or
        /// File {1} does not contain data in the {0} section.
        /// or
        /// Section {0} of file {1} does not have the same number of columns in all lines.
        /// </exception>
        private void ParseLedControlIni(FileInfo LedControlIniFile, bool ThrowExceptions = false)
        {
            string[] ColorStartStrings         = { "[Colors DOF]", "[Colors LedWiz]" };
            string[] OutStartStrings           = { "[Config DOF]", "[Config outs]" };
            string[] VariableStartStrings      = { "[Variables DOF]" };
            string[] VersionStartStrings       = { "[version]" };
            string[] TableVariableStartStrings = { "[TableVariables]" };;
            string   FileData = "";

            #region Read file
            try
            {
                FileData = General.FileReader.ReadFileToString(LedControlIniFile);
            }
            catch (Exception E)
            {
                Log.Exception("Could not read file {0}.".Build(LedControlIniFile), E);
                if (ThrowExceptions)
                {
                    throw new Exception("Could not read file {0}.".Build(LedControlIniFile), E);
                }
            }
            if (FileData.IsNullOrWhiteSpace())
            {
                Log.Warning("File {0} does not contain data.".Build(LedControlIniFile));
                if (ThrowExceptions)
                {
                    throw new Exception("File {0} does not contain data.".Build(LedControlIniFile));
                }
            }
            #endregion

            Dictionary <string, List <string> > Sections = new Dictionary <string, List <String> >();

            #region Read sections

            List <string> SectionData   = new List <string>();
            String        SectionHeader = null;
            foreach (string RawIniLine in FileData.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
            {
                string IniLine = RawIniLine.Trim();
                if (IniLine.Length > 0 && !IniLine.StartsWith("#"))
                {
                    if (IniLine.StartsWith("[") && IniLine.EndsWith("]") && IniLine.Length > 2)
                    {
                        //This is a section header
                        if (!SectionHeader.IsNullOrWhiteSpace())
                        {
                            if (Sections.ContainsKey(SectionHeader))
                            {
                                int Cnt = 2;
                                while (Sections.ContainsKey("{0} {1}".Build(SectionHeader, Cnt)))
                                {
                                    Cnt++;
                                    if (Cnt > 999)
                                    {
                                        throw new Exception("Section header {0} exists to many times.".Build(SectionHeader));
                                    }
                                }
                                SectionHeader = "{0} {1}".Build(SectionHeader, Cnt);
                            }
                            Sections.Add(SectionHeader, SectionData);
                            SectionData = new List <string>();
                        }
                        SectionHeader = IniLine;
                    }
                    else
                    {
                        //Its a data line
                        SectionData.Add(IniLine);
                    }
                }
            }

            if (!SectionHeader.IsNullOrWhiteSpace())
            {
                if (Sections.ContainsKey(SectionHeader))
                {
                    int Cnt = 2;
                    while (Sections.ContainsKey("{0} {1}".Build(SectionHeader, Cnt)))
                    {
                        Cnt++;
                        if (Cnt > 999)
                        {
                            throw new Exception("Section header {0} exists to many times.".Build(SectionHeader));
                        }
                    }
                    SectionHeader = "{0} {1}".Build(SectionHeader, Cnt);
                }
                Sections.Add(SectionHeader, SectionData);
                SectionData = new List <string>();
            }
            SectionData = null;
            #endregion

            FileData = null;

            List <string> ColorData         = GetSection(Sections, ColorStartStrings);
            List <string> OutData           = GetSection(Sections, OutStartStrings);
            List <string> VariableData      = GetSection(Sections, VariableStartStrings);
            List <string> VersionData       = GetSection(Sections, VersionStartStrings);
            List <string> TableVariableData = GetSection(Sections, TableVariableStartStrings);

            if (VersionData != null && VersionData.Count > 0)
            {
                MinDOFVersion = null;

                string MinDofVersionLine = VersionData.FirstOrDefault(S => S.ToLowerInvariant().StartsWith("mindofversion="));

                if (MinDofVersionLine != null)
                {
                    string MinDofVersionString = MinDofVersionLine.Substring("mindofversion=".Length);

                    try
                    {
                        MinDOFVersion = new Version(MinDofVersionString);
                    }
                    catch (Exception E)
                    {
                        Log.Exception("Could not parse line {1} from file {0}".Build(LedControlIniFile, MinDofVersionLine));
                        MinDOFVersion = null;
                    }
                    if (MinDOFVersion != null)
                    {
                        Log.Write("Min DOF Version is {0} for file {1}".Build(MinDOFVersion.ToString(), LedControlIniFile.Name));
                    }
                }
                else
                {
                    Log.Warning("No DOF version information found in file {0}.".Build(LedControlIniFile));
                }
            }
            else
            {
                Log.Warning("No version section found in file {0}.".Build(LedControlIniFile));
            }

            if (ColorData == null)
            {
                Log.Warning("Could not find color definition section in file {0}.".Build(LedControlIniFile));
                if (ThrowExceptions)
                {
                    throw new Exception("Could not find  color definition section in file {0}.".Build(LedControlIniFile));
                }
                return;
            }
            else if (ColorData.Count < 1)
            {
                Log.Warning("File {0} does not contain data in the color definition section.".Build(LedControlIniFile));
                if (ThrowExceptions)
                {
                    throw new Exception("File {0} does not contain data in the color definition section.".Build(LedControlIniFile));
                }
                return;
            }

            if (OutData == null)
            {
                Log.Warning("Could not find table config section in file {0}.".Build(LedControlIniFile));
                if (ThrowExceptions)
                {
                    throw new Exception("Could not find table config section section in file {1}.".Build(LedControlIniFile));
                }
                return;
            }
            else if (OutData.Count < 1)
            {
                Log.Warning("File {0} does not contain data in the table config section.".Build(LedControlIniFile));
                if (ThrowExceptions)
                {
                    throw new Exception("File {0} does not contain data in the table config section".Build(LedControlIniFile));
                }
                return;
            }

            if (VariableData != null)
            {
                ResolveVariables(OutData, VariableData);
            }

            if (TableVariableData != null)
            {
                ResolveTableVariables(OutData, TableVariableData);
            }



            ColorConfigurations.ParseLedControlData(ColorData, ThrowExceptions);

            TableConfigurations.ParseLedcontrolData(OutData, ThrowExceptions);

            //ResolveOutputNumbers();
            ResolveRGBColors();

            this.LedControlIniFile = LedControlIniFile;
        }
        /// <summary>
        /// Parses the ledcontrol.ini file.
        /// </summary>
        /// <param name="LedControlIniFile">The ledcontrol.ini FileInfo object.</param>
        /// <param name="ThrowExceptions">if set to <c>true</c> [throw exceptions].</param>
        /// <exception cref="System.Exception">
        /// File {0} does not contain data.
        /// or
        /// Could not find {0} section in file {1}.
        /// or
        /// File {1} does not contain data in the {0} section.
        /// or
        /// Section {0} of file {1} does not have the same number of columns in all lines.
        /// </exception>
        private void ParseLedControlIni(FileInfo LedControlIniFile, bool ThrowExceptions = false)
        {
            string[] ColorStartStrings = { "[Colors DOF]", "[Colors LedWiz]" };
            string[] OutStartStrings   = { "[Config DOF]", "[Config outs]" };

            //Read the file
            string Data = "";

            try
            {
                Data = General.FileReader.ReadFileToString(LedControlIniFile);
            }
            catch (Exception E)
            {
                Log.Exception("Could not read file {0}.".Build(LedControlIniFile), E);
                if (ThrowExceptions)
                {
                    throw new Exception("Could not read file {0}.".Build(LedControlIniFile), E);
                }
            }
            if (Data.IsNullOrWhiteSpace())
            {
                Log.Warning("File {0} does not contain data.".Build(LedControlIniFile));
                if (ThrowExceptions)
                {
                    throw new Exception("File {0} does not contain data.".Build(LedControlIniFile));
                }
            }

            //Find starting positions of both sections
            int    ColorStart       = -1;
            string ColorStartString = "";

            foreach (string S in ColorStartStrings)
            {
                ColorStart       = Data.IndexOf(S, StringComparison.InvariantCultureIgnoreCase);
                ColorStartString = S;
                if (ColorStart >= 0)
                {
                    break;
                }
            }
            int    OutStart       = -1;
            string OutStartString = "";

            foreach (string S in OutStartStrings)
            {
                OutStart       = Data.IndexOf(S, StringComparison.InvariantCultureIgnoreCase);
                OutStartString = S;
                if (OutStart >= 0)
                {
                    break;
                }
            }

            if (ColorStart < 0)
            {
                Log.Exception("Could not find color definition section in file {0}.".Build(LedControlIniFile));
                if (ThrowExceptions)
                {
                    throw new Exception("Could not find  color definition section in file {0}.".Build(LedControlIniFile));
                }
                return;
            }

            if (OutStart < 0)
            {
                Log.Exception("Could not find table config section in file {0}.".Build(LedControlIniFile));
                if (ThrowExceptions)
                {
                    throw new Exception("Could not find table config section section in file {1}.".Build(LedControlIniFile));
                }
                return;
            }

            string[] ColorData;
            string[] OutData;

            //Extract data of the sections, split into string arrays and remove empty lines
            if (ColorStart < OutStart)
            {
                ColorData = Data.Substring(ColorStart + ColorStartString.Length, OutStart - (ColorStart + ColorStartString.Length)).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
                OutData   = Data.Substring(OutStart + OutStartString.Length).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
            }
            else
            {
                OutData   = Data.Substring(OutStart + OutStartString.Length, ColorStart - (OutStart + OutStartString.Length)).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
                ColorData = Data.Substring(ColorStart + ColorStartString.Length).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
            }


            if (OutData.Length == 0)
            {
                Log.Exception("File {1} does not contain data in the {0} section.".Build(OutStartString, LedControlIniFile));
                if (ThrowExceptions)
                {
                    throw new Exception("File {1} does not contain data in the {0} section.".Build(OutStartString, LedControlIniFile));
                }
                return;
            }

            if (ColorData.Length == 0)
            {
                Log.Exception("File {1} does not contain data in the {0} section.".Build(ColorStartString, LedControlIniFile));
                if (ThrowExceptions)
                {
                    throw new Exception("File {1} does not contain data in the {0} section.".Build(ColorStartString, LedControlIniFile));
                }
                return;
            }



            ColorConfigurations.ParseLedControlData(ColorData, ThrowExceptions);

            TableConfigurations.ParseLedcontrolData(OutData, ThrowExceptions);

            ResolveOutputNumbers();
            ResolveRGBColors();
        }