308 lines
8.7 KiB
C#
308 lines
8.7 KiB
C#
/*-------------------------------------------------------------------------
|
|
// UNCLASSIFIED
|
|
/*-------------------------------------------------------------------------
|
|
RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION
|
|
PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS
|
|
AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO
|
|
UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO
|
|
RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS
|
|
CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS
|
|
OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON
|
|
COMPANY.
|
|
|
|
THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S.
|
|
GOVERNMENT.
|
|
|
|
UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
|
|
-------------------------------------------------------------------------*/
|
|
using System;
|
|
using System.DirectoryServices;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using NLog;
|
|
|
|
namespace ProgramLib
|
|
{
|
|
/// <summary>
|
|
/// Provides utility functions for other classes to use
|
|
/// </summary>
|
|
internal static class Util
|
|
{
|
|
/// <summary>
|
|
/// Check if directory is empty
|
|
/// </summary>
|
|
public static bool IsDirectoryEmpty(string path)
|
|
{
|
|
return !Directory.EnumerateFileSystemEntries(path).Any();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generate random fractional part
|
|
/// </summary>
|
|
public static float GenerateRandomFraction()
|
|
{
|
|
Random rnd = new Random();
|
|
int randNum = 0;
|
|
const int minimum = 1;
|
|
|
|
randNum = rnd.Next(20);
|
|
|
|
if (randNum <= minimum)
|
|
{
|
|
randNum += minimum;
|
|
}
|
|
|
|
return (float)(1.0 / (float)randNum);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generate unique file name
|
|
/// Format: [Prefix]_YYYY_MM_DD_HH_MM_SS.[ext]
|
|
/// </summary>
|
|
public static string GenerateUniqueFilenameUsingDateTime(string prefix, string fileExtension)
|
|
{
|
|
string filename = prefix + "_" + DateTime.Now.ToString("yyyy_MM_dd") + "_" + DateTime.Now.ToString("HH_mm_ss");
|
|
|
|
if (fileExtension[0] != '.')
|
|
{
|
|
fileExtension = "." + fileExtension;
|
|
}
|
|
|
|
filename += fileExtension;
|
|
|
|
return filename;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Log Exception
|
|
/// </summary>
|
|
public static string LogException(Exception ex, ILogger logger)
|
|
{
|
|
string errorMsg = ex.Message;
|
|
string stackTrace = ex.StackTrace;
|
|
|
|
Exception innerEx = ex.InnerException;
|
|
while (innerEx != null)
|
|
{
|
|
errorMsg = innerEx.Message;
|
|
stackTrace = innerEx.StackTrace + "\r\n" + stackTrace;
|
|
|
|
innerEx = innerEx.InnerException;
|
|
}
|
|
|
|
logger.Error(errorMsg + "\r\n" + stackTrace);
|
|
|
|
return errorMsg;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Auto format any numeric type to string
|
|
/// </summary>
|
|
/// <param name="wholeNumberMaxDigits">number of digits to display for the whole number before it display in scientific notation</param>
|
|
public static string AutoFormatNumberToString<T>(T o, int wholeNumberMaxDigits = 3, int numDecimalPlaces = 2) where T : IConvertible
|
|
{
|
|
try
|
|
{
|
|
if (Convert.ChangeType(o, typeof(double)) is double @double)
|
|
{
|
|
string originalStr = @double.ToString("G"); // Most compact
|
|
string tempStr = originalStr;
|
|
int indexOfDecimalPoint = tempStr.IndexOf(".");
|
|
if (indexOfDecimalPoint == -1)
|
|
tempStr += ".0";
|
|
indexOfDecimalPoint = tempStr.IndexOf(".");
|
|
int tempStrIndex = 0;
|
|
int indexOfMinusSign = tempStr.IndexOf("-");
|
|
if (indexOfMinusSign != -1)
|
|
tempStrIndex = 1;
|
|
int wholeNumberNumDigits = tempStr.Substring(tempStrIndex, indexOfDecimalPoint - tempStrIndex).Length;
|
|
int fractionalNumDigits = tempStr.Substring(indexOfDecimalPoint + 1, tempStr.Length - (indexOfDecimalPoint + 1)).Length;
|
|
if (wholeNumberNumDigits > wholeNumberMaxDigits || tempStr.IndexOf("E") != -1)
|
|
{
|
|
string decimalPlaces = String.Empty;
|
|
for (int i = 1; i <= numDecimalPlaces; i++)
|
|
{
|
|
decimalPlaces += "#";
|
|
}
|
|
var custom = @double.ToString($"0.{decimalPlaces}E0").Replace("E+", "E");
|
|
while (custom.Contains("E0")) custom = custom.Replace("E0", "E");
|
|
while (custom.Contains("-0")) custom = custom.Replace("-0", "-");
|
|
if ((@double < 0) && custom[0] != '-')
|
|
{
|
|
custom.Insert(0, "-");
|
|
}
|
|
return custom;
|
|
}
|
|
else if (fractionalNumDigits > numDecimalPlaces)
|
|
{
|
|
return @double.ToString("0.##");
|
|
}
|
|
else
|
|
{
|
|
return originalStr;
|
|
}
|
|
}
|
|
}
|
|
catch { }
|
|
return o.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get full name of windows user
|
|
/// </summary>
|
|
public static string GetWindowsUserFullName(string domain, string userName)
|
|
{
|
|
string name = "";
|
|
|
|
try
|
|
{
|
|
DirectoryEntry userEntry = new DirectoryEntry("WinNT://" + domain + "/" + userName + ",User");
|
|
name = (string)userEntry.Properties["fullname"].Value;
|
|
}
|
|
catch
|
|
{
|
|
name = userName;
|
|
}
|
|
|
|
return name;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert struct to byte array
|
|
/// </summary>
|
|
public static byte[] StructToByteList<T>(T data)
|
|
{
|
|
int size = Marshal.SizeOf(data);
|
|
|
|
byte[] arr = new byte[size];
|
|
|
|
GCHandle h = default(GCHandle);
|
|
|
|
try
|
|
{
|
|
h = GCHandle.Alloc(arr, GCHandleType.Pinned);
|
|
|
|
Marshal.StructureToPtr(data, h.AddrOfPinnedObject(), false);
|
|
}
|
|
finally
|
|
{
|
|
if (h.IsAllocated)
|
|
{
|
|
h.Free();
|
|
}
|
|
}
|
|
|
|
return arr;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert byte array to struct
|
|
/// </summary>
|
|
public static T ByteArrayToStruct<T>(byte[] array)
|
|
{
|
|
object obj;
|
|
|
|
GCHandle h = default(GCHandle);
|
|
|
|
try
|
|
{
|
|
h = GCHandle.Alloc(array, GCHandleType.Pinned);
|
|
|
|
obj = Marshal.PtrToStructure(h.AddrOfPinnedObject(), typeof(T));
|
|
|
|
}
|
|
finally
|
|
{
|
|
if (h.IsAllocated)
|
|
{
|
|
h.Free();
|
|
}
|
|
}
|
|
|
|
return (T)obj;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Two's Compliment Checksum
|
|
/// </summary>
|
|
public static byte GetTwosComplimentChecksum(byte[] array)
|
|
{
|
|
return (byte)(0x100u - ComputeSum(array));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add up all bytes in a byte array
|
|
/// </summary>
|
|
public static byte ComputeSum(byte[] array)
|
|
{
|
|
byte sum = 0;
|
|
|
|
for (int i = 0; i < array.Length; i++)
|
|
sum += array[i];
|
|
|
|
return sum;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Display each byte in byte array as hex string
|
|
/// </summary>
|
|
public static string ByteArrayToHexString(byte[] bytes, int numBytes = -1)
|
|
{
|
|
StringBuilder Result = new StringBuilder(bytes.Length * 2);
|
|
string HexAlphabet = "0123456789ABCDEF";
|
|
|
|
if (numBytes < 1)
|
|
{
|
|
numBytes = bytes.Length;
|
|
}
|
|
|
|
for (int i = 0; i < numBytes; i++)
|
|
{
|
|
Result.Append("0x" + HexAlphabet[(int)(bytes[i] >> 4)]);
|
|
Result.Append(HexAlphabet[(int)(bytes[i] & 0xF)] + " ");
|
|
}
|
|
|
|
return Result.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Given a variable of type TimeSpan, describe the time in days, hours, mins, seconds
|
|
/// </summary>
|
|
public static string DescribeTimeElapsed(TimeSpan ts)
|
|
{
|
|
string describe = "";
|
|
|
|
if (ts.Days > 0)
|
|
describe += ts.Days.ToString() + " d";
|
|
|
|
if (ts.Hours > 0)
|
|
{
|
|
if (describe.Length > 0)
|
|
describe += " ";
|
|
describe += ts.Hours.ToString() + " h";
|
|
}
|
|
|
|
if (ts.Minutes > 0)
|
|
{
|
|
if (describe.Length > 0)
|
|
describe += " ";
|
|
describe += ts.Minutes.ToString() + " m";
|
|
}
|
|
|
|
if (ts.Seconds > 0)
|
|
{
|
|
if (describe.Length > 0)
|
|
describe += " ";
|
|
describe += ts.Seconds.ToString() + " s";
|
|
}
|
|
|
|
if (describe.Length == 0)
|
|
describe = "0 s";
|
|
|
|
return describe;
|
|
}
|
|
}
|
|
}
|