Archive for Agosto 1st, 2008

PTO_PWM Signature

CodeKeep C# Feed Agosto 1st, 2008

Description: Serach for all the PTO_PWM signatures in all the preCompile contexts.

Link: http://www.codekeep.net/snippets/a97fb380-fdd4-4298-ae51-869eda95fc09.aspx

IPreCompileContext[] allPreCompileContexts = SystemInstances.LanguageModelMgr.AllPreCompileContexts(true, true);
if (allPreCompileContexts != null)
{
	foreach (IPreCompileContext preCompileContext in allPreCompileContexts)
        {
		ISignature[] signatures;
                if((signatures = preCompileContext.FindSignature("PTO_PWM")) != null)
                {
                	MsgDebug.DisplayInfo(string.Format(
                                "{0} PTO_PMW signatures found in {1} preCompiledContext",
                                signatures.Length,
                                preCompileContext.Namespace));
                        foreach (ISignature sign in signatures)
                        {
                         	foreach (IVariable var in sign.All)
                                {
                                    if (var.Type.ToString() == "PTOSimple")
                                        MsgDebug.DisplayInfo(string.Format(
                                            "PTOSimple type found in {0} var in {1} signature",
                                            var.Name,
                                            sign.Name));
                                }
                            }
                        }
		}
	}
}

Recursive multi-word string capitalizer

CodeKeep C# Feed Agosto 1st, 2008

Description: The method capitalize the first char of each word in a string, or the string's first char only (param TutteLeParole). It manages the characters and too.

Link: http://www.codekeep.net/snippets/f3b3db00-30ef-4f0d-aa9a-c66099fa4fb2.aspx

/// <summary>
/// Il metodo mette in maiuscolo l'iniziale di alcune o tutte le parole del testo in input.
/// </summary>
/// <param name="Testo">Testo da modificare.</param>
/// <param name="TutteLeParole">Booleano che dice se la modifica è da applicare su tutte o su alcune parole.</param>
/// <returns>Stringa contenente il testo modificato.</returns>
public static String CapitalizeFirstChar(String Testo, Boolean TutteLeParole)
{           
	Testo = Testo.Trim().ToLower();
            
            if (TutteLeParole)
            {
                if (Testo.Length > 1)
                {

                    if (Testo.Contains(" "))
                    {
                        Int32 ind = Testo.IndexOf(' ');

                        return CapitalizeFirstChar(Testo.Substring(0, ind), TutteLeParole) + " " + CapitalizeFirstChar(Testo.Substring(ind + 1), TutteLeParole);
                    }
                    else if (Testo.Contains("."))
                    {
                        Int32 ind = Testo.IndexOf('.');

                        //se il carattere separatore è in fondo alla stringa evitiamo
                        //la chiamata al metodo con la stringa vuota
                        if (ind != Testo.Length - 1)
                        {
                            return CapitalizeFirstChar(Testo.Substring(0, ind), TutteLeParole) + "." + CapitalizeFirstChar(Testo.Substring(ind + 1), TutteLeParole);
                        }
                        else
                            return CapitalizeFirstChar(Testo.Substring(0, ind), TutteLeParole);
                    }
                    else if (Testo.Contains("'"))
                    {
                        Int32 ind = Testo.IndexOf("'");

                        if (ind != Testo.Length - 1)
                        {
                            return CapitalizeFirstChar(Testo.Substring(0, ind), TutteLeParole) + "'" + CapitalizeFirstChar(Testo.Substring(ind + 1), TutteLeParole);
                        }
                        else return CapitalizeFirstChar(Testo.Substring(0, ind), TutteLeParole);
                    }
                    //caso base: capitalizziamo la stringa
                    else return Testo[0].ToString().ToUpper() + Testo.Substring(1, Testo.Length - 1);
                }
                else return Testo.ToString().ToUpper();
            }
            else
            {
                return Testo[0].ToString().ToUpper() + Testo.Substring(1, Testo.Length - 1);
            }
        }

Get the index of the string within another string without match case

CodeKeep C# Feed Agosto 1st, 2008

Description: using CompareInfo.IndexOf() in System.Globalization name space. The old way is first make it all ToTower(), then index.

Link: http://www.codekeep.net/snippets/b42e983a-87c8-409a-98ee-cf0d4c76bc42.aspx

using System.Globalization;

string strParent = "The Codeproject site is very informative.";

string strChild = "codeproject";
// We create a object of CompareInfo class for a neutral culture or a culture insensitive object
CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo;

int i = Compare.IndexOf(strParent,strChild,CompareOptions.IgnoreCase);

Get Disk info, e.g. free space

CodeKeep C# Feed Agosto 1st, 2008

Description: using GetDiskFreeSpaceEx in kernel32.dll

Link: http://www.codekeep.net/snippets/818d0a01-b060-4499-8845-60512d699d35.aspx

public sealed class DriveInfo
{
    [DllImport("kernel32.dll", EntryPoint = "GetDiskFreeSpaceExA")]
    private static extern long GetDiskFreeSpaceEx(string lpDirectoryName,
        out long lpFreeBytesAvailableToCaller,
        out long lpTotalNumberOfBytes,
        out long lpTotalNumberOfFreeBytes);

    public static long GetInfo(string drive, out long available, out long total, out long free)
    {
        return GetDiskFreeSpaceEx(drive, out available, out total, out free);
    }

    public static DriveInfoSystem GetInfo(string drive)
    {
        long result, available, total, free;
        result = GetDiskFreeSpaceEx(drive, out available, out total, out free);
        return new DriveInfoSystem(drive, result, available, total, free);
    }
}

public struct DriveInfoSystem
{
    public readonly string Drive;
    public readonly long Result;
    public readonly long Available;
    public readonly long Total;
    public readonly long Free;

    public DriveInfoSystem(string drive, long result, long available, long total, long free)
    {
        this.Drive = drive;
        this.Result = result;
        this.Available = available;
        this.Total = total;
        this.Free = free;
    }
}

Read file with each line

CodeKeep C# Feed Agosto 1st, 2008

Description: Read file with StreamReader

Link: http://www.codekeep.net/snippets/17f697c8-e856-4bd6-b2b7-894562a94333.aspx

            using (System.IO.StreamReader sr = new System.IO.StreamReader(strFileName))
            {
                String line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }

Write XML

CodeKeep C# Feed Agosto 1st, 2008

Description: Write xml with XmlWriter

Link: http://www.codekeep.net/snippets/683f2faa-b51a-4637-a3b2-249a78e9d7dc.aspx

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = ("    ");

            string strXmlFileName = strFileName + ".xml";

            using (XmlWriter writer = XmlWriter.Create(strXmlFileName, settings))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("Trace");

                writer.WriteEndElement();
                writer.WriteEndDocument();

                writer.Flush();
            }