Exemplo n.º 1
0
        private void WriteBlock(StringBuilder sb, CFGBlock block, int level, bool noCasing = false)
        {
            if (!noCasing)
            {
                if (block.HasValue)
                {
                    sb.AppendLine(String.Format("{0}{1} {2}", GetTabSpacing(level), block.Name, GetValue(block.Value)));
                }
                else
                {
                    sb.AppendLine(String.Format("{0}{1}", new String('\t', level), block.Name));
                }

                sb.AppendLine(String.Format("{0}{{", GetTabSpacing(level)));
            }

            int newLevel = level;

            if (!noCasing)
            {
                newLevel++;
            }

            foreach (CFGBlock b in block.Blocks)
            {
                WriteBlock(sb, b, newLevel);
                sb.AppendLine();
            }

            foreach (CFGProperty property in block)
            {
                WriteProperty(sb, property, newLevel);
            }

            if (!noCasing)
            {
                sb.AppendLine(String.Format("{0}}}", GetTabSpacing(level)));
            }
        }
Exemplo n.º 2
0
        public CFGReader(string inputText)
            : this()
        {
            //try {
            Stack <CFGBlock> blockStack = new Stack <CFGBlock>();

            blockStack.Push(new CFGBlock());

            StringReader reader = new StringReader(inputText);

            for (; ;)
            {
                string line = reader.ReadLine();

                if (line == null)
                {
                    break;
                }

                line = ParseLine(line);

                if (line.Length == 0)
                {
                    continue;
                }

                int colonIndex = SpecialIndexOf(line, ':');
                if (colonIndex != -1)
                {
                    //Its a property
                    blockStack.Peek().Properties.Add(new CFGProperty(line));
                }
                else if (line.StartsWith("{"))
                {
                    continue;
                }
                else if (line.StartsWith("}"))
                {
                    CFGBlock block = blockStack.Pop();

                    if (blockStack.Count > 0)
                    {
                        blockStack.Peek().Blocks.Add(block);
                    }
                    else
                    {
                        mDocument.Blocks.Add(block);
                    }
                }
                else
                {
                    //Block
                    int space = SpecialIndexOf(line, ' ');
                    int tab   = SpecialIndexOf(line, '\t');

                    int firstGap = space;
                    if (tab != -1 && tab < space)
                    {
                        firstGap = tab;
                    }

                    if (firstGap != -1)
                    {
                        CFGBlock block = new CFGBlock();
                        block.Name  = line.Substring(0, firstGap);
                        block.Value = Dequote(line.Substring(firstGap, line.Length - firstGap).Trim());

                        blockStack.Push(block);
                    }
                    else
                    {
                        CFGBlock block = new CFGBlock();
                        block.Name = line;

                        blockStack.Push(block);
                    }
                }
            }

            mDocument.Blocks.Add(blockStack.Pop());

            //} catch (Exception ex) {
            //    System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);

            //    MessageBox.Show(trace.GetFrame(0).GetMethod().Name);
            //    MessageBox.Show("Line: " + trace.GetFrame(0).GetFileLineNumber());
            //    MessageBox.Show("Column: " + trace.GetFrame(0).GetFileColumnNumber());
            //}
        }