using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace Eugen.ESystem.Text { public class FormattingLongStrings { #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(); //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); } if (startYear.Contains("-")) { startYear = startYear.Remove(startYear.IndexOf("-")); } else { 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 { string temp = ((AssemblyCopyrightAttribute)attributes[0]).Copyright; string start = temp.Remove(temp.IndexOf(startYear) - 1); string end = (temp.Remove(0, temp.IndexOf(" by"))); return String.Concat(start, " ", startYear, "-", currentYear, end); } } #endregion #region DLL-Info /// /// Contains the name of the dll file. /// public static string DllName { private set; get; } /// /// Contains the version of the dll file. /// public static string DllVersion { private set; get; } /// /// Contains the copyright notice. /// public static string Copyright { private set; get; } /// /// Contains the name-, version- and copyright-information of the dll file. /// public static string[] DllInfo { private set; get; } private static void SetDllInfo() { //Name, Version und Copyright setzen DllName = dllName; DllVersion = dllVersion; Copyright = copyright; string[] dllInfo = new string[3]; dllInfo[0] = dllName; dllInfo[1] = dllVersion; dllInfo[2] = copyright; DllInfo = dllInfo; } protected string ProgramName { private set; get; } protected string ProgramVersion { private set; get; } protected string ProgramCopyright { private set; get; } #endregion static FormattingLongStrings() { SetDllInfo(); } //public FormattingLongStrings() //{ // SetDllInfo(); //} /// /// Defines the length type /// public enum LengthType { LineLength, IndentLength } /// /// Splits a longer text into sections with a defined maximum length for each section /// /// Text to be edited /// Length of the first line in characters /// Length of the following line or the indentation in characters /// Defines whether the line length or the indentation is measured /// Returns a string array with individual lines public static string[] FormattingString(string text, int lengthFirstLine, int lengthSubsequentLines, LengthType lengthType) { // If the text line is longer than the desired first line length, then split it, otherwise it is already ok if (text.Length > lengthFirstLine) { lengthSubsequentLines = LengthSubsequentLines(lengthFirstLine, lengthSubsequentLines, lengthType); // The text length of the other lines is calculated int indentLength = lengthFirstLine - lengthSubsequentLines; ; List lines = new List(); lines.Add(CreateLine(text, lengthFirstLine)); // Create the first line in the length of the first line text = ShortenStringByAlreadyUsed(text, lines[0]); // The text already used is removed from the text text = RemoveLeadingSpaces(text); // If the text line begins with a space, remove it string tempText = ""; // If the text line is longer than the desired line length, then split it while (text.Length > lengthSubsequentLines) { tempText = CreateLine(text, lengthSubsequentLines); // Generate the other lines with the corresponding length if (lengthType == LengthType.LineLength) { lines.Add(tempText); // If the line length of the additional lines } else { // Line length is the length of the first line minus the length of the indentation // Spaces in the number of indents are placed in front of the text lines.Add(tempText.PadLeft(indentLength + tempText.Length, ' ')); } text = ShortenStringByAlreadyUsed(text, tempText); // The text already used is removed from the text text = RemoveLeadingSpaces(text); // If the text line begins with a space, remove it } // If the text still contains characters, then edit if (text.Length > 0) { if (lengthType == LengthType.LineLength) { lines.Add(text); // If the line type is 'Line length' then add } else { lines.Add(text.PadLeft(indentLength + text.Length, ' ')); // Otherwise, precede with a space and then add } } return lines.ToArray(); } else { string[] value = {text}; // If the text is shorter than the line length, then use it return value; } } private static int LengthSubsequentLines(int lengthFirstLine, int lengthSubsequentLines, LengthType lengthType) { // Returns the length of the subsequent lines, depending on the line type if (lengthType == LengthType.LineLength) { return lengthSubsequentLines; // Line length } else { return lengthFirstLine - lengthSubsequentLines; // Line length with indentation } } private static string CreateLine(string text, int lineLength) { // Returns a text section of the specified length string tempLine = text.Remove(lineLength); string tempRemainingLine = text.Remove(0, lineLength); // If the next part begins with a space, the line ends with a whole word if (tempRemainingLine.StartsWith(" ")) { return tempLine; // Then use the line as it is } else { return tempLine.Remove(tempLine.LastIndexOf(" ")); // Otherwise shorten the line up to the last space } } private static string ShortenStringByAlreadyUsed(string text, string usedCharacters) { // Remove the piece of text used from the entire text return text.Replace(usedCharacters, ""); } private static string RemoveLeadingSpaces(string text) { // If the text line begins with a space, remove the space while (text.StartsWith(" ")) { text = text.Remove(0, 1); } return text; } } }