Beispiel #1
0
        private void comboBoxSample_SelectedIndexChanged(object sender, EventArgs e)
        {
            string lang = this.comboBoxSample.SelectedItem.ToString();

            this.scintillaControlSample.ConfigurationLanguage = lang;

            ConfigResource resource = new ConfigResource(System.Reflection.Assembly.GetExecutingAssembly(), "Scintilla.Configuration.Samples", lang);

            if (resource.Exists)
            {
                StreamReader reader = new StreamReader(resource.OpenRead());
                this.scintillaControlSample.SetText(reader.ReadToEnd());
                reader.Close();
            }
        }
        public static void Read(ConfigResource resource, SciTEPropertyDelegate propertyRead)
        {
            if (!resource.Exists)
            {
                return;
            }
            StreamReader reader = new StreamReader(resource.OpenRead());

            char          c = ' ', prev = '\\';
            int           lastStart = 0, ignoreCount = 0;
            bool          ignoreProperties = false;
            string        key = null, var = null;
            StringBuilder currentVar   = new StringBuilder();
            StringBuilder currentToken = new StringBuilder();

            Queue <string> queue             = new Queue <string>();
            StringBuilder  currentTokenPiece = new StringBuilder();

            ReadMode mode = ReadMode.Key;
            ReadMode nextModeAfterSpaces = ReadMode.Key;

            string line = reader.ReadLine();

            while (line != null)
            {
                int  start    = 0;
                bool skipLine = false;

                while ((start < line.Length) && char.IsWhiteSpace(line[start]))
                {
                    ++start;
                }

                if (start >= line.Length)
                {
                    PropertyRead(resource, SciTEPropertyType.EmptyLine, queue, string.Empty, string.Empty);
                }
                else if (line[start] == '#')
                {
                    PropertyRead(resource, SciTEPropertyType.Comment, queue, "#", line);
                }
                else
                {
                    if (ignoreProperties)
                    {
                        if ((ignoreCount == 0) || (start == lastStart))
                        {
                            ignoreCount++;
                            lastStart = start;
                            skipLine  = true;
                        }
                        else
                        {
                            ignoreCount      = 0;
                            ignoreProperties = false;
                        }
                    }

                    if (skipLine)
                    {
                        PropertyRead(resource, SciTEPropertyType.EmptyLine, queue, string.Empty, string.Empty);
                    }
                    else
                    {
                        for (int i = start; i < line.Length; i++)
                        {
                            c = line[i];

                            if (mode == ReadMode.Key)
                            {
                                if (c == '=')
                                {
                                    if (currentTokenPiece.Length > 0)
                                    {
                                        queue.Enqueue(currentTokenPiece.ToString());
                                    }

                                    currentTokenPiece.Remove(0, currentTokenPiece.Length);

                                    key = currentToken.ToString();
                                    currentToken.Remove(0, currentToken.Length);

                                    mode = ReadMode.Value;
                                    continue;
                                }
                                else if (char.IsWhiteSpace(c))
                                {
                                    key = currentToken.ToString();
                                    currentToken.Remove(0, currentToken.Length);
                                    currentTokenPiece.Remove(0, currentTokenPiece.Length);

                                    if (key == "if")
                                    {
                                        nextModeAfterSpaces = ReadMode.If;
                                    }
                                    else if (key == "import")
                                    {
                                        nextModeAfterSpaces = ReadMode.Import;
                                    }
                                    else
                                    {
                                        break;
                                    }

                                    mode = ReadMode.FlushWhiteSpace;
                                    continue;
                                }
                                else if (c == '.')
                                {
                                    currentToken.Append(c);

                                    queue.Enqueue(currentTokenPiece.ToString());
                                    currentTokenPiece.Remove(0, currentTokenPiece.Length);
                                }
                                else
                                {
                                    currentTokenPiece.Append(c);
                                    currentToken.Append(c);
                                }
                            }
                            else if (mode == ReadMode.FlushWhiteSpace)
                            {
                                if (!char.IsWhiteSpace(c))
                                {
                                    currentToken.Append(c);
                                    mode = nextModeAfterSpaces;
                                }
                            }
                            else if (mode == ReadMode.Import)
                            {
                                currentToken.Append(c);
                            }
                            else if (mode == ReadMode.If)
                            {
                                currentToken.Append(c);
                            }
                            else if (mode == ReadMode.Value)
                            {
                                currentToken.Append(c);
                            }
                            prev = c;
                        }

                        if (prev != '\\')
                        {
                            var = currentToken.ToString();
                            if (mode == ReadMode.If)
                            {
                                ignoreProperties = PropertyRead(resource, SciTEPropertyType.If, queue, key, var);
                            }
                            else if (mode == ReadMode.Import)
                            {
                                // Open another file inline with this one.
                                if (PropertyRead(resource, SciTEPropertyType.Import, queue, key, var))
                                {
                                    if (resource.Exists)
                                    {
                                        Read(resource, propertyRead);
                                    }
                                }
                            }
                            else if (mode == ReadMode.Value)
                            {
                                PropertyRead(resource, SciTEPropertyType.Property, queue, key, var);
                            }
                            currentToken.Remove(0, currentToken.Length);
                            queue.Clear();
                            key  = null;
                            mode = ReadMode.Key;
                        }
                        else
                        {
                            currentToken.Remove(currentToken.Length - 1, 1);
                        }
                    }
                }
                line = reader.ReadLine();
            }
            reader.Close();

            if (key != null)
            {
                var = currentToken.ToString();
                PropertyRead(resource, SciTEPropertyType.Property, queue, key, var);
            }
        }
        public static void Read(ConfigResource resource, SciTEPropertyDelegate propertyRead)
        {
            if (!resource.Exists) return;
            StreamReader reader = new StreamReader(resource.OpenRead());

            char c = ' ', prev = '\\';
            int lastStart = 0, ignoreCount = 0;
            bool ignoreProperties = false;
            string key = null, var = null;
            StringBuilder currentVar = new StringBuilder();
            StringBuilder currentToken = new StringBuilder();

            Queue<string> queue = new Queue<string>();
            StringBuilder currentTokenPiece = new StringBuilder();

            ReadMode mode = ReadMode.Key;
            ReadMode nextModeAfterSpaces = ReadMode.Key;

            string line = reader.ReadLine();
            while (line != null)
            {
                int start = 0;
                bool skipLine = false;

                while ((start < line.Length) && char.IsWhiteSpace(line[start])) ++start;

                if (start >= line.Length)
                {
                    PropertyRead(resource, SciTEPropertyType.EmptyLine, queue, string.Empty, string.Empty);
                }
                else if (line[start] == '#')
                {
                    PropertyRead(resource, SciTEPropertyType.Comment, queue, "#", line);
                }
                else
                {
                    if (ignoreProperties)
                    {
                        if ((ignoreCount == 0) || (start == lastStart))
                        {
                            ignoreCount++;
                            lastStart = start;
                            skipLine = true;
                        }
                        else
                        {
                            ignoreCount = 0;
                            ignoreProperties = false;
                        }
                    }

                    if (skipLine)
                    {
                        PropertyRead(resource, SciTEPropertyType.EmptyLine, queue, string.Empty, string.Empty);
                    }
                    else
                    {
                        for (int i = start; i < line.Length; i++)
                        {
                            c = line[i];

                            if (mode == ReadMode.Key)
                            {
                                if (c == '=')
                                {
                                    if (currentTokenPiece.Length > 0)
                                    {
                                        queue.Enqueue(currentTokenPiece.ToString());
                                    }

                                    currentTokenPiece.Remove(0, currentTokenPiece.Length);

                                    key = currentToken.ToString();
                                    currentToken.Remove(0, currentToken.Length);

                                    mode = ReadMode.Value;
                                    continue;
                                }
                                else if (char.IsWhiteSpace(c))
                                {
                                    key = currentToken.ToString();
                                    currentToken.Remove(0, currentToken.Length);
                                    currentTokenPiece.Remove(0, currentTokenPiece.Length);

                                    if (key == "if")
                                    {
                                        nextModeAfterSpaces = ReadMode.If;
                                    }
                                    else if (key == "import")
                                    {
                                        nextModeAfterSpaces = ReadMode.Import;
                                    }
                                    else
                                    {
                                        break;
                                    }

                                    mode = ReadMode.FlushWhiteSpace;
                                    continue;
                                }
                                else if (c == '.')
                                {
                                    currentToken.Append(c);

                                    queue.Enqueue(currentTokenPiece.ToString());
                                    currentTokenPiece.Remove(0, currentTokenPiece.Length);
                                }
                                else
                                {
                                    currentTokenPiece.Append(c);
                                    currentToken.Append(c);
                                }
                            }
                            else if (mode == ReadMode.FlushWhiteSpace)
                            {
                                if (!char.IsWhiteSpace(c))
                                {
                                    currentToken.Append(c);
                                    mode = nextModeAfterSpaces;
                                }
                            }
                            else if (mode == ReadMode.Import)
                            {
                                currentToken.Append(c);
                            }
                            else if (mode == ReadMode.If)
                            {
                                currentToken.Append(c);
                            }
                            else if (mode == ReadMode.Value)
                            {
                                currentToken.Append(c);
                            }
                            prev = c;
                        }

                        if (prev != '\\')
                        {
                            var = currentToken.ToString();
                            if (mode == ReadMode.If)
                            {
                                ignoreProperties = PropertyRead(resource, SciTEPropertyType.If, queue, key, var);
                            }
                            else if (mode == ReadMode.Import)
                            {
                                // Open another file inline with this one.
                                if (PropertyRead(resource, SciTEPropertyType.Import, queue, key, var))
                                {
                                    if (resource.Exists)
                                    {
                                        Read(resource, propertyRead);
                                    }
                                }
                            }
                            else if (mode == ReadMode.Value)
                            {
                                PropertyRead(resource, SciTEPropertyType.Property, queue, key, var);
                            }
                            currentToken.Remove(0, currentToken.Length);
                            queue.Clear();
                            key = null;
                            mode = ReadMode.Key;
                        }
                        else
                        {
                            currentToken.Remove(currentToken.Length - 1, 1);
                        }
                    }
                }
                line = reader.ReadLine();
            }
            reader.Close();

            if (key != null)
            {
                var = currentToken.ToString();
                PropertyRead(resource, SciTEPropertyType.Property, queue, key, var);
            }
        }