Files
cms-core-active/CMS_CORE_Library/Utils/Nc_Utils.cs
T
Lucio Maranta c8f5fd6bbb
2018-08-29 15:35:45 +00:00

213 lines
6.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static CMS_CORE_Library.DataStructures;
namespace CMS_CORE.Utils
{
public static class Nc_Utils
{
//-------------------------------------------------------------------------------------------------------------------------------------------------
// NC <-> .NET Conversions
// INT -> 2 WORD
public static void IntTOWord(int IntVal, out ushort LowWord, out ushort HighWord)
{
byte[] bytes = BitConverter.GetBytes(IntVal);
//Split into 2 word
LowWord = BitConverter.ToUInt16(bytes, 0);
HighWord = BitConverter.ToUInt16(bytes, 2);
}
// 2 WORD -> INT
public static void WordTOInt(out int IntVal, ushort LowWord, ushort HighWord)
{
byte[] bytes = new byte[4];
//fill the bytes from 2 word
bytes[0] = BitConverter.GetBytes(LowWord)[0];
bytes[1] = BitConverter.GetBytes(LowWord)[1];
bytes[2] = BitConverter.GetBytes(HighWord)[0];
bytes[3] = BitConverter.GetBytes(HighWord)[1];
//Create the value
IntVal = BitConverter.ToInt32(bytes, 0);
}
// INT -> 2 WORD (LIST)
public static List<ushort> IntTOWordList(List<int> UintVal)
{
ushort low, high;
//initialize the new List
List<ushort> Word = new List<ushort>();
//Fill the List
foreach (int val in UintVal)
{
IntTOWord(val, out low, out high);
Word.Add(low);
Word.Add(high);
}
return Word;
}
// 2 WORD -> INT (LIST)
public static List<int> WordTOIntList(List<ushort> UintVal)
{
int value;
//initialize the new List
List<int> Int = new List<int>();
//Fill the List
for (int i = 0; i < UintVal.Count; i = i + 2)
{
WordTOInt(out value, UintVal[i], UintVal[i + 1]);
Int.Add(value);
}
return Int;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
// NC <-> .NET Conversions
// UINT -> 2 WORD
public static void UIntTOWord(uint UintVal, out ushort LowWord, out ushort HighWord)
{
byte[] bytes = BitConverter.GetBytes(UintVal);
//Split into 2 word
LowWord = BitConverter.ToUInt16(bytes, 0);
HighWord = BitConverter.ToUInt16(bytes, 2);
}
// 2 WORD -> UINT
public static void WordTOUint(out uint UintVal, ushort LowWord, ushort HighWord)
{
byte[] bytes = new byte[4];
//fill the bytes from 2 word
bytes[0] = BitConverter.GetBytes(LowWord)[0];
bytes[1] = BitConverter.GetBytes(LowWord)[1];
bytes[2] = BitConverter.GetBytes(HighWord)[0];
bytes[3] = BitConverter.GetBytes(HighWord)[1];
//Create the value
UintVal = BitConverter.ToUInt32(bytes, 0);
}
// UINT -> 2 WORD (LIST)
public static List<ushort> UIntTOWordList(List<uint> UintVal)
{
ushort low, high;
//initialize the new List
List<ushort> Word = new List<ushort>();
//Fill the List
foreach (uint val in UintVal)
{
UIntTOWord(val, out low, out high);
Word.Add(low);
Word.Add(high);
}
return Word;
}
// 2 WORD -> UINT (LIST)
public static List<uint> WordTOUintList(List<ushort> UintVal)
{
uint value;
//initialize the new List
List<uint> Uint = new List<uint>();
//Fill the List
for (int i = 0; i < (UintVal.Count / 2); i = i + 2)
{
WordTOUint(out value, UintVal[i], UintVal[i + 1]);
Uint.Add(value);
}
return Uint;
}
public static void WriteLocalFile(string fileContent, ref FileStream fileReference)
{
// Write Part Program
using (StreamWriter writer = new StreamWriter(fileReference))
writer.Write(fileContent);
}
public static string ReadLocalFile(FileStream fileReference)
{
// Get File Content
StreamReader reader = new StreamReader(fileReference);
string fileContent = reader.ReadToEnd();
return fileContent;
}
public static string GetUntilOrEmpty(string text, string stopAt = "[")
{
// Get string unti
if (!String.IsNullOrWhiteSpace(text))
{
// Find character index
int charLocation = text.IndexOf(stopAt, StringComparison.Ordinal);
if (charLocation > 0)
return text.Substring(0, charLocation);
}
return String.Empty;
}
public static string FindImageBase64String(string directoryPath, string imageName)
{
foreach (string ext in VALID_IMAGE_FORMATS)
{
if (File.Exists(directoryPath + "/" + imageName + ext))
{
// Convert image to a base 64 string
return "data:image/" + ext + ";base64," + Convert.ToBase64String(File.ReadAllBytes(directoryPath + "/" + imageName + ext));
}
}
return "";
}
public static IEnumerable<TSource> DistinctBy<TSource, TKey> (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
HashSet<TKey> seenKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (seenKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
public static bool[] IntToBool(int val)
{
return IntToBool(new int[] { val });
}
public static bool[] IntToBool(int[] val)
{
return new BitArray(val.ToArray())
.Cast<bool>()
.ToArray();
}
}
}