/// <summary> /// Process the current line and extract out values /// </summary> private void ProcessLine() { // Comment if (CurrentLine.StartsWith(";")) { KeyValuePair = null; RowType = IniRowType.Comment; } // Section else if (CurrentLine.StartsWith("[") && CurrentLine.EndsWith("]")) { KeyValuePair = null; RowType = IniRowType.SectionHeader; Section = CurrentLine.TrimStart('[').TrimEnd(']'); } // KeyValuePair else if (CurrentLine.Contains("=")) { // Split the line by '=' for key-value pairs string[] data = CurrentLine.Split('='); // If the value field contains an '=', we need to put them back in string key = data[0].Trim(); string value = string.Join("=", data.Skip(1)).Trim(); KeyValuePair = new KeyValuePair <string, string>(key, value); RowType = IniRowType.KeyValue; } // Empty else if (string.IsNullOrEmpty(CurrentLine)) { KeyValuePair = null; CurrentLine = string.Empty; RowType = IniRowType.None; } // Invalid else { KeyValuePair = null; RowType = IniRowType.Invalid; if (ValidateRows) { throw new InvalidDataException($"Invalid INI row found, cannot continue: {CurrentLine}"); } } }
/// <summary> /// Returns a new FuzzySystem of the appropriate sub-type. /// </summary> /// <param name="FileName"></param> /// <returns></returns> public static FuzzySystem GetFuzzySystemFromFile(string FileName) { // Open the file StreamReader file; string CurrentLine; try { file = new StreamReader(FileName); } catch (FileNotFoundException exc) { throw exc; } FuzzySystem fuzzy = null; // Find the type and create the fuzzy system accordingly while ((CurrentLine = file.ReadLine()) != null) { if (CurrentLine.StartsWith("Type=")) { FuzzyType type = FuzzySystem.StringToFuzzyType(CurrentLine.Replace("Type=", "").Replace("'", "")); switch (type) { case FuzzyType.Mamdani: fuzzy = new MamdaniFS(); break; case FuzzyType.Larsen: fuzzy = new LarsenFS(); break; case FuzzyType.Sugeno: fuzzy = new SugenoFS(); break; case FuzzyType.Sparse: default: throw new NotImplementedException(); } file = new StreamReader(FileName); break; } } bool FoundSystem = false; bool FoundIn = false; bool FoundOut = false; bool FoundRules = false; while ((CurrentLine = file.ReadLine()) != null) { // When we reach the [System] tag, we read the system setting from the following lines until we hit an empty line or the end of the file if (CurrentLine == "[System]") { FoundSystem = true; while ((CurrentLine = file.ReadLine()) != null && CurrentLine != "") { if (CurrentLine.StartsWith("Name=")) { fuzzy.Name = CurrentLine.Replace("Name=", "").Replace("'", ""); } //else if (CurrentLine.StartsWith("Type=")) //fuzzy.Type = StringToFuzzyType(CurrentLine.Replace("Type=", "").Replace("'", "")); else if (CurrentLine.StartsWith("Version=")) { fuzzy.Version = CurrentLine.Replace("Version=", ""); } else if (CurrentLine.StartsWith("NumInputs=")) { fuzzy.NumInputs = int.Parse(CurrentLine.Replace("NumInputs=", "")); } else if (CurrentLine.StartsWith("NumOutputs=")) { fuzzy.NumOutputs = int.Parse(CurrentLine.Replace("NumOutputs=", "")); } else if (CurrentLine.StartsWith("NumRules=")) { fuzzy.NumRules = int.Parse(CurrentLine.Replace("NumRules=", "")); } // Method types else if (CurrentLine.StartsWith("AndMethod=")) { fuzzy.AndMethod = FuzzySystem.StringToAndMethod(CurrentLine.Split('\'')[1]); } else if (CurrentLine.StartsWith("OrMethod=")) { fuzzy.OrMethod = FuzzySystem.StringToOrMethod(CurrentLine.Split('\'')[1]); } else if (CurrentLine.StartsWith("ImpMethod=")) { fuzzy.ImpMethod = FuzzySystem.StringToImpMethod(CurrentLine.Split('\'')[1]); } else if (CurrentLine.StartsWith("AggMethod=")) { fuzzy.AggMethod = FuzzySystem.StringToAggMethod(CurrentLine.Split('\'')[1]); } else if (CurrentLine.StartsWith("DefuzzMethod=")) { fuzzy.DefuzzMethod = FuzzySystem.StringToDefuzzMethod(CurrentLine.Split('\'')[1]); } } } // When we reach an [Input_] tag, we read the setting from the following lines until we hit an empty line or the end of the file if (CurrentLine.StartsWith("[Input") && CurrentLine.EndsWith("]")) { FoundIn = true; int index = int.Parse(CurrentLine.Replace("[Input", "").Replace("]", "")) - 1; fuzzy.Inputs[index] = new InputOutput { IsInput = true, Index = index + 1 }; while ((CurrentLine = file.ReadLine()) != null && CurrentLine != "") { if (CurrentLine.StartsWith("Name=")) { fuzzy.Inputs[index].Name = CurrentLine.Replace("Name=", "").Replace("'", ""); } else if (CurrentLine.StartsWith("Range=")) { fuzzy.Inputs[index].Range[0] = float.Parse(CurrentLine.Replace("Range=[", "").Split(' ')[0], System.Globalization.CultureInfo.InvariantCulture); fuzzy.Inputs[index].Range[1] = float.Parse(CurrentLine.Replace("]", "").Split(' ')[1], System.Globalization.CultureInfo.InvariantCulture); } else if (CurrentLine.StartsWith("NumMFs=")) { fuzzy.Inputs[index].NumMFs = int.Parse(CurrentLine.Replace("NumMFs=", "")); } else if (CurrentLine.StartsWith("MF")) { try { fuzzy.Inputs[index].MFs[int.Parse(CurrentLine.Split('=')[0].Remove(0, 2)) - 1] = new MembershipFunction(CurrentLine.Split('=')[1]); } catch (Exception exc) { throw exc; } } } } // When we reach an [Output_] tag, we read the setting from the following lines until we hit an empty line or the end of the file if (CurrentLine.StartsWith("[Output") && CurrentLine.EndsWith("]")) { FoundOut = true; int index = int.Parse(CurrentLine.Replace("[Output", "").Replace("]", "")) - 1; fuzzy.Outputs[index] = new InputOutput { IsInput = false, Index = index + 1 }; while ((CurrentLine = file.ReadLine()) != null && CurrentLine != "") { if (CurrentLine.StartsWith("Name=")) { fuzzy.Outputs[index].Name = CurrentLine.Replace("Name=", "").Replace("'", ""); } else if (CurrentLine.StartsWith("Range=")) { fuzzy.Outputs[index].Range[0] = float.Parse(CurrentLine.Replace("Range=[", "").Split(' ')[0], System.Globalization.CultureInfo.InvariantCulture); fuzzy.Outputs[index].Range[1] = float.Parse(CurrentLine.Replace("]", "").Split(' ')[1], System.Globalization.CultureInfo.InvariantCulture); } else if (CurrentLine.StartsWith("NumMFs=")) { fuzzy.Outputs[index].NumMFs = int.Parse(CurrentLine.Replace("NumMFs=", "")); } else if (CurrentLine.StartsWith("MF")) { fuzzy.Outputs[index].MFs[int.Parse(CurrentLine.Split('=')[0].Remove(0, 2)) - 1] = new MembershipFunction(CurrentLine.Split('=')[1]); } } } // When we reach the [Rules] tag, we read the setting from the following lines until we hit an empty line or the end of the file if (CurrentLine.StartsWith("[Rules") && CurrentLine.EndsWith("]")) { FoundRules = true; int index = 0; while ((CurrentLine = file.ReadLine()) != null && CurrentLine != "") { fuzzy.Rules[index] = new Rule(CurrentLine); index++; } } } // Check if we found all relevant data if (!(FoundSystem && FoundIn && FoundOut && FoundRules)) { string missing = "Could not find the following tags:"; if (!FoundSystem) { missing += " [System]"; } if (!FoundIn) { missing += " [Input]"; } if (!FoundSystem) { missing += " [Output]"; } if (!FoundSystem) { missing += " [Rules]"; } throw new Exception(missing); } if (fuzzy != null) { return(fuzzy); } throw new Exception("Couldn't read fuzzy system."); }
internal static void ReadDataBase() { StreamReader DataBaseStreamReader = new StreamReader(UserDatabaseDir); bool ErrorsFound = false; string CurrentLine; int LineNumber = 0; while ((CurrentLine = DataBaseStreamReader.ReadLine()) != null) { if (CurrentLine.StartsWith("/u") && CurrentLine.EndsWith(".")) { Usernames.Add(GetElement(CurrentLine, "/u ", " /p")); Passwords.Add(GetElement(CurrentLine, "/p ", " /s")); Statuses.Add(GetElement(CurrentLine, "/s ", ".")); try { ProfilePictures.Add(GetBitmapFromBytes(File.ReadAllBytes(MainServerWorker.UserDirectories + Usernames[LineNumber] + "\\ProfilePicture_" + Usernames[LineNumber] + ".png"))); } catch { Bitmap ProfPicToAdd = new Bitmap(Properties.Resources.DefaultProfilePicture); WriteToConsole("[ERROR] Could not find profile picture for " + Usernames[LineNumber] + " using the default picture instead"); try { ProfPicToAdd.Save(MainServerWorker.UserDirectories + Usernames[LineNumber] + "\\ProfilePicture_" + Usernames[LineNumber] + ".png"); } catch (Exception exc) { WriteToConsole("[ERROR] Could not find profile picture for " + Usernames[LineNumber] + " due to " + exc); } ProfilePictures.Add(ProfPicToAdd); //use default pic } if (Usernames[LineNumber] != null && Passwords[LineNumber] != null && Statuses[LineNumber] != null) { WriteToConsole("[INFO] Successfully verified account " + Usernames[LineNumber]); } else { WriteToConsole("[ERROR] Could not verify account for " + Usernames[LineNumber]); ErrorsFound = true; } WriteToConsole("[INFO] Checking user directory for " + Usernames[LineNumber]); if (Directory.Exists(MainServerWorker.UserDirectories + Usernames[LineNumber]) == false) { WriteToConsole("[ERROR] Couldn't validate directory for user " + Usernames[LineNumber] + " creating it now..."); try { Directory.CreateDirectory(MainServerWorker.UserDirectories + Usernames[LineNumber]); WriteToConsole("[INFO] Succcessfully created user directory for " + Usernames[LineNumber]); WriteToConsole("[INFO] Creating default profile picture for " + Usernames[LineNumber]); try { Bitmap DeaultProfilePicture = new Bitmap(Properties.Resources.DefaultProfilePicture); System.Drawing.Image DefaultProfilePictureImage = DeaultProfilePicture; DefaultProfilePictureImage.Save(MainServerWorker.UserDirectories + Usernames[LineNumber] + "\\ProfilePicture_" + Usernames[LineNumber] + ".png", ImageFormat.Png); WriteToConsole("[INFO] Successfully created user profile picture for " + Usernames[LineNumber]); DeaultProfilePicture.Dispose(); } catch (Exception exc) { WriteToConsole("[INFO] Could not create default profile picture for user " + Usernames[LineNumber] + " due to " + exc); } } catch { WriteToConsole("[ERROR] Could not create directory for " + Usernames[LineNumber]); } } else { WriteToConsole("[INFO] Successfully validated directory for user " + Usernames[LineNumber]); } } else { WriteToConsole("[INFO] Incorrect initialization in the database format"); ErrorsFound = true; break; } LineNumber++; } DataBaseStreamReader.Close(); DataBaseStreamReader.Dispose(); if (ErrorsFound == false) { WriteToConsole("[INFO] Successfully appended " + LineNumber + " accounts from the database."); } else { WriteToConsole("[ERROR] There were one or more errors in the database format. If you know how to fix them then please inspect the database, otherwise delete it so a new one can generate"); } }