using System; using System.Text; using System.Globalization; using Woodpecker.Specialized.Encoding; namespace Woodpecker.Specialized.Text { /// /// An enhanced System.Text.StringBuilder for creating strings for the FUSE protocol. /// public class fuseStringBuilder { #region Fields /// /// The actual content of the string builder. /// protected StringBuilder Content; #endregion #region Constructors /// /// Initializes the System.Text.StringBuilder inside the FUSE stringbuilder. /// public fuseStringBuilder() { this.Content = new StringBuilder(); } #endregion #region Methods #region Normal appending methods /// /// Appends a string to the string builder. /// /// The string to append. public void Append(string s) { this.Content.Append(s); } /// /// Appends an integer as a string to the string builder. /// /// The integer to append. public void Append(int i) { this.Content.Append(i); } /// /// Appends a floating point value to the string builder, after formatting it to 0.00 format. /// /// The floating point value to format and append. public void Append(float f) { this.Content.Append(f.ToString("0.00", CultureInfo.InvariantCulture)); } #endregion #region Special appending methods /// /// Appends a Unicode character to the string builder. /// /// The number of the character to append. public void appendChar(int x) { this.Content.Append(Convert.ToChar(x)); } /// /// Appends a wire encoded integer to the string builder. /// /// The integer to encode and append. public void appendWired(int i) { this.Content.Append(wireEncoding.Encode(i)); } /// /// Appends a wire encoded boolean to the string builder. /// /// The boolean to encode and append. public void appendWired(bool b) { if (b) this.Content.Append('I'); else this.Content.Append('H'); } /// /// Appends a value in the 'key'-'value' format, with a given separator. The key-value pair is is closed by char13. /// /// The key of the value to append as a string. /// The value to append. /// The Unicode character that separates the key and the value. public void appendKeyValueParameter(string Key, object Value, char Separator) { this.Content.Append(Key); this.Content.Append(Separator); this.Content.Append(Value.ToString()); this.appendChar(13); } /// /// Appends a value in the 'key'-'value' format, with ':' as separator. The key-value pair is is closed by char13. /// /// The key of the value to append as a string. /// The value to append. /// appendKeyValueParameter(string,object,char) public void appendKeyValueParameter(string Key, object Value) { this.appendKeyValueParameter(Key, Value, ':'); } /// /// Appends a value followed by a char9 to the string builder. /// /// The value to append. public void appendTabbedValue(string Value) { this.Content.Append(Value); this.appendChar(9); } /// /// Appends a value closed by char2 to the string builder. /// /// The value to append. public void appendClosedValue(string Value) { if(Value != null) this.Content.Append(Value); this.appendChar(2); } /// /// Appends a value closed by char13 to the string builder. /// /// The value to append. public void appendNewLineValue(string Value) { if (Value != null) this.Content.Append(Value); this.appendChar(13); } /// /// Appends a value followed by a char30 to the string builder. /// /// The value to append. public void appendStripValue(string Value) { this.Content.Append(Value); this.appendChar(30); } /// /// Appends a value followed by a single whitespace ('spacebar') character to the string builder. /// The value to append. /// public void appendWhiteSpacedValue(string Value) { this.Content.Append(Value); this.Content.Append(" "); } #endregion #region Other methods /// /// Clears the content from the string builder. /// public void Clear() { this.Content = new StringBuilder(); } /// /// Returns the content of the stringbuilder inside this object as a string. /// public override string ToString() { return this.Content.ToString(); } #endregion #endregion } }