mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-09 01:35:06 +00:00
Merge branch 'MetLife_v5.3' into XmlConverters
This commit is contained in:
135
ICD.Common.Utils/Csv/CsvWriter.cs
Normal file
135
ICD.Common.Utils/Csv/CsvWriter.cs
Normal file
@@ -0,0 +1,135 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ICD.Common.Properties;
|
||||
using ICD.Common.Utils.IO;
|
||||
|
||||
namespace ICD.Common.Utils.Csv
|
||||
{
|
||||
public sealed class CsvWriter : IDisposable
|
||||
{
|
||||
private const string QUOTATION_MARK = "\"";
|
||||
private const string DOUBLE_QUOTE_MARK = "\"\"";
|
||||
|
||||
private readonly IcdTextWriter m_Writer;
|
||||
|
||||
private readonly string m_Seperator;
|
||||
private readonly string m_LineTerminator;
|
||||
private readonly bool m_AlwaysEscape;
|
||||
|
||||
private bool m_NewLine;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
public CsvWriter(IcdTextWriter writer, bool spaceAfterComma, bool alwaysEscape, string newline, params string[] header)
|
||||
{
|
||||
m_NewLine = true;
|
||||
m_Writer = writer;
|
||||
m_Seperator = spaceAfterComma ? ", " : ",";
|
||||
m_AlwaysEscape = alwaysEscape;
|
||||
m_LineTerminator = newline;
|
||||
|
||||
if(header.Any())
|
||||
AppendRow(header);
|
||||
}
|
||||
|
||||
~CsvWriter()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calls ToString() for each item and adds the row to the builder.
|
||||
/// </summary>
|
||||
/// <param name="row"></param>
|
||||
[PublicAPI]
|
||||
public void AppendRow(params object[] row)
|
||||
{
|
||||
foreach (object value in row)
|
||||
AppendValue(value);
|
||||
AppendNewline();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the row to the builder.
|
||||
/// </summary>
|
||||
/// <param name="row"></param>
|
||||
[PublicAPI]
|
||||
public void AppendRow(params string[] row)
|
||||
{
|
||||
foreach (string value in row)
|
||||
AppendValue(value);
|
||||
AppendNewline();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calls ToString() on the item and adds it to the builder.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
[PublicAPI]
|
||||
public void AppendValue(object value)
|
||||
{
|
||||
AppendValue(string.Format("{0}", value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a value to the builder.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
[PublicAPI]
|
||||
public void AppendValue(string value)
|
||||
{
|
||||
if (!m_NewLine)
|
||||
m_Writer.WrappedTextWriter.Write(m_Seperator);
|
||||
|
||||
if (m_AlwaysEscape || value.Contains(","))
|
||||
{
|
||||
value = value.Replace(QUOTATION_MARK, DOUBLE_QUOTE_MARK);
|
||||
|
||||
// Append the value, surrounded by quotes
|
||||
m_Writer.WrappedTextWriter.Write(QUOTATION_MARK);
|
||||
m_Writer.WrappedTextWriter.Write(value);
|
||||
m_Writer.WrappedTextWriter.Write(QUOTATION_MARK);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Writer.WrappedTextWriter.Write(value);
|
||||
}
|
||||
|
||||
m_NewLine = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a New Line To the Builder
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public void AppendNewline()
|
||||
{
|
||||
m_Writer.WrappedTextWriter.Write(m_LineTerminator);
|
||||
|
||||
m_NewLine = true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
m_Writer.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Instantiates a new CsvWriter with the properties given in the CsvWriterSettings.
|
||||
/// </summary>
|
||||
/// <param name="writer"></param>
|
||||
/// <param name="settings"></param>
|
||||
/// <param name="header"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static CsvWriter Create(IcdTextWriter writer, CsvWriterSettings settings, params string[] header)
|
||||
{
|
||||
return new CsvWriter(writer,
|
||||
settings.InsertSpaceAfterComma,
|
||||
settings.AlwaysEscapeEveryValue,
|
||||
settings.NewLineSequence,
|
||||
header);
|
||||
}
|
||||
}
|
||||
}
|
||||
46
ICD.Common.Utils/Csv/CsvWriterSettings.cs
Normal file
46
ICD.Common.Utils/Csv/CsvWriterSettings.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using ICD.Common.Properties;
|
||||
|
||||
namespace ICD.Common.Utils.Csv
|
||||
{
|
||||
public sealed class CsvWriterSettings
|
||||
{
|
||||
private bool m_InsertSpaceAfterComma = true;
|
||||
private bool m_AlwaysEscapeEveryValue = true;
|
||||
private string m_NewLineSequence = IcdEnvironment.NewLine;
|
||||
|
||||
/// <summary>
|
||||
/// Gets/Sets whether to insert a space between elements, after the comma
|
||||
/// Defaults to true.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public bool InsertSpaceAfterComma
|
||||
{
|
||||
get { return m_InsertSpaceAfterComma; }
|
||||
set { m_InsertSpaceAfterComma = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets/Sets whether to always escape the values.
|
||||
/// If true, values are recorded surrounded by quotes, regardless of if they contain a comma or not. Quotes are escaped.
|
||||
/// If false, values are recorded as the value without quotes, unless escaping is required.
|
||||
/// Defaults to true.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public bool AlwaysEscapeEveryValue
|
||||
{
|
||||
get { return m_AlwaysEscapeEveryValue; }
|
||||
set { m_AlwaysEscapeEveryValue = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets/Sets the newline character or characters to deliniate records.
|
||||
/// Defaults to System.NewLine.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public string NewLineSequence
|
||||
{
|
||||
get { return m_NewLineSequence; }
|
||||
set { m_NewLineSequence = value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,6 +85,8 @@
|
||||
<Compile Include="Comparers\SequenceComparer.cs" />
|
||||
<Compile Include="ConsoleColor.cs" />
|
||||
<Compile Include="EncodingUtils.cs" />
|
||||
<Compile Include="Csv\CsvWriter.cs" />
|
||||
<Compile Include="Csv\CsvWriterSettings.cs" />
|
||||
<Compile Include="EventArguments\BoolEventArgs.cs" />
|
||||
<Compile Include="EventArguments\CharEventArgs.cs" />
|
||||
<Compile Include="EventArguments\DateTimeEventArgs.cs" />
|
||||
@@ -109,6 +111,7 @@
|
||||
<Compile Include="IO\Compression\IcdZipEntry.cs" />
|
||||
<Compile Include="IO\IcdBinaryReader.cs" />
|
||||
<Compile Include="IO\eSeekOrigin.cs" />
|
||||
<Compile Include="IO\IcdStreamWriter.cs" />
|
||||
<Compile Include="ProcessorUtils.SimplSharp.cs" />
|
||||
<Compile Include="ProcessorUtils.Standard.cs" />
|
||||
<Compile Include="ProgramUtils.SimplSharp.cs" />
|
||||
|
||||
@@ -94,5 +94,11 @@ namespace ICD.Common.Utils.IO
|
||||
{
|
||||
return new IcdFileStream(File.Create(path));
|
||||
}
|
||||
|
||||
[PublicAPI]
|
||||
public static IcdStreamWriter AppendText(string path)
|
||||
{
|
||||
return new IcdStreamWriter(File.AppendText(path));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
22
ICD.Common.Utils/IO/IcdStreamWriter.cs
Normal file
22
ICD.Common.Utils/IO/IcdStreamWriter.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
#if SIMPLSHARP
|
||||
using Crestron.SimplSharp.CrestronIO;
|
||||
#elif STANDARD
|
||||
using System.IO;
|
||||
#endif
|
||||
|
||||
namespace ICD.Common.Utils.IO
|
||||
{
|
||||
public sealed class IcdStreamWriter : IcdTextWriter
|
||||
{
|
||||
public StreamWriter WrappedStreamWriter { get { return WrappedTextWriter as StreamWriter; } }
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="baseStreamWriter"></param>
|
||||
public IcdStreamWriter(StreamWriter baseStreamWriter) : base(baseStreamWriter)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user