1 // 2 // Copyright (c) ZeroC, Inc. All rights reserved. 3 // 4 5 namespace IceUtilInternal 6 { 7 8 using System.Collections.Generic; 9 using System.IO; 10 using System.Diagnostics; 11 12 public class OutputBase 13 { 14 public OutputBase()15 OutputBase() 16 { 17 out_ = null; 18 pos_ = 0; 19 indent_ = 0; 20 indentSize_ = 4; 21 useTab_ = true; 22 indentSave_ = new Stack<int>(); 23 separator_ = true; 24 } 25 26 public OutputBase(TextWriter writer)27 OutputBase(TextWriter writer) 28 { 29 out_ = writer; 30 pos_ = 0; 31 indent_ = 0; 32 indentSize_ = 4; 33 useTab_ = true; 34 indentSave_ = new Stack<int>(); 35 separator_ = true; 36 } 37 38 public OutputBase(string s)39 OutputBase(string s) 40 { 41 out_ = new StreamWriter(s); 42 pos_ = 0; 43 indent_ = 0; 44 indentSize_ = 4; 45 useTab_ = true; 46 indentSave_ = new Stack<int>(); 47 separator_ = true; 48 } 49 50 virtual public void setIndent(int indentSize)51 setIndent(int indentSize) 52 { 53 indentSize_ = indentSize; 54 } 55 56 virtual public void setUseTab(bool useTab)57 setUseTab(bool useTab) 58 { 59 useTab_ = useTab; 60 } 61 62 public virtual void open(string s)63 open(string s) 64 { 65 try 66 { 67 out_ = new StreamWriter(s); 68 } 69 catch(IOException) 70 { 71 } 72 } 73 74 public virtual void print(string s)75 print(string s) 76 { 77 char[] arr = s.ToCharArray(); 78 for(int i = 0; i < arr.Length; i++) 79 { 80 if(arr[i] == '\n') 81 { 82 pos_ = 0; 83 } 84 else 85 { 86 } 87 } 88 89 out_.Write(s); 90 } 91 92 public virtual void inc()93 inc() 94 { 95 indent_ += indentSize_; 96 } 97 98 public virtual void dec()99 dec() 100 { 101 Debug.Assert(indent_ >= indentSize_); 102 indent_ -= indentSize_; 103 } 104 105 public virtual void useCurrentPosAsIndent()106 useCurrentPosAsIndent() 107 { 108 indentSave_.Push(indent_); 109 indent_ = pos_; 110 } 111 112 public virtual void zeroIndent()113 zeroIndent() 114 { 115 indentSave_.Push(indent_); 116 indent_ = 0; 117 } 118 119 public virtual void restoreIndent()120 restoreIndent() 121 { 122 Debug.Assert(indentSave_.Count != 0); 123 indent_ = (int)indentSave_.Pop(); 124 } 125 126 public virtual void nl()127 nl() 128 { 129 out_.WriteLine(); 130 pos_ = 0; 131 separator_ = true; 132 133 int indent = indent_; 134 135 if(useTab_) 136 { 137 while(indent >= 8) 138 { 139 indent -= 8; 140 out_.Write('\t'); 141 pos_ += 8; 142 } 143 } 144 else 145 { 146 while(indent >= indentSize_) 147 { 148 indent -= indentSize_; 149 out_.Write(" "); 150 pos_ += indentSize_; 151 } 152 } 153 154 while(indent > 0) 155 { 156 --indent; 157 out_.Write(" "); 158 ++pos_; 159 } 160 161 out_.Flush(); 162 } 163 164 public virtual void sp()165 sp() 166 { 167 if(separator_) 168 { 169 out_.WriteLine(); 170 } 171 } 172 173 public virtual bool valid()174 valid() 175 { 176 return out_ != null; 177 } 178 179 protected internal TextWriter out_; 180 protected internal int pos_; 181 protected internal int indent_; 182 protected internal int indentSize_; 183 protected internal Stack<int> indentSave_; 184 protected internal bool useTab_; 185 protected internal bool separator_; 186 } 187 188 } 189