public static Boolean TryParse(List <String> originalWordDataList, Crozzle aCrozzle, out WordDataList aWordDataList) { List <WordData> aList = new List <WordData>(); Errors = new List <String>(); aWordDataList = new WordDataList(originalWordDataList); foreach (String line in originalWordDataList) { WordData aWordData; if (WordData.TryParse(line, aCrozzle, out aWordData)) { aWordDataList.Add(aWordData); } else { Errors.AddRange(WordData.Errors); } } aWordDataList.Valid = Errors.Count == 0; return(aWordDataList.Valid); }
private void OpenCrozzleFile() { DialogResult result; // indicate crozzle file, and crozzle are not valid, and clear GUI. crozzleToolStripMenuItem.Enabled = false; //menu across the top crozzleWebBrowser.DocumentText = ""; //area where the crozzle will be displayed errorWebBrowser.DocumentText = ""; // area where the errors will be displayed //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", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } else { string filename = configurationFileName.Trim(); //sends to the class validator to find out if there are deliminators //if comes back true //remove the deliminators double quotes if (Validator.IsDelimited(filename, Crozzle.StringDelimiters)) { filename = filename.Trim(Crozzle.StringDelimiters); } configurationFileName = filename; //asks whether the path contains a root //if not it will get the directory name if (!Path.IsPathRooted(configurationFileName)) { configurationFileName = Path.GetDirectoryName(openFileDialog1.FileName) + @"\" + configurationFileName; } } //validate 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", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); 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(); errorWebBrowser.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 originalWordDataData, Crozzle aCrozzle, out WordData aWordData) { String[] originalWordData = originalWordDataData.Split(new Char[] { '=', ',' }); Errors = new List <String>(); aWordData = new WordData(originalWordData); // Check that the original word data has the correct number of fields. if (originalWordData.Length != NumberOfFields) { Errors.Add(String.Format(WordDataErrors.FieldCountError, originalWordData.Length, originalWordDataData, NumberOfFields)); } // Check that each field is not empty. for (int field = 0; field < originalWordData.Length; field++) { if (originalWordData[field].Length == 0) { Errors.Add(String.Format(WordDataErrors.BlankFieldError, originalWordDataData, field)); } } if (originalWordData.Length > 0) { // Check that the 1st field is an orientation value. Orientation anOrientation; if (!Orientation.TryParse(originalWordData[0], out anOrientation)) { Errors.AddRange(Orientation.Errors); } aWordData.Orientation = anOrientation; if (anOrientation.Valid) { // Check that the 2nd and 4th fields are a Coordinate. if (originalWordData.Length >= NumberOfFields) { String rowValue = ""; String columnValue = ""; if (anOrientation.IsHorizontal) { rowValue = originalWordData[1]; columnValue = originalWordData[3]; } else if (anOrientation.IsVertical) { rowValue = originalWordData[3]; columnValue = originalWordData[1]; } if (rowValue.Length > 0 && columnValue.Length > 0) { Coordinate aCoordinate; if (!Coordinate.TryParse(rowValue, columnValue, aCrozzle, out aCoordinate)) { Errors.AddRange(Coordinate.Errors); } aWordData.Location = aCoordinate; } } // Check that the 3rd field is alphabetic, and in the wordlist. if (originalWordData.Length >= NumberOfFields - 1) { String originalWord = originalWordData[2]; if (originalWord.Length > 0) { if (Regex.IsMatch(originalWord, Configuration.allowedCharacters)) { aWordData.Letters = originalWord; // Check that the 3rd field is in the wordlist. if (!aCrozzle.WordList.Contains(originalWord)) { Errors.Add(String.Format(WordDataErrors.MissingWordError, originalWord)); } } else { Errors.Add(String.Format(WordDataErrors.AlphabeticError, originalWord)); } } } } } aWordData.Valid = Errors.Count == 0; return(aWordData.Valid); }
public static Boolean TryParse(String originalRowData, String originalColumnData, Crozzle aCrozzle, out Coordinate aCoordinate) { int anInteger; String[] originalCoordinate = new String[] { originalRowData, originalColumnData }; Errors = new List<String>(); aCoordinate = new Coordinate(originalCoordinate); // Check that the row value is an integer and in range. if (Validator.IsInt32(originalRowData, out anInteger)) { aCoordinate.Row = anInteger; if (!Validator.TryRange(aCoordinate.Row, 1, aCrozzle.Rows)) Errors.Add(String.Format(CoordinateErrors.RowValueError, originalRowData, Validator.Errors[0])); } else Errors.Add(String.Format(CoordinateErrors.RowValueError, originalRowData, Validator.Errors[0])); // Check that the column value is an integer and in range. if (Validator.IsInt32(originalColumnData, out anInteger)) { aCoordinate.Column = anInteger; if (!Validator.TryRange(aCoordinate.Column, 1, aCrozzle.Columns)) Errors.Add(String.Format(CoordinateErrors.ColumnValueError, originalColumnData, Validator.Errors[0])); } else Errors.Add(String.Format(CoordinateErrors.ColumnValueError, originalColumnData, Validator.Errors[0])); aCoordinate.Valid = Errors.Count == 0; return (aCoordinate.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>(); // 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); }