-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to set charset for export (#31)
* fix: replace UDE.CSharp with Ude.NetStandard because Ude.NetStandard is actually compatible with net5.0 * feat: add ability to set charset for export because that can sometimes be an issue for certain providers * test: fix failing test
- Loading branch information
Showing
14 changed files
with
131 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Text; | ||
|
||
namespace CsvProc9000.Utils.Contracts | ||
{ | ||
internal interface IFileHelper | ||
{ | ||
Encoding DetectEncodingOfFile(string fileName); | ||
Encoding GetEncodingFromCharsetString(string charset); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using CsvProc9000.Utils.Contracts; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.IO.Abstractions; | ||
using System.Text; | ||
using Ude; | ||
|
||
namespace CsvProc9000.Utils | ||
{ | ||
[ExcludeFromCodeCoverage] // mostly untestable because of file-magic | ||
internal sealed class FileHelper : IFileHelper | ||
{ | ||
private readonly IFileSystem _fileSystem; | ||
private readonly ILogger<FileHelper> _logger; | ||
|
||
public FileHelper( | ||
[NotNull] IFileSystem fileSystem, | ||
[NotNull] ILogger<FileHelper> logger) | ||
{ | ||
_fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem)); | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
public Encoding DetectEncodingOfFile(string fileName) | ||
{ | ||
using var fileStream = _fileSystem.FileStream.Create(fileName, FileMode.Open, FileAccess.Read); | ||
|
||
_logger.LogTrace("Trying to detect charset of '{File}'", fileName); | ||
|
||
var detector = new CharsetDetector(); | ||
detector.Feed(fileStream); | ||
detector.DataEnd(); | ||
var charset = detector.Charset; | ||
|
||
_logger.LogTrace("Found charset '{Charset}' with a confidence of {Confidence}", | ||
charset, detector.Confidence); | ||
|
||
var encodingFromCharset = GetEncodingFromCharsetString(charset); | ||
return encodingFromCharset; | ||
} | ||
|
||
public Encoding GetEncodingFromCharsetString(string charset) | ||
{ | ||
var encoding = Encoding.GetEncoding(charset); | ||
if (encoding == null) | ||
throw new ArgumentException($"Could not find encoding for charset '{charset}'", nameof(charset)); | ||
|
||
return encoding; | ||
} | ||
} | ||
} |