TextGenerator.cs 5.4 KB

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