Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        public static Boolean TryParse(TitleSequence Block, Crozzle aCrozzle, out WordData aWordData)
        {
            String[] originalWordData = Block.SequenceLine.Split(new Char[] { ',' }, 2);

            Errors    = new List <String>();
            aWordData = new WordData();
            String[] OldOriginalWordData = new string[4];


            // Check that the original word data has the correct number of fields.
            if (originalWordData.Length != NumberOfFields)
            {
                Errors.Add(String.Format(WordDataErrors.FieldCountError, originalWordData.Length, Block.SequenceLine, NumberOfFields));
            }

            // If the title symbol exist
            if (Block.Title.Length > 0)
            {
                // Check whether the title is an orientation value.
                Orientation anOrientation;
                if (!Orientation.TryParse(Block.Title, out anOrientation))
                {
                    Errors.AddRange(Orientation.Errors);
                }

                aWordData.Orientation = anOrientation;

                if (anOrientation.Valid)
                {
                    // Handle the name & sequence side
                    String[] WordName = originalWordData[0].Split('=');

                    // Figure out whether WordName = ['SEQUENCE','(words)']
                    if (WordName[0] != "SEQUENCE")
                    {
                        Errors.Add(String.Format(WordDataErrors.MissingSymbol, Block.SequenceLine));
                    }
                    else
                    {   // Figure out whether the word is missing
                        if (String.IsNullOrEmpty(WordName[1]))
                        {
                            Errors.Add(String.Format(WordDataErrors.BlankFieldError, Block.SequenceLine, WordName[0]));
                        }
                        // Find out whether the word is alphabet
                        else if (!Regex.IsMatch(WordName[1], Configuration.allowedCharacters))
                        {
                            Errors.Add(String.Format(WordDataErrors.AlphabeticError, WordName[1]));
                        }
                        else
                        {
                            aWordData.Letters = WordName[1];
                        }
                    }

                    // Handle the coordinate side
                    String[] WordCoordinate = originalWordData[1].Split('=');

                    // If the format is correct
                    if (WordName[0] == "SEQUENCE")
                    {
                        //find out whether the right side is seperated by equeal symbol
                        if (WordCoordinate.Length != 2)
                        {
                            Errors.Add(String.Format(WordDataErrors.MissingSymbol, Block.SequenceLine));
                        }

                        // Find out whether the coordinate exists
                        else if (String.IsNullOrEmpty(WordCoordinate[1]))
                        {
                            Errors.Add(String.Format(WordDataErrors.BlankFieldError, Block.SequenceLine, WordCoordinate[0]));
                        }
                        else
                        {
                            // Get the Coordinate in string format
                            String[] CoordinateString = WordCoordinate[1].Split(',');

                            // Find out whether the CoordinateString is complete
                            if (CoordinateString.Length != 2)
                            {
                                Errors.Add(String.Format(WordDataErrors.CoordinateIncomplete, CoordinateString[0], originalWordData[1]));
                            }
                            else
                            {
                                // Get the coordinate of each word
                                String rowValue    = CoordinateString[0];
                                String columnValue = CoordinateString[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;
                                }
                            }
                        }
                    }
                }
            }

            // Switch to the old format
            if (aWordData.Orientation != null)
            {
                OldOriginalWordData[0] = aWordData.Orientation.Direction;
            }
            else if (aWordData.Location != null)
            {
                OldOriginalWordData[1] = aWordData.Location.Row.ToString();
                OldOriginalWordData[3] = aWordData.Location.Column.ToString();
            }
            else if (aWordData.Letters != null)
            {
                OldOriginalWordData[2] = aWordData.Letters;
            }

            // Pass the old format to old OldOriginalWordData
            aWordData.OriginalWordData = OldOriginalWordData;

            aWordData.Valid = Errors.Count == 0;
            return(aWordData.Valid);
        }