public static Boolean TryParse(String path, Configuration aConfiguration, out WordList aWordList) { StreamReader fileIn = new StreamReader(path); Errors = new List <String>(); aWordList = new WordList(path, aConfiguration); // Split the original wordlist from the file. aWordList.OriginalList = fileIn.ReadLine().Split(WordSeparators); // Check each field in the wordlist. int fieldNumber = 0; foreach (String potentialWord in aWordList.OriginalList) { // Check that the field is not empty. if (potentialWord.Length > 0) { // Check that the field is alphabetic. if (Regex.IsMatch(potentialWord, Configuration.allowedCharacters)) { aWordList.Add(potentialWord); } else { Errors.Add(String.Format(WordListErrors.AlphabeticError, potentialWord, fieldNumber)); } } else { Errors.Add(String.Format(WordListErrors.MissingWordError, fieldNumber)); } fieldNumber++; } // Check the minimmum word limit. if (aWordList.Count < aConfiguration.MinimumNumberOfUniqueWords) { Errors.Add(String.Format(WordListErrors.MinimumSizeError, aWordList.Count, aConfiguration.MinimumNumberOfUniqueWords)); } // Check the maximum word limit. if (aWordList.Count > aConfiguration.MaximumNumberOfUniqueWords) { Errors.Add(String.Format(WordListErrors.MaximumSizeError, aWordList.Count, aConfiguration.MaximumNumberOfUniqueWords)); } aWordList.Valid = Errors.Count == 0; return(aWordList.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 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); }