TextGenerator.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. using System;
  17. using System.IO;
  18. using System.Text;
  19. namespace Google.ProtocolBuffers {
  20. /// <summary>
  21. /// Helper class to control indentation. Used for TextFormat and by ProtoGen.
  22. /// </summary>
  23. public sealed class TextGenerator {
  24. /// <summary>
  25. /// Writer to write formatted text to.
  26. /// </summary>
  27. private readonly TextWriter writer;
  28. /// <summary>
  29. /// Keeps track of whether the next piece of text should be indented
  30. /// </summary>
  31. bool atStartOfLine = true;
  32. /// <summary>
  33. /// Keeps track of the current level of indentation
  34. /// </summary>
  35. readonly StringBuilder indent = new StringBuilder();
  36. /// <summary>
  37. /// Creates a generator writing to the given writer. The writer
  38. /// is not closed by this class.
  39. /// </summary>
  40. public TextGenerator(TextWriter writer) {
  41. this.writer = writer;
  42. }
  43. /// <summary>
  44. /// Indents text by two spaces. After calling Indent(), two spaces
  45. /// will be inserted at the beginning of each line of text. Indent() may
  46. /// be called multiple times to produce deeper indents.
  47. /// </summary>
  48. public void Indent() {
  49. indent.Append(" ");
  50. }
  51. /// <summary>
  52. /// Reduces the current indent level by two spaces.
  53. /// </summary>
  54. public void Outdent() {
  55. if (indent.Length == 0) {
  56. throw new InvalidOperationException("Too many calls to Outdent()");
  57. }
  58. indent.Length -= 2;
  59. }
  60. public void WriteLine(string text) {
  61. Print(text);
  62. Print("\n");
  63. }
  64. public void WriteLine(string format, params object[] args) {
  65. WriteLine(string.Format(format, args));
  66. }
  67. public void WriteLine() {
  68. WriteLine("");
  69. }
  70. /// <summary>
  71. /// Prints the given text to the output stream, indenting at line boundaries.
  72. /// </summary>
  73. /// <param name="text"></param>
  74. public void Print(string text) {
  75. int pos = 0;
  76. for (int i = 0; i < text.Length; i++) {
  77. if (text[i] == '\n') {
  78. // TODO(jonskeet): Use Environment.NewLine?
  79. Write(text.Substring(pos, i - pos + 1));
  80. pos = i + 1;
  81. atStartOfLine = true;
  82. }
  83. }
  84. Write(text.Substring(pos));
  85. }
  86. public void Write(string format, params object[] args) {
  87. Write(string.Format(format, args));
  88. }
  89. private void Write(string data) {
  90. if (data.Length == 0) {
  91. return;
  92. }
  93. if (atStartOfLine) {
  94. atStartOfLine = false;
  95. writer.Write(indent);
  96. }
  97. writer.Write(data);
  98. }
  99. }
  100. }