<?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>Cymen&#039;s Blog &#187; Programming</title>
	<atom:link href="http://blog.cymen.org/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cymen.org</link>
	<description></description>
	<lastBuildDate>Fri, 03 Feb 2012 17:53:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>jQuery JCrop plugin, Chrome/Webkit: fails to initialize after the first attempt</title>
		<link>http://blog.cymen.org/2011/11/10/jquery-jcrop-plugin-chromewebkit-fails-to-initialize-after-the-first-attempt/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jquery-jcrop-plugin-chromewebkit-fails-to-initialize-after-the-first-attempt</link>
		<comments>http://blog.cymen.org/2011/11/10/jquery-jcrop-plugin-chromewebkit-fails-to-initialize-after-the-first-attempt/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 16:35:45 +0000</pubDate>
		<dc:creator>Cymen</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.cymen.org/?p=261</guid>
		<description><![CDATA[I recently ran into a bug when using recent versions of Chrome and the jQuery JCrop plugin. I am adding some HTML to a jQuery UI modal window that contains the image I want to crop. I am initializing JCrop using the onload event of the image within this HTML. That worked fine in the [...]]]></description>
			<content:encoded><![CDATA[<p>I recently ran into a bug when using recent versions of Chrome and the jQuery JCrop plugin. I am adding some HTML to a jQuery UI modal window that contains the image I want to crop. I am initializing JCrop using the onload event of the image within this HTML. That worked fine in the past but stopped working sometime in the past year or so.</p>
<p>According to <a href="http://code.google.com/p/chromium/issues/detail?id=7731#c12">issue 7731</a> on chromium, the cause of this problem is that Webkit is more strict &#8212; the onload event is only triggered the first time the image is loaded. So the issue was the onload event triggered immediately when the modal window was shown &#8212; it triggered before the image was displayed (except for the first time). This is a tricky thing to work around. I verified that this was my issue by appending to the image URL the current timestamp (so + &#8216;?&#8217; + new Date().getTime()) in order to force the browser to reload the image each time. That did fix the problem but it also introduced UI lag as the image had to be fetched each time a crop was attempted.</p>
<p>I put in this short term fix: put my JCrop initialization code in a function named crop and then (still bound to the load event), attempt to initialize it. If the image isn&#8217;t loaded, try again in 25ms up to 5 times. I can tell if the image is loaded by checking for a height &gt; 0. The code:</p>
<pre class="brush: javascript">
    $('#cropbox').load(function (event) {
        var boxHeight = Math.floor($('#crop').closest('.ui-dialog').height() * 0.8);

        var $img = $(event.target);
        var productDiv = $('div#' + id);
        var sku = $('div.sku', productDiv).text();
        var product = productsData[sku];
        var height, width;

        // put crop loading code into function -- see comment below about chrome hack
        var crop = function () {
            height = $img.height();
            width = $img.width();

            var jcrop = $.Jcrop(
                ...
                });

            ...
        }

        // Ugly hack for chrome -- the load event triggers before the image is actually displayed/in DOM
        // but only on crop attempts after the initial one. One way to detect this is to check if the image
        // height is 0 -- if so, retry.
        // Update: issue probably this: http://code.google.com/p/chromium/issues/detail?id=7731#c12

        var worked = false;
        var attempts = 0;
        var attempt = function () {
            if (worked) return;

            height = $img.height();
            if (typeof height === 'number' &#038;&#038; height > 0) {
                crop();
                worked = true;
            }
            else {
                attempts++;
                if (attempts < 5) {
                    // try again in 25 milliseconds
                    setTimeout(attempt, 25);
                }
                else {
                    alert('Bug with cropping image.');
                }
            }
        }

        attempt();
    });
</pre>
<p>I reported this problem in <a href="http://code.google.com/p/jcrop/issues/detail?id=63">issue 63</a> for the JCrop plugin. Hopefully, there is a better way to do this however if you need a quick work around now...</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cymen.org/2011/11/10/jquery-jcrop-plugin-chromewebkit-fails-to-initialize-after-the-first-attempt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mime types for ASP.NET</title>
		<link>http://blog.cymen.org/2011/09/14/mime-types-for-asp-net/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mime-types-for-asp-net</link>
		<comments>http://blog.cymen.org/2011/09/14/mime-types-for-asp-net/#comments</comments>
		<pubDate>Wed, 14 Sep 2011 17:28:59 +0000</pubDate>
		<dc:creator>Cymen</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Mono MVC]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://blog.cymen.org/?p=229</guid>
		<description><![CDATA[One of the annoyances working on the Windows/IIS stack is that getting mime types is a pain. They are located in multiple places and there is no really ideal &#8220;best practice&#8221; method to get mime types without what I consider overly-complicated solutions. In light of this observation I wrote a basic C# program that fetches [...]]]></description>
			<content:encoded><![CDATA[<p>One of the annoyances working on the Windows/IIS stack is that getting mime types is a pain. They are located in multiple places and there is no really ideal &#8220;best practice&#8221; method to get mime types without what I consider overly-complicated solutions. In light of this observation I wrote a basic C# program that fetches the mime.types file from the Apache project and converts it to a C# Dictionary<string, string> keyed by file extension. It is a basic program but might be useful for others wondering why in the world this is so complicated.</p>
<p><a href="https://github.com/cymen/ApacheMimeTypesToDotNet" title="ApacheMimeTypesToDotNet">ApacheMimeTypesToDotNet</a> on github</p>
<p>The output looks like this: <a href="https://github.com/cymen/ApacheMimeTypesToDotNet/blob/master/ApacheMimeTypes.cs" title="ApacheMimeTypes.cs">ApacheMimeTypes.cs</a></p>
<p><code></p>
<pre class="brush: c-sharp">
using System;
using System.Collections.Generic;

namespace ApacheMimeTypes
{
	class Apache
	{
		public static Dictionary<string, string> MimeTypes = new Dictionary<string, string>
		{
			{ "123", "application/vnd.lotus-1-2-3" },
			{ "3dml", "text/vnd.in3d.3dml" },
			{ "3g2", "video/3gpp2" },
			{ "3gp", "video/3gpp" },
			{ "7z", "application/x-7z-compressed" },
			{ "aab", "application/x-authorware-bin" },
			{ "aac", "audio/x-aac" },
			{ "aam", "application/x-authorware-map" },
			{ "aas", "application/x-authorware-seg" },
			{ "abw", "application/x-abiword" },
			{ "ac", "application/pkix-attr-cert" },
			{ "acc", "application/vnd.americandynamics.acc" },
			{ "ace", "application/x-ace-compressed" },
			{ "acu", "application/vnd.acucobol" },
			{ "acutc", "application/vnd.acucorp" },
			{ "adp", "audio/adpcm" },
			{ "aep", "application/vnd.audiograph" },
			{ "afm", "application/x-font-type1" },
			{ "afp", "application/vnd.ibm.modcap" },
			{ "ahead", "application/vnd.ahead.space" },
			{ "ai", "application/postscript" },
			{ "aif", "audio/x-aiff" },
			{ "aifc", "audio/x-aiff" },
			{ "aiff", "audio/x-aiff" },
			{ "air", "application/vnd.adobe.air-application-installer-package+zip" },
			{ "ait", "application/vnd.dvb.ait" },
			{ "ami", "application/vnd.amiga.ami" },
			{ "apk", "application/vnd.android.package-archive" },
			{ "application", "application/x-ms-application" },
			{ "apr", "application/vnd.lotus-approach" },
			{ "asc", "application/pgp-signature" },
			{ "asf", "video/x-ms-asf" },
			{ "asm", "text/x-asm" },
			{ "aso", "application/vnd.accpac.simply.aso" },
			{ "asx", "video/x-ms-asf" },
			{ "atc", "application/vnd.acucorp" },
			{ "atom", "application/atom+xml" },
			{ "atomcat", "application/atomcat+xml" },
			{ "atomsvc", "application/atomsvc+xml" },
			{ "atx", "application/vnd.antix.game-component" },
			{ "au", "audio/basic" },
			{ "avi", "video/x-msvideo" },
			{ "aw", "application/applixware" },
			{ "azf", "application/vnd.airzip.filesecure.azf" },
			{ "azs", "application/vnd.airzip.filesecure.azs" },
			{ "azw", "application/vnd.amazon.ebook" },
			{ "bat", "application/x-msdownload" },
			{ "bcpio", "application/x-bcpio" },
			{ "bdf", "application/x-font-bdf" },
			{ "bdm", "application/vnd.syncml.dm+wbxml" },
			{ "bed", "application/vnd.realvnc.bed" },
			{ "bh2", "application/vnd.fujitsu.oasysprs" },
			{ "bin", "application/octet-stream" },
			{ "bmi", "application/vnd.bmi" },
			{ "bmp", "image/bmp" },
			{ "book", "application/vnd.framemaker" },
			{ "box", "application/vnd.previewsystems.box" },
			{ "boz", "application/x-bzip2" },
			{ "bpk", "application/octet-stream" },
			{ "btif", "image/prs.btif" },
			{ "bz", "application/x-bzip" },
			{ "bz2", "application/x-bzip2" },
			{ "c", "text/x-c" },
			{ "c11amc", "application/vnd.cluetrust.cartomobile-config" },
			{ "c11amz", "application/vnd.cluetrust.cartomobile-config-pkg" },
			{ "c4d", "application/vnd.clonk.c4group" },
			{ "c4f", "application/vnd.clonk.c4group" },
			{ "c4g", "application/vnd.clonk.c4group" },
			{ "c4p", "application/vnd.clonk.c4group" },
			{ "c4u", "application/vnd.clonk.c4group" },
			{ "cab", "application/vnd.ms-cab-compressed" },
			{ "car", "application/vnd.curl.car" },
			{ "cat", "application/vnd.ms-pki.seccat" },
			{ "cc", "text/x-c" },
			{ "cct", "application/x-director" },
			{ "ccxml", "application/ccxml+xml" },
			...
		};
	}
}</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cymen.org/2011/09/14/mime-types-for-asp-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Yet another round on the ModelState PercentComplete() extension</title>
		<link>http://blog.cymen.org/2011/08/16/yet-another-round-on-the-modelstate-percentcomplete-extension/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=yet-another-round-on-the-modelstate-percentcomplete-extension</link>
		<comments>http://blog.cymen.org/2011/08/16/yet-another-round-on-the-modelstate-percentcomplete-extension/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 15:09:51 +0000</pubDate>
		<dc:creator>Cymen</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://blog.cymen.org/?p=216</guid>
		<description><![CDATA[So there were a few issues with the previous version &#8212; at least when I wanted to extend it for some custom calculations so here is yet another version: public static int PercentComplete(this ModelStateDictionary modelStateDictionary, int? ScalePercentWithValueAsZero = null, int? MaxValue = 100) { int totalItems = 0; int validItems = 0; int percentComplete = [...]]]></description>
			<content:encoded><![CDATA[<p>So there were a few issues with the previous version &#8212; at least when I wanted to extend it for some custom calculations so here is yet another version:</p>
<pre class="brush: csharp">
        public static int PercentComplete(this ModelStateDictionary modelStateDictionary, int? ScalePercentWithValueAsZero = null, int? MaxValue = 100)
        {
            int totalItems = 0;
            int validItems = 0;
            int percentComplete = 0;

            if (MaxValue.HasValue &#038;&#038; (MaxValue.Value < 0 || MaxValue.Value > 100))
                throw new ArgumentOutOfRangeException("MaxValue must between 0 and 100!");

            if (modelStateDictionary.IsValid)
            {
                percentComplete = MaxValue.Value;
            }
            else
            {
                foreach (var item in modelStateDictionary)
                {
                    totalItems++;
                    if (item.Value.Errors.Count == 0)
                        validItems++;
                }

                if (totalItems > 0)
                    percentComplete = (100 * validItems) / totalItems;

                if (ScalePercentWithValueAsZero.HasValue)
                {
                    if (ScalePercentWithValueAsZero.Value >= percentComplete)
                    {
                        percentComplete = 0;
                    }
                    else
                    {
                        percentComplete = Convert.ToInt32(Math.Ceiling((double)(percentComplete - ScalePercentWithValueAsZero.Value) / (100 - ScalePercentWithValueAsZero.Value) * 100));
                    }
                }

                if (MaxValue.HasValue)
                {
                    percentComplete = percentComplete * MaxValue.Value / 100;
                }
            }

            return percentComplete;
        }
</pre>
<p>And if a model has a particularly complicated percentage complete calculation in which one needs to manually check some things and add to the total that can be done:</p>
<pre class="brush: csharp">
        public int CustomPercentComplete(Func&lt;int?, int?, int&gt; PercentComplete)
        {
            // scale percent complete 50-100% as 0-100%
            int basePercentAsZero = 50;

            // actually, scale 50-100% as 0-91%
            int max = 91;            

            // call default PercentComplete
            int percentComplete = PercentComplete(basePercentAsZero, max);

            // implement here your custom percente complete for the remaining 9%

            return percentComplete;
        }
</pre>
<p>An example of calling the custom percentage complete calculator on your model:</p>
<pre class="brush: csharp">
        myModelInstance.CustomPercentComplete(ModelState.PercentComplete);
</pre>
<p>And if you need to call either calculator but your controller action doesn&#8217;t bind to an instance of the model you need to calculate the percentage on, you can add a method in your controller like this one (hopefully there is a better way &#8212; let me know if you know of one):</p>
<pre class="brush: csharp">
        // Work around for getting percentage complete when in another action where the model is not the application
        // like so:
        // int percentComplete = MyPercentComplete(application);
        private int MyPercentComplete(MyApplication application)
        {
            return Convert.ToInt32(MyPercentCompleteAction(application).Content);
        }

        // Work around for getting percentage complete when in another action where the model is not the application
        // like so:
        // int percentComplete = Convert.ToInt32(MyPercentComplete(application).Content);
        private ContentResult MyPercentCompleteAction(MyApplication application)
        {
            return Content(application.CustomPercentComplete(ModelState.PercentComplete).ToString());
        }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.cymen.org/2011/08/16/yet-another-round-on-the-modelstate-percentcomplete-extension/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Published PHPGatewayInterface to github&#8230;</title>
		<link>http://blog.cymen.org/2009/08/29/published-phpgatewayinterface-to-github/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=published-phpgatewayinterface-to-github</link>
		<comments>http://blog.cymen.org/2009/08/29/published-phpgatewayinterface-to-github/#comments</comments>
		<pubDate>Sun, 30 Aug 2009 05:51:03 +0000</pubDate>
		<dc:creator>Cymen</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.cymen.org/?p=84</guid>
		<description><![CDATA[I wrote a hopefully generic class in PHP to proxy CGI scripts. The idea being that sometimes one wants to get the output of a CGI script and do various things with it before displaying it. In my particular case, wrap CVSWeb so the output can be put into a HTML DIV. The code is [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a hopefully generic class in PHP to proxy CGI scripts. The idea being that sometimes one wants to get the output of a CGI script and do various things with it before displaying it. In my particular case, wrap CVSWeb so the output can be put into a HTML DIV.</p>
<p>The code is <a href="http://github.com/cymen/PHPGatewayInterface/tree/master">published on github as PHPGatewayInterface</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cymen.org/2009/08/29/published-phpgatewayinterface-to-github/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Regular Expression Tools</title>
		<link>http://blog.cymen.org/2009/06/13/regular-expression-tools/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=regular-expression-tools</link>
		<comments>http://blog.cymen.org/2009/06/13/regular-expression-tools/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 00:54:41 +0000</pubDate>
		<dc:creator>Cymen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Regular Expressions]]></category>

		<guid isPermaLink="false">http://blog.cymen.org/?p=16</guid>
		<description><![CDATA[Regular Expressions tools: The Regex Coach &#8211; windows desktop Regex Powertoy &#8211; windows desktop strfriend.com &#8211; web regex to graphical view (SVG) regex-graph &#8211; web regex to graphical view (PNG) quanetic.com/Regex &#8211; web regular expression tester regexpal.com &#8211; web jsregex.com &#8211; web]]></description>
			<content:encoded><![CDATA[<p>Regular Expressions tools:</p>
<ul>
<li><a href="http://www.weitz.de/regex-coach/">The Regex Coach</a> &#8211; windows desktop</li>
<li><a href="http://regex.powertoy.org/">Regex Powertoy</a> &#8211; windows desktop</li>
<li><a href="http://www.strfriend.com/">strfriend.com</a> &#8211; web regex to graphical view (SVG)</li>
<li><a href="http://pleasedieinafire.net/cgi-bin/regex-graph">regex-graph</a> &#8211; web regex to graphical view (PNG)</li>
<li><a href="http://www.quanetic.com/Regex">quanetic.com/Regex</a> &#8211; web regular expression tester</a></li>
<li><a href="http://regexpal.com/">regexpal.com</a> &#8211; web</li>
<li><a href="http://jsregex.com/">jsregex.com</a> &#8211; web</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.cymen.org/2009/06/13/regular-expression-tools/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Shooting Yourself in the Foot with Perl</title>
		<link>http://blog.cymen.org/2008/12/22/shooting-yourself-in-the-foot-with-perl/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=shooting-yourself-in-the-foot-with-perl</link>
		<comments>http://blog.cymen.org/2008/12/22/shooting-yourself-in-the-foot-with-perl/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 16:14:42 +0000</pubDate>
		<dc:creator>Cymen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://blog.cymen.org/?p=36</guid>
		<description><![CDATA[Returning Multiple Values $error, $hash = function_a( ... ); if ( $error ) { error(); } // never runs Correction: ($error, $hash) = function_a( ... ); if ( $error ) { error(); } // runs on error]]></description>
			<content:encoded><![CDATA[<p><strong>Returning Multiple Values</strong></p>
<pre>$error, $hash = function_a( ... );
if ( $error ) { error(); }    // never runs</pre>
<p>Correction:</p>
<pre>($error, $hash) = function_a( ... );
if ( $error ) { error(); }    // runs on error</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.cymen.org/2008/12/22/shooting-yourself-in-the-foot-with-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

