//new match to regex public static Boolean Test(string word, string REgex) { if (Validator.IsDelimited(REgex, Crozzle.StringDelimiters)) { REgex = REgex.Trim(Crozzle.StringDelimiters).Trim(); } return(Regex.IsMatch(word, @"^" + REgex + "$")); }
private void openCrozzleFile() { DialogResult result; // As we are opening a crozzle file, // indicate crozzle file, and crozzle are not valid, and clear GUI. crozzleToolStripMenuItem.Enabled = false; crozzleWebBrowser.DocumentText = ""; ErrorListViewer.WebBrowser.DocumentText = ""; // Process crozzle file. result = openFileDialog1.ShowDialog(); if (result == DialogResult.OK) { // Get configuration filename. String configurationFileName = GetConfigurationFileName(openFileDialog1.FileName); if (configurationFileName == null) { MessageBox.Show("configuration filename is missing from the crozzle file", ApplicationAboutBox.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Add root to the path else { String filename = configurationFileName.Trim(); if (Validator.IsDelimited(filename, Crozzle.StringDelimiters)) { filename = filename.Trim(Crozzle.StringDelimiters); } configurationFileName = filename; if (!Path.IsPathRooted(configurationFileName)) { configurationFileName = Path.GetDirectoryName(openFileDialog1.FileName) + @"\" + configurationFileName; } } // Parse configuration file. Configuration aConfiguration = null; Configuration.TryParse(configurationFileName, out aConfiguration); // Get wordlist filename. String wordListFileName = GetWordlistFileName(openFileDialog1.FileName); if (wordListFileName == null) { MessageBox.Show("wordlist filename is missing from the crozzle file", ApplicationAboutBox.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { String filename = wordListFileName.Trim(); if (Validator.IsDelimited(filename, Crozzle.StringDelimiters)) { filename = filename.Trim(Crozzle.StringDelimiters); } wordListFileName = filename; if (!Path.IsPathRooted(wordListFileName)) { wordListFileName = Path.GetDirectoryName(openFileDialog1.FileName) + @"\" + wordListFileName; } } // Parse wordlist file. WordList wordList = null; WordList.TryParse(wordListFileName, aConfiguration, out wordList); // Parse crozzle file. Crozzle aCrozzle; Crozzle.TryParse(openFileDialog1.FileName, aConfiguration, wordList, out aCrozzle); SIT323Crozzle = aCrozzle; // Update GUI - menu enabled, display crozzle data (whether valid or invalid), and crozzle file errors. if (SIT323Crozzle.FileValid && SIT323Crozzle.Configuration.Valid && SIT323Crozzle.WordList.Valid) { crozzleToolStripMenuItem.Enabled = true; } crozzleWebBrowser.DocumentText = SIT323Crozzle.ToStringHTML(); ErrorListViewer.WebBrowser.DocumentText = SIT323Crozzle.FileErrorsHTML + SIT323Crozzle.Configuration.FileErrorsHTML + SIT323Crozzle.WordList.FileErrorsHTML; // Log errors. SIT323Crozzle.LogFileErrors(SIT323Crozzle.FileErrorsTXT); SIT323Crozzle.LogFileErrors(SIT323Crozzle.Configuration.FileErrorsTXT); SIT323Crozzle.LogFileErrors(SIT323Crozzle.WordList.FileErrors); } }
public static Boolean TryParse(String path, Configuration aConfiguration, WordList wordList, out Crozzle aCrozzle, bool isRemote = false) { Errors = new List <String>(); aCrozzle = new Crozzle(path, aConfiguration, wordList, isRemote); // Open file. StreamReader fileIn = null; if (!isRemote) { fileIn = new StreamReader(path); } else { WebClient webClient = new WebClient(); Stream stream = webClient.OpenRead(path); fileIn = new StreamReader(stream); } List <String> wordData = new List <string>(); // Validate file. while (!fileIn.EndOfStream) { // Read a line. String line = fileIn.ReadLine(); // Parse a crozzle file item. CrozzleFileItem aCrozzleFileItem; if (CrozzleFileItem.TryParse(line, out aCrozzleFileItem)) { if (aCrozzleFileItem.IsConfigurationFile) { // Get the configuration file name. String configurationPath = aCrozzleFileItem.KeyValue.Value; if (configurationPath == null) { Errors.Add(CrozzleFileErrors.ConfigurationFilenameMissing); } else { configurationPath = configurationPath.Trim(); if (Validator.IsDelimited(configurationPath, StringDelimiters)) { configurationPath = configurationPath.Trim(StringDelimiters); } if (!isRemote && !Path.IsPathRooted(configurationPath)) { String directoryName = Path.GetDirectoryName(path); configurationPath = directoryName + @"\" + configurationPath; } aCrozzle.ConfigurationPath = configurationPath; } } else if (aCrozzleFileItem.IsWordListFile) { // Get the word list file name. String wordListPath = aCrozzleFileItem.KeyValue.Value; if (wordListPath == null) { Errors.Add(CrozzleFileErrors.WordlistFilenameMissing); } else { wordListPath = wordListPath.Trim(); if (Validator.IsDelimited(wordListPath, StringDelimiters)) { wordListPath = wordListPath.Trim(StringDelimiters); } if (!isRemote && !Path.IsPathRooted(wordListPath)) { String directoryName = Path.GetDirectoryName(path); wordListPath = directoryName + @"\" + wordListPath; } aCrozzle.WordListPath = wordListPath; } } else if (aCrozzleFileItem.IsRows) { // Get the number of rows. int rows; if (Validator.IsInt32(aCrozzleFileItem.KeyValue.Value.Trim(), out rows)) { aCrozzle.Rows = rows; } else { Errors.Add(String.Format(CrozzleFileErrors.RowError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aCrozzleFileItem.IsColumns) { // Get the number of columns. int columns; if (Validator.IsInt32(aCrozzleFileItem.KeyValue.Value.Trim(), out columns)) { aCrozzle.Columns = columns; } else { Errors.Add(String.Format(CrozzleFileErrors.ColumnError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aCrozzleFileItem.IsRow) { // Collect potential word data for a horizontal word. wordData.Add(aCrozzleFileItem.KeyValue.OriginalKeyValue); } else if (aCrozzleFileItem.IsColumn) { // Collect potential word data for a vertical word. wordData.Add(aCrozzleFileItem.KeyValue.OriginalKeyValue); } } else { Errors.AddRange(CrozzleFileItem.Errors); } } // Close files. fileIn.Close(); aCrozzle.Node = Node.GreedyAlgorithm(aCrozzle.Rows, aCrozzle.Columns, aConfiguration, wordList.List); // Validate file sections. // Check the configuration file name. if (aCrozzle.Configuration != null) { if (aCrozzle.Configuration.ConfigurationPath != aCrozzle.ConfigurationPath) { Errors.Add(String.Format(CrozzleFileErrors.ConfigurationFilenameError, aCrozzle.ConfigurationPath, aCrozzle.Configuration.ConfigurationFileName)); } } // Check the word list file name. if (aCrozzle.WordList != null) { if (aCrozzle.WordList.WordlistPath != aCrozzle.WordListPath) { Errors.Add(String.Format(CrozzleFileErrors.WordlistFilenameError, aCrozzle.WordListPath, aCrozzle.WordList.WordlistFileName)); } } aCrozzle.CrozzleRows = aCrozzle.Node.Grid.GridRows; aCrozzle.CrozzleColumns = aCrozzle.Node.Grid.GridColumns; // Store validity. aCrozzle.FileValid = Errors.Count == 0; return(aCrozzle.FileValid); }
public static Boolean TryParse(String path, Configuration aConfiguration, WordList wordList, out Crozzle aCrozzle) { Errors = new List <String>(); aCrozzle = new Crozzle(path, aConfiguration, wordList); // Open file. StreamReader fileIn = new StreamReader(path); List <String> wordData = new List <string>(); // Validate file. while (!fileIn.EndOfStream) { // Read a line. String line = fileIn.ReadLine(); // Parse a crozzle file item. CrozzleFileItem aCrozzleFileItem; if (CrozzleFileItem.TryParse(line, out aCrozzleFileItem)) { if (aCrozzleFileItem.IsConfigurationFile) { // Get the configuration file name. String configurationPath = aCrozzleFileItem.KeyValue.Value; if (configurationPath == null) { Errors.Add(CrozzleFileErrors.ConfigurationFilenameMissing); } else { configurationPath = configurationPath.Trim(); if (Validator.IsDelimited(configurationPath, StringDelimiters)) { configurationPath = configurationPath.Trim(StringDelimiters); } if (!Path.IsPathRooted(configurationPath)) { String directoryName = Path.GetDirectoryName(path); configurationPath = directoryName + @"\" + configurationPath; } aCrozzle.ConfigurationPath = configurationPath; } } else if (aCrozzleFileItem.IsWordListFile) { // Get the word list file name. String wordListPath = aCrozzleFileItem.KeyValue.Value; if (wordListPath == null) { Errors.Add(CrozzleFileErrors.WordlistFilenameMissing); } else { wordListPath = wordListPath.Trim(); if (Validator.IsDelimited(wordListPath, StringDelimiters)) { wordListPath = wordListPath.Trim(StringDelimiters); } if (!Path.IsPathRooted(wordListPath)) { String directoryName = Path.GetDirectoryName(path); wordListPath = directoryName + @"\" + wordListPath; } aCrozzle.WordListPath = wordListPath; } } else if (aCrozzleFileItem.IsRows) { // Get the number of rows. int rows; if (Validator.IsInt32(aCrozzleFileItem.KeyValue.Value.Trim(), out rows)) { aCrozzle.Rows = rows; } else { Errors.Add(String.Format(CrozzleFileErrors.RowError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aCrozzleFileItem.IsColumns) { // Get the number of columns. int columns; if (Validator.IsInt32(aCrozzleFileItem.KeyValue.Value.Trim(), out columns)) { aCrozzle.Columns = columns; } else { Errors.Add(String.Format(CrozzleFileErrors.ColumnError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aCrozzleFileItem.IsRow) { // Collect potential word data for a horizontal word. wordData.Add(aCrozzleFileItem.KeyValue.OriginalKeyValue); } else if (aCrozzleFileItem.IsColumn) { // Collect potential word data for a vertical word. wordData.Add(aCrozzleFileItem.KeyValue.OriginalKeyValue); } } else { Errors.AddRange(CrozzleFileItem.Errors); } } // Close files. fileIn.Close(); // Get potential word data list. WordDataList wordDataList; if (!WordDataList.TryParse(wordData, aCrozzle, out wordDataList)) { Errors.AddRange(WordDataList.Errors); } aCrozzle.WordDataList = wordDataList; // Validate file sections. // Check the configuration file name. if (aCrozzle.Configuration != null) { if (aCrozzle.Configuration.ConfigurationPath != aCrozzle.ConfigurationPath) { Errors.Add(String.Format(CrozzleFileErrors.ConfigurationFilenameError, aCrozzle.ConfigurationPath, aCrozzle.Configuration.ConfigurationFileName)); } } // Check the word list file name. if (aCrozzle.WordList != null) { if (aCrozzle.WordList.WordlistPath != aCrozzle.WordListPath) { Errors.Add(String.Format(CrozzleFileErrors.WordlistFilenameError, aCrozzle.WordListPath, aCrozzle.WordList.WordlistFileName)); } } // Raw word data of horizontal and vertical words were obtained when reading the crozzle file, // but now we need to create crozzle rows and crozzle columns that represent the crozzle. aCrozzle.CreateCrozzleRows(aCrozzle.WordDataList); aCrozzle.CreateCrozzleColumns(aCrozzle.WordDataList); // Store validity. aCrozzle.FileValid = Errors.Count == 0; return(aCrozzle.FileValid); }
public static Boolean TryParse(String path, out Configuration aConfiguration) { Errors = new List <String>(); ActualIntersectingKeys = new Boolean[26]; ActualNonIntersectingKeys = new Boolean[26]; ActualKeys = new List <string>(); aConfiguration = new Configuration(path); if (aConfiguration.ConfigurationFileName.IndexOfAny(Path.GetInvalidFileNameChars()) > -1) { Errors.Add(String.Format(ConfigurationErrors.FilenameError, path)); } else { StreamReader fileIn = new StreamReader(path); // Validate file. while (!fileIn.EndOfStream) { // Read a line. String line = fileIn.ReadLine(); // Parse a configuration item. ConfigurationFileItem aConfigurationItem; if (ConfigurationFileItem.TryParse(line, out aConfigurationItem)) { if (aConfigurationItem.KeyValue != null && ActualKeys.Contains(aConfigurationItem.KeyValue.Key)) { Errors.Add(String.Format(ConfigurationErrors.DuplicateKeyError, aConfigurationItem.KeyValue.OriginalKeyValue)); } else { // Record that this key has been found. if (aConfigurationItem.KeyValue != null) { ActualKeys.Add(aConfigurationItem.KeyValue.Key); } // Process the key-value. if (aConfigurationItem.IsLogFile) { // Get the value representing an invalid score. aConfiguration.LogFileName = aConfigurationItem.KeyValue.Value.Trim(); if (Validator.IsDelimited(aConfiguration.LogFileName, Crozzle.StringDelimiters)) { aConfiguration.LogFileName = aConfiguration.LogFileName.Trim(Crozzle.StringDelimiters); if (!Validator.IsFilename(aConfiguration.LogFileName)) { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMinimumNumberOfUniqueWords) { // Get the value of the minimum number of unique words allowed in the wordlist. int minimum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumNumberOfUniqueWords = minimum; if (!Validator.TryRange(minimum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMaximumNumberOfUniqueWords) { // Get the value of the maximum number of unique words allowed in the wordlist. int maximum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumNumberOfUniqueWords = maximum; if (!Validator.TryRange(maximum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsInvalidCrozzleScore) { // Get the value representing an invalid score. aConfiguration.InvalidCrozzleScore = aConfigurationItem.KeyValue.Value.Trim(); if (Validator.IsDelimited(aConfiguration.InvalidCrozzleScore, Crozzle.StringDelimiters)) { aConfiguration.InvalidCrozzleScore = aConfiguration.InvalidCrozzleScore.Trim(Crozzle.StringDelimiters); } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsUppercase) { // Get the Boolean value that determines whether to display the crozzle letters in uppercase or lowercase. Boolean uppercase = true; if (!Validator.IsMatch(aConfigurationItem.KeyValue.Value, allowedBooleans)) { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } aConfiguration.Uppercase = uppercase; } else if (aConfigurationItem.IsStyle) { // Get the value of the HTML style to display the crozzle in an HTML table. aConfiguration.Style = aConfigurationItem.KeyValue.Value.Trim(); if (Validator.IsDelimited(aConfiguration.Style, Crozzle.StringDelimiters)) { aConfiguration.Style = aConfiguration.Style.Trim(Crozzle.StringDelimiters); if (!Validator.IsStyleTag(aConfiguration.Style)) { Errors.Add(String.Format(ConfigurationErrors.StyleError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsBGcolourEmptyTD) { // Get the value of the background colour for an empty TD (HTML table data). aConfiguration.BGcolourEmptyTD = aConfigurationItem.KeyValue.Value.Trim(); if (!Validator.IsHexColourCode(aConfiguration.BGcolourEmptyTD)) { Errors.Add(String.Format(ConfigurationErrors.ColourError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsBGcolourNonEmptyTD) { // Get the value of the background colour for a non empty TD (HTML table data). aConfiguration.BGcolourNonEmptyTD = aConfigurationItem.KeyValue.Value.Trim(); if (!Validator.IsHexColourCode(aConfiguration.BGcolourNonEmptyTD)) { Errors.Add(String.Format(ConfigurationErrors.ColourError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMinimumNumberOfRows) { // Get the value of the minimum number of rows per crozzle. int minimum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumNumberOfRows = minimum; if (!Validator.TryRange(minimum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMaximumNumberOfRows) { // Get the value of the maximum number of rows per crozzle. int maximum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumNumberOfRows = maximum; if (!Validator.TryRange(maximum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMinimumNumberOfColumns) { // Get the value of the minimum number of columns per crozzle. int minimum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumNumberOfColumns = minimum; if (!Validator.TryRange(minimum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMaximumNumberOfColumns) { // Get the value of the maximum number of columns per crozzle. int maximum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumNumberOfColumns = maximum; if (!Validator.TryRange(maximum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMinimumHorizontalWords) { // Get the value of the minimum number of horizontal words in a crozzle. int minimum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumHorizontalWords = minimum; if (!Validator.TryRange(minimum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMaximumHorizontalWords) { // Get the value of the maximum number of horizontal words in a crozzle. int maximum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumHorizontalWords = maximum; if (!Validator.TryRange(maximum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMinimumVerticalWords) { // Get the value of the minimum number of vertical words in a crozzle. int minimum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumVerticalWords = minimum; if (!Validator.TryRange(minimum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMaximumVerticalWords) { // Get the value of the maximum number of vertical words in a crozzle. int maximum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumVerticalWords = maximum; if (!Validator.TryRange(maximum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMinimumIntersectionsInHorizontalWords) { // Get the value of the minimum number of the intersections in a horizontal word. int minimum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumIntersectionsInHorizontalWords = minimum; if (!Validator.TryRange(minimum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMaximumIntersectionsInHorizontalWords) { // Get the value of the maximum number of the intersections in a horizontal word. int maximum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumIntersectionsInHorizontalWords = maximum; if (!Validator.TryRange(maximum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMinimumIntersectionsInVerticalWords) { // Get the value of the minimum number of the intersections in a vertical word. int minimum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumIntersectionsInVerticalWords = minimum; if (!Validator.TryRange(minimum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMaximumIntersectionsInVerticalWords) { // Get the value of the maximum number of the intersections in a vertical word. int maximum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumIntersectionsInVerticalWords = maximum; if (!Validator.TryRange(maximum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMinimumNumberOfTheSameWord) { // Get the value of the minimum number of the same word per crozzle limit. int minimum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumNumberOfTheSameWord = minimum; if (!Validator.TryRange(minimum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMaximumNumberOfTheSameWord) { // Get the value of the maximum number of the same word per crozzle limit. int maximum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumNumberOfTheSameWord = maximum; if (!Validator.TryRange(maximum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMinimumNumberOfGroups) { // Get the value of the minimum number of groups per crozzle limit. int minimum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumNumberOfGroups = minimum; if (!Validator.TryRange(minimum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsMaximumNumberOfGroups) { // Get the value of the maximum number of groups per crozzle limit. int maximum; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumNumberOfGroups = maximum; if (!Validator.TryRange(maximum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsPointsPerWord) { // Get the value of points per words. int pointsPerWord; if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out pointsPerWord)) { aConfiguration.PointsPerWord = pointsPerWord; if (!Validator.TryRange(pointsPerWord, 0, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aConfigurationItem.IsIntersecting) { // Get the values of each INTERSECTING point. String originalValues = aConfigurationItem.KeyValue.Value.Trim(); if (Validator.IsDelimited(originalValues, Crozzle.StringDelimiters)) { originalValues = originalValues.Trim(Crozzle.StringDelimiters).Trim(); } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, originalValues, Validator.Errors[0])); } String[] intersectingPoints = originalValues.Split(PointSeparators); foreach (String intersectingPoint in intersectingPoints) { KeyValue aKeyValue; if (KeyValue.TryParse(intersectingPoint, @"[A-Z]", out aKeyValue)) { int points; if (Validator.IsInt32(aKeyValue.Value, out points)) { int index = (int)aKeyValue.Key[0] - (int)'A'; aConfiguration.IntersectingPointsPerLetter[index] = points; ActualIntersectingKeys[index] = true; } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aKeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.AddRange(KeyValue.Errors); } } } else if (aConfigurationItem.IsNonIntersecting) { // Get the value of each NONINTERSECTING point. String originalValues = aConfigurationItem.KeyValue.Value.Trim(); if (Validator.IsDelimited(originalValues, Crozzle.StringDelimiters)) { originalValues = originalValues.Trim(Crozzle.StringDelimiters).Trim(); } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, originalValues, Validator.Errors[0])); } String[] nonIntersectingPoints = originalValues.Split(PointSeparators); foreach (String nonIntersectingPoint in nonIntersectingPoints) { KeyValue aKeyValue; if (KeyValue.TryParse(nonIntersectingPoint, @"[A-Z]", out aKeyValue)) { int points; if (Validator.IsInt32(aKeyValue.Value, out points)) { int index = (int)aKeyValue.Key[0] - (int)'A'; aConfiguration.NonIntersectingPointsPerLetter[index] = points; ActualNonIntersectingKeys[index] = true; } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aKeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.AddRange(KeyValue.Errors); } } } } } else { Errors.AddRange(ConfigurationFileItem.Errors); } } // Close files. fileIn.Close(); // Check which keys are missing from the configuration file. foreach (string expectedKey in ExpectedKeys) { if (!ActualKeys.Contains(expectedKey)) { Errors.Add(String.Format(ConfigurationErrors.MissingKeyError, expectedKey)); } } for (char ch = 'A'; ch <= 'Z'; ch++) { if (!ActualIntersectingKeys[(int)ch - (int)'A']) { Errors.Add(String.Format(ConfigurationErrors.MissingIntersectionKeyError, ch.ToString())); } } for (char ch = 'A'; ch <= 'Z'; ch++) { if (!ActualNonIntersectingKeys[(int)ch - (int)'A']) { Errors.Add(String.Format(ConfigurationErrors.MissingNonIntersectionKeyError, ch.ToString())); } } // Check that minimum values are <= to their maximmum counterpart values. if (ActualKeys.Contains("MINIMUM_NUMBER_OF_UNIQUE_WORDS") && ActualKeys.Contains("MAXIMUM_NUMBER_OF_UNIQUE_WORDS")) { if (aConfiguration.MinimumNumberOfUniqueWords > aConfiguration.MaximumNumberOfUniqueWords) { Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM_NUMBER_OF_UNIQUE_WORDS", aConfiguration.MinimumNumberOfUniqueWords, aConfiguration.MaximumNumberOfUniqueWords)); } } if (ActualKeys.Contains("MINIMUM_NUMBER_OF_ROWS") && ActualKeys.Contains("MAXIMUM_NUMBER_OF_ROWS")) { if (aConfiguration.MinimumNumberOfRows > aConfiguration.MaximumNumberOfRows) { Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM_NUMBER_OF_ROWS", aConfiguration.MinimumNumberOfRows, aConfiguration.MaximumNumberOfRows)); } } if (ActualKeys.Contains("MINIMUM_NUMBER_OF_COLUMNS") && ActualKeys.Contains("MAXIMUM_NUMBER_OF_COLUMNS")) { if (aConfiguration.MinimumNumberOfColumns > aConfiguration.MaximumNumberOfColumns) { Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM_NUMBER_OF_COLUMNS", aConfiguration.MinimumNumberOfColumns, aConfiguration.MaximumNumberOfColumns)); } } if (ActualKeys.Contains("MINIMUM_HORIZONTAL_WORDS") && ActualKeys.Contains("MAXIMUM_HORIZONTAL_WORDS")) { if (aConfiguration.MinimumHorizontalWords > aConfiguration.MaximumHorizontalWords) { Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM_HORIZONTAL_WORDS", aConfiguration.MinimumHorizontalWords, aConfiguration.MaximumHorizontalWords)); } } if (ActualKeys.Contains("MINIMUM_VERTICAL_WORDS") && ActualKeys.Contains("MAXIMUM_VERTICAL_WORDS")) { if (aConfiguration.MinimumVerticalWords > aConfiguration.MaximumVerticalWords) { Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM_VERTICAL_WORDS", aConfiguration.MinimumVerticalWords, aConfiguration.MaximumVerticalWords)); } } if (ActualKeys.Contains("MINIMUM_INTERSECTIONS_IN_HORIZONTAL_WORDS") && ActualKeys.Contains("MAXIMUM_INTERSECTIONS_IN_HORIZONTAL_WORDS")) { if (aConfiguration.MinimumIntersectionsInHorizontalWords > aConfiguration.MaximumIntersectionsInHorizontalWords) { Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM_INTERSECTIONS_IN_HORIZONTAL_WORDS", aConfiguration.MinimumIntersectionsInHorizontalWords, aConfiguration.MaximumIntersectionsInHorizontalWords)); } } if (ActualKeys.Contains("MAXIMUM_INTERSECTIONS_IN_VERTICAL_WORDS") && ActualKeys.Contains("MAXIMUM_INTERSECTIONS_IN_VERTICAL_WORDS")) { if (aConfiguration.MinimumIntersectionsInVerticalWords > aConfiguration.MaximumIntersectionsInVerticalWords) { Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MAXIMUM_INTERSECTIONS_IN_VERTICAL_WORDS", aConfiguration.MinimumIntersectionsInVerticalWords, aConfiguration.MaximumIntersectionsInVerticalWords)); } } if (ActualKeys.Contains("MINIMUM_NUMBER_OF_THE_SAME_WORD") && ActualKeys.Contains("MAXIMUM_NUMBER_OF_THE_SAME_WORD")) { if (aConfiguration.MinimumNumberOfTheSameWord > aConfiguration.MaximumNumberOfTheSameWord) { Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM_NUMBER_OF_THE_SAME_WORD", aConfiguration.MinimumNumberOfTheSameWord, aConfiguration.MaximumNumberOfTheSameWord)); } } if (ActualKeys.Contains("MINIMUM_NUMBER_OF_GROUPS") && ActualKeys.Contains("MAXIMUM_NUMBER_OF_GROUPS")) { if (aConfiguration.MinimumNumberOfGroups > aConfiguration.MaximumNumberOfGroups) { Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM_NUMBER_OF_GROUPS", aConfiguration.MinimumNumberOfGroups, aConfiguration.MaximumNumberOfGroups)); } } } // Store validity. aConfiguration.Valid = Errors.Count == 0; return(aConfiguration.Valid); }
public static Boolean TryParse(String path, Configuration aConfiguration, WordList wordList, out Crozzle aCrozzle) { Errors = new List <String>(); aCrozzle = new Crozzle(path, aConfiguration, wordList); // Open file. StreamReader fileIn = new StreamReader(path); List <String> wordData = new List <string>(); String section = null; bool newBlock = false; // Validate file while (!fileIn.EndOfStream) { // Read a line. String line = fileIn.ReadLine(); // Processing empty lines if (Regex.IsMatch(line, @"^\s*$")) { continue; } line = line.Trim(); // Processing comments if (line.Contains("//")) { if (line.StartsWith("//")) { continue; } else { Errors.Add(String.Format(ConfigurationErrors.MixConfigWithComentError, line)); continue; } } // Section check switch (line) { case "FILE-DEPENDENCIES": case "CROZZLE-SIZE": case "HORIZONTAL-SEQUENCES": case "VERTICAL-SEQUENCES": section = line; newBlock = true; break; case "END-FILE-DEPENDENCIES": case "END-CROZZLE-SIZE": case "END-HORIZONTAL-SEQUENCES": case "END-VERTICAL-SEQUENCES": section = null; newBlock = true; break; default: break; } if (newBlock) { newBlock = false; continue; } // Parse a crozzle file item. CrozzleFileItem aCrozzleFileItem; // Out of section comment if (section == null) { Errors.Add(String.Format(ConfigurationErrors.OutOfSectionError, line)); } // Parse a crozzle item else if (CrozzleFileItem.TryParse(line, out aCrozzleFileItem)) { if (aCrozzleFileItem.IsConfigurationFile) { // Get the configuration file name. String configurationPath = aCrozzleFileItem.KeyValue.Value; if (configurationPath == null) { Errors.Add(CrozzleFileErrors.ConfigurationFilenameMissing); } else { configurationPath = configurationPath.Trim(); if (Validator.IsDelimited(configurationPath, StringDelimiters)) { configurationPath = configurationPath.Trim(StringDelimiters); } if (!Path.IsPathRooted(configurationPath)) { String directoryName = Path.GetDirectoryName(path); configurationPath = directoryName + @"\" + configurationPath; } aCrozzle.ConfigurationPath = configurationPath; } } else if (aCrozzleFileItem.IsWordListFile) { // Get the word list file name. String wordListPath = aCrozzleFileItem.KeyValue.Value; if (wordListPath == null) { Errors.Add(CrozzleFileErrors.WordlistFilenameMissing); } else { wordListPath = wordListPath.Trim(); if (Validator.IsDelimited(wordListPath, StringDelimiters)) { wordListPath = wordListPath.Trim(StringDelimiters); } if (!Path.IsPathRooted(wordListPath)) { String directoryName = Path.GetDirectoryName(path); wordListPath = directoryName + @"\" + wordListPath; } aCrozzle.WordListPath = wordListPath; } } // Replace else if (aCrozzleFileItem.IsSize) { // Split row & col String rawSizeData = aCrozzleFileItem.KeyValue.Value.Trim(); String[] sizeSplit = rawSizeData.Split(new String[] { SizeDelimiter }, 2, StringSplitOptions.None); if (sizeSplit.Length != SizeValueLength) { Errors.Add(String.Format(KeyValueErrors.FieldCountError, sizeSplit.Length, rawSizeData, SizeValueLength)); } else { int rows; if (Validator.IsInt32(sizeSplit[0].Trim(), out rows)) { aCrozzle.Rows = rows; } else { Errors.Add(String.Format(CrozzleFileErrors.RowError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } int columns; if (Validator.IsInt32(sizeSplit[1].Trim(), out columns)) { aCrozzle.Columns = columns; } else { Errors.Add(String.Format(CrozzleFileErrors.ColumnError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } } else if (aCrozzleFileItem.IsSequence) { // Collect potential word data for a horizontal word. //wordData.Add(aCrozzleFileItem.KeyValue.OriginalKeyValue); String type; if (section == "HORIZONTAL-SEQUENCES") { type = "ROW"; try { String oldFormat = aCrozzleFileItem.KeyValue.OriginalKeyValue; oldFormat = oldFormat.Substring("SEQUENCE=".Length); string[] split; split = oldFormat.Split(','); String key = split[0]; String row = split[1].Split('=')[1]; String col = split[2]; String newFormat = type + "=" + row + "," + key + "," + col; Console.WriteLine("New Format = " + newFormat); wordData.Add(newFormat); } catch (Exception e) { Errors.Add(String.Format(CrozzleFileErrors.SequenceFormatError, aCrozzleFileItem.KeyValue.OriginalKeyValue)); } } else if (section == "VERTICAL-SEQUENCES") { type = "COLUMN"; try { String oldFormat = aCrozzleFileItem.KeyValue.OriginalKeyValue; oldFormat = oldFormat.Substring("SEQUENCE=".Length); string[] split; split = oldFormat.Split(','); String key = split[0]; String row = split[1].Split('=')[1]; String col = split[2]; String newFormat = type + "=" + col + "," + key + "," + row; Console.WriteLine("New Format = " + newFormat); wordData.Add(newFormat); } catch (Exception e) { Errors.Add(String.Format(CrozzleFileErrors.SequenceFormatError, aCrozzleFileItem.KeyValue.OriginalKeyValue)); } } else { Errors.AddRange(CrozzleFileItem.Errors); break; } } } else { Errors.AddRange(CrozzleFileItem.Errors); } } // Close files. fileIn.Close(); // Get potential word data list. WordDataList wordDataList; if (!WordDataList.TryParse(wordData, aCrozzle, out wordDataList)) { Errors.AddRange(WordDataList.Errors); } aCrozzle.WordDataList = wordDataList; // Validate file sections. // Check the configuration file name. if (aCrozzle.Configuration != null) { if (aCrozzle.Configuration.ConfigurationPath != aCrozzle.ConfigurationPath) { Errors.Add(String.Format(CrozzleFileErrors.ConfigurationFilenameError, aCrozzle.ConfigurationPath, aCrozzle.Configuration.ConfigurationFileName)); } } // Check the word list file name. if (aCrozzle.WordList != null) { if (aCrozzle.WordList.WordlistPath != aCrozzle.WordListPath) { Errors.Add(String.Format(CrozzleFileErrors.WordlistFilenameError, aCrozzle.WordListPath, aCrozzle.WordList.WordlistFileName)); } } // Raw word data of horizontal and vertical words were obtained when reading the crozzle file, // but now we need to create crozzle rows and crozzle columns that represent the crozzle. aCrozzle.CreateCrozzleRows(aCrozzle.WordDataList); aCrozzle.CreateCrozzleColumns(aCrozzle.WordDataList); // Store validity. aCrozzle.FileValid = Errors.Count == 0; return(aCrozzle.FileValid); }
public static Boolean TryParse(String path, out Configuration aConfiguration) { Errors = new List <String>(); ActualIntersectingKeys = new Boolean[26]; ActualNonIntersectingKeys = new Boolean[26]; ActualKeys = new List <string>(); aConfiguration = new Configuration(path); if (aConfiguration.ConfigurationFileName.IndexOfAny(Path.GetInvalidFileNameChars()) > -1) { Errors.Add(String.Format(ConfigurationErrors.FilenameError, path)); } else { StreamReader fileIn = new StreamReader(path); // Validate file. while (!fileIn.EndOfStream) { List <String> fragment = new List <String>(); do { /// Add multiple lines as part of a text fragment until the last element is empty or null. fragment.Add(fileIn.ReadLine()); } while (!String.IsNullOrEmpty(fragment.Last())); // Parse a configuration fragment. FileFragment <ConfigurationFileItem> aConfigurationFileFragment; if (ConfigurationFileItem.TryParse(fragment, out aConfigurationFileFragment)) { List <ConfigurationFileItem> aItemsWithErrors; if ((aItemsWithErrors = aConfigurationFileFragment.Items.Where(item => item.KeyValue != null && ActualKeys.Contains(item.KeyValue.Key)).ToList()).Any()) { foreach (ConfigurationFileItem error in aItemsWithErrors) { Errors.Add(String.Format(ConfigurationErrors.DuplicateKeyError, error.KeyValue.OriginalKeyValue)); } } else { /// TODO: Process key record and key-value validation. String aFragmentKey = aConfigurationFileFragment.Name; foreach (ConfigurationFileItem aItem in aConfigurationFileFragment.Items) { /// Record that this key has been found. if (aItem.KeyValue.Key != null) { /// Identified fragments contain keys that have the same string. To minimise duplicate key error, lead the key with the fragment's name. if (new string[] { ConfigurationKeys.UNIQUESEQ_OPENBRACKET, ConfigurationKeys.DUPE_OPENBRACKET, ConfigurationKeys.VALIDGROUP_OPENBRACKET, ConfigurationKeys.DIRECTIONALSEQ_OPENBRACKET, ConfigurationKeys.DIRECTIONALINTERSECT_OPENBRACKET }.Contains(aFragmentKey)) { ActualKeys.Add(aFragmentKey + "-" + aItem.KeyValue.Key); } else if (!(new string[] { ConfigurationKeys.INTERSECT_OPENBRACKET, ConfigurationKeys.NONINTERSECT_OPENBRACKET }.Contains(aFragmentKey))) { ActualKeys.Add(aItem.KeyValue.Key); } } /// Process the key-value, given the current fragment key. if (aFragmentKey.Equals(ConfigurationKeys.LOGFILE_OPENBRACKET)) { if (aItem.Name == ConfigurationKeys.LOGFILE_FILENAME) { aConfiguration.LogFileName = aItem.KeyValue.Value.Trim(); if (Validator.IsDelimited(aConfiguration.LogFileName, Crozzle.StringDelimiters)) { String value = aConfiguration.LogFileName.Trim(Crozzle.StringDelimiters); aConfiguration.LogFileName = (!String.IsNullOrEmpty(value)) ? value : DefaultLogFileName; if (!Validator.IsFilename(aConfiguration.LogFileName)) { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } } else if (aFragmentKey.Equals(ConfigurationKeys.UNIQUESEQ_OPENBRACKET)) { if (aItem.Name == ConfigurationKeys.UNIQUESEQ_MINIMUM) { int minimum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumNumberOfUniqueWords = minimum; if (!Validator.TryRange(minimum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aItem.Name == ConfigurationKeys.UNIQUESEQ_MAXIMUM) { int maximum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumNumberOfUniqueWords = maximum; if (!Validator.TryRange(maximum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } } else if (aFragmentKey.Equals(ConfigurationKeys.OUTPUT_OPENBRACKET)) { if (aItem.Name == ConfigurationKeys.OUTPUT_INVALID) { aConfiguration.InvalidCrozzleScore = aItem.KeyValue.Value.Trim(); if (Validator.IsDelimited(aConfiguration.InvalidCrozzleScore, Crozzle.StringDelimiters)) { aConfiguration.InvalidCrozzleScore = aConfiguration.InvalidCrozzleScore.Trim(Crozzle.StringDelimiters); } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aItem.Name == ConfigurationKeys.OUTPUT_UPPERCASE) { Boolean uppercase = true; if (!Validator.IsMatch(aItem.KeyValue.Value, allowedBooleans)) { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } aConfiguration.Uppercase = uppercase; } else if (aItem.Name == ConfigurationKeys.OUTPUT_STYLE) { aConfiguration.Style = aItem.KeyValue.Value.Trim(); if (Validator.IsDelimited(aConfiguration.Style, Crozzle.StringDelimiters)) { aConfiguration.Style = aConfiguration.Style.Trim(Crozzle.StringDelimiters); if (!Validator.IsStyleTag(aConfiguration.Style)) { Errors.Add(String.Format(ConfigurationErrors.StyleError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aItem.Name == ConfigurationKeys.OUTPUT_BGCOLOUR_EMPTY) { aConfiguration.BGcolourEmptyTD = aItem.KeyValue.Value.Trim(); if (!Validator.IsHexColourCode(aConfiguration.BGcolourEmptyTD)) { Errors.Add(String.Format(ConfigurationErrors.ColourError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aItem.Name == ConfigurationKeys.OUTPUT_BGCOLOUR_NONEMPTY) { aConfiguration.BGcolourNonEmptyTD = aItem.KeyValue.Value.Trim(); if (!Validator.IsHexColourCode(aConfiguration.BGcolourNonEmptyTD)) { Errors.Add(String.Format(ConfigurationErrors.ColourError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } } else if (aFragmentKey.Equals(ConfigurationKeys.SIZE_OPENBRACKET)) { if (aItem.Name == ConfigurationKeys.SIZE_MINIMUMROWS) { int minimum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumNumberOfRows = minimum; if (!Validator.TryRange(minimum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aItem.Name == ConfigurationKeys.SIZE_MAXIMUMROWS) { int maximum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumNumberOfRows = maximum; if (!Validator.TryRange(maximum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aItem.Name == ConfigurationKeys.SIZE_MINIMUMCOLUMNS) { int minimum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumNumberOfColumns = minimum; if (!Validator.TryRange(minimum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aItem.Name == ConfigurationKeys.SIZE_MAXIMUMCOLUMNS) { int maximum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumNumberOfColumns = maximum; if (!Validator.TryRange(maximum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } } else if (aFragmentKey.Equals(ConfigurationKeys.DIRECTIONALSEQ_OPENBRACKET)) { if (aItem.Name == ConfigurationKeys.DIRECTIONALSEQ_MINIMUMHORZ) { int minimum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumHorizontalWords = minimum; if (!Validator.TryRange(minimum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aItem.Name == ConfigurationKeys.DIRECTIONALSEQ_MAXIMUMHORZ) { int maximum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumHorizontalWords = maximum; if (!Validator.TryRange(maximum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aItem.Name == ConfigurationKeys.DIRECTIONALSEQ_MINIMUMVERT) { int minimum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumVerticalWords = minimum; if (!Validator.TryRange(minimum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aItem.Name == ConfigurationKeys.DIRECTIONALSEQ_MAXIMUMVERT) { int maximum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumVerticalWords = maximum; if (!Validator.TryRange(maximum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } } else if (aFragmentKey.Equals(ConfigurationKeys.DIRECTIONALINTERSECT_OPENBRACKET)) { if (aItem.Name == ConfigurationKeys.DIRECTIONALINTERSECT_MINIMUMHORZ) { int minimum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumIntersectionsInHorizontalWords = minimum; if (!Validator.TryRange(minimum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aItem.Name == ConfigurationKeys.DIRECTIONALINTERSECT_MAXIMUMHORZ) { int maximum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumIntersectionsInHorizontalWords = maximum; if (!Validator.TryRange(maximum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aItem.Name == ConfigurationKeys.DIRECTIONALINTERSECT_MINIMUMVERT) { int minimum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumIntersectionsInVerticalWords = minimum; if (!Validator.TryRange(minimum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aItem.Name == ConfigurationKeys.DIRECTIONALINTERSECT_MAXIMUMVERT) { int maximum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumIntersectionsInVerticalWords = maximum; if (!Validator.TryRange(maximum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } } else if (aFragmentKey.Equals(ConfigurationKeys.DUPE_OPENBRACKET)) { if (aItem.Name == ConfigurationKeys.DUPE_MINIMUMVALID) { int minimum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumNumberOfTheSameWord = minimum; if (!Validator.TryRange(minimum, 0, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aItem.Name == ConfigurationKeys.DUPE_MAXIMUMVALID) { int maximum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumNumberOfTheSameWord = maximum; if (!Validator.TryRange(maximum, 0, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } } else if (aFragmentKey.Equals(ConfigurationKeys.VALIDGROUP_OPENBRACKET)) { if (aItem.Name == ConfigurationKeys.VALIDGROUP_MINIMUM) { int minimum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum)) { aConfiguration.MinimumNumberOfGroups = minimum; if (!Validator.TryRange(minimum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aItem.Name == ConfigurationKeys.VALIDGROUP_MAXIMUM) { int maximum; if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum)) { aConfiguration.MaximumNumberOfGroups = maximum; if (!Validator.TryRange(maximum, 1, Int32.MaxValue)) { Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } } else if (aFragmentKey.Equals(ConfigurationKeys.INTERSECT_OPENBRACKET)) { if (aItem.Name.Length == 1) /// ConfigurationFileItem.cs defines the key as the letter used in the CFG { String originalValues = aItem.KeyValue.Value.Trim(); int points; if (Validator.IsInt32(aItem.KeyValue.Value, out points)) { int index = (int)aItem.KeyValue.Key[0] - (int)'A'; aConfiguration.IntersectingPointsPerLetter[index] = points; ActualIntersectingKeys[index] = true; } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aFragmentKey.Equals(ConfigurationKeys.NONINTERSECT_OPENBRACKET)) { if (aItem.Name.Length == 1) /// ConfigurationFileItem.cs defines the key as the letter used in the CFG { String originalValues = aItem.KeyValue.Value.Trim(); int points; if (Validator.IsInt32(aItem.KeyValue.Value, out points)) { int index = (int)aItem.KeyValue.Key[0] - (int)'A'; aConfiguration.NonIntersectingPointsPerLetter[index] = points; ActualNonIntersectingKeys[index] = true; } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else { Errors.AddRange(ConfigurationFileItem.Errors); } } } } } // Close files. fileIn.Close(); // Check which keys are missing from the configuration file. foreach (string expectedKey in ExpectedKeys) { if (!ActualKeys.Contains(expectedKey)) { Errors.Add(String.Format(ConfigurationErrors.MissingKeyError, expectedKey)); } } for (char ch = 'A'; ch <= 'Z'; ch++) { if (!ActualIntersectingKeys[(int)ch - (int)'A']) { Errors.Add(String.Format(ConfigurationErrors.MissingIntersectionKeyError, ch.ToString())); } } for (char ch = 'A'; ch <= 'Z'; ch++) { if (!ActualNonIntersectingKeys[(int)ch - (int)'A']) { Errors.Add(String.Format(ConfigurationErrors.MissingNonIntersectionKeyError, ch.ToString())); } } // Check that minimum values are <= to their maximmum counterpart values. if (ActualKeys.Contains("SEQUENCES-IN-FILE-MINIMUM") && ActualKeys.Contains("SEQUENCES-IN-FILE-MAXIMUM")) { if (aConfiguration.MinimumNumberOfUniqueWords > aConfiguration.MaximumNumberOfUniqueWords) { Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "SEQUENCES-IN-FILE-MINIMUM", aConfiguration.MinimumNumberOfUniqueWords, aConfiguration.MaximumNumberOfUniqueWords)); } } if (ActualKeys.Contains("MINIMUM-ROWS") && ActualKeys.Contains("MAXIMUM-ROWS")) { if (aConfiguration.MinimumNumberOfRows > aConfiguration.MaximumNumberOfRows) { Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM-ROWS", aConfiguration.MinimumNumberOfRows, aConfiguration.MaximumNumberOfRows)); } } if (ActualKeys.Contains("MINIMUM-COLUMNS") && ActualKeys.Contains("MAXIMUM-COLUMNS")) { if (aConfiguration.MinimumNumberOfColumns > aConfiguration.MaximumNumberOfColumns) { Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM-COLUMNS", aConfiguration.MinimumNumberOfColumns, aConfiguration.MaximumNumberOfColumns)); } } if (ActualKeys.Contains("SEQUENCES-IN-CROZZLE-MINIMUM-HORIZONTAL") && ActualKeys.Contains("SEQUENCES-IN-CROZZLE-MAXIMUM-HORIZONTAL")) { if (aConfiguration.MinimumHorizontalWords > aConfiguration.MaximumHorizontalWords) { Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "SEQUENCES-IN-CROZZLE-MINIMUM-HORIZONTAL", aConfiguration.MinimumHorizontalWords, aConfiguration.MaximumHorizontalWords)); } } if (ActualKeys.Contains("SEQUENCES-IN-CROZZLE-MINIMUM-VERTICAL") && ActualKeys.Contains("SEQUENCES-IN-CROZZLE-MAXIMUM-VERTICAL")) { if (aConfiguration.MinimumVerticalWords > aConfiguration.MaximumVerticalWords) { Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "SEQUENCES-IN-CROZZLE-MINIMUM-VERTICAL", aConfiguration.MinimumVerticalWords, aConfiguration.MaximumVerticalWords)); } } if (ActualKeys.Contains("INTERSECTIONS-IN-SEQUENCES-MINIMUM-HORIZONTAL") && ActualKeys.Contains("INTERSECTIONS-IN-SEQUENCES-MINIMUM-HORIZONTAL")) { if (aConfiguration.MinimumIntersectionsInHorizontalWords > aConfiguration.MaximumIntersectionsInHorizontalWords) { Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "INTERSECTIONS-IN-SEQUENCES-MINIMUM-VERTICAL", aConfiguration.MinimumIntersectionsInHorizontalWords, aConfiguration.MaximumIntersectionsInHorizontalWords)); } } if (ActualKeys.Contains("INTERSECTIONS-IN-SEQUENCES-MINIMUM-VERTICAL") && ActualKeys.Contains("INTERSECTIONS-IN-SEQUENCES-MINIMUM-VERTICAL")) { if (aConfiguration.MinimumIntersectionsInVerticalWords > aConfiguration.MaximumIntersectionsInVerticalWords) { Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "INTERSECTIONS-IN-SEQUENCES-MINIMUM-VERTICAL", aConfiguration.MinimumIntersectionsInVerticalWords, aConfiguration.MaximumIntersectionsInVerticalWords)); } } if (ActualKeys.Contains("DUPLICATE-SEQUENCES-MINIMUM") && ActualKeys.Contains("DUPLICATE-SEQUENCES-MAXIMUM")) { if (aConfiguration.MaximumNumberOfTheSameWord < aConfiguration.MinimumNumberOfTheSameWord) { Errors.Add(String.Format(ConfigurationErrors.MaxDupeLowerThanMinError, "DUPLICATE-SEQUENCES-MAXIMUM", aConfiguration.MaximumNumberOfTheSameWord, aConfiguration.MaximumNumberOfTheSameWord)); } } if (ActualKeys.Contains("VALID-GROUPS-MINIMUM") && ActualKeys.Contains("VALID-GROUPS-MAXIMUM")) { if (aConfiguration.MinimumNumberOfGroups > aConfiguration.MaximumNumberOfGroups) { Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "VALID-GROUPS-MINIMUM", aConfiguration.MinimumNumberOfGroups, aConfiguration.MaximumNumberOfGroups)); } } } // Store validity. aConfiguration.Valid = Errors.Count == 0; return(aConfiguration.Valid); }
public static Boolean TryParse(String path, Configuration aConfiguration, WordList wordList, out Crozzle aCrozzle) { Errors = new List <String>(); aCrozzle = new Crozzle(path, aConfiguration, wordList); StreamReader fileIn; if (path.Contains("http")) { WebClient webClient = new WebClient(); Stream aStream = webClient.OpenRead(path); fileIn = new StreamReader(aStream); } else { fileIn = new StreamReader(path); } List <String> wordData = new List <string>(); // Validate file. while (!fileIn.EndOfStream) { // Read a line. String line = fileIn.ReadLine(); // Parse a crozzle file item. CrozzleFileItem aCrozzleFileItem; if (CrozzleFileItem.TryParse(line, out aCrozzleFileItem)) { if (aCrozzleFileItem.IsConfigurationFile) { // Get the configuration file name. String configurationPath = aCrozzleFileItem.KeyValue.Value; if (configurationPath == null) { Errors.Add(CrozzleFileErrors.ConfigurationFilenameMissing); } else { configurationPath = configurationPath.Trim(); if (Validator.IsDelimited(configurationPath, StringDelimiters)) { configurationPath = configurationPath.Trim(StringDelimiters); } if (!Path.IsPathRooted(configurationPath)) { // Seperate local files and files from web server if (!path.Contains("http")) { String directoryName = Path.GetDirectoryName(path); configurationPath = directoryName + @"\" + configurationPath; } } aCrozzle.ConfigurationPath = configurationPath; } } else if (aCrozzleFileItem.IsWordListFile) { // Get the word list file name. String wordListPath = aCrozzleFileItem.KeyValue.Value; if (wordListPath == null) { Errors.Add(CrozzleFileErrors.WordlistFilenameMissing); } else { wordListPath = wordListPath.Trim(); if (Validator.IsDelimited(wordListPath, StringDelimiters)) { wordListPath = wordListPath.Trim(StringDelimiters); } if (!Path.IsPathRooted(wordListPath)) { // Seperate local files and files from web server if (!path.Contains("http")) { String directoryName = Path.GetDirectoryName(path); wordListPath = directoryName + @"\" + wordListPath; } } aCrozzle.WordListPath = wordListPath; } } else if (aCrozzleFileItem.IsRows) { // Get the number of rows. int rows; if (Validator.IsInt32(aCrozzleFileItem.KeyValue.Value.Trim(), out rows)) { aCrozzle.Rows = rows; } else { Errors.Add(String.Format(CrozzleFileErrors.RowError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aCrozzleFileItem.IsColumns) { // Get the number of columns. int columns; if (Validator.IsInt32(aCrozzleFileItem.KeyValue.Value.Trim(), out columns)) { aCrozzle.Columns = columns; } else { Errors.Add(String.Format(CrozzleFileErrors.ColumnError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (aCrozzleFileItem.IsRow) { // Collect potential word data for a horizontal word. wordData.Add(aCrozzleFileItem.KeyValue.OriginalKeyValue); } else if (aCrozzleFileItem.IsColumn) { // Collect potential word data for a vertical word. wordData.Add(aCrozzleFileItem.KeyValue.OriginalKeyValue); } } else { Errors.AddRange(CrozzleFileItem.Errors); } } // Close files. fileIn.Close(); // If the file does not have location and orientation of words if (wordData.Count == 0) { // Initialize an empty key-value group List <String> GenerateKeyValueGroup = new List <String>(); // Use an dictionary to store inserting and non-inserting words score Dictionary <char, int> IntersectingPointsPerLetter = new Dictionary <char, int>(); Dictionary <char, int> NonIntersectingPointsPerLetter = new Dictionary <char, int>(); // Append score to corresponding letter char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; for (int i = 0; i < alphabet.Length; i++) { IntersectingPointsPerLetter.Add(alphabet[i], aConfiguration.IntersectingPointsPerLetter[i]); NonIntersectingPointsPerLetter.Add(alphabet[i], aConfiguration.NonIntersectingPointsPerLetter[i]); } // Use greedy algorithm to get a group with maximun number of words Grid grid = new Grid(aCrozzle.Rows, aCrozzle.Columns, IntersectingPointsPerLetter, NonIntersectingPointsPerLetter, aConfiguration.PointsPerWord); grid.GetConfiguration(aCrozzle.Configuration); GenerateKeyValueGroup = grid.GreedyAlgorithm(wordList.List, aConfiguration.MaximumNumberOfGroups, aConfiguration.MinimumNumberOfGroups, out aCrozzle.timeConsume); // Append NewkeyValue instead of OriginalKeyValue to old program in terms of compatibility foreach (string GenerateKeyValue in GenerateKeyValueGroup) { wordData.Add(GenerateKeyValue); } } // Get potential word data list. WordDataList wordDataList; if (!WordDataList.TryParse(wordData, aCrozzle, out wordDataList)) { Errors.AddRange(WordDataList.Errors); } aCrozzle.WordDataList = wordDataList; // Validate file sections. // Check the configuration file name. if (aCrozzle.Configuration != null) { if (aCrozzle.Configuration.ConfigurationPath != aCrozzle.ConfigurationPath) { Errors.Add(String.Format(CrozzleFileErrors.ConfigurationFilenameError, aCrozzle.ConfigurationPath, aCrozzle.Configuration.ConfigurationFileName)); } } // Check the word list file name. if (aCrozzle.WordList != null) { if (aCrozzle.WordList.WordlistPath != aCrozzle.WordListPath) { Errors.Add(String.Format(CrozzleFileErrors.WordlistFilenameError, aCrozzle.WordListPath, aCrozzle.WordList.WordlistFileName)); } } // Raw word data of horizontal and vertical words were obtained when reading the crozzle file, // but now we need to create crozzle rows and crozzle columns that represent the crozzle. aCrozzle.CreateCrozzleRows(aCrozzle.WordDataList); aCrozzle.CreateCrozzleColumns(aCrozzle.WordDataList); // Store validity. aCrozzle.FileValid = Errors.Count == 0; return(aCrozzle.FileValid); }
private void openCrozzleFileRemotely() { crozzleToolStripMenuItem.Enabled = false; // Process crozzle file to get configuration filename String configurationFileName = GetConfigurationFileNameRemotely(crozzleComboBox.Text); if (configurationFileName == null) { MessageBox.Show("configuration filename is missing from the crozzle file", ApplicationAboutBox.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { String filename = configurationFileName.Trim(); if (Validator.IsDelimited(filename, Crozzle.StringDelimiters)) { filename = filename.Trim(Crozzle.StringDelimiters); } configurationFileName = filename; } // Parse configuration file. Configuration aConfiguration = null; Configuration.TryParse(configurationFileName, out aConfiguration, true); // Get wordlist filename. String wordListFileName = GetWordlistFileNameRemotely(crozzleComboBox.Text); if (wordListFileName == null) { MessageBox.Show("wordlist filename is missing from the crozzle file", ApplicationAboutBox.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { String filename = wordListFileName.Trim(); if (Validator.IsDelimited(filename, Crozzle.StringDelimiters)) { filename = filename.Trim(Crozzle.StringDelimiters); } wordListFileName = filename; } // Parse wordlist file. WordList wordList = null; WordList.TryParse(wordListFileName, aConfiguration, out wordList, true); // Parse crozzle file. Crozzle aCrozzle; Crozzle.TryParse(crozzleComboBox.Text, aConfiguration, wordList, out aCrozzle, true); SIT323Crozzle = aCrozzle; // Update GUI - menu enabled, display crozzle data (whether valid or invalid), and crozzle file errors. if (SIT323Crozzle.FileValid && SIT323Crozzle.Configuration.Valid && SIT323Crozzle.WordList.Valid) { crozzleToolStripMenuItem.Enabled = true; } crozzleWebBrowser.DocumentText = SIT323Crozzle.ToStringHTML(); ErrorListViewer.WebBrowser.DocumentText = SIT323Crozzle.FileErrorsHTML + SIT323Crozzle.Configuration.FileErrorsHTML + SIT323Crozzle.WordList.FileErrorsHTML; // Log errors. SIT323Crozzle.LogFileErrors(SIT323Crozzle.FileErrorsTXT, true); SIT323Crozzle.LogFileErrors(SIT323Crozzle.Configuration.FileErrorsTXT, true); SIT323Crozzle.LogFileErrors(SIT323Crozzle.WordList.FileErrors, true); }
public static Boolean TryParse(String path, Configuration aConfiguration, WordList wordList, out Crozzle aCrozzle) { Errors = new List <String>(); aCrozzle = new Crozzle(path, aConfiguration, wordList); // Open file. StreamReader fileIn = new StreamReader(path); List <SequenceFragment> wordData = new List <SequenceFragment>(); // Validate file. while (!fileIn.EndOfStream) { List <String> fragment = new List <String>(); do { /// Add multiple lines as part of a text fragment until the last element is empty or null. fragment.Add(fileIn.ReadLine()); } while (!String.IsNullOrEmpty(fragment.Last())); // Parse a crozzle file fragment. FileFragment <CrozzleFileItem> aCrozzleFileFragment; if (CrozzleFileItem.TryParse(fragment, out aCrozzleFileFragment)) { String aFragmentKey = aCrozzleFileFragment.Name; foreach (CrozzleFileItem aItem in aCrozzleFileFragment.Items) { /// Process the key-value, given the current fragment key. if (aFragmentKey == CrozzleFileKeys.DEPENDENCIES_OPENBRACKET) { if (aItem.Name == CrozzleFileKeys.DEPENDENCIES_CONFIGDATA) { String configurationPath = aItem.KeyValue.Value; if (configurationPath == null) { Errors.Add(CrozzleFileErrors.ConfigurationFilenameMissing); } else { configurationPath = configurationPath.Trim(); if (Validator.IsDelimited(configurationPath, StringDelimiters)) { configurationPath = configurationPath.Trim(StringDelimiters); } if (!Path.IsPathRooted(configurationPath)) { String directoryName = Path.GetDirectoryName(path); configurationPath = directoryName + @"\" + configurationPath; } aCrozzle.ConfigurationPath = configurationPath; } } else if (aItem.Name == CrozzleFileKeys.DEPENDENCIES_SEQDATA) { String wordListPath = aItem.KeyValue.Value; if (wordListPath == null) { Errors.Add(CrozzleFileErrors.WordlistFilenameMissing); } else { wordListPath = wordListPath.Trim(); if (Validator.IsDelimited(wordListPath, StringDelimiters)) { wordListPath = wordListPath.Trim(StringDelimiters); } if (!Path.IsPathRooted(wordListPath)) { String directoryName = Path.GetDirectoryName(path); wordListPath = directoryName + @"\" + wordListPath; } aCrozzle.WordListPath = wordListPath; } } } else if (aFragmentKey == CrozzleFileKeys.SIZE_OPENBRACKET) { int rows, columns; String[] values = aItem.KeyValue.Value.Trim().Split(','); if (Validator.IsInt32(values[0], out rows)) { aCrozzle.Rows = rows; } else { Errors.Add(String.Format(CrozzleFileErrors.RowError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } if (Validator.IsInt32(values[1], out columns)) { aCrozzle.Columns = columns; } else { Errors.Add(String.Format(CrozzleFileErrors.ColumnError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0])); } } else if (new[] { CrozzleFileKeys.HORZSEQ_OPENBRACKET, CrozzleFileKeys.VERTSEQ_OPENBRACKET }.Contains(aFragmentKey)) { wordData.Add(new SequenceFragment(aFragmentKey, aItem.KeyValue.OriginalKeyValue)); } else { Errors.AddRange(CrozzleFileItem.Errors); } } } } // Close files. fileIn.Close(); // Get potential word data list. WordDataList wordDataList; if (!WordDataList.TryParse(wordData, aCrozzle, out wordDataList)) { Errors.AddRange(WordDataList.Errors); } aCrozzle.WordDataList = wordDataList; // Validate file sections. // Check the configuration file name. if (aCrozzle.Configuration != null) { if (aCrozzle.Configuration.ConfigurationPath != aCrozzle.ConfigurationPath) { Errors.Add(String.Format(CrozzleFileErrors.ConfigurationFilenameError, aCrozzle.ConfigurationPath, aCrozzle.Configuration.ConfigurationFileName)); } } // Check the word list file name. if (aCrozzle.WordList != null) { if (aCrozzle.WordList.WordlistPath != aCrozzle.WordListPath) { Errors.Add(String.Format(CrozzleFileErrors.WordlistFilenameError, aCrozzle.WordListPath, aCrozzle.WordList.WordlistFileName)); } } // Raw word data of horizontal and vertical words were obtained when reading the crozzle file, // but now we need to create crozzle rows and crozzle columns that represent the crozzle. aCrozzle.CreateCrozzleRows(aCrozzle.WordDataList); aCrozzle.CreateCrozzleColumns(aCrozzle.WordDataList); // Store validity. aCrozzle.FileValid = Errors.Count == 0; return(aCrozzle.FileValid); }
/* This section is added for Assessment 2 */ private void button1_Click(object sender, EventArgs e) { // Crozzle generataion start label1.Text = "Generating crozzle..."; // Get URL address crozzleName = URL.Text; String configurationFileName = GetConfigurationFileName(crozzleName); if (configurationFileName == null) { MessageBox.Show("configuration filename is missing from the crozzle file", ApplicationAboutBox.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { String filename = configurationFileName.Trim(); if (Validator.IsDelimited(filename, Crozzle.StringDelimiters)) { filename = filename.Trim(Crozzle.StringDelimiters); } configurationFileName = filename; if (!Path.IsPathRooted(configurationFileName) && !crozzleName.Contains("http")) { configurationFileName = Path.GetDirectoryName(crozzleName) + @"\" + configurationFileName; } } // Parse configuration file. Configuration aConfiguration = null; Configuration.TryParse(configurationFileName, out aConfiguration); // Get wordlist filename. String wordListFileName = GetWordlistFileName(crozzleName); if (wordListFileName == null) { MessageBox.Show("wordlist filename is missing from the crozzle file", ApplicationAboutBox.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { String filename = wordListFileName.Trim(); if (Validator.IsDelimited(filename, Crozzle.StringDelimiters)) { filename = filename.Trim(Crozzle.StringDelimiters); } wordListFileName = filename; if (!Path.IsPathRooted(wordListFileName) && !crozzleName.Contains("http")) { wordListFileName = Path.GetDirectoryName(crozzleName) + @"\" + wordListFileName; } } // Parse wordlist file. WordList wordList = null; WordList.TryParse(wordListFileName, aConfiguration, out wordList); // Parse crozzle file. Crozzle aCrozzle; Crozzle.TryParse(crozzleName, aConfiguration, wordList, out aCrozzle); SIT323Crozzle = aCrozzle; // Update GUI - menu enabled, display crozzle data (whether valid or invalid), and crozzle file errors. if (SIT323Crozzle.FileValid && SIT323Crozzle.Configuration.Valid && SIT323Crozzle.WordList.Valid) { crozzleToolStripMenuItem.Enabled = true; } crozzleWebBrowser.DocumentText = SIT323Crozzle.ToStringHTML(); ErrorListViewer.WebBrowser.DocumentText = SIT323Crozzle.FileErrorsHTML + SIT323Crozzle.Configuration.FileErrorsHTML + SIT323Crozzle.WordList.FileErrorsHTML; // Crozzle generataion complete label1.Text = "Crozzle generation succeed"; // Log errors. SIT323Crozzle.LogFileErrors(SIT323Crozzle.FileErrorsTXT); SIT323Crozzle.LogFileErrors(SIT323Crozzle.Configuration.FileErrorsTXT); SIT323Crozzle.LogFileErrors(SIT323Crozzle.WordList.FileErrors); }
public static Boolean TryParse(String path, Configuration aConfiguration, WordList wordList, out Crozzle aCrozzle) { Errors = new List <String>(); aCrozzle = new Crozzle(path, aConfiguration, wordList); // Open file. StreamReader fileIn = new StreamReader(path); List <TitleSequence> wordData = new List <TitleSequence>(); // Validate file. while (!fileIn.EndOfStream) { // Create a Block to store a group of data List <String> Block = new List <String>(); // Read a line. String line = fileIn.ReadLine(); while (!String.IsNullOrEmpty(line)) { Block.Add(line); line = fileIn.ReadLine(); } // Parse a block in crozzle file CzlGroup aCrozzleFileGroup; if (CrozzleFileItem.TryParse(Block, out aCrozzleFileGroup)) { String title = aCrozzleFileGroup.CzlTitle; for (int i = 0; i < aCrozzleFileGroup.Lines.Count; i++) { // Whether the title equals to "FILE-DEPENDENCIES" if (title == CrozzleKeys.FileDependencies) { if (aCrozzleFileGroup.Lines[i].Name == CrozzleKeys.ConfigData) { String configurationPath = aCrozzleFileGroup.Lines[i].KeyValue.Value; if (configurationPath == null) { Errors.Add(CrozzleFileErrors.ConfigurationFilenameMissing); } else { configurationPath = configurationPath.Trim(); if (Validator.IsDelimited(configurationPath, StringDelimiters)) { configurationPath = configurationPath.Trim(StringDelimiters); } if (!Path.IsPathRooted(configurationPath)) { String directoryName = Path.GetDirectoryName(path); configurationPath = directoryName + @"\" + configurationPath; } aCrozzle.ConfigurationPath = configurationPath; } } else if (aCrozzleFileGroup.Lines[i].Name == CrozzleKeys.SequenceData) { String wordListPath = aCrozzleFileGroup.Lines[i].KeyValue.Value; if (wordListPath == null) { Errors.Add(CrozzleFileErrors.ConfigurationFilenameMissing); } else { wordListPath = wordListPath.Trim(); if (Validator.IsDelimited(wordListPath, StringDelimiters)) { wordListPath = wordListPath.Trim(StringDelimiters); } if (!Path.IsPathRooted(wordListPath)) { String directoryName = Path.GetDirectoryName(path); wordListPath = directoryName + @"\" + wordListPath; } aCrozzle.WordListPath = wordListPath; } } } // Whether the title equals to "CROZZLE-SIZE" else if (title == CrozzleKeys.CrozzleSize) { int rows, columns; String[] coordinate = aCrozzleFileGroup.Lines[i].KeyValue.Value.Split(','); if (Validator.IsInt32(coordinate[0].Trim(), out rows)) { aCrozzle.Rows = rows; } else { Errors.Add(String.Format(CrozzleFileErrors.RowError, aCrozzleFileGroup.Lines[i].KeyValue.OriginalKeyValue, Errors[0])); } if (Validator.IsInt32(coordinate[1].Trim(), out columns)) { aCrozzle.Columns = columns; } else { Errors.Add(String.Format(CrozzleFileErrors.ColumnError, aCrozzleFileGroup.Lines[i].KeyValue.OriginalKeyValue, Errors[1])); } } // Whether the title equals to "HORIZONTAL-SEQUENCES" or "VERTICAL-SEQUENCE" else if (title == CrozzleKeys.HorizontalSequence || title == CrozzleKeys.VerticalSequence) { TitleSequence temp = new TitleSequence(title, aCrozzleFileGroup.Lines[i].KeyValue.OriginalKeyValue); wordData.Add(temp); } // If there exist unidentified titles else { Errors.AddRange(CrozzleFileItem.Errors); } } } } // Close files. fileIn.Close(); // Get potential word data list. WordDataList wordDataList; if (!WordDataList.TryParse(wordData, aCrozzle, out wordDataList)) { Errors.AddRange(WordDataList.Errors); } aCrozzle.WordDataList = wordDataList; // Validate file sections. // Check the configuration file name. if (aCrozzle.Configuration != null) { if (aCrozzle.Configuration.ConfigurationPath != aCrozzle.ConfigurationPath) { Errors.Add(String.Format(CrozzleFileErrors.ConfigurationFilenameError, aCrozzle.ConfigurationPath, aCrozzle.Configuration.ConfigurationPath)); } } // Check the word list file name. if (aCrozzle.WordList != null) { if (aCrozzle.WordList.WordlistPath != aCrozzle.WordListPath) { Errors.Add(String.Format(CrozzleFileErrors.WordlistFilenameError, aCrozzle.WordListPath, aCrozzle.WordList.WordlistFileName)); } } // Raw word data of horizontal and vertical words were obtained when reading the crozzle file, // but now we need to create crozzle rows and crozzle columns that represent the crozzle. aCrozzle.CreateCrozzleRows(aCrozzle.WordDataList); aCrozzle.CreateCrozzleColumns(aCrozzle.WordDataList); // Store validity. aCrozzle.FileValid = Errors.Count == 0; return(aCrozzle.FileValid); }