diff --git a/OSCADSharp/OSCADSharp/OSCADSharp.csproj b/OSCADSharp/OSCADSharp/OSCADSharp.csproj index 23a88dd..6a7f166 100644 --- a/OSCADSharp/OSCADSharp/OSCADSharp.csproj +++ b/OSCADSharp/OSCADSharp/OSCADSharp.csproj @@ -43,6 +43,7 @@ + diff --git a/OSCADSharp/OSCADSharp/Scripting/StatementBuilder.cs b/OSCADSharp/OSCADSharp/Scripting/StatementBuilder.cs new file mode 100644 index 0000000..3498a90 --- /dev/null +++ b/OSCADSharp/OSCADSharp/Scripting/StatementBuilder.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OSCADSharp.Scripting +{ + /// + /// Extends the capabilities of StringBuilder with domain-specific behavior + /// + internal class StatementBuilder + { + private StringBuilder SB { get; set; } = new StringBuilder(); + + /// + /// Special append method for conditionally adding value-pairs + /// + /// The Name of the value-pair + /// The value - if null this method does nothing + /// (optional) Flag indicating whether a comma should be added before the value-pair + public void AppendValuePairIfExists(string name, object value, bool prefixWithComma = false) + { + if (!String.IsNullOrEmpty(value?.ToString())) + { + if (prefixWithComma) + { + SB.Append(", "); + } + + SB.Append(name); + SB.Append("="); + SB.Append(value); + } + } + + /// + /// Pass-through for StringBuilder.Append + /// + /// + public void Append(string text) + { + SB.Append(text); + } + + /// + /// Pass-through for StringBuilder.AppendLine + /// + /// + public void AppendLine(string text) + { + SB.AppendLine(text); + } + + /// + /// Gets this builder's full string + /// + /// + public override string ToString() + { + return SB.ToString(); + } + } +}