fixedlinelength_dll_visuals.../FixedLineLength/FixedLineLength.cs
Eugen Höglinger 067a6a87a0 Änderung
- Funktion komplett überarbeitet und optimiert
- Trennzeichen für neue Zeile ' ' und '\'
   - Leichtere lesbarkeit von langen Datei-Pfaden
- Keine führenden Leerzeichen mehr am Zeilenanfang
- Zeilenumbrüche im Text werden erkannt und bleiben erhalten
- Leerzeilen werden erkannt und bleiben erhalten
2021-06-01 11:28:34 +02:00

206 lines
6.7 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Eugen.ESystem
{
public static class FixedLineLength
{
#region Version und Copyright
// Version und Copyright
static string dllName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen
static string dllVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
static string copyright = GenerateCopyright();
static string icon = Application.StartupPath + "\\Info.bmp";
//Assembly Datum und Zeit
static DateTime value = AssemblyDateTime();
static string date = value.ToShortDateString();
static string time = value.ToLongTimeString();
private static DateTime AssemblyDateTime()
{
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
var buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(TimeSpan.TicksPerDay * version.Build + TimeSpan.TicksPerSecond * 2 * version.Revision));
//Tage seit dem 1. Januar 2000 und Sekunden seit Mitternacht, (multiplizier mit 2 ergibt das Original)
return buildDateTime;
}
private static string CurrentYear()
{
DateTime dtn = DateTime.Now;
return dtn.Year.ToString();
}
private static string StartYear()
{
string startYear = "";
if (((AssemblyCopyrightAttribute)attributes[0]).Copyright.Contains("Copyright © "))
{
startYear = ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace("Copyright © ", "");
while (startYear.StartsWith(" "))
{
startYear = startYear.Remove(0, 1);
}
startYear = startYear.Remove(startYear.IndexOf(" "));
return startYear;
}
else
{
DateTime dtn = DateTime.Now;
return dtn.Year.ToString();
}
}
private static string GenerateCopyright()
{
string startYear = StartYear();
string currentYear = CurrentYear();
if (startYear == currentYear)
{
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
}
else
{
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace(startYear, String.Concat(startYear, "-", currentYear));
}
}
#endregion
#region DLL-Info
/// <summary>
/// Contains the name of the dll file
/// </summary>
public static string DllName { set; get; }
/// <summary>
/// Contains the version of the dll file
/// </summary>
public static string DllVersion { set; get; }
/// <summary>
/// Contains the copyright notice
/// </summary>
public static string Copyright { set; get; }
static private void SetDllInfo()
{
//Name, Version und Copyright setzen
DllName = dllName;
DllVersion = dllVersion;
Copyright = copyright;
}
#endregion
static FixedLineLength()
{
SetDllInfo();
}
/// <summary>
/// Formats a string with a fixed line length.
/// </summary>
/// <param name="text">String to be formatted.</param>
/// <param name="lineLenght">Maximum line length</param>
/// <returns></returns>
public static string Format(string text, int lineLenght)
{
int ratedLenght = 0;
int spacePos = 0;
int currentPos = 0;
string character = "";
string resultText = "";
if (text.Length <= lineLenght)
{
resultText = text;
}
else
{
while (text.Length > 0)
{
while (ratedLenght < lineLenght)
{
if (currentPos < text.Length)
{
character = text.Substring(currentPos, 1);
}
else
{
break;
}
if (character == "\n" || character == "\r")
{
currentPos++;
ratedLenght = 0;
}
if (character == " " || character == "\\")
{
spacePos = currentPos;
}
currentPos++;
ratedLenght++;
}
if (spacePos == 0)
{
spacePos = currentPos;
}
if (text.Length <= lineLenght || text.Length == spacePos)
{
resultText = resultText + text;
text = "";
}
else
{
resultText = resultText + text.Remove(spacePos) + Environment.NewLine;
text = text.Remove(0, spacePos);
if (text.StartsWith(" "))
{
text = text.Remove(0, 1);
}
}
ratedLenght = 0;
currentPos = 0;
spacePos = 0;
}
}
resultText = CorrectLastLine(resultText, lineLenght);
return resultText;
}
private static string CorrectLastLine(string text, int lineLenght)
{
string tempMid = "";
string tempEnd = "";
tempEnd = text.Remove(0, text.LastIndexOf(Environment.NewLine) + 2);
tempMid = text.Remove(text.LastIndexOf(Environment.NewLine));
tempMid = tempMid.Remove(0, tempMid.LastIndexOf(Environment.NewLine) + 2);
if ((tempMid.Length + tempEnd.Length) <= lineLenght)
{
return text.Remove(text.LastIndexOf(Environment.NewLine), 2);
}
else
{
return text;
}
}
}
}