private string GetConfigurationFileName(string path) { CrozzleFileItem aCrozzleFileItem = null; //this will be the line in the text file stream is reading StreamReader fileIn = new StreamReader(path); // REads the crozzle.txt file and Search for line in the txt file CONFIGERATION_FILE while (!fileIn.EndOfStream) { //Try Parse is a function that has been overidden, it is located in CrozzleFileItem if (CrozzleFileItem.TryParse(fileIn.ReadLine(), out aCrozzleFileItem)) { if (aCrozzleFileItem.IsConfigurationFile) { break; } } } // Close files. fileIn.Close(); // Return file name. if (aCrozzleFileItem == null) { return(null); } else { return(aCrozzleFileItem.KeyValue.Value); } }
private String GetWordlistFileName(String path) { CrozzleFileItem aCrozzleFileItem = null; StreamReader fileIn = new StreamReader(path); // Search for file name. while (!fileIn.EndOfStream) { if (CrozzleFileItem.TryParse(fileIn.ReadLine(), out aCrozzleFileItem)) { if (aCrozzleFileItem.IsWordListFile) { break; } } } // Close files. fileIn.Close(); // Return file name. if (aCrozzleFileItem == null) { return(null); } else { return(aCrozzleFileItem.KeyValue.Value); } }
//get the key value pairs for the constants //crozzlefileitem is the line read from the crozzle.txt file public static Boolean TryParse(String crozzleFileItem, out CrozzleFileItem aCrozzleFileItem) { Errors = new List <String>(); //makes a new crozzlefileitem and sets teh originalitem property to the line from teh file aCrozzleFileItem = new CrozzleFileItem(crozzleFileItem); // Discard comment. if (crozzleFileItem.Contains("//")) { int index = crozzleFileItem.IndexOf("//"); crozzleFileItem = crozzleFileItem.Remove(index); crozzleFileItem = crozzleFileItem.Trim(); } //if line is empty adds the constant NO CROZZLE ITEM if (Regex.IsMatch(crozzleFileItem, @"^\s*$")) { // Check for only 0 or more white spaces. aCrozzleFileItem.Name = NoCrozzleItem; } else if (Regex.IsMatch(crozzleFileItem, @"^" + ConfigurationFileSymbol + @".*")) { // Get the CONFIGURATION_FILE key-value pair. KeyValue aKeyValue; if (!KeyValue.TryParse(crozzleFileItem, ConfigurationFileSymbol, out aKeyValue)) { Errors.AddRange(KeyValue.Errors); } aCrozzleFileItem.Name = ConfigurationFileSymbol; aCrozzleFileItem.KeyValue = aKeyValue; } else if (Regex.IsMatch(crozzleFileItem, @"^" + WordListFileSymbol + @".*")) { // Get the WORDLIST_FILE key-value pair. KeyValue aKeyValue; if (!KeyValue.TryParse(crozzleFileItem, WordListFileSymbol, out aKeyValue)) { Errors.AddRange(KeyValue.Errors); } aCrozzleFileItem.Name = WordListFileSymbol; aCrozzleFileItem.KeyValue = aKeyValue; } else if (Regex.IsMatch(crozzleFileItem, @"^" + RowsSymbol + @".*")) { // Get the number of rows for the crozzle. KeyValue aKeyValue; if (!KeyValue.TryParse(crozzleFileItem, RowsSymbol, out aKeyValue)) { Errors.AddRange(KeyValue.Errors); } aCrozzleFileItem.Name = RowsSymbol; aCrozzleFileItem.KeyValue = aKeyValue; } else if (Regex.IsMatch(crozzleFileItem, @"^" + ColumnsSymbol + @".*")) { // Get the number of columns for the crozzle. KeyValue aKeyValue; if (!KeyValue.TryParse(crozzleFileItem, ColumnsSymbol, out aKeyValue)) { Errors.AddRange(KeyValue.Errors); } aCrozzleFileItem.Name = ColumnsSymbol; aCrozzleFileItem.KeyValue = aKeyValue; } else if (Regex.IsMatch(crozzleFileItem, @"^" + RowSymbol + @".*")) { // Get data for a horizontal word. KeyValue aKeyValue; if (!KeyValue.TryParse(crozzleFileItem, RowSymbol, out aKeyValue)) { Errors.AddRange(KeyValue.Errors); } aCrozzleFileItem.Name = RowSymbol; aCrozzleFileItem.KeyValue = aKeyValue; } else if (Regex.IsMatch(crozzleFileItem, @"^" + ColumnSymbol + @".*")) { // Get data for a vertical word. KeyValue aKeyValue; if (!KeyValue.TryParse(crozzleFileItem, ColumnSymbol, out aKeyValue)) { Errors.AddRange(KeyValue.Errors); } aCrozzleFileItem.Name = ColumnSymbol; aCrozzleFileItem.KeyValue = aKeyValue; } else { Errors.Add(String.Format(CrozzleFileItemErrors.SymbolError, crozzleFileItem)); } //aCrozzleFileItem.Valid property is set to true if the errors count is 0 aCrozzleFileItem.Valid = Errors.Count == 0; return(aCrozzleFileItem.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); }