Beispiel #1
0
        private String GetWordlistFileName(String path)
        {
            CrozzleFileItem aCrozzleFileItem = null;
            StreamReader    fileIn           = new StreamReader(path);

            // Search for file name.
            while (!fileIn.EndOfStream)
            {
                FileFragment <CrozzleFileItem> aFragment;
                List <String> fragment = new List <String>();
                do
                {
                    fragment.Add(fileIn.ReadLine());
                } while (!String.IsNullOrEmpty(fragment.Last()));
                if (CrozzleFileItem.TryParse(fragment, out aFragment))
                {
                    if ((aCrozzleFileItem = aFragment.Items.Where(item => item.Name == CrozzleFileKeys.DEPENDENCIES_SEQDATA).First()) != null)
                    {
                        break;
                    }
                }
            }

            // Close files.
            fileIn.Close();

            // Return file name.
            return((aCrozzleFileItem == null) ? null : aCrozzleFileItem.KeyValue.Value);
        }
        private String GetWordlistFileNameRemotely(String url)
        {
            CrozzleFileItem aCrozzleFileItem = null;
            WebClient       webClient        = new WebClient();
            Stream          stream           = webClient.OpenRead(url);
            StreamReader    fileIn           = new StreamReader(stream);

            // 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);
            }
        }
Beispiel #3
0
        private String GetConfigurationFileName(String path)
        {
            CrozzleFileItem aCrozzleFileItem = null;

            StreamReader fileIn;

            if (path.Contains("http"))
            {
                WebClient webClient = new WebClient();
                Stream    aStream   = webClient.OpenRead(path);
                fileIn = new StreamReader(aStream);
            }
            else
            {
                fileIn = new StreamReader(path);
            }


            // Search for file name.
            while (!fileIn.EndOfStream)
            {
                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);
            }
        }
Beispiel #4
0
        public static Boolean TryParse(List <string> crozzleFileFragment, out FileFragment <CrozzleFileItem> aCrozzleFileFragment)
        {
            Errors = new List <String>();
            crozzleFileFragment.RemoveAll(s => String.IsNullOrEmpty(s));
            aCrozzleFileFragment = new FileFragment <CrozzleFileItem>();

            String   formattedLine = String.Empty;
            DataKeys flag          = DataKeys.UNDEFINED;

            foreach (String line in crozzleFileFragment)
            {
                /// Discard comment and trim possible whitespace.
                if (line.Contains("//"))
                {
                    int index = line.IndexOf("//");
                    formattedLine = line.Remove(index);
                }
                else
                {
                    formattedLine = line;
                }
                formattedLine = formattedLine.Trim();

                CrozzleFileItem aCrozzleFileItem = new CrozzleFileItem(formattedLine);

                if (Regex.IsMatch(formattedLine, @"^\s*$"))
                {
                    continue;
                }
                else if (Regex.IsMatch(formattedLine, ForeignCharacters))
                {
                    Errors.Add(String.Format(ConfigurationFileItemErrors.SymbolError, aCrozzleFileItem));
                }
                else
                {
                    /// TODO: switch case with flag
                    switch (flag)
                    {
                    case DataKeys.UNDEFINED:
                        /// fragment specification
                        if (Regex.IsMatch(formattedLine, @"^" + FileDependenciesKeys.OpenBracket + @".*"))
                        {
                            flag = DataKeys.DEPENDENCIES;
                            aCrozzleFileFragment.Name = FileDependenciesKeys.OpenBracket;
                        }
                        else if (Regex.IsMatch(formattedLine, @"^" + CrozzleSizeKeys.OpenBracket + @".*"))
                        {
                            flag = DataKeys.SIZE;
                            aCrozzleFileFragment.Name = CrozzleSizeKeys.OpenBracket;
                        }
                        else if (Regex.IsMatch(formattedLine, @"^" + HorizontalSequenceKeys.OpenBracket + @".*"))
                        {
                            flag = DataKeys.HORZSEQUENCE;
                            aCrozzleFileFragment.Name = HorizontalSequenceKeys.OpenBracket;
                        }
                        else if (Regex.IsMatch(formattedLine, @"^" + VerticalSequenceKeys.OpenBracket + @".*"))
                        {
                            flag = DataKeys.VERTSEQUENCE;
                            aCrozzleFileFragment.Name = VerticalSequenceKeys.OpenBracket;
                        }
                        break;

                    case DataKeys.DEPENDENCIES:
                        if (Regex.IsMatch(formattedLine, @"^" + FileDependenciesKeys.ConfigFileName + @".*"))
                        {
                            KeyValue aKeyValue;
                            if (!KeyValue.TryParse(formattedLine, FileDependenciesKeys.ConfigFileName, out aKeyValue))
                            {
                                Errors.AddRange(KeyValue.Errors);
                            }
                            aCrozzleFileItem.Name     = FileDependenciesKeys.ConfigFileName;
                            aCrozzleFileItem.KeyValue = aKeyValue;
                        }
                        else if (Regex.IsMatch(formattedLine, @"^" + FileDependenciesKeys.SequenceFileName + @".*"))
                        {
                            KeyValue aKeyValue;
                            if (!KeyValue.TryParse(formattedLine, FileDependenciesKeys.SequenceFileName, out aKeyValue))
                            {
                                Errors.AddRange(KeyValue.Errors);
                            }
                            aCrozzleFileItem.Name     = FileDependenciesKeys.SequenceFileName;
                            aCrozzleFileItem.KeyValue = aKeyValue;
                        }
                        else if (Regex.IsMatch(formattedLine, @"^" + FileDependenciesKeys.EndBracket + @".*"))
                        {
                            flag = DataKeys.UNDEFINED;
                        }
                        break;

                    case DataKeys.SIZE:
                        if (Regex.IsMatch(formattedLine, @"^" + CrozzleSizeKeys.Size + @".*"))
                        {
                            KeyValue aKeyValue;
                            if (!KeyValue.TryParse(formattedLine, CrozzleSizeKeys.Size, out aKeyValue))
                            {
                                Errors.AddRange(KeyValue.Errors);
                            }
                            aCrozzleFileItem.Name     = CrozzleSizeKeys.Size;
                            aCrozzleFileItem.KeyValue = aKeyValue;
                        }
                        else if (Regex.IsMatch(formattedLine, @"^" + CrozzleSizeKeys.EndBracket + @".*"))
                        {
                            flag = DataKeys.UNDEFINED;
                        }
                        break;

                    case DataKeys.HORZSEQUENCE:
                        if (Regex.IsMatch(formattedLine, @"^" + HorizontalSequenceKeys.Sequence + @".*"))
                        {
                            KeyValue aKeyValue;
                            if (!KeyValue.TryParse(formattedLine, HorizontalSequenceKeys.Sequence, out aKeyValue))
                            {
                                Errors.AddRange(KeyValue.Errors);
                            }
                            aCrozzleFileItem.Name     = HorizontalSequenceKeys.Sequence;
                            aCrozzleFileItem.KeyValue = aKeyValue;
                        }
                        else if (Regex.IsMatch(formattedLine, @"^" + HorizontalSequenceKeys.EndBracket + @".*"))
                        {
                            flag = DataKeys.UNDEFINED;
                        }
                        break;

                    case DataKeys.VERTSEQUENCE:
                        if (Regex.IsMatch(formattedLine, @"^" + VerticalSequenceKeys.Sequence + @".*"))
                        {
                            KeyValue aKeyValue;
                            if (!KeyValue.TryParse(formattedLine, VerticalSequenceKeys.Sequence, out aKeyValue))
                            {
                                Errors.AddRange(KeyValue.Errors);
                            }
                            aCrozzleFileItem.Name     = VerticalSequenceKeys.Sequence;
                            aCrozzleFileItem.KeyValue = aKeyValue;
                        }
                        else if (Regex.IsMatch(formattedLine, @"^" + VerticalSequenceKeys.EndBracket + @".*"))
                        {
                            flag = DataKeys.UNDEFINED;
                        }
                        break;
                    }

                    aCrozzleFileItem.Valid = Errors.Count == 0;
                    /// If the current line is the opening bracket to a fragment, do not add as a new item. Else, do so.
                    if (aCrozzleFileItem.Name != null)
                    {
                        aCrozzleFileFragment.AddNewItem(aCrozzleFileItem);
                    }
                }
            }
            /// FALSE TRUE FALSE
            /// FALSE FALSE TRUE
            return(!aCrozzleFileFragment.Items.Exists(item => item.Valid == false));
        }
Beispiel #5
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 <String> wordData = new List <string>();

            String section  = null;
            bool   newBlock = false;

            // Validate file
            while (!fileIn.EndOfStream)
            {
                // Read a line.
                String line = fileIn.ReadLine();

                // Processing empty lines
                if (Regex.IsMatch(line, @"^\s*$"))
                {
                    continue;
                }
                line = line.Trim();

                // Processing comments
                if (line.Contains("//"))
                {
                    if (line.StartsWith("//"))
                    {
                        continue;
                    }
                    else
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MixConfigWithComentError, line));
                        continue;
                    }
                }

                // Section check
                switch (line)
                {
                case "FILE-DEPENDENCIES":
                case "CROZZLE-SIZE":
                case "HORIZONTAL-SEQUENCES":
                case "VERTICAL-SEQUENCES":
                    section  = line;
                    newBlock = true;
                    break;

                case "END-FILE-DEPENDENCIES":
                case "END-CROZZLE-SIZE":
                case "END-HORIZONTAL-SEQUENCES":
                case "END-VERTICAL-SEQUENCES":
                    section  = null;
                    newBlock = true;
                    break;

                default:
                    break;
                }

                if (newBlock)
                {
                    newBlock = false;
                    continue;
                }

                // Parse a crozzle file item.
                CrozzleFileItem aCrozzleFileItem;

                // Out of section comment
                if (section == null)
                {
                    Errors.Add(String.Format(ConfigurationErrors.OutOfSectionError, line));
                }
                // Parse a crozzle item
                else 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;
                        }
                    }
                    // Replace
                    else if (aCrozzleFileItem.IsSize)
                    {
                        // Split row & col
                        String   rawSizeData = aCrozzleFileItem.KeyValue.Value.Trim();
                        String[] sizeSplit   = rawSizeData.Split(new String[] { SizeDelimiter }, 2, StringSplitOptions.None);
                        if (sizeSplit.Length != SizeValueLength)
                        {
                            Errors.Add(String.Format(KeyValueErrors.FieldCountError, sizeSplit.Length, rawSizeData, SizeValueLength));
                        }
                        else
                        {
                            int rows;
                            if (Validator.IsInt32(sizeSplit[0].Trim(), out rows))
                            {
                                aCrozzle.Rows = rows;
                            }
                            else
                            {
                                Errors.Add(String.Format(CrozzleFileErrors.RowError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                            }

                            int columns;
                            if (Validator.IsInt32(sizeSplit[1].Trim(), out columns))
                            {
                                aCrozzle.Columns = columns;
                            }
                            else
                            {
                                Errors.Add(String.Format(CrozzleFileErrors.ColumnError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                            }
                        }
                    }

                    else if (aCrozzleFileItem.IsSequence)
                    {
                        // Collect potential word data for a horizontal word.
                        //wordData.Add(aCrozzleFileItem.KeyValue.OriginalKeyValue);
                        String type;

                        if (section == "HORIZONTAL-SEQUENCES")
                        {
                            type = "ROW";

                            try
                            {
                                String oldFormat = aCrozzleFileItem.KeyValue.OriginalKeyValue;

                                oldFormat = oldFormat.Substring("SEQUENCE=".Length);
                                string[] split;
                                split = oldFormat.Split(',');
                                String key = split[0];
                                String row = split[1].Split('=')[1];
                                String col = split[2];

                                String newFormat = type + "=" + row + "," + key + "," + col;
                                Console.WriteLine("New Format = " + newFormat);
                                wordData.Add(newFormat);
                            }
                            catch (Exception e)
                            {
                                Errors.Add(String.Format(CrozzleFileErrors.SequenceFormatError, aCrozzleFileItem.KeyValue.OriginalKeyValue));
                            }
                        }
                        else if (section == "VERTICAL-SEQUENCES")
                        {
                            type = "COLUMN";


                            try
                            {
                                String oldFormat = aCrozzleFileItem.KeyValue.OriginalKeyValue;

                                oldFormat = oldFormat.Substring("SEQUENCE=".Length);
                                string[] split;
                                split = oldFormat.Split(',');
                                String key = split[0];
                                String row = split[1].Split('=')[1];
                                String col = split[2];

                                String newFormat = type + "=" + col + "," + key + "," + row;
                                Console.WriteLine("New Format = " + newFormat);
                                wordData.Add(newFormat);
                            }
                            catch (Exception e)
                            {
                                Errors.Add(String.Format(CrozzleFileErrors.SequenceFormatError, aCrozzleFileItem.KeyValue.OriginalKeyValue));
                            }
                        }
                        else
                        {
                            Errors.AddRange(CrozzleFileItem.Errors);
                            break;
                        }
                    }
                }
                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);
        }
Beispiel #6
0
        public static Boolean TryParse(String path, Configuration aConfiguration, WordList wordList, out Crozzle aCrozzle)
        {
            Errors   = new List <String>();
            aCrozzle = new Crozzle(path, aConfiguration, wordList);

            StreamReader fileIn;

            if (path.Contains("http"))
            {
                WebClient webClient = new WebClient();
                Stream    aStream   = webClient.OpenRead(path);
                fileIn = new StreamReader(aStream);
            }
            else
            {
                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))
                            {
                                // Seperate local files and files from web server
                                if (!path.Contains("http"))
                                {
                                    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))
                            {
                                // Seperate local files and files from web server
                                if (!path.Contains("http"))
                                {
                                    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 the file does not have location and orientation of words
            if (wordData.Count == 0)
            {
                // Initialize an empty key-value group
                List <String> GenerateKeyValueGroup = new List <String>();

                // Use an dictionary to store inserting and non-inserting words score
                Dictionary <char, int> IntersectingPointsPerLetter    = new Dictionary <char, int>();
                Dictionary <char, int> NonIntersectingPointsPerLetter = new Dictionary <char, int>();

                // Append score to corresponding letter
                char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
                for (int i = 0; i < alphabet.Length; i++)
                {
                    IntersectingPointsPerLetter.Add(alphabet[i], aConfiguration.IntersectingPointsPerLetter[i]);
                    NonIntersectingPointsPerLetter.Add(alphabet[i], aConfiguration.NonIntersectingPointsPerLetter[i]);
                }

                // Use greedy algorithm to get a group with maximun number of words
                Grid grid = new Grid(aCrozzle.Rows, aCrozzle.Columns, IntersectingPointsPerLetter, NonIntersectingPointsPerLetter, aConfiguration.PointsPerWord);
                grid.GetConfiguration(aCrozzle.Configuration);
                GenerateKeyValueGroup = grid.GreedyAlgorithm(wordList.List, aConfiguration.MaximumNumberOfGroups, aConfiguration.MinimumNumberOfGroups, out aCrozzle.timeConsume);

                // Append NewkeyValue instead of OriginalKeyValue to old program in terms of compatibility
                foreach (string GenerateKeyValue in GenerateKeyValueGroup)
                {
                    wordData.Add(GenerateKeyValue);
                }
            }

            // 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(List <String> crozzleFileGroup, out CzlGroup aCrozzleFileGroup)
        {
            Errors            = new List <String>();
            aCrozzleFileGroup = new CzlGroup();

            String CrozzleLine = "";
            String SymbolMark  = "NULL";

            foreach (String line in crozzleFileGroup)
            {
                if (String.IsNullOrEmpty(line))
                {
                    break;
                }
                // Discard comment.
                else if (line.Contains("//"))
                {
                    int index = line.IndexOf("//");
                    CrozzleLine = line.Remove(index);
                }

                else
                {
                    CrozzleLine = line;
                }
                CrozzleLine = CrozzleLine.Trim();

                // Use Crozzle object to store name and keyvalue
                CrozzleFileItem aCrozzleFileItem = new CrozzleFileItem(CrozzleLine);

                if (Regex.IsMatch(line, @"^\s*$"))
                {
                    aCrozzleFileItem.Name = CrozzleKeys.NoCrozzleItem;
                }
                else
                {   // Process the file-dependencies block
                    if (Regex.IsMatch(CrozzleLine, @"^" + CrozzleKeys.FileDependencies + @".*"))
                    {
                        SymbolMark = "FILE-OPEN";
                        aCrozzleFileGroup.CzlTitle = CrozzleKeys.FileDependencies;
                    }
                    else if (Regex.IsMatch(CrozzleLine, @"^" + CrozzleKeys.ConfigData + @".*") && SymbolMark == "FILE-OPEN")
                    {
                        KeyValue aKeyValue;
                        if (!KeyValue.TryParse(CrozzleLine, CrozzleKeys.ConfigData, out aKeyValue))
                        {
                            Errors.AddRange(KeyValue.Errors);
                        }
                        aCrozzleFileItem.Name     = CrozzleKeys.ConfigData;
                        aCrozzleFileItem.KeyValue = aKeyValue;
                    }
                    else if (Regex.IsMatch(CrozzleLine, @"^" + CrozzleKeys.SequenceData + @".*") && SymbolMark == "FILE-OPEN")
                    {
                        KeyValue aKeyValue;
                        if (!KeyValue.TryParse(CrozzleLine, CrozzleKeys.SequenceData, out aKeyValue))
                        {
                            Errors.AddRange(KeyValue.Errors);
                        }
                        aCrozzleFileItem.Name     = CrozzleKeys.SequenceData;
                        aCrozzleFileItem.KeyValue = aKeyValue;
                    }
                    else if (Regex.IsMatch(CrozzleLine, @"^" + CrozzleKeys.EndFileDependencies + @".*"))
                    {
                        SymbolMark = "NULL";
                    }

                    // Process the crozzle-size block
                    else if (Regex.IsMatch(CrozzleLine, @"^" + CrozzleKeys.CrozzleSize + @".*"))
                    {
                        SymbolMark = "SIZE-OPEN";
                        aCrozzleFileGroup.CzlTitle = CrozzleKeys.CrozzleSize;
                    }
                    else if (Regex.IsMatch(CrozzleLine, @"^" + CrozzleKeys.Size + @".*") && SymbolMark == "SIZE-OPEN")
                    {
                        KeyValue aKeyValue;
                        if (!KeyValue.TryParse(CrozzleLine, CrozzleKeys.Size, out aKeyValue))
                        {
                            Errors.AddRange(KeyValue.Errors);
                        }
                        aCrozzleFileItem.Name     = CrozzleKeys.Size;
                        aCrozzleFileItem.KeyValue = aKeyValue;
                    }
                    else if (Regex.IsMatch(CrozzleLine, @"^" + CrozzleKeys.EndCrozzleSize + @".*"))
                    {
                        SymbolMark = "NULL";
                    }

                    // Process horizontal-sequences block
                    else if (Regex.IsMatch(CrozzleLine, @"^" + CrozzleKeys.HorizontalSequence + @".*"))
                    {
                        SymbolMark = "HORIZON-OPEN";
                        aCrozzleFileGroup.CzlTitle = CrozzleKeys.HorizontalSequence;
                    }
                    else if (Regex.IsMatch(CrozzleLine, @"^" + CrozzleKeys.HSequence + @".*") && SymbolMark == "HORIZON-OPEN")
                    {
                        KeyValue aKeyValue;
                        if (!KeyValue.TryParse(CrozzleLine, CrozzleKeys.HSequence, out aKeyValue))
                        {
                            Errors.AddRange(KeyValue.Errors);
                        }
                        aCrozzleFileItem.Name     = CrozzleKeys.HSequence;
                        aCrozzleFileItem.KeyValue = aKeyValue;
                    }
                    else if (Regex.IsMatch(CrozzleLine, @"^" + "LOCATION" + @".*") && SymbolMark == "HORIZON-OPEN")
                    {
                        KeyValue aKeyValue;
                        if (!KeyValue.TryParse(CrozzleLine, "LOCATION", out aKeyValue))
                        {
                            Errors.AddRange(KeyValue.Errors);
                        }
                        aCrozzleFileItem.Name     = "LOCATION";
                        aCrozzleFileItem.KeyValue = aKeyValue;
                    }
                    else if (Regex.IsMatch(CrozzleLine, @"^" + CrozzleKeys.EndHorizontalSequence + @".*"))
                    {
                        SymbolMark = "NULL";
                    }

                    // Process vertical-sequences block
                    else if (Regex.IsMatch(CrozzleLine, @"^" + CrozzleKeys.VerticalSequence + @".*"))
                    {
                        SymbolMark = "VERTICAL-OPEN";
                        aCrozzleFileGroup.CzlTitle = CrozzleKeys.VerticalSequence;
                    }
                    else if (Regex.IsMatch(CrozzleLine, @"^" + CrozzleKeys.VSequence + @".*") && SymbolMark == "VERTICAL-OPEN")
                    {
                        KeyValue aKeyValue;
                        if (!KeyValue.TryParse(CrozzleLine, CrozzleKeys.VSequence, out aKeyValue))
                        {
                            Errors.AddRange(KeyValue.Errors);
                        }
                        aCrozzleFileItem.Name     = CrozzleKeys.VSequence;
                        aCrozzleFileItem.KeyValue = aKeyValue;
                    }
                    else if (Regex.IsMatch(CrozzleLine, @"^" + CrozzleKeys.EndVerticalSequence + @".*"))
                    {
                        SymbolMark = "NULL";
                    }
                }
                // Determine whether the line is valid
                aCrozzleFileItem.Valid = Errors.Count == 0;
                // Add the core content in each block
                if (aCrozzleFileItem.Name != null)
                {
                    aCrozzleFileGroup.Lines.Add(aCrozzleFileItem);
                }
            }

            // Check th Validity
            Boolean Valid = true;

            foreach (CrozzleFileItem line in aCrozzleFileGroup.Lines)
            {
                if (line.Valid == false)
                {
                    Valid = false;
                }
            }
            return(Valid);
        }
Beispiel #8
0
        public static Boolean TryParse(String crozzleFileItem, out CrozzleFileItem aCrozzleFileItem)
        {
            Debug.WriteLine(crozzleFileItem);
            Errors           = new List <String>();
            aCrozzleFileItem = new CrozzleFileItem(crozzleFileItem);
            // Discard comment.
            if (crozzleFileItem.Contains("//"))
            {
                int index = crozzleFileItem.IndexOf("//");
                crozzleFileItem       = crozzleFileItem.Remove(index);
                crozzleFileItem       = crozzleFileItem.Trim();
                aCrozzleFileItem.Name = "COMMENTED";
            }
            else if (Regex.IsMatch(crozzleFileItem, @"^\s*$"))
            {
                // Check for only 0 or more white spaces.
                aCrozzleFileItem.Name = NoCrozzleItem;
            }
            else if (crozzleFileItem.Contains("-END-"))
            {
                // New UPDATE
                // discard -end-
                BufferClass.Identifier = "";
                int index = crozzleFileItem.IndexOf("-END-");
                crozzleFileItem       = crozzleFileItem.Remove(index);
                crozzleFileItem       = crozzleFileItem.Trim();
                aCrozzleFileItem.Name = "END";
            }
            else if (crozzleFileItem.Contains(FileDependencies))
            {
                // New UPDATE
                // giving name for unrecognised line
                aCrozzleFileItem.Name = FileDependencies;
            }
            else if (crozzleFileItem.Contains(CrozzleSize))
            {
                // New UPDATE
                // giving name for unrecognised line
                aCrozzleFileItem.Name = CrozzleSize;
            }
            else if (Regex.IsMatch(crozzleFileItem, @"^" + VerticalSequences + "$"))
            {
                // Check for check if the list is vertical sequences
                BufferClass.Identifier = VerticalSequences;
                aCrozzleFileItem.Name  = VerticalSequences;
            }
            else if (Regex.IsMatch(crozzleFileItem, @"^" + HorizontalSequences + "$"))
            {
                // Check for check if the list is horizontal sequences
                BufferClass.Identifier = HorizontalSequences;
                aCrozzleFileItem.Name  = HorizontalSequences;
            }
            else if (Regex.IsMatch(crozzleFileItem, @"^" + ConfigDataSymbol + ".*"))
            {
                // Get the CONFIGURATION_FILE key-value pair.
                KeyValue aKeyValue;
                if (!KeyValue.TryParse(crozzleFileItem, ConfigDataSymbol, out aKeyValue))
                {
                    Errors.AddRange(KeyValue.Errors);
                }
                aCrozzleFileItem.Name     = ConfigDataSymbol;
                aCrozzleFileItem.KeyValue = aKeyValue;
            }

            else if (Regex.IsMatch(crozzleFileItem, @"^" + SequenceDataSymbol + ".*"))
            {
                // Get the WORDLIST_FILE key-value pair.
                KeyValue aKeyValue;
                if (!KeyValue.TryParse(crozzleFileItem, SequenceDataSymbol, out aKeyValue))
                {
                    Errors.AddRange(KeyValue.Errors);
                }
                aCrozzleFileItem.Name     = SequenceDataSymbol;
                aCrozzleFileItem.KeyValue = aKeyValue;
            }
            else if (Regex.IsMatch(crozzleFileItem, @"^" + HorizontalSequencesLine + ".*"))
            {
                // Get the WORDLIST_FILE key-value pair.
                KeyValue aKeyValue;
                if (!KeyValue.TryParse(crozzleFileItem, HorizontalSequencesLine, out aKeyValue))
                {
                    Errors.AddRange(KeyValue.Errors);
                }
                aCrozzleFileItem.Name     = HorizontalSequencesLine;
                aCrozzleFileItem.KeyValue = aKeyValue;
            }
            else if (Regex.IsMatch(crozzleFileItem, @"^" + VerticalSequencesLine + ".*"))
            {
                // Get the WORDLIST_FILE key-value pair.
                KeyValue aKeyValue;
                if (!KeyValue.TryParse(crozzleFileItem, VerticalSequencesLine, out aKeyValue))
                {
                    Errors.AddRange(KeyValue.Errors);
                }
                aCrozzleFileItem.Name     = VerticalSequencesLine;
                aCrozzleFileItem.KeyValue = aKeyValue;
            }
            else if (Regex.IsMatch(crozzleFileItem, @"^" + SizeSymbol + ".*"))
            {
                // Get the number of rows for the crozzle.
                KeyValue aKeyValue;
                if (!KeyValue.TryParse(crozzleFileItem, SizeSymbol, out aKeyValue))
                {
                    Errors.AddRange(KeyValue.Errors);
                }
                aCrozzleFileItem.Name     = SizeSymbol;
                aCrozzleFileItem.KeyValue = aKeyValue;
            }
            else if (Regex.IsMatch(crozzleFileItem, @"^" + SequenceSymbol + ".*"))
            {
                // Get data for a horizontal word.
                KeyValue aKeyValue;
                if (!KeyValue.TryParse(crozzleFileItem, SequenceSymbol, out aKeyValue))
                {
                    Errors.AddRange(KeyValue.Errors);
                }
                aCrozzleFileItem.Name     = SequenceSymbol;
                aCrozzleFileItem.KeyValue = aKeyValue;
            }
            else if (Regex.IsMatch(crozzleFileItem, @"^" + LocationSymbol + ".*"))
            {
                // Get data for a vertical word.
                KeyValue aKeyValue;
                if (!KeyValue.TryParse(crozzleFileItem, LocationSymbol, out aKeyValue))
                {
                    Errors.AddRange(KeyValue.Errors);
                }
                aCrozzleFileItem.Name     = LocationSymbol;
                aCrozzleFileItem.KeyValue = aKeyValue;
            }
            else
            {
                Errors.Add(String.Format(CrozzleFileItemErrors.SymbolError, crozzleFileItem));
            }

            aCrozzleFileItem.Valid = Errors.Count == 0;
            return(aCrozzleFileItem.Valid);
        }
Beispiel #9
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 <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);
        }
Beispiel #10
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);
        }