Files
Vocaluxe/VocaluxeLib/Menu/CHelper.cs
T
2013-04-15 19:10:38 +02:00

233 lines
6.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Text;
namespace VocaluxeLib.Menu
{
public static class CHelper
{
public static int CombinationCount(int n, int k)
{
if (k > n)
return 0;
if (k == 0 || k == n)
return 1;
if (k < 0 || n <= 0)
return 0; //is not defined
if (k * 2 > n)
k = n - k;
long result = n;
for (long i = 1; i <= k - 1; i++)
result = result * (n - i) / (i + 1);
return (int)result;
}
/// <summary>
/// Concat strings into one string with ", " as separator.
/// </summary>
public static string ListStrings(string[] str)
{
string result = string.Empty;
for (int i = 0; i < str.Length; i++)
{
result += str[i];
if (i < str.Length - 1)
result += ", ";
}
return result;
}
public static int TryReadInt(StreamReader sr)
{
char chr = '0';
string value = String.Empty;
try
{
int tmp = sr.Peek();
//Check for ' ', ?, ?, \n, \r
while (tmp != 32 && tmp != 19 && tmp != 16 && tmp != 13 && tmp != 10)
{
chr = (char)sr.Read();
value += chr.ToString();
tmp = sr.Peek();
}
}
catch (Exception)
{
return 0;
}
int result = 0;
if (int.TryParse(value, out result))
return result;
return 0;
}
public static void SetRect(RectangleF bounds, ref RectangleF rect, float rectAspect, EAspect aspect)
{
float rW = bounds.Right - bounds.Left;
float rH = bounds.Bottom - bounds.Top;
float rA = rW / rH;
float scaledWidth = rW;
float scaledHeight = rH;
switch (aspect)
{
case EAspect.Crop:
if (rA >= rectAspect)
{
scaledWidth = rW;
scaledHeight = rH * rA / rectAspect;
}
else
{
scaledHeight = rH;
scaledWidth = rW * rectAspect / rA;
}
break;
case EAspect.LetterBox:
if (rectAspect >= 1)
{
scaledWidth = rW;
scaledHeight = rH * rA / rectAspect;
}
else
{
scaledHeight = rH;
scaledWidth = rW * rectAspect / rA;
}
break;
default:
scaledWidth = rW;
scaledHeight = rH;
break;
}
float left = (rW - scaledWidth) / 2 + bounds.Left;
float rigth = left + scaledWidth;
float upper = (rH - scaledHeight) / 2 + bounds.Top;
float lower = upper + scaledHeight;
rect = new RectangleF(left, upper, rigth - left, lower - upper);
}
public static List<string> ListFiles(string path, string cast)
{
return ListFiles(path, cast, false, false);
}
public static List<string> ListFiles(string path, string cast, bool recursive)
{
return ListFiles(path, cast, recursive, false);
}
public static List<string> ListFiles(string path, string cast, bool recursive, bool fullpath)
{
List<string> files = new List<string>();
DirectoryInfo dir = new DirectoryInfo(path);
try
{
foreach (FileInfo file in dir.GetFiles(cast))
{
if (!fullpath)
files.Add(file.Name);
else
files.Add(file.FullName);
}
if (recursive)
{
foreach (DirectoryInfo di in dir.GetDirectories())
files.AddRange(ListFiles(di.FullName, cast, recursive, fullpath));
}
}
catch (Exception) {}
return files;
}
public static bool TryParse<T>(string value, out T result)
where T : struct
{
return TryParse(value, out result, false);
}
public static bool TryParse<T>(string value, out T result, bool ignoreCase)
where T : struct
{
result = default(T);
try
{
result = (T)Enum.Parse(typeof(T), value, ignoreCase);
return true;
}
catch {}
return false;
}
public static bool TryParse(string value, out float result)
{
value = value.Replace(',', '.');
return float.TryParse(value, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, NumberFormatInfo.InvariantInfo, out result);
}
public static bool IsInBounds(SRectF bounds, SMouseEvent mouseEvent)
{
return IsInBounds(bounds, mouseEvent.X, mouseEvent.Y);
}
public static bool IsInBounds(SRectF bounds, int x, int y)
{
return (bounds.X <= x) && (bounds.X + bounds.W >= x) && (bounds.Y <= y) && (bounds.Y + bounds.H >= y);
}
}
static class CEncoding
{
public static Encoding GetEncoding(string encodingName)
{
switch (encodingName)
{
case "AUTO":
return Encoding.Default;
case "CP1250":
return Encoding.GetEncoding(1250);
case "CP1252":
return Encoding.GetEncoding(1252);
case "LOCALE":
return Encoding.Default;
case "UTF8":
return Encoding.UTF8;
default:
return Encoding.UTF8;
}
}
public static string GetEncodingName(Encoding enc)
{
string result = "UTF8";
if (enc.CodePage == 1250)
result = "CP1250";
if (enc.CodePage == 1252)
result = "CP1252";
return result;
}
}
}