From 1bed62ee80f217a2318e2165ea7ae15cf181cb13 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sun, 8 Dec 2019 19:29:09 -0500 Subject: [PATCH] file name shortener for 8.3 filenames, etc. --- .../IO/FileNameShortener.cs | 60 +++++++++++++++++++ .../UniversalEditor.Core.csproj | 1 + 2 files changed, 61 insertions(+) create mode 100644 CSharp/Libraries/UniversalEditor.Core/IO/FileNameShortener.cs diff --git a/CSharp/Libraries/UniversalEditor.Core/IO/FileNameShortener.cs b/CSharp/Libraries/UniversalEditor.Core/IO/FileNameShortener.cs new file mode 100644 index 00000000..ed958424 --- /dev/null +++ b/CSharp/Libraries/UniversalEditor.Core/IO/FileNameShortener.cs @@ -0,0 +1,60 @@ +// +// FileNameShortener - truncates a file name with the MS-DOS style filena~1.ext +// +// Author: +// Mike Becker +// +// Copyright (c) 2019 Mike Becker +// +// 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 3 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, see . +using System; +using System.Collections.Generic; + +namespace UniversalEditor.IO +{ + public class FileNameShortener + { + private Dictionary _TruncatedCount = new Dictionary(); + + public int MaxFileNameLength { get; set; } = 8; + public int MaxExtensionLength { get; set; } = 3; + public int Count { get; set; } = 0; + + public string Shorten(string value) + { + string filename = System.IO.Path.GetFileNameWithoutExtension(value); + string extension = System.IO.Path.GetExtension(value); + if (extension.StartsWith(".")) + extension = extension.Substring(1); + + filename = filename.Replace(".", String.Empty); + + if (filename.Length <= MaxFileNameLength && extension.Length <= MaxExtensionLength) + return filename + '.' + extension; + + extension = extension.Substring(0, MaxExtensionLength); + + int len = Math.Min(filename.Length, MaxFileNameLength) - 1 - Count.ToString().Length;// with ~xx.xxx + string truncated = filename.Substring(0, len); + string truncatedWithExtension = truncated + '.' + extension; + if (!_TruncatedCount.ContainsKey(truncatedWithExtension)) + { + _TruncatedCount[truncatedWithExtension] = 0; + } + _TruncatedCount[truncatedWithExtension]++; + + return truncated + '~' + _TruncatedCount[truncatedWithExtension].ToString() + '.' + extension; + } + } +} diff --git a/CSharp/Libraries/UniversalEditor.Core/UniversalEditor.Core.csproj b/CSharp/Libraries/UniversalEditor.Core/UniversalEditor.Core.csproj index e9f46160..a46fc118 100644 --- a/CSharp/Libraries/UniversalEditor.Core/UniversalEditor.Core.csproj +++ b/CSharp/Libraries/UniversalEditor.Core/UniversalEditor.Core.csproj @@ -87,6 +87,7 @@ +