From 0f109aa980548276f566461fe686dda60b930f32 Mon Sep 17 00:00:00 2001
From: Michael Krisper <michael.krisper@tugraz.at>
Date: Thu, 25 Feb 2016 13:46:42 +0100
Subject: [PATCH] read/write files with UTF8, replace invalid characters in
 filenames

---
 VectoCore/OutputData/SummaryDataContainer.cs | 17 +++++++++++------
 VectoCore/Utils/VectoCSVFile.cs              |  8 +++++---
 2 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/VectoCore/OutputData/SummaryDataContainer.cs b/VectoCore/OutputData/SummaryDataContainer.cs
index 712a33aa54..f31abc6f2f 100644
--- a/VectoCore/OutputData/SummaryDataContainer.cs
+++ b/VectoCore/OutputData/SummaryDataContainer.cs
@@ -140,9 +140,9 @@ namespace TUGraz.VectoCore.OutputData
 			string cycleFileName)
 		{
 			var row = _table.NewRow();
-			row[JOB] = jobName;
-			row[INPUTFILE] = jobFileName;
-			row[CYCLE] = cycleFileName;
+			row[JOB] = ReplaceNotAllowedCharacters(jobName);
+			row[INPUTFILE] = ReplaceNotAllowedCharacters(jobFileName);
+			row[CYCLE] = ReplaceNotAllowedCharacters(cycleFileName);
 			row[STATUS] = data.RunStatus;
 			row[TIME] = data.Duration();
 			row[PPOS] = data.EnginePowerPositiveAverage().ConvertTo().Kilo.Watt;
@@ -163,9 +163,9 @@ namespace TUGraz.VectoCore.OutputData
 
 			var row = _table.NewRow();
 			_table.Rows.Add(row);
-			row[JOB] = jobName;
-			row[INPUTFILE] = jobFileName;
-			row[CYCLE] = cycleFileName;
+			row[JOB] = ReplaceNotAllowedCharacters(jobName);
+			row[INPUTFILE] = ReplaceNotAllowedCharacters(jobFileName);
+			row[CYCLE] = ReplaceNotAllowedCharacters(cycleFileName);
 			row[STATUS] = data.RunStatus;
 			row[TIME] = data.Duration();
 
@@ -243,6 +243,11 @@ namespace TUGraz.VectoCore.OutputData
 			row[PSTOP] = data.PercentStopTime();
 		}
 
+		private static string ReplaceNotAllowedCharacters(string text)
+		{
+			return text.Replace('#', '_').Replace(',', '_').Replace('\n', '_').Replace('\r', '_');
+		}
+
 		[MethodImpl(MethodImplOptions.Synchronized)]
 		private void WriteAuxiliaries(IModalDataContainer data, DataRow row)
 		{
diff --git a/VectoCore/Utils/VectoCSVFile.cs b/VectoCore/Utils/VectoCSVFile.cs
index 8bf819c42a..acd6ef4bdc 100644
--- a/VectoCore/Utils/VectoCSVFile.cs
+++ b/VectoCore/Utils/VectoCSVFile.cs
@@ -32,10 +32,12 @@
 using System;
 using System.Collections.Generic;
 using System.Data;
+using System.Diagnostics;
 using System.Diagnostics.Contracts;
 using System.Globalization;
 using System.IO;
 using System.Linq;
+using System.Text;
 using System.Text.RegularExpressions;
 using TUGraz.VectoCore.Exceptions;
 using TUGraz.VectoCore.Models;
@@ -71,7 +73,7 @@ namespace TUGraz.VectoCore.Utils
 		public static DataTable Read(string fileName, bool ignoreEmptyColumns = false, bool fullHeader = false)
 		{
 			try {
-				return ReadData(File.ReadAllLines(fileName), ignoreEmptyColumns, fullHeader);
+				return ReadData(File.ReadAllLines(fileName, Encoding.UTF8), ignoreEmptyColumns, fullHeader);
 			} catch (Exception e) {
 				Logger<VectoCSVFile>().Error(e);
 				throw new VectoException("File {0}: {1}", fileName, e.Message);
@@ -160,7 +162,7 @@ namespace TUGraz.VectoCore.Utils
 
 		private static string[] RemoveComments(string[] lines)
 		{
-			Contract.Requires(lines != null);
+			Debug.Assert(lines != null);
 
 			lines = lines.
 				Select(line => line.Contains('#') ? line.Substring(0, line.IndexOf(Comment)) : line).
@@ -177,7 +179,7 @@ namespace TUGraz.VectoCore.Utils
 		/// <param name="table">The Datatable.</param>
 		public static void Write(string fileName, DataTable table)
 		{
-			var stream = new StreamWriter(fileName);
+			var stream = new StreamWriter(new FileStream(fileName, FileMode.Create), Encoding.UTF8);
 			Write(stream, table);
 			stream.Close();
 		}
-- 
GitLab