Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtocolBuffers/TextGenerator.cs @ 11062

Last change on this file since 11062 was 8295, checked in by abeham, 12 years ago

#1897:

  • Removed protocol buffers 0.9.1
  • Added protocol buffers 2.4.1
  • Updated proto processing command
File size: 5.4 KB
Line 
1#region Copyright notice and license
2
3// Protocol Buffers - Google's data interchange format
4// Copyright 2008 Google Inc.  All rights reserved.
5// http://github.com/jskeet/dotnet-protobufs/
6// Original C++/Java/Python code:
7// http://code.google.com/p/protobuf/
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13//     * Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//     * Redistributions in binary form must reproduce the above
16// copyright notice, this list of conditions and the following disclaimer
17// in the documentation and/or other materials provided with the
18// distribution.
19//     * Neither the name of Google Inc. nor the names of its
20// contributors may be used to endorse or promote products derived from
21// this software without specific prior written permission.
22//
23// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35#endregion
36
37using System;
38using System.IO;
39using System.Text;
40
41namespace Google.ProtocolBuffers
42{
43    /// <summary>
44    /// Helper class to control indentation. Used for TextFormat and by ProtoGen.
45    /// </summary>
46    public sealed class TextGenerator
47    {
48        /// <summary>
49        /// The string to use at the end of each line. We assume that "Print" is only called using \n
50        /// to indicate a line break; that's what we use to detect when we need to indent etc, and
51        /// *just* the \n is replaced with the contents of lineBreak.
52        /// </summary>
53        private readonly string lineBreak;
54
55        /// <summary>
56        /// Writer to write formatted text to.
57        /// </summary>
58        private readonly TextWriter writer;
59
60        /// <summary>
61        /// Keeps track of whether the next piece of text should be indented
62        /// </summary>
63        private bool atStartOfLine = true;
64
65        /// <summary>
66        /// Keeps track of the current level of indentation
67        /// </summary>
68        private readonly StringBuilder indent = new StringBuilder();
69
70        /// <summary>
71        /// Creates a generator writing to the given writer. The writer
72        /// is not closed by this class.
73        /// </summary>
74        public TextGenerator(TextWriter writer, string lineBreak)
75        {
76            this.writer = writer;
77            this.lineBreak = lineBreak;
78        }
79
80        /// <summary>
81        /// Indents text by two spaces. After calling Indent(), two spaces
82        /// will be inserted at the beginning of each line of text. Indent() may
83        /// be called multiple times to produce deeper indents.
84        /// </summary>
85        public void Indent()
86        {
87            indent.Append("  ");
88        }
89
90        /// <summary>
91        /// Reduces the current indent level by two spaces.
92        /// </summary>
93        public void Outdent()
94        {
95            if (indent.Length == 0)
96            {
97                throw new InvalidOperationException("Too many calls to Outdent()");
98            }
99            indent.Length -= 2;
100        }
101
102        public void WriteLine(string text)
103        {
104            Print(text);
105            Print("\n");
106        }
107
108        public void WriteLine(string format, params object[] args)
109        {
110            WriteLine(string.Format(format, args));
111        }
112
113        public void WriteLine()
114        {
115            WriteLine("");
116        }
117
118        /// <summary>
119        /// Prints the given text to the output stream, indenting at line boundaries.
120        /// </summary>
121        /// <param name="text"></param>
122        public void Print(string text)
123        {
124            int pos = 0;
125
126            for (int i = 0; i < text.Length; i++)
127            {
128                if (text[i] == '\n')
129                {
130                    // Strip off the \n from what we write
131                    Write(text.Substring(pos, i - pos));
132                    Write(lineBreak);
133                    pos = i + 1;
134                    atStartOfLine = true;
135                }
136            }
137            Write(text.Substring(pos));
138        }
139
140        public void Write(string format, params object[] args)
141        {
142            Write(string.Format(format, args));
143        }
144
145        private void Write(string data)
146        {
147            if (data.Length == 0)
148            {
149                return;
150            }
151            if (atStartOfLine)
152            {
153                atStartOfLine = false;
154                writer.Write(indent);
155            }
156            writer.Write(data);
157        }
158    }
159}
Note: See TracBrowser for help on using the repository browser.