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); }
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 (configurationPath.Contains("http://")) { configurationPath = "TempConfiguration.txt"; } 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 (wordListPath.Contains("http://")) { wordListPath = "TempWordlist.txt"; } 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(); if (wordData.Count == 0) { int totalScore = 0; wordData = CreateCrozzelByGreedyAlgorithm(aCrozzle.Rows, aCrozzle.Columns, wordList.List, aConfiguration.IntersectingPointsPerLetter, aConfiguration.NonIntersectingPointsPerLetter, ref totalScore); TotalScore = totalScore; } // 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); }