Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/0.9.1/ProtobufCS/src/ProtocolBuffers/TextGenerator.cs @ 5370

Last change on this file since 5370 was 3857, checked in by abeham, 15 years ago

#866

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