TextGenerator.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. using System;
  35. using System.IO;
  36. using System.Text;
  37. namespace Google.ProtocolBuffers {
  38. /// <summary>
  39. /// Helper class to control indentation. Used for TextFormat and by ProtoGen.
  40. /// </summary>
  41. public sealed class TextGenerator {
  42. /// <summary>
  43. /// The string to use at the end of each line. We assume that "Print" is only called using \n
  44. /// to indicate a line break; that's what we use to detect when we need to indent etc, and
  45. /// *just* the \n is replaced with the contents of lineBreak.
  46. /// </summary>
  47. private readonly string lineBreak;
  48. /// <summary>
  49. /// Writer to write formatted text to.
  50. /// </summary>
  51. private readonly TextWriter writer;
  52. /// <summary>
  53. /// Keeps track of whether the next piece of text should be indented
  54. /// </summary>
  55. bool atStartOfLine = true;
  56. /// <summary>
  57. /// Keeps track of the current level of indentation
  58. /// </summary>
  59. readonly StringBuilder indent = new StringBuilder();
  60. /// <summary>
  61. /// Creates a generator writing to the given writer. The writer
  62. /// is not closed by this class.
  63. /// </summary>
  64. public TextGenerator(TextWriter writer, string lineBreak) {
  65. this.writer = writer;
  66. this.lineBreak = lineBreak;
  67. }
  68. /// <summary>
  69. /// Indents text by two spaces. After calling Indent(), two spaces
  70. /// will be inserted at the beginning of each line of text. Indent() may
  71. /// be called multiple times to produce deeper indents.
  72. /// </summary>
  73. public void Indent() {
  74. indent.Append(" ");
  75. }
  76. /// <summary>
  77. /// Reduces the current indent level by two spaces.
  78. /// </summary>
  79. public void Outdent() {
  80. if (indent.Length == 0) {
  81. throw new InvalidOperationException("Too many calls to Outdent()");
  82. }
  83. indent.Length -= 2;
  84. }
  85. public void WriteLine(string text) {
  86. Print(text);
  87. Print("\n");
  88. }
  89. public void WriteLine(string format, params object[] args) {
  90. WriteLine(string.Format(format, args));
  91. }
  92. public void WriteLine() {
  93. WriteLine("");
  94. }
  95. /// <summary>
  96. /// Prints the given text to the output stream, indenting at line boundaries.
  97. /// </summary>
  98. /// <param name="text"></param>
  99. public void Print(string text) {
  100. int pos = 0;
  101. for (int i = 0; i < text.Length; i++) {
  102. if (text[i] == '\n') {
  103. // Strip off the \n from what we write
  104. Write(text.Substring(pos, i - pos));
  105. Write(lineBreak);
  106. pos = i + 1;
  107. atStartOfLine = true;
  108. }
  109. }
  110. Write(text.Substring(pos));
  111. }
  112. public void Write(string format, params object[] args) {
  113. Write(string.Format(format, args));
  114. }
  115. private void Write(string data) {
  116. if (data.Length == 0) {
  117. return;
  118. }
  119. if (atStartOfLine) {
  120. atStartOfLine = false;
  121. writer.Write(indent);
  122. }
  123. writer.Write(data);
  124. }
  125. }
  126. }