diff options
author | Sverrir Sigmundarson <[email protected]> | 2015-11-18 23:57:03 +0100 |
---|---|---|
committer | Sverrir Sigmundarson <[email protected]> | 2015-11-21 01:16:57 +0100 |
commit | 9e107ee850e396044c4c80c3257a4623995b88b3 (patch) | |
tree | 9abf59556c18f9c24c3841a9573fc78f0de62992 /win/CS/HandBrakeWPF/Utilities/Output | |
parent | 6c731e1353608b909ce1e721e9b31b2fea6f932c (diff) |
Fixing importing and exporting of chapters via CSV files. Adding proper handling of escape characters, handling of most common alternative value separators. Fixing resource leakage via undisposed FileDialogs.
Diffstat (limited to 'win/CS/HandBrakeWPF/Utilities/Output')
-rw-r--r-- | win/CS/HandBrakeWPF/Utilities/Output/CsvHelper.cs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/win/CS/HandBrakeWPF/Utilities/Output/CsvHelper.cs b/win/CS/HandBrakeWPF/Utilities/Output/CsvHelper.cs new file mode 100644 index 000000000..cf2d7a38d --- /dev/null +++ b/win/CS/HandBrakeWPF/Utilities/Output/CsvHelper.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HandBrakeWPF.Utilities.Output +{ + /// <summary> + /// Utilitiy functions for writing CSV files + /// </summary> + internal sealed class CsvHelper + { + private const string QUOTE = "\""; + private const string ESCAPED_QUOTE = "\"\""; + private static readonly char[] CHARACTERS_THAT_MUST_BE_QUOTED = { ',', '"', '\n' }; + + /// <summary> + /// Properly escapes a string value containing reserved characters with double quotes "..." before it is written to a CSV file. + /// </summary> + /// <param name="value">Value to be escaped</param> + /// <returns>Fully escaped value</returns> + public static string Escape(string value) + { + if (value.Contains(QUOTE)) + value = value.Replace(QUOTE, ESCAPED_QUOTE); + + if (value.IndexOfAny(CHARACTERS_THAT_MUST_BE_QUOTED) > -1) + value = QUOTE + value + QUOTE; + + return value; + } + } +} |