[longomatch] Add a helper funtion to sanitize paths



commit da575730591722597c383467d6d5cc217403e36c
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Thu Jan 29 16:32:57 2015 +0100

    Add a helper funtion to sanitize paths

 LongoMatch.Core/Common/Utils.cs        |   38 ++++++++++++++++++++++++++++++++
 LongoMatch.Core/LongoMatch.Core.csproj |    1 +
 LongoMatch.Core/Makefile.am            |    1 +
 LongoMatch/Main.cs                     |    4 +--
 4 files changed, 41 insertions(+), 3 deletions(-)
---
diff --git a/LongoMatch.Core/Common/Utils.cs b/LongoMatch.Core/Common/Utils.cs
new file mode 100644
index 0000000..f06ffcb
--- /dev/null
+++ b/LongoMatch.Core/Common/Utils.cs
@@ -0,0 +1,38 @@
+//
+//  Copyright (C) 2015 Andoni Morales Alastruey
+//
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation; either version 2 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+using System;
+using System.IO;
+
+namespace LongoMatch.Core.Common
+{
+       public class Utils
+       {
+               public static string SanitizePath(string path, params char[] replaceChars)
+               {
+                       path = path.Trim();
+                       foreach (char c in Path.GetInvalidFileNameChars ()) {
+                               path = path.Replace(c, '_');
+                       }
+                       foreach (char c in replaceChars) {
+                               path = path.Replace(c, '_');
+                       }
+                       return path;
+               }
+       }
+}
+
diff --git a/LongoMatch.Core/LongoMatch.Core.csproj b/LongoMatch.Core/LongoMatch.Core.csproj
index 070a6ab..a7c1b54 100644
--- a/LongoMatch.Core/LongoMatch.Core.csproj
+++ b/LongoMatch.Core/LongoMatch.Core.csproj
@@ -135,6 +135,7 @@
     <Compile Include="Stats\PlayerEventTypeStats.cs" />
     <Compile Include="Stats\PlayerStats.cs" />
     <Compile Include="Stats\TimerStats.cs" />
+    <Compile Include="Common\Utils.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="Common\" />
diff --git a/LongoMatch.Core/Makefile.am b/LongoMatch.Core/Makefile.am
index f8d54d3..f8fc8be 100644
--- a/LongoMatch.Core/Makefile.am
+++ b/LongoMatch.Core/Makefile.am
@@ -26,6 +26,7 @@ SOURCES = Common/Area.cs \
        Common/Log.cs \
        Common/Serializer.cs \
        Common/SysInfo.cs \
+       Common/Utils.cs \
        Common/VideoStandards.cs \
        Config.cs \
        Handlers/Drawing.cs \
diff --git a/LongoMatch/Main.cs b/LongoMatch/Main.cs
index 977bf49..dfa3c6d 100644
--- a/LongoMatch/Main.cs
+++ b/LongoMatch/Main.cs
@@ -128,9 +128,7 @@ namespace LongoMatch
                        string logFile = Constants.SOFTWARE_NAME + "-" + DateTime.Now + ".log";
                        string message;
 
-                       logFile = logFile.Replace ("/", "-");
-                       logFile = logFile.Replace (" ", "-");
-                       logFile = logFile.Replace (":", "-");
+                       logFile = Utils.SanitizePath (logFile, ' ', ':');
                        logFile = System.IO.Path.Combine (Config.HomeDir, logFile);
 
                        message = SysInfo.PrintInfo (Assembly.GetExecutingAssembly ().GetName ().Version);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]