|
using System;
using System.Collections;
using System.Configuration;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using log4net;
namespace uk.org.aspellclark.common
{
/// <summary>
/// <name>CommonFunctions</name>
/// <namespace>uk.org.aspellclark.common</namespace>
/// <version>1.0</version>
/// <author>Andy Aspell-Clark</author>
/// <description>
/// </description>
/// <remarks> a simple class tha contains simple functions
/// useable by any applications
/// </remarks>
/// <history>
/// <historyitem> 1 Aug 2004 1.0 ARA Initial Version.</historyitem>
/// </history>
/// </summary>
public sealed class CommonFunctions
{
// Create a logger for use in this class
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static Regex _isNumber = new Regex(@"^\d+$");
public const string M_LOCALHOST = "127.0.0.1";
public static ArrayList getResourceList()
{
ArrayList results = new ArrayList();
// get a reference to the current assembly
Assembly a = Assembly.GetExecutingAssembly();
// get a list of resource names from the manifest
string[] resNames = a.GetManifestResourceNames();
foreach (string s in resNames)
{
log.Info(String.Format("Resource [{0}]",s));
results.Add(s);
}
return results;
}
public static ArrayList getSystemFontList()
{
ArrayList results = new ArrayList();
FontFamily[] fontFamilies;
System.Drawing.Text.InstalledFontCollection installedFontCollection = new System.Drawing.Text.InstalledFontCollection();
fontFamilies = installedFontCollection.Families;
for (int j = 0; j < fontFamilies.Length; ++j)
{
log.Info(String.Format("Adding system font [{0}]", fontFamilies[j].Name));
results.Add(fontFamilies[j].Name);
}
log.Info("CommonFunctions.GetSystemFontList: got font list");
return results;
}
public static FontStyle Fontstyle(string fntstyle)
{
if (fntstyle.ToLower().Equals("bold"))
{
return FontStyle.Bold;
}
else if (fntstyle.ToLower().Equals("italic"))
{
return FontStyle.Italic;
}
else if (fntstyle.ToLower().Equals("strikeout"))
{
return FontStyle.Strikeout;
}
else if (fntstyle.ToLower().Equals("underline"))
{
return FontStyle.Underline;
}
return FontStyle.Regular;
}
public static Font GetFont(string fntName, string fntSize, string fntStyle)
{
//if ( GetMyFontList.Contains(fntName) )
//{
// return GetFontResource(fntName, fntSize, fntStyle);
//}
return new Font(fntName, int.Parse(fntSize), Fontstyle(fntStyle));
}
public static Font GetFontResource(string fntName, string fntSize, string fntStyle)
{
System.Resources.ResourceManager rm =
new System.Resources.ResourceManager(
"uk.co.aspellclark.common.MyFonts",
System.Reflection.Assembly.GetExecutingAssembly());
rm.IgnoreCase = true;
//Now you can easily retrieve your specific objects and strings
//by using the GetObject and GetString methods.
Font fnt = (Font)rm.GetObject(fntName);
return fnt;
}
public static void DumpAssemblyAttributes(string assemblyName)
{
log.Info("DumpAssemblyAttributes '"+assemblyName+"'");
Assembly assembly = Assembly.LoadFrom(assemblyName);
object[] attibutes = assembly.GetCustomAttributes(true);
if (attibutes.Length > 0)
{
log.Info(String.Format("Assembly attributes for [{0}]", assembly));
foreach (object o in attibutes)
{
log.Info("Attribute: {"+o.ToString()+"}");
}
}
}
public static bool IsInteger(string theValue)
{
Match m = _isNumber.Match(theValue);
return m.Success;
} //IsInteger()
public static string NoSpace(string strIn)
{
return strIn.Replace(" ", "");
}
public static string MakeSafe(string strIn)
{
string newStr;
if (strIn == null)
{
return "";
}
else
{
newStr = strIn.Replace("'", "''");
return newStr.Replace("\x22", "''");
}
}
public static void wasNumberPressed(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 48 && e.KeyChar <= 57)
{
e.Handled = false;
}
else if ((e.KeyChar == '\b') || (e.KeyChar == '\r') ||
(e.KeyChar == '.'))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}//wasNumberPressed()
public static string GregorianToJulian(DateTime dt)
{
double A = Math.Floor((double)(dt.Year / 100));
double B = Math.Floor(A / 4);
double C = 2 - A + B;
double D = dt.Day;
double E = Math.Floor(365.25 * (dt.Year + 4716));
double F = Math.Floor(30.6001 * (dt.Month + 1));
double JD = C + D + E + F - 1524.5;
string myJulianDate = JD.ToString(); // 2299160.5
return myJulianDate;
}
public static DateTime JulianToGregorian(double JD)
{
double Z = Math.Floor(JD + 0.5);
double W = Math.Floor((Z - 1867216.25) / 36524.25);
double X = Math.Floor(W / 4);
double AA = Math.Floor(Z + 1 + W - X);
double BB = Math.Floor(AA + 1524);
double CC = Math.Floor((BB - 122.1) / 365.25);
double DD = Math.Floor(365.25 * CC);
double EE = Math.Floor((BB - DD) / 30.6001);
double FF = Math.Floor(30.6001 * EE);
double Day = BB - DD - FF;
double Month;
double Year;
if ((EE - 13) <= 12 && (EE - 13) > 0)
{
Month = EE - 13;
}
else
{
Month = EE - 1;
}
if (Month == 1 || Month == 2)
{
Year = CC - 4715;
}
else
{
Year = CC - 4716;
}
string GregorianDate = string.Format("{0}/{1}/{2}", Year, Month, Day); // 1582/10/15
return new DateTime(Convert.ToInt16(Year), Convert.ToInt16(Month), Convert.ToInt16(Day));
}
public static System.DateTime OleDateToDateTime(double date)
{
return DateTime.FromOADate(date);
}
public static double DateTimeToOleDate(System.DateTime date)
{
return date.ToOADate();
}
/// <summary>
/// grabbed from http://www.egilh.com/blog/archive/2004/09/24/207.aspx
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static System.DateTime OleDateToDateTimeOld(double date)
{
const Int32 hex1 = 86400000; //0x5265c00
const Int64 hex2 = 59926435200000; //0x3680b5e1fc00
const Int64 hex3 = 315537897600000; //0x11efae44cb400
const Int32 hex4 = 10000; //0x2710
const double real1 = 86400000;
const double real2 = 2958466;
const double real3 = -657435;
if (date >= real2 || date <= real3)
{
throw new ArgumentException("Arg_OleAutDateInvalid");
}
double toAdd = date >= 0 ? 0.5 : -0.5;
long loc0 = (long)(date * real1 + toAdd);
if (loc0 < 0)
{
loc0 -= (2 * getReminder(loc0, hex1));
}
loc0 += hex2;
if (loc0 < 0 || loc0 >= hex3)
{
throw new ArgumentException("Arg_OleAutDateScale");
}
return new DateTime(loc0 * hex4);
}
/// <summary>
/// grabbed from http://www.egilh.com/blog/archive/2004/09/24/207.aspx
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static double DateTimeToOleDateOld(System.DateTime date)
{
const Int32 hex1 = 86400000; // '0x5265c00
const Int64 hex2 = 599264352000000000; // '0x85103c0cb83c000
const Int64 hex3 = 31241376000000000; // '0x6efdddaec64000
const Int32 hex4 = 10000; // '0x2710
const Int64 hex5 = 864000000000; // '0xc92a69c000
const double real1 = 86400000;
long ticks = date.Ticks;
if (0 == ticks)
{
return 0.0;
}
if (ticks < hex5)
{
ticks += hex2;
}
if (ticks < hex3)
{
throw new ArgumentException("Arg_OleAutDateInvalid");
}
long loc1;
long loc0 = (long)((ticks - hex2) / hex4);
if (loc0 < 0)
{
loc1 = getReminder(loc0, hex1);
if (0 != loc1)
{
loc0 -= (hex1 + loc1) * (2);
}
}
return ((double)loc0) / real1;
}
/// <summary>
/// grabbed from http://www.egilh.com/blog/archive/2004/09/24/207.aspx
/// </summary>
/// <param name="value1"></param>
/// <param name="value2"></param>
/// <returns></returns>
private static long getReminder(long value1, long value2)
{
long res;
res = value1 - value2 * (value1 / value2);
return res;
}
}//class()
}//namespace()
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.