// *********************************************************************** // Assembly : FCS.Lib // Author : FH // Created : 27-08-2016 // // Last Modified By : Frede H. // Last Modified On : 2021-02-24 // *********************************************************************** // // Copyright © FCS 2015-2022 // // // Part of FCS.Lib - a set of utilities for C# - pieced together from fragments // Copyright (C) 2021 FCS // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero 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 Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . // // *********************************************************************** using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace FCS.Lib { /// /// Class Converters /// public static class Mogrifiers { /// /// Reverse boolean /// /// if set to true [value]. /// true if XXXX, false otherwise. public static bool BoolReverse(bool value) { return !value; } /// /// Boolean to integer /// /// if set to true [value]. /// System.Int32. public static int BoolToInt(bool value) { return value ? 1 : 0; } /// /// Boolean to string /// /// if set to true [value]. /// System.String. public static string BoolToString(bool value) { return value ? "true" : "false"; } /// /// Enum to integer /// /// The enumeration. /// System.Int32. public static int EnumToInt(object enumeration) { return Convert.ToInt32(enumeration, CultureInfo.InvariantCulture); } /// /// Enum to string. /// /// The value. /// System.String. public static string EnumToString(Enum value) { return value == null ? string.Empty : value.ToString(); } public static IEnumerable GetEnumList() { return (T[])Enum.GetValues(typeof(T)); } /// /// Integer to boolean. /// /// The value. /// true if XXXX, false otherwise. public static bool IntToBool(int value) { return value > 0 || value < 0; } /// /// Integer to enum. /// /// /// The value. /// T. public static T IntToEnum(int value) { return (T)Enum.ToObject(typeof(T), value); } /// /// Integer to letter. /// /// The value. /// System.String. public static string IntToLetter(int value) { var empty = string.Empty; var num = 97; var str = ""; var num1 = 0; var num2 = 97; for (var i = 0; i <= value; i++) { num1++; empty = string.Concat(str, Convert.ToString(Convert.ToChar(num), CultureInfo.InvariantCulture)); num++; if (num1 != 26) continue; num1 = 0; str = Convert.ToChar(num2).ToString(CultureInfo.InvariantCulture); num2++; num = 97; } return empty; } /// /// Lists to string using semicolon(;) /// /// /// The list. /// System.String. public static string ListToString(List list) { return ListToString(list, ";"); } /// /// Lists to string userdefined delimiter /// /// /// The list. /// The delimiter. /// System.String. public static string ListToString(List list, string delimiter) { var empty = string.Empty; if (list == null) return empty; var enumerator = (IEnumerator)list.GetType().GetMethod("GetEnumerator")?.Invoke(list, null); while (enumerator != null && enumerator.MoveNext()) if (enumerator.Current != null) empty = string.Concat(empty, enumerator.Current.ToString(), delimiter); return empty; } /// /// Pascals to lower. /// /// The value. /// System.String. public static string PascalToLower(string value) { var result = string.Join("-", Regex.Split(value, @"(? /// String to bool. /// /// The value. /// true if XXXX, false otherwise. public static bool StringToBool(string value) { if (string.IsNullOrEmpty(value)) return false; var flag = false; var upper = value.ToUpperInvariant(); if (string.Compare(upper, "true", StringComparison.OrdinalIgnoreCase) == 0) { flag = true; } else if (string.CompareOrdinal(upper, "false") == 0) { } else if (string.CompareOrdinal(upper, "1") == 0) { flag = true; } return flag; } /// /// String to decimal. /// /// The in string. /// System.Nullable<System.Decimal>. public static decimal? StringToDecimal(string inString) { if (string.IsNullOrEmpty(inString)) return 0; return !decimal.TryParse(inString.Replace(",", "").Replace(".", ""), NumberStyles.Number, CultureInfo.InvariantCulture, out var num) ? null : decimal.Divide(num, new decimal((long)100)); } /// /// String to enum. /// /// /// The value. /// T. public static T StringToEnum(string value) { return (T)Enum.Parse(typeof(T), value, true); } /// /// String to list using semicolon(;). /// /// /// The value. /// List<T>. public static List StringToList(string value) { return StringToList(value, ";"); } /// /// String to list userdefined delimiter. /// /// /// The value. /// The delimiter. /// List<T>. /// value /// delimiter /// value /// delimiter /// value /// delimiter /// value public static List StringToList(string value, string delimiter) { if (string.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(value)); if (string.IsNullOrEmpty(delimiter)) throw new ArgumentNullException(nameof(delimiter)); var ts = new List(); var strArrays = value.Split(delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); foreach (var str in strArrays) { var o = typeof(T).FullName; if (o == null) continue; var upperInvariant = o.ToUpperInvariant(); if (string.CompareOrdinal(upperInvariant, "system.string") == 0) { ts.Add((T)Convert.ChangeType(str, typeof(T), CultureInfo.InvariantCulture)); } else if (string.CompareOrdinal(upperInvariant, "system.int32") == 0) { ts.Add((T)Convert.ChangeType(str, typeof(T), CultureInfo.InvariantCulture)); } else if (string.CompareOrdinal(upperInvariant, "system.guid") == 0) { var guid = new Guid(str); ts.Add((T)Convert.ChangeType(guid, typeof(T), CultureInfo.InvariantCulture)); } } return ts; } /// /// String to stream using system default encoding. /// /// The value. /// Stream. public static Stream StringToStream(string value) { return StringToStream(value, Encoding.Default); } /// /// Strings to stream with userdefined encoding. /// /// The value. /// The encoding. /// Stream. public static Stream StringToStream(string value, Encoding encoding) { return encoding == null ? null : new MemoryStream(encoding.GetBytes(value ?? "")); } /// /// Returns timestamp for current date-time object. /// /// System.Int32. public static long CurrentDateTimeToTimeStamp() { return Convert.ToUInt32(DateTimeToTimeStamp(DateTime.Now)); } /// /// Currents the date time to alpha. /// /// System.String. public static string CurrentDateTimeToAlpha() { var dt = DateTime.UtcNow.ToString("yy MM dd HH MM ss"); var sb = new StringBuilder(); var dts = dt.Split(' '); sb.Append((char)int.Parse(dts[0]) + 65); sb.Append((char)int.Parse(dts[1]) + 65); sb.Append((char)int.Parse(dts[2]) + 97); sb.Append((char)int.Parse(dts[3]) + 65); sb.Append((char)int.Parse(dts[4]) + 97); sb.Append((char)int.Parse(dts[5]) + 97); return sb.ToString(); } /// /// Convert a DateTime object to timestamp /// /// The date time. /// System.Int64. public static long DateTimeToTimeStamp(DateTime dateTime) { var bigDate = new DateTime(2038, 1, 19, 0, 0, 0, 0); var nixDate = new DateTime(1970, 1, 1, 0, 0, 0, 0); if (dateTime >= bigDate) return Convert.ToInt64((bigDate - nixDate).TotalSeconds) + Convert.ToInt64((dateTime - bigDate).TotalSeconds); return Convert.ToInt64((dateTime - nixDate).TotalSeconds); } /// /// Convert timestamp to DataTime format /// /// The time stamp. /// DateTime. public static DateTime TimeStampToDateTime(long timeStamp) { var nixDate = new DateTime(1970, 1, 1, 0, 0, 0, 0); return nixDate.AddSeconds(timeStamp); } /// /// Convert timespan to seconds /// /// The timespan. /// System.Int32. public static long TimeSpanToSeconds(TimeSpan timespan) { return Convert.ToUInt32(timespan.Ticks / 10000000L); } /// /// Converts seconds to timespan /// /// The seconds. /// TimeSpan. public static TimeSpan SecondsToTimeSpan(long seconds) { return TimeSpan.FromTicks(10000000L * seconds); } /// /// Converts timespan to minutes /// /// The timespan. /// System.Int32. public static long TimespanToMinutes(TimeSpan timespan) { return Convert.ToUInt32(timespan.Ticks / 10000000L) / 60; } } }