<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Vince Files &#187; Programming</title>
	<atom:link href="http://www.thevincefiles.net/category/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://www.thevincefiles.net</link>
	<description>My rants, ramblings, and reflections</description>
	<lastBuildDate>Sun, 28 Jun 2009 22:37:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Finding files: Recursion vs Stack&#8230;FIGHT!</title>
		<link>http://www.thevincefiles.net/2009/06/07/finding-files-recursion-vs-stackfight</link>
		<comments>http://www.thevincefiles.net/2009/06/07/finding-files-recursion-vs-stackfight#comments</comments>
		<pubDate>Sun, 07 Jun 2009 07:22:57 +0000</pubDate>
		<dc:creator>Vince</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.thevincefiles.net/?p=196</guid>
		<description><![CDATA[I was just poking around the Internet and came across a blog post highlighting a solution to a common problem: find all files matching a criteria (e.g. *.dll) from a folder and all its subfolders (e.g. c:\windows and all subdirectories).  In this post, I compare the speed of two methods using code in C#.
Method [...]<p>a</p>
]]></description>
			<content:encoded><![CDATA[<p>I was just poking around the Internet and came across a blog post highlighting a solution to a common problem: find all files matching a criteria (e.g. *.dll) from a folder and all its subfolders (e.g. c:\windows and all subdirectories).  In this post, I compare the speed of two methods using code in C#.<span id="more-196"></span></p>
<p>Method 1 involves recursion. I saw a sample at <a href="http://www.gfilter.net/default.aspx?Post=Code-Snippet-of-the-Day%3a-Recursive-File-Finder" target="_blank">http://www.gfilter.net/default.aspx?Post=Code-Snippet-of-the-Day%3a-Recursive-File-Finder</a> and <a href="http://support.microsoft.com/kb/303974" target="_blank">MSDN</a>.</p>
<p>Method 2 involves using a stack to process subdirectories in a Last in, first out manner.  I saw this from <a href="http://dotnetperls.com/Content/Recursively-Find-Files.aspx" target="_blank">http://dotnetperls.com/Content/Recursively-Find-Files.aspx</a>.</p>
<p>The result from finding all 13,300+ DLLs in c:\windows and all subdirectories:</p>
<p>FileFinderRecursive Elapsed: 6876ms.<br />
FileFinderStack Elapsed: 5725ms.</p>
<p>The stack method was almost 20% faster.</p>
<p>Here&#8217;s some code from a quick C# console app I just wrote to compare the two:</p>
<pre name="code" class="c-sharp">
class Program
{
    static void Main(string[] args)
    {
        Stopwatch s;
        IFinder finder;
        string startDir = @"c:\windows";
        string matchType = "*.dll";

        //Test Recursion method
        s = Stopwatch.StartNew();
        finder = new FileFinderRecursive();
        var filesRecursive = finder.Find(startDir, matchType);
        s.Stop();
        Console.WriteLine("FileFinderRecursive Elapsed: " + s.ElapsedMilliseconds + "ms.");

        //Test Stack method
        s = Stopwatch.StartNew();
        finder = new FileFinderStack();
        var filesStack = finder.Find(startDir, matchType);
        s.Stop();
        Console.WriteLine("FileFinderStack Elapsed: " + s.ElapsedMilliseconds + "ms.");    

        Console.Read();
    }
}
public interface IFinder
{
    List&lt;FileInfo&gt; Find(string startDir, string matchType);
}

/// &lt;summary&gt;
/// Recursively finds a list of files that match the specified critiria
/// &lt;/summary&gt;
public class FileFinderRecursive : IFinder
{
    private Dictionary&lt;FileInfo, int&gt; files;

    public List&lt;FileInfo&gt; Find(string startDir, string matchType)
    {
        files = new Dictionary&lt;FileInfo, int&gt;();
        findFiles(startDir, matchType);

        return files.Keys.ToList();
    }

    private void findFiles(string startDir, string matchType)
    {
        DirectoryInfo d = new DirectoryInfo(startDir);
        FileInfo[] fileInfos;
        try
        {
            fileInfos = d.GetFiles(matchType);
        }
        catch
        {
            return; //most likely Access Exception.  BAIL!
        }
        foreach (FileInfo f in fileInfos)
        {
            if (!files.ContainsKey(f))
            {
                files.Add(f, 0);
            }
        }

        if (d.GetDirectories().Length &gt; 0)
        {
            foreach (DirectoryInfo subD in d.GetDirectories())
            {
                findFiles(subD.FullName, matchType);
            }
        }
    }
}

/// &lt;summary&gt;
/// Finds a list of files that match the specified criteria using a stack
/// &lt;/summary&gt;
public class FileFinderStack : IFinder
{
    public List&lt;FileInfo&gt; Find(string startDir, string matchType)
    {
        DirectoryInfo dirStart = new DirectoryInfo(startDir);
        List&lt;FileInfo&gt; result = new List&lt;FileInfo&gt;();
        Stack&lt;DirectoryInfo&gt; stack = new Stack&lt;DirectoryInfo&gt;();
        stack.Push(dirStart);
        while (stack.Count &gt; 0)
        {
            DirectoryInfo dir = stack.Pop();
            try
            {
                result.AddRange(dir.GetFiles(matchType));
                DirectoryInfo[] subDirs = dir.GetDirectories();
                foreach (DirectoryInfo dn in subDirs)
                {
                    stack.Push(dn);
                }
            }
            catch { }
        }
        return result;
    }
}
</pre>
<p>a</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thevincefiles.net/2009/06/07/finding-files-recursion-vs-stackfight/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The fastest SIN validator</title>
		<link>http://www.thevincefiles.net/2008/12/05/the-fastest-sin-validator</link>
		<comments>http://www.thevincefiles.net/2008/12/05/the-fastest-sin-validator#comments</comments>
		<pubDate>Sat, 06 Dec 2008 05:22:00 +0000</pubDate>
		<dc:creator>Vince</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.thevincefiles.net/?p=85</guid>
		<description><![CDATA[The fastest implementation I&#8217;ve seen of a validator for a Canadian Social Insurance number is from hackcanada.
I&#8217;ve rewritten his JavaScript code here in C# with a few guard clauses at the top.  Enjoy!
================================

private static bool ValidateSIN(string sin)
{
    int dummy;
    if (!Int32.TryParse(sin, out dummy)) return false;
    [...]<p>a</p>
]]></description>
			<content:encoded><![CDATA[<p>The fastest implementation I&#8217;ve seen of a validator for a Canadian Social Insurance number is from <a href="http://www.hackcanada.com/canadian/other/sin.html" target="_blank">hackcanada</a>.</p>
<p>I&#8217;ve rewritten his JavaScript code here in C# with a few guard clauses at the top.  Enjoy!<br />
================================</p>
<pre name="code" class="c-sharp">
private static bool ValidateSIN(string sin)
{
    int dummy;
    if (!Int32.TryParse(sin, out dummy)) return false;
    if (sin.Length != 9) return false;

    int s = 0;
    for (int i = 0; i &lt; 9; i++)
    {
        int x = Convert.ToInt32(sin.Substring(i, 1));
        if (i % 2 != 0)
        {
            if ((x &lt;&lt; 1) &gt; 9)
            {
                s += ((x &lt;&lt; 1) - 9);
            }
            else
            {
                s += (x &lt;&lt; 1);
            }
        }
        else
        {
            s += x;
        }
    }
    return (s % 10 == 0);
}</pre>
<p>EDIT: Neil correctly pointed out I was missing a null check as my first statement; I was checking (sin.Length != 9) first.  I&#8217;ve changed it and decided to do the Int32.TryParse() first before checking for length.  According to <a href="http://www.red-gate.com/products/reflector/" target="_blank">Reflector</a>, <a href="http://msdn.microsoft.com/en-us/library/f02979c7.aspx" target="_blank">System.Int32.TryParse(string s, out int result)</a> calls the internal method System.Number.TryParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) which in turn calls <a href="http://www.koders.com/csharp/fid3C52C0D806349C6FB2E0802CB444C53B35853491.aspx?s=datatable" target="_blank">System.Number.TryStringToNumber(string str, NumberStyles options, ref NumberBuffer number, NumberFormatInfo numfmt, bool parseDecimal)</a> which does a null check. <img src='http://www.thevincefiles.net/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>I like Rick Brewster&#8217;s <a href="http://blog.getpaint.net/2008/12/06/a-fluent-approach-to-c-parameter-validation/" target="_blank">fluent approach to parameter validation</a> using .NET 3.0 and higher.  It&#8217;s pretty slick.</p>
<p>a</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thevincefiles.net/2008/12/05/the-fastest-sin-validator/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Just bought a DLink DNS-323</title>
		<link>http://www.thevincefiles.net/2008/07/30/just-bought-a-dlink-dns-323</link>
		<comments>http://www.thevincefiles.net/2008/07/30/just-bought-a-dlink-dns-323#comments</comments>
		<pubDate>Thu, 31 Jul 2008 07:26:23 +0000</pubDate>
		<dc:creator>Vince</dc:creator>
				<category><![CDATA[Life]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[DLink]]></category>
		<category><![CDATA[DNS-323]]></category>
		<category><![CDATA[linksys wrt54g]]></category>
		<category><![CDATA[network attached storage]]></category>
		<category><![CDATA[seagate barracuda]]></category>
		<category><![CDATA[vmware server]]></category>

		<guid isPermaLink="false">http://www.thevincefiles.net/?p=70</guid>
		<description><![CDATA[So NCIX was having another one of their sales again.  This time the deals were pretty good.
The DLink DNS-323 is a network-attached storage enclosure that holds 2 SATA drives.  I decided to buy 2 1-terabyte Seagate Barracuda 7200.11 drives which were also on sale.

For the past little while, we&#8217;ve been mostly using stand-alone drives with [...]<p>a</p>
]]></description>
			<content:encoded><![CDATA[<p>So <a href="http://www.ncix.com" target="_blank">NCIX</a> was having another one of their sales again.  This time the deals were pretty good.<span id="more-70"></span></p>
<p>The DLink DNS-323 is a network-attached storage enclosure that holds 2 SATA drives.  I decided to buy 2 1-terabyte Seagate Barracuda 7200.11 drives which were also on sale.</p>
<div class="g2image_normal"><a href="http://www.thevincefiles.net/index.php/wpg2?g2_itemId=750&amp;g2_GALLERYSID=TMP_SESSION_ID_DI_NOISSES_PMT" target="_blank"><a href="http://www.thevincefiles.net/wpg2?g2_itemId=750&amp;g2_GALLERYSID=TMP_SESSION_ID_DI_NOISSES_PMT"><img class="g2image_normal" title="DLink_DNS-323_Volume_1" src="http://gallery.thevincefiles.net/main.php?g2_view=core.DownloadItem&amp;g2_itemId=766&amp;g2_GALLERYSID=TMP_SESSION_ID_DI_NOISSES_PMT" alt="DLink_DNS-323_Volume_1" width="150" height="140" /></a><a href="http://www.thevincefiles.net/wpg2?g2_itemId=750&amp;g2_GALLERYSID=TMP_SESSION_ID_DI_NOISSES_PMT"><img class="g2image_normal" title="DLink DNS-323 with Seagate 7200.11 1TB drive" src="http://gallery.thevincefiles.net/main.php?g2_view=core.DownloadItem&amp;g2_itemId=752&amp;g2_GALLERYSID=TMP_SESSION_ID_DI_NOISSES_PMT" alt="IMG_0073" width="150" height="113" /></a><a href="http://www.thevincefiles.net/wpg2?g2_itemId=750&amp;g2_GALLERYSID=TMP_SESSION_ID_DI_NOISSES_PMT"><img class="g2image_normal" title="DLink DNS-323 with Seagate 7200.11 1TB drive" src="http://gallery.thevincefiles.net/main.php?g2_view=core.DownloadItem&amp;g2_itemId=757&amp;g2_GALLERYSID=TMP_SESSION_ID_DI_NOISSES_PMT" alt="IMG_0074" width="150" height="113" /></a><a href="http://www.thevincefiles.net/wpg2?g2_itemId=750&amp;g2_GALLERYSID=TMP_SESSION_ID_DI_NOISSES_PMT"><img class="g2image_normal" title="DLink DNS-323 with Seagate 7200.11 1TB drive" src="http://gallery.thevincefiles.net/main.php?g2_view=core.DownloadItem&amp;g2_itemId=760&amp;g2_GALLERYSID=TMP_SESSION_ID_DI_NOISSES_PMT" alt="IMG_0075" width="150" height="113" /></a><a href="http://www.thevincefiles.net/wpg2?g2_itemId=750&amp;g2_GALLERYSID=TMP_SESSION_ID_DI_NOISSES_PMT"><img class="g2image_normal" title="DLink DNS-323 with Seagate 7200.11 1TB drive" src="http://gallery.thevincefiles.net/main.php?g2_view=core.DownloadItem&amp;g2_itemId=763&amp;g2_GALLERYSID=TMP_SESSION_ID_DI_NOISSES_PMT" alt="IMG_0076" width="150" height="113" /></a></a></div>
<p>For the past little while, we&#8217;ve been mostly using stand-alone drives with USB enclosures.  Some of them we&#8217;ve bought as a retail package.  Others, we&#8217;ve bought the drive first and then slapped an enclosure around it.  But all are USB-attached and provide no fault tolerance.</p>
<p>Herein lies the appeal of the DNS-323.  This unit can take 2 SATA drives and can use them in the following configurations:</p>
<ul>
<li>Normal &#8211; The 2 disks are 2 individual drives</li>
<li><a href="http://en.wikipedia.org/wiki/Standard_RAID_levels" target="_blank">JBOD &#8211; aka &#8220;Just a Bunch of disks&#8221;</a></li>
<li><a href="http://en.wikipedia.org/wiki/Redundant_array_of_independent_disks" target="_blank">RAID 0 &#8211; Striped</a></li>
<li><a href="http://en.wikipedia.org/wiki/Redundant_array_of_independent_disks" target="_blank">RAID 1 &#8211; Mirrored</a></li>
</ul>
<p>For our purposes, we will be storing some valuable data (pics, videos, etc.) on this unit so reliability, redundancy and fault-tolerance is top priority.  I opted, therefore, to configure this in RAID 1.</p>
<p>Installation was a breeze &#8211; install hard drives, connect to router and power up.  I did have a problem with my <a href="http://www.thevincefiles.net/index.php/2008/07/25/switching-to-vmware-server">VMWare Server</a>&#8217;s built-in DHCP server assigning the DNS-323 an IP address.  The DNS-323 was supposed to get its IP address from my Linksys WRT54G router instead.  Luckily, the DNS-323&#8217;s included software has a tool to find your unit on the network.  Once I found the unit on the network, I accessed its web-based configuration interface and assigned a static IP address.</p>
<p>The DNS-323 has a built-in FTP server, iTunes server, BitTorrent client and also acts as a UPnP AV media server.  The DNS-323 runs Linux and is accessible via Telnet.  For more info on the DNS-323, you can <a href="http://wiki.dns323.info/" target="_blank">visit the Wiki</a>.</p>
<p>The bottleneck with our setup will most likely be our network until we can upgrade our router to Gigabit Ethernet.  My aging WRT54G is only a 100 Mbps router.  So transferring files from my computer to the DNS-323 will run at 12.5 megabytes per second at the fastest.  It will most likely be around 10 MB/s.</p>
<p>a</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thevincefiles.net/2008/07/30/just-bought-a-dlink-dns-323/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Design Patterns Quick Reference</title>
		<link>http://www.thevincefiles.net/2008/01/30/design-patterns-quick-reference</link>
		<comments>http://www.thevincefiles.net/2008/01/30/design-patterns-quick-reference#comments</comments>
		<pubDate>Thu, 31 Jan 2008 04:15:12 +0000</pubDate>
		<dc:creator>Vince</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://www.thevincefiles.net/index.php/2008/01/30/design-patterns-quick-reference</guid>
		<description><![CDATA[Jason McDonald has put together a cheatsheet of some Gang of Four design patterns.
I used to visit DoFactory&#8217;s patterns pages for all the patterns but now I have a sheet with all of them on a page.  Cool!
a
<p>a</p>
]]></description>
			<content:encoded><![CDATA[<p>Jason McDonald has put together <a href="http://www.mcdonaldland.info/2007/11/28/40/" target="_blank">a cheatsheet</a> of some Gang of Four design patterns.</p>
<p>I used to visit <a href="http://dofactory.com/Patterns/Patterns.aspx" target="_blank">DoFactory&#8217;s patterns pages</a> for all the patterns but now I have a sheet with all of them on a page.  Cool!</p>
<p>a</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thevincefiles.net/2008/01/30/design-patterns-quick-reference/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Adding 2 integers (32-bit)</title>
		<link>http://www.thevincefiles.net/2008/01/27/adding-2-integers-32-bit</link>
		<comments>http://www.thevincefiles.net/2008/01/27/adding-2-integers-32-bit#comments</comments>
		<pubDate>Mon, 28 Jan 2008 04:45:29 +0000</pubDate>
		<dc:creator>Vince</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[int]]></category>
		<category><![CDATA[long]]></category>

		<guid isPermaLink="false">http://www.thevincefiles.net/index.php/2008/01/27/adding-2-integers-32-bit/</guid>
		<description><![CDATA[A 32-bit signed integer has valid values ranging from−2,147,483,648 to +2,147,483,647.
So why do I always see sample code of an integer addition method written in C# like this:


private static int add(int a, int b){
    return a + b;
}
So if I were to add 2,000,000,000 and 2,000,000,000 &#8211; an integer datatype would overflow. [...]<p>a</p>
]]></description>
			<content:encoded><![CDATA[<p>A 32-bit signed integer has valid values ranging from−2,147,483,648 to +2,147,483,647.</p>
<p>So why do I always see sample code of an integer addition method written in C# like this:</p>
<p><span id="more-11"></span></p>
<pre name="code" class="c-sharp">
private static int add(int a, int b){
    return a + b;
}</pre>
<p>So if I were to add 2,000,000,000 and 2,000,000,000 &#8211; an integer datatype would overflow.  But the solution isn&#8217;t merely to set the return value to a &#8220;long&#8221; (i.e. 64-bit integer):</p>
<pre name="code" class="c-sharp">
private static long add(int a, int b){
    return a + b;
}</pre>
<p>You have to cast each parameter variable to a long before performing the addition:</p>
<pre name="code" class="c-sharp">
private static long add(int a, int b){
    return (long)a + (long)b;
}</pre>
<p>Here are some I found:</p>
<p><a title="Not a good example of C# integer addition" href="http://www.cs.tufts.edu/comp/194NET/notes/csharp.php3" target="_blank">http://www.cs.tufts.edu/comp/194NET/notes/csharp.php3</a></p>
<p><a href="http://www.harding.edu/fmccown/java1_5_csharp_comparison.html" target="_blank"> http://www.harding.edu/fmccown/java1_5_csharp_comparison.html</a></p>
<p>http://www.developernotes.com/post/Use-Ruby-to-Unit-Test-C.aspx</p>
<p>a</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thevincefiles.net/2008/01/27/adding-2-integers-32-bit/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
