Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Tests/OldTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static void writeData( TextBasedTabularDataFileWriter writer, TextWriter
}

private static void testCsv() {
var csvParser = TabularDataParser.CreateForCsvFile( @"..\..\..\..\TestFiles\TewlTestBook.csv", [ ] );
var csvParser = TabularDataParser.CreateForCsvFile( "../../../../TestFiles/TewlTestBook.csv", [ ] );
var validationErrors = new List<DataValidationError>();

csvParser.ParseAndProcessAllLines( importThing, validationErrors );
Expand All @@ -59,7 +59,7 @@ private static void testCsv() {
}

private static void testXls() {
var xlsParser = TabularDataParser.CreateForExcelFile( @"..\..\..\..\TestFiles\TewlTestBook.xlsx", [ ] );
var xlsParser = TabularDataParser.CreateForExcelFile( "../../../../TestFiles/TewlTestBook.xlsx", [ ] );
var validationErrors = new List<DataValidationError>();

xlsParser.ParseAndProcessAllLines( importThing, validationErrors );
Expand Down
4 changes: 1 addition & 3 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0-windows</TargetFramework>
</PropertyGroup>
<PropertyGroup />

<ItemGroup>
<PackageReference Include="NUnit" Version="4.4.0" />
Expand Down
4 changes: 2 additions & 2 deletions Tewl/FormattingMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static string GetAddressWithNewLines( string deliveryAddress, string city
addOnCode = "";

return StringTools.ConcatenateWithDelimiter(
Environment.NewLine,
Newline,
deliveryAddress,
StringTools.ConcatenateWithDelimiter(
" ",
Expand All @@ -104,7 +104,7 @@ public static string GetAddressWithNewLines( string deliveryAddress, string city
/// Formats the specified address in a single-line format. Do not pass null for any parameters.
/// </summary>
public static string GetAddressOneLine( string deliveryAddress, string city, string stateAbbreviation, string zipCode, string addOnCode ) =>
GetAddressWithNewLines( deliveryAddress, city, stateAbbreviation, zipCode, addOnCode ).Replace( Environment.NewLine, ", " );
GetAddressWithNewLines( deliveryAddress, city, stateAbbreviation, zipCode, addOnCode ).Replace( Newline, ", " );

/// <summary>
/// Uses GetFormattedBytes to return a string in the form "60.1 GiB/s".
Expand Down
3 changes: 2 additions & 1 deletion Tewl/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
global using JetBrains.Annotations;
global using Tewl;
global using Tewl.Tools;
global using static Humanizer.StringExtensions;
global using static Humanizer.StringExtensions;
global using static Tewl.NewlineConstants;
77 changes: 37 additions & 40 deletions Tewl/IO/CsvFileWriter.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,45 @@
using System.IO;
using JetBrains.Annotations;
namespace Tewl.IO;

/// <summary>
/// Helps in writing data to a file in CSV format.
/// </summary>
[ PublicAPI ]
public class CsvFileWriter: TextBasedTabularDataFileWriter {
private string line = "";

namespace Tewl.IO {
/// <summary>
/// Helps in writing data to a file in CSV format.
/// Clears the current line. This does not affect the file at all, it simply undoes any
/// calls to AddValueToLine made since the last WriteCurrentLineToFile call.
/// </summary>
[ PublicAPI ]
public class CsvFileWriter: TextBasedTabularDataFileWriter {
private string line = "";

/// <summary>
/// Clears the current line. This does not affect the file at all, it simply undoes any
/// calls to AddValueToLine made since the last WriteCurrentLineToFile call.
/// </summary>
public void ClearLine() => line = "";
public void ClearLine() => line = "";

/// <summary>
/// Adds the given value as a column on the current line. Value may be null. If
/// it is not null, val.ToString() determines what is added to the line.
/// </summary>
public void AddValueToLine( object val ) {
if( val == null || val.ToString() == "" )
line += ",";
else
line += "\"" + val.ToString().Replace( "\"", "\"\"" ) + "\",";
}
/// <summary>
/// Adds the given value as a column on the current line. Value may be null. If
/// it is not null, val.ToString() determines what is added to the line.
/// </summary>
public void AddValueToLine( object? val ) {
if( val is null || val.ToString() == "" )
line += ",";
else
line += "\"" + val.ToString()!.Replace( "\"", "\"\"" ) + "\",";
}

/// <summary>
/// Add several values to the current line. Collection can be empty but not null.
/// </summary>
public void AddValuesToLine( params object[] values ) {
foreach( var value in values )
AddValueToLine( value );
}
/// <summary>
/// Add several values to the current line. Collection can be empty but not null.
/// </summary>
public void AddValuesToLine( params object[] values ) {
foreach( var value in values )
AddValueToLine( value );
}

/// <summary>
/// Writes the current line to the file using the given open text writer.
/// This clears the current line after writing.
/// </summary>
public void WriteCurrentLineToFile( TextWriter writer ) {
line = line.TrimEnd( ',' );
writer.WriteLine( line );
writer.Flush();
line = "";
}
/// <summary>
/// Writes the current line to the file using the given open text writer.
/// This clears the current line after writing.
/// </summary>
public void WriteCurrentLineToFile( TextWriter writer ) {
line = line.TrimEnd( ',' );
writer.WriteLine( line );
writer.Flush();
line = "";
}
}
96 changes: 46 additions & 50 deletions Tewl/IO/TabDelimitedFileWriter.cs
Original file line number Diff line number Diff line change
@@ -1,60 +1,56 @@
using System;
using System.IO;
using JetBrains.Annotations;
namespace Tewl.IO;

/// <summary>
/// Helps in writing data to a file in tab-separated values format.
/// </summary>
[ PublicAPI ]
public class TabDelimitedFileWriter: TextBasedTabularDataFileWriter {
private const char delimiter = '\t';
private string line = "";

namespace Tewl.IO {
/// <summary>
/// Helps in writing data to a file in tab-separated values format.
/// Clears the current line. This does not affect the file at all, it simply undoes any
/// calls to AddValueToLine made since the last WriteCurrentLineToFile call.
/// </summary>
[ PublicAPI ]
public class TabDelimitedFileWriter: TextBasedTabularDataFileWriter {
private const char delimiter = '\t';
private string line = "";

/// <summary>
/// Clears the current line. This does not affect the file at all, it simply undoes any
/// calls to AddValueToLine made since the last WriteCurrentLineToFile call.
/// </summary>
public void ClearLine() => line = "";
public void ClearLine() => line = "";

/// <summary>
/// Adds the given value as a column on the current line. Value may be null. If
/// it is not null, val.ToString() determines what is added to the line.
/// </summary>
public void AddValueToLine( object val ) {
if( val == null || val.ToString() == "" )
line += delimiter;
else {
var s = val.ToString();
/// <summary>
/// Adds the given value as a column on the current line. Value may be null. If
/// it is not null, val.ToString() determines what is added to the line.
/// </summary>
public void AddValueToLine( object? val ) {
if( val == null || val.ToString() == "" )
line += delimiter;
else {
var s = val.ToString()!;

if( s.Contains(
delimiter.ToString() /*This ToString is unnecessary, but is here for the accidental reason of forcing the correct overload to be selected. An intrusive extension method was introduced in ClosedXML version 102. Once we get beyond 103 we should remove this.*/ ) ||
s.Contains( Environment.NewLine ) )
throw new ApplicationException( "The tab-separated values format does not support tabs or newline sequences in a value." );
line += s + delimiter;
}
if( s.Contains(
delimiter.ToString() /*This ToString is unnecessary, but is here for the accidental reason of forcing the correct overload to be selected. An intrusive extension method was introduced in ClosedXML version 102. Once we get beyond 103 we should remove this.*/ ) ||
s.Contains( Newline ) )
throw new ApplicationException( "The tab-separated values format does not support tabs or newline sequences in a value." );
line += s + delimiter;
}
}

/// <summary>
/// Add several values to the current line. Collection can be empty but not null.
/// Values may be null. If a value is not null, a call to ToString determines what text is added to the line.
/// This method may be called repeatedly to add several sets of values to the same line. The line is only advanced
/// when WriteCurrentLineToFile is called.
/// </summary>
public void AddValuesToLine( params object[] values ) {
foreach( var val in values )
AddValueToLine( val );
}
/// <summary>
/// Add several values to the current line. Collection can be empty but not null.
/// Values may be null. If a value is not null, a call to ToString determines what text is added to the line.
/// This method may be called repeatedly to add several sets of values to the same line. The line is only advanced
/// when WriteCurrentLineToFile is called.
/// </summary>
public void AddValuesToLine( params object[] values ) {
foreach( var val in values )
AddValueToLine( val );
}

/// <summary>
/// Writes the current line to the file using the given open text writer.
/// This clears the current line after writing.
/// </summary>
public void WriteCurrentLineToFile( TextWriter writer ) {
line = line.TrimEnd( delimiter );
writer.WriteLine( line );
writer.Flush();
line = "";
}
/// <summary>
/// Writes the current line to the file using the given open text writer.
/// This clears the current line after writing.
/// </summary>
public void WriteCurrentLineToFile( TextWriter writer ) {
line = line.TrimEnd( delimiter );
writer.WriteLine( line );
writer.Flush();
line = "";
}
}
2 changes: 1 addition & 1 deletion Tewl/IO/XmlOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static TMainElement DeserializeFromStream<TMainElement>( Stream sourceStr
if( validationErrors.Count > 0 ) {
var errorMessage = "";
foreach( var error in validationErrors )
errorMessage += Environment.NewLine + error;
errorMessage += Newline + error;
throw new InvalidDataContractException( "One or more XML validation errors occurred:" + errorMessage );
}

Expand Down
14 changes: 14 additions & 0 deletions Tewl/NewlineConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Tewl;

[ PublicAPI ]
public static class NewlineConstants {
/// <summary>
/// The Unix newline sequence (U+000A LINE FEED). Use this as the default line ending for cross-platform text.
/// </summary>
public const string Newline = "\n";

/// <summary>
/// The Windows newline sequence (U+000D CARRIAGE RETURN followed by U+000A LINE FEED).
/// </summary>
public const string WindowsNewline = "\r\n";
}