<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Nerdworks Blogorama]]></title><description><![CDATA[Mostly nerdy stuff.]]></description><link>http://blog1.nerdworks.in/</link><generator>Ghost v0.4.2</generator><lastBuildDate>Tue, 09 Jun 2026 07:07:42 GMT</lastBuildDate><atom:link href="http://blog1.nerdworks.in/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Debugging Docker containers from Visual Studio]]></title><description><![CDATA[<p>I use Visual Studio a fair bit at work during development. Most of the code I write however actually ends up running on some kind of Linux. Microsoft has over the last few years <em>really</em> embraced Linux as an operating system in pretty much every respect including, as it turns out, when it comes to <em>developing</em> native Linux applications. Consider the <a href='https://marketplace.visualstudio.com/items?itemName=VisualCPPTeam.VisualCforLinuxDevelopment' >Visual C++ for Linux Development</a> extension for Visual Studio for instance - which is freely available for all users (including the free <a href='https://www.visualstudio.com/vs/community/' >community edition</a>). This extension makes it remarkably straightforward to use Visual Studio as your IDE for building and debugging native Linux applications. Information on how you can get setup with this extension and use it to build native Linux apps is available as a <a href='https://blogs.msdn.microsoft.com/vcblog/2016/03/30/visual-c-for-linux-development/' >blog post</a>.</p>

<p><img src='http://blog1.nerdworks.in/content/images/2017/Jan/linux-new-project.png'  alt="Linux VS New Project" /></p>

<p>While this is great, I wanted to see how far I can get trying to get Visual Studio to connect and remote-debug a native app running inside a Linux Docker container. As it turns out there's an <a href='https://blogs.msdn.microsoft.com/vcblog/2016/11/08/developing-linux-c-applications-with-azure-docker-containers/' >informative blog post</a> on this topic as well. The basic idea is that we build a Docker image with all the development tools we need along with an SSH server and then we spin it up and remote debug from Visual Studio like how we do with normal Linux servers. Visual Studio remains oblivious to the fact that the program is running inside a Docker container.</p>

<h3 id="securecomputingmodepolicies">Secure computing mode policies</h3>

<p>When I attempted to follow along with what's in the <a href='https://blogs.msdn.microsoft.com/vcblog/2016/11/08/developing-linux-c-applications-with-azure-docker-containers/' >blog post</a> however I ran into the following error when I tried to launch the debugger:</p>

<pre><code>Unable to start debugging. Unexpected GDB output from command "-interpreter-exec console "target remote localhost:18358"". Remote connection closed  
</code></pre>

<p>After much googling I stumbled upon an <a href='https://hub.docker.com/r/ducatel/visual-studio-linux-build-box/' >image in Docker hub</a> which included some instructions as to how we are to use that image to debug from Visual Studio. Here's the Docker run command that was being proposed that we use:</p>

<pre><code>docker run -d -p 12345:22 --security-opt seccomp:unconfined ducatel/visual-studio-linux-build-box  
</code></pre>

<p>The specific option of interest is <code>--security-opt seccomp:unconfined</code>. I use <a href='https://www.docker.com/products/docker' #/windows">Docker for Windows</a> as my local Docker installation. <em>Docker for Windows</em> makes it drop-dead easy to get going with a fully functional Docker environment. You download and install an MSI (or a DMG on macOS to get Docker setup on your Mac) and everything pretty much <em>just works</em>. As it turns out this results in the creation of a Hyper-V virtual machine (VM) on my Windows host running a MobyLinux distribution. The Linux kernel in the VM appears to have <a href='https://en.wikipedia.org/wiki/Seccomp' >seccomp</a> enabled. <em>Secure computing mode</em> or <em>seccomp</em> is an execution mode on Linux that restricts the set of system calls a given process is allowed to make. One can define a "seccomp profile" to be explicit about exactly which system calls are allowed and which aren't.</p>

<p>Docker now has built in support for seccomp, i.e., it spins up containers with a default seccomp profile enabled. While this default profile tends to work well for a vast majority of the use cases out there, it so happens that it disables some system calls that are necessary for enabling remote debugging from Visual Studio. Passing <code>--security-opt seccomp:unconfined</code> while running a container essentially causes Docker to disable seccomp restrictions on the container.</p>

<p>After disabling the seccomp profile Visual Studio was able to connect and remote debug perfectly. Visual Studio also includes a built-in <em>Linux Console</em> which is essentially a shell connected to your program's standard input and output (yep, input too!). This means you get to see the console output right there within Visual Studio and you can interactively respond to your program as well.</p>

<p><img src='http://blog1.nerdworks.in/content/images/2017/Jan/linux-console.png'  alt="Linux VS Console" /></p>

<h3 id="generatingdockerfilesforyourapplications">Generating Dockerfiles for your applications</h3>

<p>After having figured out how to get remote debugging working I wanted to be able to automate the creation of Dockerfile definitions for my needs. I put together a small Node.js app to implement this automation. The source is hosted up on GitHub here:</p>

<blockquote>
  <p><a href='https://github.com/avranju/docker-linux-dev-image' >https://github.com/avranju/docker-linux-dev-image</a></p>
</blockquote>

<p>To run the app do the following:</p>

<ul>
<li><p>Install a recent version of <a href='https://nodejs.org/' >Node.js</a></p></li>
<li><p>Install the <a href='https://yarnpkg.com/' >Yarn</a> package manager (if you prefer <strong>npm</strong> however then that should work too - just replace running <code>yarn</code> below with <code>npm install</code>)</p></li>
<li><p>Clone the repo</p></li>
</ul>

<p>The repo includes a file called <code>Dockerfile.template</code> which you can customize to fit your needs. Just make sure you don’t remove the instructions already there for setting up an SSH server. Now open a terminal/command prompt and run the following from the folder where you cloned the repo:</p>

<pre><code>$ yarn
$ node app.js
</code></pre>

<p>This will produce output that looks like this:</p>

<pre><code>$ node app.js
[*] Reading Dockerfile template.
[*] Creating output folder C:\code\docker-linux-dev-image\output\HyoI9qTUe.
[*] Generating new keypair.
[*] Saving private key file.
[*] Saving public key file.
[*] Generating Dockerfile.

&gt; The SSH keys and Dockerfile are in the folder C:\code\docker-linux-dev-image\output\HyoI9qTUe.
</code></pre>

<p>The generated <code>Dockerfile</code> along with SSH keys is dropped into the output folder. CD into the output folder and build the Docker image the usual way:</p>

<pre><code>$ cd output/HyoI9qTUe
$ docker build -t vsdebug .
</code></pre>

<p>If everything goes well you'll find the newly minted image in your Docker engine. Now to run a container from this image you'd run the following command:</p>

<pre><code>docker run -d -p 2222:22 \  
           --security-opt seccomp:unconfined \
           vsdebug
</code></pre>

<p>This will spin a container up with an SSH server listening on local port <code>2222</code>. You can test this out by SSHing to this server like so:</p>

<pre><code>$ cd output/HyoI9qTUe
$ ssh -i id_rsa -p 2222 root@localhost
</code></pre>

<p>That’s it! Now you should be able to remote debug your apps to your heart’s content. Combining this with the support for volume mounting Windows folders into a Linux Docker container using <a href='https://www.docker.com/products/docker' #/windows">Docker for Windows</a>, I am able to get the perfect setup! You first enable sharing your Windows drives with Docker from the <em>Settings</em> screen in <em>Docker for Windows</em>.</p>

<p><img src='http://blog1.nerdworks.in/content/images/2017/Jan/volume-mount.png'  alt="Docker Volume Mounting" /></p>

<p>And then you spin your container up like so and you find yourself lost in debugging nirvana!</p>

<pre><code>$ docker run -d -p 2222:22 \
             -v c:/code/my-linux-app:/code/my-linux-app \
             --security-opt seccomp:unconfined \
             vsdebug
</code></pre>

<p>Hope this helps you get a jump-start with your own Visual Studio/Windows/Linux development setup.</p>]]></description><link>http://blog1.nerdworks.in/debugging-docker-containers-from-visual-studio/</link><guid isPermaLink="false">79ff4721-850a-4add-845a-12cbce2d3cc0</guid><dc:creator><![CDATA[Rajasekharan Vengalil]]></dc:creator><pubDate>Sun, 29 Jan 2017 21:43:33 GMT</pubDate></item><item><title><![CDATA[Using Docker Swarm clusters on Azure]]></title><description><![CDATA[<p>One of the demos I prepared for the <a href='http://www.microsoftazureconference.in/' >Microsoft Azure Conference</a> in Pune, India in March of 2015 was about running orchestration engines on Azure to manage clusters of hosts and containers using <a href='https://www.docker.com/docker-swarm' >Docker Swarm</a>. Docker Swarm, if you didn't know is a clustering solution for Docker containers from the open source <a href='https://github.com/docker/docker' >Docker project</a>. You should probably <a href='https://docs.docker.com/swarm/' >read the documentation</a> to understand what Swarm does but in case you aren't in the mood or are just plain lazy then here's an extremely brief primer.</p>

<h2 id="sowhatisdockerswarm">So what is Docker Swarm?</h2>

<p><em>Swarm</em> basically allows you to treat a cluster of <em>nodes</em>, i.e. a collection of physical/virtual machines, as one giant Docker host. It attempts to abstract away from you the fact that there are a cluster of individual Docker hosts that are actually running your containers for you. Simple enough, isn't it? The basic idea is that you stick a bunch of VMs (or physical machines) behind a <em>swarm manager</em> service which then sets about providing the Docker host REST API for you. The <em>swarm manager</em>'s implementation of the Docker host API will simply delegate the actual work to one or more of the worker nodes that it has access to.</p>

<p>Since the <em>swarm manager</em>'s API is basically exactly equivalent to the Docker host API, it means that in order to manage the cluster, you can use pretty much the exact same tooling that you use today when you deal with a single Docker host. You can, for instance, use the <strong>Docker CLI</strong> to deal with a <em>swarm manager</em> host just as you would a regular Docker host. For those who prefer looking at a picture instead of reading a bunch of text (well, any more than you already have), here's a graphic that shows how it works. Image credits: <a href='http://www.slideshare.net/Docker/docker-swarm-020' ><em>Docker Inc. on SlideShare</em></a>.</p>

<p><img src='http://blog1.nerdworks.in/content/images/2015/Sep/swarm.PNG' "Docker Swarm"" alt="Docker Swarm" /></p>

<p>The swarm software itself is available as a public <a href='https://hub.docker.com/_/swarm/' >Docker image</a> on <a href='https://hub.docker.com/' >Docker Hub</a> (apart from being <a href='https://github.com/docker/swarm' >open source</a> that is).</p>

<h3 id="pluggableschedulers">Pluggable Schedulers</h3>

<p>So when you ask a swarm manager to spin up a container how exactly does it know which of the possibly 100s of nodes you've got it configured with to use? Swarm comes with a built-in scheduler that can figure things out by itself but is also designed to make the scheduling process pluggable - meaning, you can replace it's built-in scheduler with another one of your choice if you so feel like it. For example, you can use the <a href='http://mesos.apache.org/' >Apache Mesos</a> project's scheduler and hook it up with Docker Swarm so that Mesos takes care of picking out the best node to spin up a given container.</p>

<h3 id="pluggablenodediscovery">Pluggable node discovery</h3>

<p>Swarm supports multiple mechanisms for associating nodes (which can be physical or virtual machines) with an instance of the swarm manager. As with scheduling, there's a bulit-in <em>hosted</em> discovery service provided by docker.com which you can choose to use or set one up yourself using <a href='https://github.com/coreos/etcd' ><em>etcd</em></a>, <a href='https://www.consul.io/' ><em>consul</em></a>, <a href='http://zookeeper.apache.org/' ><em>zookeeper</em></a> or just a plain text file containing host names and IP addresses.</p>

<h2 id="mydemoformytalk">My demo for my talk</h2>

<p>So there, now you know what Docker Swarm is all about (kind of). For <a href='http://bit.ly/1N8WPAm' >my talks</a> though I needed a way of easily spinning up and tearing down Docker Swarm clusters on Azure. Remember, this was in March of 2015 and support for Docker Swarm was just beginning to show up in <a href='https://www.docker.com/docker-machine' >Docker Machine</a> which is a tool that lets you easily create and manage Docker host VMs and Swarm clusters. So I quickly put together a few bash scripts to automate provisioning of the VMs for my Swarm cluster. This post is about those scripts and how you can use them for your own needs.</p>

<p>Firstly, the bash scripts are open source and hosted on GitHub here:</p>

<blockquote>
  <p><a href='https://github.com/avranju/azure-swarm' >https://github.com/avranju/azure-swarm</a></p>
</blockquote>

<p>The main two script files in question are the following:</p>

<ul>
<li><strong>swarm-up.sh</strong> - this brings up the cluster for you</li>
<li><strong>swarm-down.sh</strong> - this tears down the cluster you created using <em>swarm-up.sh</em></li>
</ul>

<h3 id="settingupyourpc">Setting up your PC</h3>

<p>Before you can run the scripts you'll need to do the following:</p>

<ol>
<li>If you're on Windows, then install Git so that you get the Git Bash console. If you're on Mac/Linux, well, you already have bash.  </li>
<li>Install <a href='https://nodejs.org/en/' >Node.js</a> using your favorite method. I myself like <a href='https://github.com/creationix/nvm' >Node Version Manager</a> (NVM) to manage my node.js versions (there's a <a href='https://github.com/coreybutler/nvm-windows' >Windows version</a> available too).  </li>
<li>Install <a href='http://www.git-scm.com/' >Git</a> if you don't have it already.  </li>
<li>Install <strong>json</strong> from <a href='https://www.npmjs.com/package/json' >NPM</a> from a terminal like so: <strong>npm install -g json</strong>  </li>
<li>Install the Azure CLI like so: <strong>npm install -g azure-cli</strong>. Configure the Azure CLI with a valid Azure Subscription. If you don't know how to do that then <a href='https://azure.microsoft.com/en-us/documentation/articles/xplat-cli-connect/' >this handy guide</a> should help.  </li>
<li>Clone the repo like so:</li>
</ol>

<blockquote>
  <p><code>
  git clone https://github.com/avranju/azure-swarm.git
  </code></p>
</blockquote>

<h3 id="runningthescripts">Running the scripts</h3>

<p>Running the script isn't very hard. To setup a cluster with default <a href='https://github.com/avranju/azure-swarm/blob/master/options.sh' >options</a> (1 <em>small</em> master VM and 2 <em>small</em> worker node VMs located in "West US") just run this:</p>

<pre><code>./swarm-up.sh
</code></pre>

<p>This will do the following:</p>

<ol>
<li>Generate new SSH keys  </li>
<li>Create a new storage account and container  </li>
<li>Create a new Azure virtual network  </li>
<li>Spin up a VM to run the Swarm Manager service in the virtual network created in step 3  </li>
<li>Spin up as many worker node VMs as needed (again, in the same virtual network)  </li>
<li>Create a bunch of files in a folder called <em>output</em>.</li>
</ol>

<p>If everything goes well you should have a Docker Swarm cluster of your own with everything hooked up.</p>

<h3 id="outputfiles">Output files</h3>

<p>Each run of the script is identified by a randomly generated 8 character long hex string. For e.g. you might get this: <strong>35f8fa98</strong>. A file containing this ID is produced in the <em>output</em> folder. For instance, for the ID <strong>35f8fa98</strong>, the file would be called <strong>swarm-35f8fa98.deployment</strong>. You'll see in a bit why this is important.</p>

<p>Another file that you'll be interested in is a file containing SSH cofiguration information. For the same deployment ID as before, this file will be called <strong>ssh-35f8fa98.config</strong>. You can use this file to SSH into any of the VMs. For example, to SSH into the <em>swarm-master</em> VM, you'd run the following command:</p>

<pre><code>ssh -F output/ssh-35f8fa98.config swarm-master  
</code></pre>

<p>The same command will work for any of the worker node VMs (just change <em>swarm-master</em> to <em>swarm-00</em> or <em>swarm-01</em> and so forth).</p>

<h3 id="tearingdownthecluster">Tearing down the cluster</h3>

<p>The whole <em>deployment ID</em> shebang that I described above pays off when it comes to tearing down everything because having a deployment ID allows us to cleanly delete the deployment. Continuing with the same deployment ID as before, bringing a cluster down involves running the following script:</p>

<pre><code>./swarm-down.sh output/swarm-35f8fa98.deployment
</code></pre>

<p><em>swarm-down.sh</em> will attempt to delete everything that <em>swarm-up.sh</em> created - virtual network, cloud service, VMs and storage account. This will work even with partially deployed clusters (for e.g. you started running the script and then stopped it mid-way because, well, let's just say you had your reasons) because in that case the script will simply attempt to delete something that doesn't exist which is, well, harmless.</p>

<h3 id="customizeyourdeployment">Customize your deployment</h3>

<p>There are a few options that you can customize by editing the value of various variables in the <em>options.sh</em> file. Here're the ones you're likely to be interested in:</p>

<ul>
<li><code>VNET_LOCATION</code> - The <a href='http://azure.microsoft.com/en-us/regions/' >Azure data center</a> where your VMs will be provisioned. This is "West US" by default.</li>
<li><code>VM_SIZE</code> - The size of the VMs. Accepts any valid size string that <a href='http://azure.microsoft.com/en-us/pricing/details/virtual-machines/' >designates a VM size</a>. This is "Small" by default.</li>
<li><code>VM_IMAGE</code> - This is the name of the Linux VM image to use. By default this is Ubuntu 14.04 LTS. Ubuntu 15 doesn't work with this script at this point since Ubuntu has switched to <em>systemd</em> for running system services from v15 onwards while the script relies on it being <em>upstart</em>.</li>
<li><code>VM_USER_NAME</code> - The SSH user name. "avranju" by default.</li>
<li><code>SWARM_WORKER_NODES</code> - The number of worker VMs to spin up. This is 2 by default.</li>
</ul>

<h2 id="runningyourcontainers">Running your containers</h2>

<p>To run your containers you'll want to SSH into the <em>swarm-master</em> VM and set your environment up so that it points to the Swarm Manager service which is itself running as a container listening on port <strong>2377</strong> on the VM. Using the output files generated by <em>swarm-up</em>, you'd do the following:</p>

<pre>
<strong>$</strong> ssh -F output/ssh-35f8fa98.config swarm-master
<strong>avranju@swarm-master:~$</strong> export DOCKER_HOST=0.0.0.0:2377
<strong>avranju@swarm-master:~$</strong> docker version
Client version: 1.7.0
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 851c91a
OS/Arch (client): linux/amd64
<span style='background-color: yellow;'>Server version: swarm/0.4.0</span>
Server API version: 1.16
Go version (server): go1.4.2
Git commit (server): d647d82
OS/Arch (server): linux/amd64
</pre>

<p>As you can tell from the text in yellow highlight the CLI is talking to a Docker Swarm host. Now you can go ahead and start spinning up containers willy nilly and Docker Swarm should dutifully schedule them on your worker node VMs.</p>

<h2 id="finis">Finis</h2>

<p>That's pretty much it. As always, please feel free to fork, modify, send pull requests etc on these scripts and/or sound out in the comments below.</p>]]></description><link>http://blog1.nerdworks.in/using-docker-swarm-clusters-on-azure/</link><guid isPermaLink="false">316021b1-9c84-48fd-8e20-7d8d1d7b156b</guid><dc:creator><![CDATA[Rajasekharan Vengalil]]></dc:creator><pubDate>Mon, 07 Sep 2015 00:19:52 GMT</pubDate></item><item><title><![CDATA[On verbosity of programming languages]]></title><description><![CDATA[<p>My primary task at work for the last few weeks has been the building of an <a href='https://github.com/M' SOpenTech/msopentech-tools-for-intellij">open source plugin</a> for <a href='http://www.jetbrains.com/idea/' >IntelliJ IDEA</a> enabling tooling support for building Android applications which need to talk to <a href='http://azure.microsoft.com/en-us/documentation/services/mobile-services/' >Azure Mobile Services</a>, <a href='http://azure.microsoft.com/en-us/services/notification-hubs/' >Azure Notification Hubs</a> and various <a href='http://dev.office.com/' >Office 365 services</a>. One of the things I needed to do was a little string processing task. Specifically, given a string, the following needed to be done:</p>

<ol>
<li>Replace all instances of <code>.</code> and <code>_</code> with a single white space.  </li>
<li>Title case each white space delimited word.</li>
</ol>

<p>Simple enough. I figured it might be an interesting exercise implementing this in the various programming languages that I have varying levels of familiarity with. Here goes.</p>

<h2 id="java">Java</h2>

<p>For various reasons we needed to support Java 6 and up for the plugin. I am fairly new to the Java world so at first it seemed like I was going to have to implement this by hand till I discovered the immensely useful <a href='https://code.google.com/p/guava-libraries/' >Google Guava</a> library. With Google Guava this turns out to be a function that looks like this:</p>

<pre><code>private String scrubString(String name) {
  // replace all instances of . and _ with white space
  CharMatcher matcher = CharMatcher.anyOf("._");
  name = matcher.replaceFrom(name, ' ');

  // split the string into a sequence delimited by white space
  Iterable&lt;String&gt; tokens = Splitter.on(' ').split(name);

  // this function, given a string returns a title cased
  // version of it
  Function&lt;String, String&gt; makeTitleCase =
      new Function&lt;String, String&gt;() {
        @Override
        public String apply(String str) {
          return Character.toUpperCase(str.charAt(0)) +
              str.substring(1);
        }
      };

  // transform the tokens into their title-cased counterparts
  Iterable&lt;String&gt; titleCaseTransformer = Iterables.transform(
      tokens, makeTitleCase);

  // re-join the title-cased scrubbed strings using white space
  return Joiner.on(' ').join(titleCaseTransformer);
}
</code></pre>

<p>That's, well, verbose. If I wanted a terser version of this, I could do this:</p>

<pre><code>private String scrubString(String name) {
  return Joiner.on(' ').
      join(Iterables.transform(
          Splitter.on(' ').split(
              CharMatcher.anyOf("._").
                  replaceFrom(name, ' ')),
          new Function&lt;String, String&gt;() {
            @Override
            public String apply(String str) {
              return Character.toUpperCase(
                  str.charAt(0)) + str.substring(1);
            }
          }));
}
</code></pre>

<p>But that's of course, far less readable. With Java 8 lambda syntax however this can be simplified somewhat.</p>

<pre><code>private String scrubString(String name) {
  return Joiner.on(' ').
      join(Iterables.transform(
          Splitter.on(' ').split(
              CharMatcher.anyOf("._").
                  replaceFrom(name, ' ')),
          str -&gt; Character.toUpperCase(str.charAt(0)) +
                    str.substring(1)));
}
</code></pre>

<p>Though the only piece of code that was replaced is the callback routine that transforms regular strings to their title-cased counterparts, it does however declutter the code a fair bit.</p>

<h2 id="c35">C&#35;</h2>

<p>With C#'s support for LINQ this turns out to be far terser.</p>

<pre><code>private string ScrubString(string str)
{
  return String.Join(" ",
    from p in new Regex (@"[._]").Replace(str, " ").Split(' ')
    select Thread.CurrentThread.
            CurrentCulture.TextInfo.ToTitleCase(p));
}
</code></pre>

<p>I wrote that first and then realized that given that we have the <a href='http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase' (v=vs.110).aspx)">ToTileCase</a> method it's a bit of an overkill to split and join the string. Here's a simpler version:</p>

<pre><code>private string ScrubString (string str)
{
  return Thread.
         CurrentThread.
         CurrentCulture.
         TextInfo.
         ToTitleCase (new Regex (@"[._]").Replace (str, " "));
}
</code></pre>

<h2 id="python">Python</h2>

<p>With Python's support for list comprehension this ends up being even terser than C#.</p>

<pre><code>import string
import re

def string_scrub(str):
  return string.join([s.title() for s in \
      string.split(re.sub('[._]', ' ', str))])
</code></pre>

<h2 id="c11">C++ 11</h2>

<p>Here's my take on this using C++ 11 capabilities:</p>

<pre><code>string scrub(const string&amp; input) {
  regex re { "[._]" };
  string str = regex_replace(input, re, " ");

  vector&lt;string&gt; tokens;
  split(str, ' ', tokens);

  transform(tokens.begin(), tokens.end(), tokens.begin(),
      [](const string&amp; s) {
        return title_case(s);
      });

  return join(tokens, ' ');
}

string title_case(const string&amp; str) {
  return string(1, toupper(str[0])) + str.substr(1);
}

vector&lt;string&gt;&amp; split(
    const string&amp; str,
    char delimiter,
    vector&lt;string&gt;&amp; tokens) {
  string item;
  stringstream ss(str);
  while(getline(ss, item, delimiter))
    tokens.push_back(item);
  return tokens;
}

string join(const vector&lt;string&gt;&amp; tokens, char delimiter ) {
  ostringstream ss;
  bool first = true;
  for_each(tokens.begin(),
      tokens.end(),
      [&amp;ss, &amp;first, &amp;delimiter](const string&amp; s) {
        if(first) {
          first = false;
        } else {
          ss &lt;&lt; delimiter;
        }
        ss &lt;&lt; s;
      });

  return ss.str();
}
</code></pre>

<h2 id="javascriptofcourse">JavaScript (of course!)</h2>

<p>Here's the JavaScript version (using ES2015 syntax).</p>

<pre><code>function scrubString(str) {
  return str.
    replace(/[._]/g, ' ').
    split(' ').
    map(s =&gt; `${s.charAt(0).toUpperCase()}${s.substr(1)}`).
    join(' ');
}
</code></pre>

<p>I really like the nice fluent manner in which we are able to translate the requirements into an implementation in JS.</p>

<h2 id="commonlisp">Common Lisp</h2>

<p>It's been a while since I have dabbled in Common Lisp, but after some fervent searching here's what I came up with. Note that this does use a library that is not part of the standard Common Lisp distribution called <a href='http://weitz.de/cl-ppcre/' >CL-PPCRE</a> which appears to be a fairly popular regular expression library for Common Lisp.</p>

<pre><code>(load "~/quicklisp/setup.lisp")

(ql:quickload :cl-ppcre)

(defun scrub_string (str)
  (string-capitalize (cl-ppcre:regex-replace-all "[._]" str " ")))
</code></pre>

<p>There. If I had to pick a favorite I'd have to say I like the JavaScript version the best. The Common Lisp and the C# versions aren't too bad either. What do you think? Sound off in the comments!</p>]]></description><link>http://blog1.nerdworks.in/on-verbosity-of-programming-languages/</link><guid isPermaLink="false">87f46744-4c40-4060-975c-08da5ced795a</guid><dc:creator><![CDATA[Rajasekharan Vengalil]]></dc:creator><pubDate>Sun, 02 Nov 2014 22:24:13 GMT</pubDate></item><item><title><![CDATA[Using Unix tools to process text on Windows]]></title><description><![CDATA[<p>There was a need at work recently to perform a bunch of text processing tasks on very large XML documents spanning 10s of gigabytes in file size. The documents in question would look more or less like this:</p>

<pre><code>... some meta data tags here ...
&lt;Rows&gt;
    &lt;TableRow&gt;
      &lt;Fields&gt;
        &lt;Field&gt;Blah blah blah&lt;/Field&gt;
        &lt;Field&gt;Stuff here&lt;/Field&gt;
        &lt;Field&gt;3&lt;/Field&gt;
        &lt;Field&gt;More stuff&lt;/Field&gt;
      &lt;/Fields&gt;
    &lt;/TableRow&gt;
    &lt;TableRow&gt;
      &lt;Fields&gt;
        &lt;Field&gt;Blah blah blah and blah&lt;/Field&gt;
        &lt;Field&gt;Stuff here&lt;/Field&gt;
        &lt;Field&gt;2&lt;/Field&gt;
        &lt;Field&gt;More stuff&lt;/Field&gt;
      &lt;/Fields&gt;
    &lt;/TableRow&gt;
&lt;/Rows&gt;
</code></pre>

<p>Here's the processing that needed to be done:</p>

<ol>
<li>Extract the text contents from the first <code>Field</code> tag from under <code>Fields</code> under each <code>TableRow</code>.  </li>
<li>Filter out rows that didn't match a specific regular expression.  </li>
<li>Extract a specific sub-string from the text.  </li>
<li>Group the data on the sub-string and compute the number of times that string occurs.  </li>
<li>Produce the output as a comma separated value (CSV) file.</li>
</ol>

<h3 id="extractingtextfromthefieldtag">Extracting text from the <code>Field</code> tag</h3>

<p>For step 1, since we're dealing with extremely large XML files, using a DOM based parser was out of the question since that wouldn't be very memory efficient. I wrote a small utility in C++ (called <em>get-msg</em>) using the <a href='http://msdn.microsoft.com/en-us/library/windows/desktop/ms752838' (v=vs.85).aspx" title="XmlLite Parser">XmlLite parser</a> that's been shipping in Windows since Vista days! <em>XmlLite</em> is a native component modeled on .NET's <a href='http://msdn.microsoft.com/en-us/library/vstudio/System.Xml.XmlReader' (v=vs.100).aspx" title=".NET XmlReader class."><code>XmlReader</code></a> and <a href='http://msdn.microsoft.com/en-us/library/vstudio/system.xml.xmlwriter' (v=vs.100).aspx"><code>XmlWriter</code></a> types. It is a forward only, stream processing <em>pull</em> parser which means that it has extremely low memory footprint and can deal with XML inputs of arbitrary size. On the flip side, the programming model isn't quite as convenient as a DOM parser.</p>

<p>The following snippet shows how you can load up an XML document using <em>XmlLite</em>. <code>TableReader</code> is a simple class I put together to make working with <em>XmlLite</em> easier. The variable <code>_reader</code> below is a member instance of type <code>CComPtr&lt;IXmlReader&gt;</code> and <code>_fileStream</code> is another member of type <code>CComPtr&lt;IStream&gt;</code>.</p>

<pre><code>bool TableReader::Load(wstring file)
{
    // free up current reader and stream
    _reader.Release();
    _fileStream.Release();

    // load up file
    HRESULT hr = SHCreateStreamOnFile(
        file.c_str(),
        STGM_READ,
        &amp;_fileStream);
    if (FAILED(hr)) {
        return false;
    }
    hr = CreateXmlReader(
        __uuidof(IXmlReader),
        (void **) &amp;_reader,
        nullptr);
    if (FAILED(hr)) {
        return false;
    }
    hr = _reader-&gt;SetInput(_fileStream);
    if (FAILED(hr)) {
        return false;
    }

    // move to the "Rows" element
    if (MoveToElement(L"Rows") == false) {
        return false;
    }

    return true;
}
</code></pre>

<p>The code should be fairly self-explanatory. The <code>MoveToElement</code> method right at the end of the method is a member method of the <code>TableReader</code> class that's intended to make the job of navigating the node tree easier. Here's what this method looks like:</p>

<pre><code>bool TableReader::MoveToElement(wstring elementName)
{
    HRESULT hr;
    XmlNodeType nodeType;
    LPCWSTR wszLocalName = nullptr;

    while ((hr = _reader-&gt;Read(&amp;nodeType)) == S_OK) {
        switch (nodeType) {
            case XmlNodeType_Element:
            {
                hr = _reader-&gt;GetLocalName(&amp;wszLocalName, nullptr);
                if (FAILED(hr)) {
                    return false;
                }

                // check if the local name is the same as
                // "elementName" and if yes, then we're
                // done 
                if (elementName.compare(wszLocalName) == 0) {
                    return true;
                }
                break;
            }
        }
    }

    return SUCCEEDED(hr);
}
</code></pre>

<p>As you can tell, all it does is to keep walking the nodes in the XML document till it encounters an element whose name matches <code>elementName</code>. With this method handy, looking for the specific <code>Field</code> XML tag in question becomes fairly straightforward. Here's the method that does the job:</p>

<pre><code>bool TableReader::ReadMessage(LPCWSTR *ppwszMsg)
{
    HRESULT hr;

    // move to next "TableRow" element
    if (!MoveToElement(L"TableRow")) {
        return false;
    }

    // move to first "Field" element
    if (!MoveToElement(L"Field")) {
        return false;
    }

    // move reader to the "text" part of the element
    XmlNodeType nodeType;
    hr = _reader-&gt;Read(&amp;nodeType);
    if (nodeType != XmlNodeType_Text &amp;&amp;
        nodeType != XmlNodeType_EndElement) {
        return false;
    }

    // retrieve the message
    *ppwszMsg = nullptr;
    hr = _reader-&gt;GetValue(ppwszMsg, nullptr);
    return SUCCEEDED(hr);
}
</code></pre>

<p>The final program is then basically a tight loop that keeps calling <code>ReadMessage</code> till it returns false. Here are the relevant bits.</p>

<pre><code>wstring fileName{ argv[1] };  
TableReader reader;  
if (reader.Load(fileName) == false) {  
    wcout &lt;&lt; L"Attempt to load the XML file failed." &lt;&lt; endl;
    return 1;
}

// read and print all the messages
LPCWSTR pwszMsg;  
while (reader.ReadMessage(&amp;pwszMsg)) {  
    // we use wprintf instead of wcout because wcout seems to have
    // trouble dealing with embedded byte order mark byte sequences
    // for some reason
    wprintf(L"%s\n", pwszMsg);
}
</code></pre>

<h3 id="gettingthetoolsgnuwin">Getting the tools - GnuWin</h3>

<p>Now that we have a way of rapidly extracting the <code>Field</code> element that we're interested in from the source XML the rest of the text processing work turns out to be fairly straightforward when we have the  right tools handy. The first thing to do is to install the <a href='http://chocolatey.org/packages/GnuWin'  title="GnuWin Chocolatey Package">GnuWin</a> package via <a href='http://chocolatey.org/'  title="Chocolatey">Chocolatey</a>. If you don't know what is <em>Chocolatey</em> and you're a Windows user then you really should <a href='http://www.hanselman.com/blog/IsTheWindowsUserReadyForAptget.aspx'  title="Is the Windows user ready for apt-get?">get to know it</a>! Briefly, Chocolatey is a command line package manager for Windows - <a href='https://help.ubuntu.com/10.04/serverguide/apt-get.html' ><strong><code>apt-get</code></strong></a> for Windows if you will. <em>GnuWin</em> is a package that basically installs Win32 ports of all the key Unix/Linux tools without having to rely on a heavyweight "environment" like <a href='http://cygwin.com/'  title="Cygwin">Cygwin</a>. Installing <em>GnuWin</em> is a simple matter of running the following from a command prompt:</p>

<pre><code>cinst GnuWin
</code></pre>

<p>That's it. It does take a while to pull in all the files and get setup though.</p>

<h3 id="processingthetext">Processing the text</h3>

<p>The tools we're going to use to get the job done are essentially - <strong>grep</strong>, <strong>sed</strong>, <strong>sort</strong> and <strong>uniq</strong>. Here are the commands I used.</p>

<p>Filter out rows that didn't match a specific regular expression:</p>

<pre><code>grep "Creating OSDisk from OSImage\:.*"
</code></pre>

<p>Extract a specific sub-string from the text:</p>

<pre><code>sed -n "s/.*Creating OSDisk from OSImage:\(.*\).*/\1/p"
</code></pre>

<p>Group the data on the sub-string and compute the number of times that string occurs:</p>

<pre><code>sort | uniq -c
</code></pre>

<p>Produce the output as a comma separated value (CSV) file:</p>

<pre><code>sed -n "s/ *\([0-9]*\) \(.*\)/\2,\1/p"
</code></pre>

<p>What we do is to basically pipe everything together like so:</p>

<pre><code>get-msg input.xml |
  grep "Creating OSDisk from OSImage\:.*" |
  sed -n "s/.*Creating OSDisk from OSImage:\(.*\).*/\1/p" |
  sort | uniq -c |
  sed -n "s/ *\([0-9]*\) \(.*\)/\2,\1/p" 
</code></pre>

<p>And finally output redirect everything to a <strong>.csv</strong> file. That's pretty much it! Processing a 14 GB XML document through this pipeline on my quad core Intel i7 2014 Lenovo Carbon with 8 GB of RAM (and a truly horrendous keyboard) takes about 5 minutes. Not bad eh?</p>]]></description><link>http://blog1.nerdworks.in/using-unix-tools-to-process-text-on-windows/</link><guid isPermaLink="false">46c30d07-a063-4575-9c55-2a1f8d3abb7d</guid><dc:creator><![CDATA[Rajasekharan Vengalil]]></dc:creator><pubDate>Mon, 16 Jun 2014 03:30:23 GMT</pubDate></item><item><title><![CDATA[Converting document formats with Pandoc]]></title><description><![CDATA[<p>When I set out to convert my blog from the nearly 10 year old home-brewed ASP.NET based system to the spanking new <a href='http://ghost.org/' >Ghost</a> based blog engine, one of the somewhat trickier problems I encountered was converting all my existing 70 odd posts from HTML into <a href='http://daringfireball.net/projects/markdown/syntax' >Markdown</a> syntax. It was tricky because my HTML had, well, all kinds of code in it - arbitrary class names, IDs and such. Scrubbing all of it and producing something usable from it was a task that I wasn't exactly looking forward to with great delight. As it turned out, I ended up discovering this absolutely awesome tool called <a href='http://johnmacfarlane.net/pandoc/' >Pandoc</a> which was just the thing I needed. <em>Pandoc</em> is a "<em>universal document converter</em>" and here's how the author John McFarlane, a professor of philosophy at the University of California chooses to describe it:</p>

<blockquote>
  <p>If you need to convert files from one markup format into another, Pandoc is your swiss-army knife.</p>
</blockquote>

<p>Pandoc works by connecting <em>readers</em> and <em>writers</em> via an intermediate JSON based abstract syntax tree (AST) representation of the document.</p>

<p><img src='http://blogorama.nerdworks.in/content/images/2014/May/converting-document-formats-with-pandoc/pandoc-flow.png'  alt="Pandoc document conversion flow" /></p>

<p>This allows one to independently code up readers and writers targeting just the AST and you are automatically able to convert between pretty much all supported formats. A comprehensive library of readers and writers is supported by the tool out-of-the-box including, happily for me, a reader that can parse HTML and a writer that can produce markdown. You convert a HTML document into markdown by running the tool from the command line like so:</p>

<pre><code> pandoc -f html input.html -t markdown_strict -o output.md
</code></pre>

<p>Here, <code>-f</code> indicates that the source format is HTML, <code>-t</code> indicates that the target format is <em>markdown</em> and <code>-o</code> signifies the name of the the output file. If you don't supply a file name via the <code>-f</code> and <code>-o</code> options then the tool will simply read from and write to standard input and output respectively.</p>

<h3 id="manipulatingtheast">Manipulating the AST</h3>

<p>The real power of the tool, in my opinion, lies in the fact that you can have the tool output the AST representation of the source document as JSON which you can then programmatically manipulate any way you like. Here's how you generate the JSON for a given HTML document:</p>

<pre><code>pandoc -f html input.html -t json -o output.json
</code></pre>

<p>Pandoc also supports reading from and writing to standard input and output streams which when combined with some input/output redirection you can build some really nifty document processing pipelines. In my case, the markdown produced by Pandoc for some of my posts had some problems. For example, in some cases the markdown output would include markup like this - <code>****</code> - which basically represents the presence of empty <code>&lt;b&gt;&lt;/b&gt;</code> or <code>&lt;strong&gt;&lt;/strong&gt;</code> tags in the source HTML. I wrote up a little node.js app to fix up this issue. I had the node app read JSON from the standard input stream and output the processed JSON to standard output. Once I had it working the way I wanted, I was able to put together a command such as the following:</p>

<pre><code>pandoc -f html input.html -t json | node app.js | pandoc --no-wrap -f json -t markdown_strict -o output.md
</code></pre>

<p>The first part of the command converts the input HTML document into an AST representation which it then writes out to standard output as JSON. The JSON output is piped to the node.js app which loads it up into memory, walks the tree, fixes up document nodes that contain empty <code>Strong</code> elements by removing them and then outputs the modified AST as JSON to standard output. The modified AST JSON is then piped back to pandoc which proceeds to convert it to markdown. Easy-peasy!</p>

<h3 id="pandocfilters">Pandoc filters</h3>

<p>The node.js app that removed the redundant <code>Strong</code> elements is an example of a <em>filter</em> app. The source for the filters I used to scrub the posts from my blog is available on <a href='https://github.com/avranju/blogorama-v2/tree/master/tools/pandoc-filter'  title="Blogorama pandoc filter source.">Github</a>. First I implemented the filters I needed as an array of functions in <em><a href='https://github.com/avranju/blogorama-v2/blob/master/tools/pandoc-filter/filters.js' >filters.js</a></em>. Here for example is the filter that removes empty <code>Strong</code> elements:</p>

<pre><code>{
    name: "Remove empty bold/strong tags",
    nodeType: "Strong",
    apply: function(content) {
        // if content's c array is empty then remove the
        // node
        if(content &amp;&amp;
           content["c"] &amp;&amp;
           Util.isArray(content.c) &amp;&amp;
           content.c.length === 0 ) {
               return null;
        }

        return content;
    }
}
</code></pre>

<p>In <em><a href='https://github.com/avranju/blogorama-v2/blob/master/tools/pandoc-filter/app.js' >app.js</a></em> I first load up the JSON from standard input like so:</p>

<pre><code>function loadJsonAsync() {
    var deferred = Q.defer();
    var json = "";
    process.stdin.setEncoding("utf8");
    process.stdin.on("readable", function() {
        var chunk = process.stdin.read();
        if(chunk !== null) {
            json += chunk;
        }
    });
    process.stdin.on("end", function() {
        deferred.resolve(json);
    });

    return deferred.promise;
}
</code></pre>

<p>The filters are initialized via <a href='http://nodejs.org/api/modules.html' #modules_module_require_id">require</a> like this:</p>

<pre><code>var Filters = require("./filters.js").Filters;
</code></pre>

<p>And then the app proceeds to process the JSON like so:</p>

<pre><code>// load up the json from stdin
loadJsonAsync().done(function(json) {
    var ast = JSON.parse(json);
    // apply all of our filters
    Filters.forEach(function(filter) {
        ast[1] = walk(ast[1], filter);
    });
    console.log(JSON.stringify(ast));
});
</code></pre>

<p><code>walk</code> is a helper function I wrote to recursively visit each node in the syntax tree and apply the supplied filter on it. Here's the full function implementation:</p>

<pre><code>function walk(content, filter) {
    if(typeof(content) !== "object") {
        return content;
    }
    if(Util.isArray(content)) {
        return content.map(function(item) {
            return walk(item, filter);
        }).filter(function(item) {
            return (item !== null);
        });
    } else {
        if(filter.nodeType === "*" ||
           filter.nodeType === content.t) {
            // If a filter's `apply` method returns null then, it is
            // removed from the AST. If `apply` returns an object
            // then the node is replaced with what's returned from
            // the filter. If `apply` returns an array then the
            // array is spliced in.
            var node = filter.apply(content);
            if(node) {
                // splice array if its an array
                if(Util.isArray(node)) {
                    throw new Error("Not implemented yet.");
                } else {
                    content.t = node.t;
                    content.c = node.c;
                }
            } else {
                return null;
            }
        }

        if(content.c) {
            return {
                t: content.t,
                c: walk(content.c, filter)
            };
        }
    }

    // I don't think we should ever get here.
    return content;
}
</code></pre>

<p>And that's about it. I wrote a little driver program to iterate through all my 70 odd posts and then run them through my pandoc based document processing pipeline to be left with pristine markdown that I can then take and load up into Ghost! There were still a few manual tweaks required for some of the posts but those were few and far between and definitely doable by hand. Yay pandoc!</p>]]></description><link>http://blog1.nerdworks.in/converting-document-formats-with-pandoc/</link><guid isPermaLink="false">7a5f772b-7357-41f3-b59b-52eafc72a979</guid><dc:creator><![CDATA[Rajasekharan Vengalil]]></dc:creator><pubDate>Sat, 24 May 2014 13:57:34 GMT</pubDate></item><item><title><![CDATA[Nerdworks Blogorama v2]]></title><description><![CDATA[<p>This blog started its life way back in 2006 when I suddenly decided one day that I needed a web space of my own. I cranked out an ASP.NET based website, got myself a domain and some hosting space and one fine day the site was <a href='http://blogorama.nerdworks.in/theblogitsitsalive/' >alive</a>! Over the years I changed hosting providers a couple of times before finally moving in to its current home on <a href='http://azure.microsoft.com/en-us/services/web-sites/' >Microsoft Azure</a>. This is what the site looked like for almost a decade:</p>

<p><img src='http://blog1.nerdworks.in/content/images/2014/May/nerdworks-blogorama-v2/bv1.png'  alt="Blogorama v1 screenshot" /></p>

<h3 id="nerdworksreloaded">Nerdworks reloaded</h3>

<p>An aesthetic refresh was overdue and I also wanted to move away from my home-brew blog engine to a more contemporary implementation. After much consideration, hard work and toil I present to you ladies, gentlemen, boys and girls - this 2nd version of <em>Nerdworks Blogorama</em>! In keeping with the design zeitgeist of the day, the blog now features a spare clean typography driven design and I've kept the <em>features</em> down to a bare minimum (all there is is a search box really) and have let the content take center stage. I hope you like this!</p>

<h3 id="sowhatisthisbuilton">So what is this built on?</h3>

<p>I hope to provide some insight into how this new incarnation of the blog works over a series of blog posts the next few days. Right now though, here's some information on what some of the key pieces are:</p>

<ol>
<li>The site runs on the <a href='https://ghost.org/' >Ghost</a> blogging platform which is an <a href='http://expressjs.com/' >ExpressJS</a> based blogging framework built on <a href='http://nodejs.org/' >Node.JS</a>. I <a href='https://github.com/avranju/Ghost' >forked the</a> Ghost <a href='https://github.com/TryGhost/Ghost' >Github repository</a> and made a whole bunch of tweaks and changes to get it to work the way I wanted.  </li>
<li>I used the excellent <a href='http://johnmacfarlane.net/pandoc/' >pandoc</a> tool to convert all of the HTML from my old blog into <a href='http://daringfireball.net/projects/markdown/' >markdown</a> that I can use with Ghost. I had to do a little pre and post processing on the content to scrub the posts a bit. More on that in a separate post.  </li>
<li>I integrated the <a href='http://disqus.com/' >Disqus</a> commenting system and wrote a couple of utilities to export all the comments from the old blog into the <a href='http://blog1.nerdworks.in/help.disqus.com/customer/portal/articles/472150-custom-xml-import-format' >custom XML format</a> that Disqus requires, in order to import them into the new blog.  </li>
<li>One of the requirements I set myself when starting on this project was to make sure that all the existing links continue to work just fine. A good part of the customization I did on the base Ghost code base was in enabling additional routing logic for my old <em>.aspx</em> URLs (you don't want to end up losing all the good <em>Google</em> and <em>Bing</em> karma that you've built up over the years).  </li>
<li>I used a forked copy of the Ghost <a href='https://github.com/avranju/Vapor' >Vapor theme</a> for the layout and visual styling. Again, had to make a set of changes to incorporate everything I wanted in the UX.  </li>
<li>I setup a virtual machine on Azure to host an instance of the absolutely fantastic open source search and analytics system <a href='http://www.elasticsearch.org/' >Elastic Search</a>. I then wrote a little program to submit all of my existing blog content to the search engine and had it indexed. And finally, I modified the <em>Vapor</em> theme and Ghost to add support for search. A full treatment of how all this works warrants a separate post in its own right.  </li>
<li>And finally I also modified the <a href='http://gruntjs.com/' >Grunt</a> task to add support for automating building and deployment of the site to Azure websites.</li>
</ol>

<p>So there you have it. If you want to drop a note to me you can use my spiffy new <a href='http://blog1.nerdworks.in/contact/' >contact form</a> to write an email. Or you can leave a comment below of course. So long!</p>]]></description><link>http://blog1.nerdworks.in/nerdworks-blogorama-v2/</link><guid isPermaLink="false">e57fd1d8-3b68-4120-b2f8-fbc7b7cb18c8</guid><dc:creator><![CDATA[Rajasekharan Vengalil]]></dc:creator><pubDate>Wed, 21 May 2014 20:49:19 GMT</pubDate></item><item><title><![CDATA[Iterating over a std::tuple]]></title><description><![CDATA[<p>I’ve been trying to wrap my brain around the new <a href='http://en.wikipedia.org/wiki/Variadic_Templates' >variadic templates</a> capability in C++11 lately and wondered if it would be possible to write a generic routine to iterate over the members of a <a href='http://msdn.microsoft.com/en-us/library/bb982837.aspx' ><code>std::tuple</code></a>. I decided to start with the simple case of printing out the members of a tuple to the console via <code>std::cout.</code> First, I came up with a compile time recursive definition of a variadic template struct that overloads the function call operator like so. You might wonder why I didn’t stick with a plain variadic template function instead. That will become evident in a moment.</p>

<pre><code> template&lt;int index, typename... Ts&gt;
 struct print_tuple {
     void operator() (tuple&lt;Ts...&gt;&amp; t) {
         cout &lt;&lt; get&lt;index&gt;(t) &lt;&lt; " ";
         print_tuple&lt;index - 1, Ts...&gt;{}(t);
     }
 };
</code></pre>

<p>Clearly, we’ll need to define a base case to break out of the compile time recursion for the <code>print_tuple</code> call in the function call operator overload – which in this case would be when the non-type template parameter <code>index</code> is equal to zero. So I went ahead and defined the following partial specialization of <code>print_tuple</code> specializing on the value zero for the non-type template parameter <code>index</code>.</p>

<pre><code> template&lt;typename... Ts&gt;
 struct print_tuple&lt;0, Ts...&gt; {
     void operator() (tuple&lt;Ts...&gt;&amp; t) {
         cout &lt;&lt; get&lt;0&gt;(t) &lt;&lt; " ";
     }
 };
</code></pre>

<p>The reason I had to use a variadic template struct instead of a regular variadic template function is that it is <a href='http://www.gotw.ca/publications/mill17.htm' >not possible to partially specialize a template function in C++</a>. Using a struct/class allows us to do so. Now that we have this setup, we can write a utility <code>print</code> routine to wrap calls to <code>print_tuple</code> and we should be done. Here’s a complete example:</p>

<pre><code> #include &lt;iostream&gt;
 #include &lt;tuple&gt;

 using namespace std;

 template&lt;int index, typename... Ts&gt;
 struct print_tuple {
     void operator() (tuple&lt;Ts...&gt;&amp; t) {
         cout &lt;&lt; get&lt;index&gt;(t) &lt;&lt; " ";
         print_tuple&lt;index - 1, Ts...&gt;{}(t);
     }
 };

 template&lt;typename... Ts&gt;
 struct print_tuple&lt;0, Ts...&gt; {
     void operator() (tuple&lt;Ts...&gt;&amp; t) {
         cout &lt;&lt; get&lt;0&gt;(t) &lt;&lt; " ";
     }
 };

 template&lt;typename... Ts&gt;
 void print(tuple&lt;Ts...&gt;&amp; t) {
     const auto size = tuple_size&lt;tuple&lt;Ts...&gt;&gt;::value;
     print_tuple&lt;size - 1, Ts...&gt;{}(t);
 }

 int main() {
     auto t = make_tuple(1, 2, "abc", "def", 4.0f);
     print(t);

     return 0;
 }
</code></pre>

<p>All that <code>print</code> does is to first determine the size of the tuple via <a href='http://msdn.microsoft.com/en-us/library/bb982495.aspx' ><code>tuple_size</code></a> and then instantiates <code>print_tuple</code> and invokes the function call operator passing the tuple object in question. As you might have noticed we are essentially working our way backwards till we hit the base case where <code>index</code> is zero – i.e. it’ll print the tuple members in the reverse order. Here’s the output this produces:</p>

<pre><code> 4 def abc 2 1
</code></pre>

<h3 id="iterateinorder">Iterate in order?</h3>

<p>I figured, implementing a version that iterates over the tuple members in the order that they are specified should be fairly straightforward. We should just need to define a different base case (for the last item in the tuple instead of the first) and the recursive implementation should simply increment the index instead of decrementing it. Here’s what I came up with:</p>

<pre><code> template&lt;int index, typename... Ts&gt;
 struct print_tuple {
     void operator() (tuple&lt;Ts...&gt;&amp; t) {
         cout &lt;&lt; get&lt;index&gt;(t) &lt;&lt; " ";
         print_tuple&lt;index + 1, Ts...&gt;{}(t);
     }
 };

 template&lt;typename... Ts&gt;
 struct print_tuple&lt;tuple_size&lt;tuple&lt;Ts...&gt;&gt;::value - 1, Ts...&gt; {
     void operator() (tuple&lt;Ts...&gt;&amp; t) {
         cout &lt;&lt; get&lt;tuple_size&lt;tuple&lt;Ts...&gt;&gt;::value - 1&gt;(t) &lt;&lt; " ";
     }
 };
</code></pre>

<p>The code shown in bold above are the changes of interest. In particular, turns out, the base case definition which handles the situation when <code>print_tuple</code> is being instantiated with an <code>index</code> that is equal to the size of the tuple minus one, is not really valid C++. Non-type template specialization in C++ can only be done using “simple identifiers”. The expression <code>tuple_size&lt;tuple&lt;Ts...&gt;&gt;::value - 1</code> is a compile time constant for sure and ideally the compiler <em>should</em> be able to compute that value (which in fact it does for the code in the body of that method definition) but, well, it doesn’t! So, we’re kind of out of luck there.</p>

<h3 id="generalizediteration">Generalized iteration?</h3>

<p>One might imagine that it should be possible to generalize this iteration (even if it can be done in reverse order only) so that we are able to supply arbitrary callbacks for processing tuple members. Turns out this again, is not possible which I think is reasonable. Because at that point one really needs to question whether using a tuple is the correct choice – a <code>std::vector</code> or <code>std::list</code> or some of the other containers maybe a more appropriate option. Having said that, you might be thinking that we should still be able to generalize this by adding another template parameter for a callback routine and passing in a template function for that parameter. Maybe something like this?</p>

<pre><code> #include &lt;iostream&gt;
 #include &lt;tuple&gt;

 using namespace std;

 template&lt;int index, typename TCallback, typename... Ts&gt;
 struct iterate_tuple {
     void operator() (tuple&lt;Ts...&gt;&amp; t, TCallback callback) {
         callback(get&lt;index&gt;(t));
         iterate_tuple&lt;index - 1, TCallback, Ts...&gt;{}(t, callback);
     }
 };

 template&lt;typename TCallback, typename... Ts&gt;
 struct iterate_tuple&lt;0, TCallback, Ts...&gt; {
     void operator() (tuple&lt;Ts...&gt;&amp; t, TCallback callback) {
         callback(get&lt;0&gt;(t));
     }
 };

 template&lt;typename TCallback, typename... Ts&gt;
 void for_each(tuple&lt;Ts...&gt;&amp; t, TCallback callback) {
     iterate_tuple&lt;tuple_size&lt;tuple&lt;Ts...&gt;&gt;::value - 1, TCallback, Ts...&gt; it;
     it(t, callback);
 }

 template&lt;typename T&gt;
 void print(T v) {
     cout &lt;&lt; v &lt;&lt; " ";
 }

 int main() {
     auto t = make_tuple(1, 2, "abc", "def", 4.0f);
     for_each(t, print);

     return 0;
 }
</code></pre>

<p>This won’t work because the compiler needs to know the type of <code>TCallback</code> when it is instantiating the <code>for_each</code> template function which in this case it doesn’t and neither do we know the type because we want a different version of print to be used for each unique type in the tuple. If there is some way of telling the compiler to postpone resolution of <code>TCallback</code> till it is actually used then this might have worked. As far as I know, that isn’t possible. But then I might be wrong. If you know a way of doing that, it’ll be great if you could please let me know in the comments.</p>]]></description><link>http://blog1.nerdworks.in/iteratingoverastdtuple/</link><guid isPermaLink="false">47ab058c-7af9-4d5c-9b9e-e06f9223bff9</guid><dc:creator><![CDATA[Rajasekharan Vengalil]]></dc:creator><pubDate>Sun, 27 Apr 2014 06:01:22 GMT</pubDate></item><item><title><![CDATA[Playing in-memory audio streams on Windows 8]]></title><description><![CDATA[<p>A customer I'd been working with recently came up with a support request for a <a href='http://dev.windows.com/' >Windows 8 Store app</a> they'd been working on. They were building the app using the HTML/CSS/JS stack and wanted the ability to play audio streams completely from memory instead of loading it up from a file on the file system or a network stream. They needed this because their service implemented a custom Digital Rights Management (DRM) system where the audio content was encrypted and this needed to be decrypted before playback (duh!). They wanted however, to perform this decryption on the fly during playback instead of creating a decrypted version of the content on the file system. In this post I talk about a little sample I put together for them showing how you can achieve this on Windows 8. If you prefer to directly jump into the code and take a look at things on your own, then here's where its at:</p>

<blockquote>
  <p><a href='https://github.com/avranju/AudioPlayerWithCustomStream'  title="https://github.com/avranju/AudioPlayerWithCustomStream">https://github.com/avranju/AudioPlayerWithCustomStream</a></p>
</blockquote>

<h3 id="playingmediastreamsfrommemory">Playing media streams from memory</h3>

<p>The primary requirement proved to be fairly straightforward to accomplish. Turns out, there already exists an <a href='http://code.msdn.microsoft.com/windowsapps/MediaStreamSource-media-dfd55dff' >SDK sample</a> showing exactly this. The sample shows how to achieve media playback from memory streams using the <a href='http://msdn.microsoft.com/en-us/library/windows/apps/windows.media.core.mediastreamsource.aspx' >Windows.Media.Core.MediaStreamSource</a> object. Briefly, here are the steps:</p>

<ol>
<li><p>First you go fetch some metadata from the media stream. In case of audio content, this turns out to be the sample rate, encoding bit rate, duration and number of channels. For file based audio sources, the <a href='http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefile.aspx' >Windows.Storage.StorageFile</a> object has the ability to extract this information from the file directly via <a href='http://msdn.microsoft.com/en-us/library/windows/apps/hh770652.aspx' >Windows.Storage.StorageFile.Properties.RetrievePropertiesAsync</a>. Here's an example function that accepts a <code>StorageFile</code> object as input and then extracts and returns the said metadata from it.</p>

<pre><code>function loadProps(file) {
    var props = {
        fileName: "",
        sampleRate: 0,
        bitRate: 0,
        channelCount: 0,
        duration: 0
    };


    // save file name
    props.fileName = file.name;
    return file.properties.getMusicPropertiesAsync().then(
     function (musicProps) {
        // save duration
        props.duration = musicProps.duration;


        var encProps = [
            "System.Audio.SampleRate",
            "System.Audio.ChannelCount",
            "System.Audio.EncodingBitrate"
        ];


        return file.properties.
            retrievePropertiesAsync(encProps);
    }).then(function (encProps) {
        // save encoding properties
        props.sampleRate =
           encProps["System.Audio.SampleRate"];
        props.bitRate =
           encProps["System.Audio.EncodingBitrate"];
        props.channelCount =
           encProps["System.Audio.ChannelCount"];


        return props;
    });
}
</code></pre></li>
<li><p>Wrap the metadata gathered in step 1 in a <a href='http://msdn.microsoft.com/en-us/library/windows/apps/windows.media.mediaproperties.audioencodingproperties.aspx' >Windows.Media.MediaProperties.AudioEncodingProperties</a> object which in turn is then wrapped in a <a href='http://msdn.microsoft.com/en-us/library/windows/apps/windows.media.core.audiostreamdescriptor.aspx' >Windows.Media.Core.AudioStreamDescriptor</a> object.  </p></li>
<li>Use the <code>AudioStreamDescriptor</code> object to initialize a <code>MediaStreamSource</code> instance and setup event handlers for the <code>MediaStreamSource</code>'s <code>Starting</code>, <code>SampleRequested</code> and <code>Closed</code> events. As you might imagine, the idea is to respond to these events by handing out audio data to the <code>MediaStreamSource</code>which then proceeds to play that content.</li>
</ol>

<p>This is all fine and dandy, but how do we get this to work when the audio content is stored in memory in an <a href='http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.inmemoryrandomaccessstream.aspx' >Windows.Storage.Streams.InMemoryRandomAccessStream</a> object? The challenge of course is in extracting the metadata we need to setup a <code>MediaStreamSource</code> object.</p>

<h3 id="storagefilecanreadfromarbitrarystreams">StorageFile can read from arbitrary streams?</h3>

<p>As it happens, the <code>StorageFile</code> object has direct support for having it powered by an arbitrary stream (or pretty much anything really). I figured I'll hook up a <code>StorageFile</code> with an <code>InMemoryRandomAccessStream</code> object and have it extract the metadata that I needed. Here's how you connect a <code>StorageFile</code> with data fetched from any arbitrary source - in this case, just a string constant. You create a <code>StorageFile</code> object by calling <a href='http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefile.createstreamedfileasync.aspx' >StorageFile.CreateStreamedFileAsync</a>. <code>CreateStreamedFileAsync</code> requires that you pass a reference to a callback routine which is expected to supply the data the <code>StorageFile</code> object needs when it is first accessed. Here's a brief example:</p>

<pre><code>function init() {
    var reader;
    var size = 0;

    Windows.Storage.StorageFile.createStreamedFileAsync(
           "foo.txt", generateData, null).then(
       function (file) {
        // open a stream on the file and read the data;
        // this will cause the StorageFile object to
        // invoke the "generateData" function
        return file.openReadAsync();
    }).then(function (stream) {
        var inputStream = stream.getInputStreamAt(0);
        reader = new Windows.Storage.Streams.DataReader(inputStream);
        size = stream.size;
        return reader.loadAsync(size);
    }).then(function () {
        var str = reader.readString(size);
        console.log(str);
    });
}

function generateData(stream) {
    var writer = new Windows.Storage.Streams.DataWriter();
    writer.writeString("Some arbit random data.");

    var buffer = writer.detachBuffer();
    writer.close();

    stream.writeAsync(buffer).then(function () {
        return stream.flushAsync();
    }).done(function () {
        stream.close();
    });
}
</code></pre>

<p>The problem however, as I ended up discovering, is that <code>StorageFile</code> objects that work off of a stream created in this fashion do not support retrieval of file properties via <code>StorageFile.Properties.RetrievePropertiesAsync</code> or for that matter <code>StorageFile.Properties.GetMusicPropertiesAsync</code>. So clearly, this approach is not going to work. Having said that its useful to know that this technique is possible at all with <code>StorageFile</code> objects as it allows you to defer performing the actual work of producing the data represented by the <code>StorageFile</code> object till it is actually needed. And being a bona fide Windows Runtime object you can confidently pass this around wherever a <code>StorageFile</code> object is accepted - for instance when implementing a share source contract you might hand out a <code>StorageFile</code> object created in this manner via <a href='http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.datatransfer.datapackage.setstorageitems.aspx' >Windows.ApplicationModel.DataTransfer.DataPackage.SetStorageItems</a>.</p>

<h3 id="readingmusicmetadatausingthemicrosoftmediafoundation">Reading music metadata using the Microsoft Media Foundation</h3>

<p>After a bit of research I discovered that there is another API that can be used for fetching metadata from media streams (among other things) called the <a href='http://msdn.microsoft.com/en-us/library/windows/desktop/ms694197' (v=vs.85).aspx">Microsoft Media Foundation</a>. In particular, the API features an object called the <a href='http://msdn.microsoft.com/en-us/library/windows/desktop/dd374655' (v=vs.85).aspx">source reader</a> that can be used to get the data we are after. The trouble though is that this is a COM based API and cannot therefore be directly invoked from JavaScript. I decided to write a little wrapper Windows Runtime component in C++ and then use that from the JS app. After non-trivial help from my colleague <a href='https://github.com/ChrisGuzak' >Chris Guzak</a> and others directly from the Media Foundation team at Microsoft (perks of working for Microsoft I guess!) we managed to put together a small component that allows us to read the required meta data from an <code>InMemoryRandomAccessStream</code> object. Here's relevant snippet that does the main job (stripped out all the error handling code to de-clutter the code):</p>

<pre><code>MFAttributesHelper(InMemoryRandomAccessStream^ stream, String^ mimeType)
{
    MFStartup(MF_VERSION);

    // create an IMFByteStream from "stream"
    ComPtr&lt;IMFByteStream&gt; byteStream;
    MFCreateMFByteStreamOnStreamEx(
           reinterpret_cast&lt;IUnknown*&gt;(stream),
           &amp;byteStream);

    // assign mime type to the attributes on this byte stream
    ComPtr&lt;IMFAttributes&gt; attributes;
    byteStream.As(&amp;attributes);
    attributes-&gt;SetString(
           MF_BYTESTREAM_CONTENT_TYPE,
           mimeType-&gt;Data());

    // create a source reader from the byte stream
    ComPtr&lt;IMFSourceReader&gt; sourceReader;
    MFCreateSourceReaderFromByteStream(
           byteStream.Get(),
           nullptr,
           &amp;sourceReader);

    // get current media type
    ComPtr&lt;IMFMediaType&gt; mediaType;
    sourceReader-&gt;GetCurrentMediaType(
           MF_SOURCE_READER_FIRST_AUDIO_STREAM,
           &amp;mediaType);

    // get all the data we're looking for
    PROPVARIANT prop;
    sourceReader-&gt;GetPresentationAttribute(
           MF_SOURCE_READER_MEDIASOURCE,
           MF_PD_DURATION,
           &amp;prop);
    Duration = prop.uhVal.QuadPart;

    UINT32 data;
    sourceReader-&gt;GetPresentationAttribute(
           MF_SOURCE_READER_MEDIASOURCE,
           MF_PD_AUDIO_ENCODING_BITRATE,
           &amp;prop);
    BitRate = prop.ulVal;

    mediaType-&gt;GetUINT32(
           MF_MT_AUDIO_SAMPLES_PER_SECOND,
           &amp;data);
    SampleRate = data;

    mediaType-&gt;GetUINT32(
           MF_MT_AUDIO_NUM_CHANNELS,
           &amp;data);
    ChannelCount = data;
}
</code></pre>

<p>This is the implementation of the constructor on the <code>MFAttributesHelper</code> ref class. As you can tell, the constructor accepts a reference to an instance of an <code>InMemoryRandomAccessStream</code> object and the MIME type of the content in question and proceeds to extract the duration, encoding bitrate, sample rate and channel count from it. It does this by first creating an <a href='http://msdn.microsoft.com/en-us/library/windows/desktop/ms698720' (v=vs.85).aspx">IMFByteStream</a> object via the convenient <a href='http://msdn.microsoft.com/en-us/library/windows/desktop/hh162754' (v=vs.85).aspx">MFCreateMFByteStreamOnStreamEx</a> function which basically wraps an <a href='http://msdn.microsoft.com/en-us/library/windows/desktop/windows.storage.streams.irandomaccessstream.aspx' >IRandomAccessStream</a> object (which <code>InMemoryRandomAccessStream</code> implements) and returns an <code>IMFByteStream</code> instance. The object returned by <code>MFCreateMFByteStreamOnStreamEx</code> also implements <a href='http://msdn.microsoft.com/en-us/library/windows/desktop/ms704598' (v=vs.85).aspx">IMFAttributes</a> which we then <a href='http://msdn.microsoft.com/en-us/library/windows/desktop/ms682521' (v=vs.85).aspx">QueryInterface</a> for (via <a href='http://msdn.microsoft.com/en-us/library/br230426.aspx' >ComPtr::As</a>) and assign the MIME type value to it. Next we instantiate an object that implements <a href='http://msdn.microsoft.com/en-us/library/windows/desktop/dd374655' (v=vs.85).aspx">IMFSourceReader</a> via <a href='http://msdn.microsoft.com/en-us/library/windows/desktop/dd388106' (v=vs.85).aspx">MFCreateSourceReaderFromByteStream</a> and use that instance to fetch the duration and encoding bitrate values via the <a href='http://msdn.microsoft.com/en-us/library/windows/desktop/dd374662' (v=vs.85).aspx">GetPresentationAttribute</a> method. And finally, we retrieve an object that implements the <a href='http://msdn.microsoft.com/en-us/library/ms704850' (v=vs.85).aspx">IMFMediaType</a> interface via <a href='http://msdn.microsoft.com/en-us/library/windows/desktop/dd374660' (v=vs.85).aspx">IMFSourceReader::GetCurrentMediaType</a> and use that object to fetch the sample rate and the channel count values. Once you know how to do all this, it seems quite trivial of course but getting here, believe me, took some doing!</p>

<p>Now that we have this component, reading the metadata from JavaScript proves to be fairly straightforward. Here's an example. In the code below, <code>memoryStream</code> is an <code>InMemoryRandomAccessStream</code> instance and <code>mimeType</code> is a string with the MIME type of the content:</p>

<pre><code>var helper = MFUtils.MFAttributesHelper.create(memoryStream, mimeType);

// now, helper's sampleRate, bitRate, duration and channelCount
// properties contain the data we are looking for
</code></pre>

<p>Now with the metadata handy, we simply follow the steps as outlined earlier in this post to commence playback. As mentioned before the sample is hosted up on Github here:</p>

<blockquote>
  <p><a href='https://github.com/avranju/AudioPlayerWithCustomStream'  title="https://github.com/avranju/AudioPlayerWithCustomStream">https://github.com/avranju/AudioPlayerWithCustomStream</a></p>
</blockquote>

<p>For the sake of the sample, I took a plain <a href='http://testdriveie9.wise.glbdns.microsoft.com/ietestdrivecontent/Musopen.Com%20Symphony%20No.%205%20in%20C%20Minor,%20Op.%2067%20-%20I.%20Allegro%20con%20brio.mp3' >MP3 file</a> and applied a <a href='http://en.wikipedia.org/wiki/XOR_cipher' >XOR cipher</a> on it and then loaded it up and played back from memory applying another XOR transform on the bits before playback. It all works rather well together and again, hat-tip to <a href='https://twitter.com/Guz100' >Chris Guzak</a> for all his help in whittling down the WinRT component down to its essence and really cleaning up its interface!</p>]]></description><link>http://blog1.nerdworks.in/playinginmemoryaudiostreamsonw/</link><guid isPermaLink="false">76c26758-a16a-46f1-92f5-29699e961744</guid><dc:creator><![CDATA[Rajasekharan Vengalil]]></dc:creator><pubDate>Sun, 29 Dec 2013 02:16:09 GMT</pubDate></item><item><title><![CDATA[Add a "Web Server Here" Explorer shell extension command]]></title><description><![CDATA[<p>Sometimes I just want to spin up a web server on a folder in explorer.  Often its because browsers get nervous about running HTML pages directly off the file system and seem to feel more comfortable when its served from a web server.  I figured I'd had enough of writing little scripts or using IIS to create virtual folders every time and wanted a context menu option in Windows Explorer that'll just launch a web server pretty much anywhere I wanted.  Here's what it'll look like:</p>

<blockquote>
  <p><a href='http://blogorama.nerdworks.in/images/_img45.png' ><img src='http://blogorama.nerdworks.in/images/_img46.png'  alt="image" title="image" /></a></p>
</blockquote>

<p>Turns out, this is fairly straightforward to accomplish using <a href='http://www.iis.net/learn/extensions/introduction-to-iis-express/iis-express-overview' >IIS Express</a> and some registry tweaks.  For those of you who don't know, IIS Express is this light weight, self-contained version of IIS meant to be used for developing, debugging and testing web apps.  When you use Visual Studio, the web apps themselves run inside IIS Express when you hit F5.  Being self-contained, we are able to run a web server pretty much anywhere from a command prompt.  Documentation on how to do this is available <a href='http://www.iis.net/learn/extensions/using-iis-express/running-iis-express-from-the-command-line' >here</a>.  You can download and install IIS Express either via the <a href='http://www.microsoft.com/web/downloads/' >Web Platform Installer</a> or from <a href='http://www.microsoft.com/en-in/download/details.aspx?id=34679' >here</a> (IIS Express version 8.0 at the time of writing).   If your web app files are located in say, <strong>D:\Code\Web\Foo</strong> then you'd run a web server from that location like so:</p>

<pre><code>"C:\Program Files (x86)\IIS Express\iisexpress.exe" /path:"D:\Code\Web\Foo" /port:8080 /systray:true
</code></pre>

<p>The path to <em>iisexpress.exe</em> might be different if you're running on a 32-bit system.  It'll just be "Program Files" instead of "Program Files (x86)".  Once you've run the command, the web server starts up and you can load your web app in your favorite browser by navigating to <strong><em><a href='http://localhost:8080/' >http://localhost:8080/</a></em></strong>.  The next step is to integrate this into the Explorer shell so you can run this from wherever you want directly from Explorer.  <a href='http://haacked.com/articles/AboutHaacked.aspx' >Phil Haack</a> has written up a <a href='http://haacked.com/archive/2008/06/24/vs2008-web-server-here-shell-extension.aspx' >post</a> on how to do this with the web server that Visual Studio 2008 used to ship with way back in, well, 2008.  I adapted the basic steps described there to make it work with IIS Express.  Now, setting this up involves editing the Windows Registry, so please be careful with what you do.  This <a href='http://www.codinghorror.com/blog/2007/03/the-works-on-my-machine-certification-program.html' >works on my machine</a> and that's about all I am willing to say!</p>

<blockquote>
  <p><a href='http://blogorama.nerdworks.in/images/_img47.png' ><img src='http://blogorama.nerdworks.in/images/_img48.png'  alt="works-on-my-machine-starburst_3" title="works-on-my-machine-starburst_3" /></a></p>
</blockquote>

<p>If you're on a <strong>64-bit</strong> installation of Windows, here're the changes you need to do to your registry:</p>

<pre><code>[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\IISExpressWebServer]
@="Web Sever Here"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\IISExpressWebServer\command]
@="C:\\Program Files (x86)\\IIS Express\\iisexpress.exe /path:\"%1\" /port:8080 /systray:true"
</code></pre>

<p>And if you're on a <strong>32-bit</strong> installation, then this is what you need to do:</p>

<pre><code>[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\IISExpressWebServer]
@="Web Sever Here"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\IISExpressWebServer\command]
@="C:\\Program Files\\IIS Express\\iisexpress.exe /path:\"%1\" /port:8080 /systray:true"
</code></pre>

<p>If you need .reg files so you can just double-click to import them into your registry then they are available <a href='http://blog1.nerdworks.in/downloads/wsreg.zip' >here</a>.  You might want to edit the .reg files in case your installation paths are different from what's given there.  That's pretty much it!</p>]]></description><link>http://blog1.nerdworks.in/addawebserverhereexplorershell/</link><guid isPermaLink="false">a331ef8b-3e76-45ef-af37-62e3aeaa58e1</guid><dc:creator><![CDATA[Rajasekharan Vengalil]]></dc:creator><pubDate>Sun, 29 Sep 2013 00:55:16 GMT</pubDate></item><item><title><![CDATA[Some notes on C++11 lambda functions]]></title><description><![CDATA[<p>Lambda functions are a new capability introduced in C++ that offers a terse compact syntax for defining functions at the point of their use.  <a href='http://en.wikipedia.org/wiki/Bjarne_Stroustrup' >Bjarne Stroustrup</a> says that C++11, which is the latest ratified revision of the C++ standard, "<a href='http://herbsutter.com/elements-of-modern-c-style/' >feels like a new language</a>".  I think lambda functions are a big part of what makes the language feel so very different from C++03.  Lambda functions basically allow you to do things like this:</p>

<pre><code>vector&lt;int&gt; nums { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto evens = count_if(begin(nums), end(nums), [](int num) {
    return (num % 2) == 0;
});
</code></pre>

<p>The third parameter passed to the standard <a href='http://msdn.microsoft.com/en-us/library/464xhzk3' (v=vs.120).aspx">count_if</a> function is a predicate that is expected to return <strong>true</strong> if the value passed to it satisfies the condition and <strong>false</strong> otherwise.  In the snippet above we simply count the number of instances of even numbers in the collection.  Search for "C++ lambdas" on your favorite search engine and you should get plenty of material out there talking about this feature.  What follows in this post are some notes on certain aspects of C++ lambdas that I happened to notice as I was learning about them listed in no particular order.</p>

<ol>
<li><p>You can pass a lambda object around as you would pretty much anything else. Here's a made up example showing how you can pass a lambda as an argument to another function.</p>

<pre><code>#include &lt;iostream&gt;

using namespace std;

template &lt;typename T&gt;
void call(T);

int main() {
  auto fn = []() { cout&lt;&lt;"Lambda"&lt;&lt;endl; };
  call(fn);
  return 0;
}

template &lt;typename T&gt;
void call(T fn) {
  fn();
}
</code></pre></li>
<li><p>You can return lambdas from functions like any another object which makes for some interesting possibilities such as the following:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;functional&gt;

using namespace std;

template&lt;typename T&gt;
function&lt;T()&gt; makeAccumulator(T&amp; val, T by) {
    return [=,&amp;val]() {
        return (val += by);
    };
}

int main() {
    int val = 10;
    auto add5 = makeAccumulator(val, 5);
    cout&lt;&lt;add5()&lt;&lt;endl;
    cout&lt;&lt;add5()&lt;&lt;endl;
    cout&lt;&lt;add5()&lt;&lt;endl;
    cout&lt;&lt;endl;

    val = 100;
    auto add10 = makeAccumulator(val, 10);
    cout&lt;&lt;add10()&lt;&lt;endl;
    cout&lt;&lt;add10()&lt;&lt;endl;
    cout&lt;&lt;add10()&lt;&lt;endl;

    return 0;
}
</code></pre>

<p>Which produces the following output:</p>

<pre><code>15
20
25

110
120
130
</code></pre>

<p>The key thing to remember here is that it is your responsibility to make sure that the values you capture in a lambda remain in memory for the lifetime of the lambda itself. The compiler will not for instance, prevent you from capturing local variables by reference in a lambda and if you continue to access a variable that is no longer available, well, then the behavior is undefined.</p></li>
<li><p>Simply defining a lambda causes all variables captured by value by the lambda to be copy constructed. You don't really have to have any code that invokes the lambda in order for the variables to be copied. This is consistent with the idea that creating a lambda function essentially creates a function object which has as instance members the variables that have been captured in the lambda. Here's an example:</p>

<pre><code>#include &lt;iostream&gt;

using namespace std;

class Foo {
public:
  Foo() {
    cout&lt;&lt;"Foo::Foo()"&lt;&lt;endl;
  }

  Foo(const Foo&amp; f) {
    cout&lt;&lt;"Foo::Foo(const Foo&amp;)"&lt;&lt;endl;
  }

  ~Foo() {
    cout&lt;&lt;"Foo~Foo()"&lt;&lt;endl;
  }
};

int main() {
  Foo f;
  auto fn = [f]() { cout&lt;&lt;"lambda"&lt;&lt;endl; };
  cout&lt;&lt;"Quitting."&lt;&lt;endl;
  return 0;
}
</code></pre>

<p>Here's the output this produces:</p>

<pre><code>Foo::Foo()
Foo::Foo(const Foo&amp;)
Quitting.
Foo~Foo()
Foo~Foo()
</code></pre>

<p>As you can tell, the copy constructor gets invoked even though the lambda itself never gets invoked.</p></li>
<li><p>As an extension of the previous point, if you capture objects by value in a lambda and then proceed to pass that lambda around to other functions by value, then the variables in the closure will also get copy constructed.</p></li>
<li><p>If you reference capture a const local variable, it becomes a const reference in the lambda.</p>

<pre><code>#include &lt;iostream&gt;

using namespace std;

int main() {
    const int val = 10;
    auto f1 = [&amp;val]() {
        val = 20;  // won't compile
        cout&lt;&lt;"val = "&lt;&lt;val&lt;&lt;endl;
    };
    f1();

    return 0;
}
</code></pre></li>
<li><p>In general, when you wish to declare a variable that can hold a reference to a lambda in contexts where <em>auto</em> is not permissible (for e.g. function return types or arguments) use <strong>std::function</strong>. An example:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;functional&gt;

using namespace std;

function&lt;bool(int)&gt; makeLEPredicate(int max) {
    return [max](int val) -&gt; bool {
        return val &lt;= max;
    };
}

int main() {
    auto le10 = makeLEPredicate(10);
    cout&lt;&lt;le10(8)&lt;&lt;endl;

    return 0;
}
</code></pre>

<p>You could alternatively use function templates to achieve the same thing if the semantics of using templates makes sense to your use case.</p></li>
</ol>

<p>That's all for now. This list might get expanded as I explore lambdas further. As you might have noticed, the ability to use lambdas really does make a significant difference to productivity without sacrificing performance.</p>]]></description><link>http://blog1.nerdworks.in/somenotesonc11lambdafunctions/</link><guid isPermaLink="false">a67877b9-ca08-481b-8ff1-92baf7ee3eff</guid><dc:creator><![CDATA[Rajasekharan Vengalil]]></dc:creator><pubDate>Mon, 29 Jul 2013 12:55:11 GMT</pubDate></item><item><title><![CDATA[Implementing variable sized tiles using WinJS ListView]]></title><description><![CDATA[<p>Windows Store apps on Windows 8 often use a grouped tile style for rendering user interfaces. The modern desktop on Windows 8 is a classic example. Here's a zoomed out view of my current desktop for instance:</p>

<blockquote>
  <p><a href='http://blogorama.nerdworks.in/images/_img37.png' ><img src='http://blogorama.nerdworks.in/images/_img38.png'  alt="image" title="image" /></a></p>
</blockquote>

<p>You'll note that the tiles have been grouped into separate sections and each section contains tiles of different sizes. In this case there are only 2 sizes - a wide tile:</p>

<blockquote>
  <p><a href='http://blogorama.nerdworks.in/images/_img39.png' ><img src='http://blogorama.nerdworks.in/images/_img40.png'  alt="image" title="image" /></a></p>
</blockquote>

<p>And a square tile:</p>

<blockquote>
  <p><a href='http://blogorama.nerdworks.in/images/_img41.png' ><img src='http://blogorama.nerdworks.in/images/_img42.png'  alt="image" title="image" /></a></p>
</blockquote>

<p>Here's an example of an app that uses different tile sizes in different groups:</p>

<blockquote>
  <p><a href='http://blogorama.nerdworks.in/images/_img43.png' ><img src='http://blogorama.nerdworks.in/images/_img44.png'  alt="image" title="image" /></a></p>
</blockquote>

<p>I'd been meaning to write down exactly how we can customize the WinJS <a href='http://msdn.microsoft.com/en-us/library/windows/apps/br211837.aspx' ><strong>ListView</strong></a> to create interfaces such as this one and, well, here it is. The basic technique for implementing variable tiles with the WinJS <strong>ListView</strong> involves the following things:</p>

<ol>
<li><p>Determine what your "cell unit" is going to be.  This is the width and height of a single "unit" in pixels - the idea is that tile sizes must be a multiple of this.  For example, I might decide that my cell unit is going to be 15x20 pixels.  Then valid tile sizes would be 15x40, 30x20, 45x300 etc.  Once you know what this is, implement the <strong><a href='http://msdn.microsoft.com/en-us/library/windows/apps/br211744.aspx' >groupInfo</a></strong> property on the <strong><a href='http://msdn.microsoft.com/en-us/library/windows/apps/br211751.aspx' >GridLayout</a></strong> object on your list view's <a href='http://msdn.microsoft.com/en-us/library/windows/apps/br211833.aspx' >layout</a> like so.</p>

<pre><code>ready: function (element, options) {
    // more stuff here
    var layout = new WinJS.UI.GridLayout();
    layout.groupInfo = this.getGroupInfo.bind(this);
    // more stuff here
},


getGroupInfo: function () {
    return {
        enableCellSpanning: true,
        cellWidth: 15,
        cellHeight: 20
    }
},
</code></pre></li>
<li><p>Since different tiles in your control can be of different sizes you'll need to tell the <strong>ListView</strong> what those sizes are going to be.  You do this by implementing a method called <strong><a href='http://msdn.microsoft.com/en-us/library/windows/apps/hh758348.aspx' >itemInfo</a></strong> on your <strong>GridLayout</strong> object.  The <strong>ListView</strong>calls <strong>itemInfo</strong> for every element it renders from the data source.  The important thing to remember is that the size you return from the <strong>itemInfo</strong> method must be a multiple of the size you returned from <strong>groupInfo</strong>.</p>

<pre><code>ready: function (element, options) {
    // more stuff here
    var layout = new WinJS.UI.GridLayout();
    layout.itemInfo = this.getItemInfo.bind(this);
    // more stuff here
}


getItemInfo: function (index) {
    var data = ImageData.imagesList.getAt(index);
    var size = {
        width: 150,
        height: 200,
        newColumn: false
    };
    if (data.group.name === 
           ImageData.imageGroups.kittens.name) {
        size.height = 100;
    }
    else if (data.group.name ===
           ImageData.imageGroups.portraits.name) {
        size.width = 120;
    }
    return size;
},
</code></pre></li>
<li><p>Associate a JS function for your list view's <strong><a href='http://msdn.microsoft.com/en-us/library/windows/apps/hh700705.aspx' >itemTemplate</a></strong> property.  The job of this function is to render an item.</p>

<pre><code>listView.itemTemplate = this.selectItemTemplate.bind(this);
</code></pre>

<p>It is passed a <a href='http://msdn.microsoft.com/en-us/library/windows/apps/br211867.aspx' >WinJS.Promise</a> object as a parameter which when resolved will yield the data item which is to be rendered.  We can either manually create DOM elements using <a href='http://msdn.microsoft.com/en-us/library/ie/ms536389' (v=vs.85).aspx">document.createElement</a> from this routine or, as is more convenient, use declaratively pre-created <a href='http://msdn.microsoft.com/en-us/library/windows/apps/br229723.aspx' >WinJS.Binding.Template</a> instances from the HTML mark-up.  Here's an example implementation showing how to do this:</p>

<pre><code>selectItemTemplate: function(itemPromise, recycle) {
    return itemPromise.then(function (item) {
        var data = item.data;
        var template;
        if (data.group.name ===
                ImageData.imageGroups.kittens.name) {
            template = document.querySelector("#wide-template").
                winControl;
        }
        else if (data.group.name ===
                ImageData.imageGroups.portraits.name) {
            template = document.querySelector("#long-template").
                winControl;
        }
        else {
            template = document.
                querySelector("#default-template").winControl;
        }
        return template.render(item.data);
    });
},
</code></pre></li>
</ol>

<p>As you can tell we first wait for the promise to resolve and then take the data and do some custom template selection logic to pick a template from the DOM and then call its <strong><a href='http://msdn.microsoft.com/en-us/library/windows/apps/br229724.aspx' >render</a></strong> method passing in the data object as binding context. You will need to ensure that the styling you use on your template mark-up matches up with the size you return from <strong>itemInfo</strong> as otherwise you might end up with blank spaces in your tiles where the styling doesn't get applied (now, you might want to do this deliberately of course, in which case its totally fine). That's pretty much it!</p>]]></description><link>http://blog1.nerdworks.in/implementingvariablesizedtiles/</link><guid isPermaLink="false">c1d5ceb6-a981-417a-a935-cd69793b21e8</guid><dc:creator><![CDATA[Rajasekharan Vengalil]]></dc:creator><pubDate>Sun, 16 Jun 2013 04:34:25 GMT</pubDate></item><item><title><![CDATA[Debugging existing Windows Store apps]]></title><description><![CDATA[<p>Did you know that you can debug pretty much any installed store app on your machine?  Let's say you want to know exactly why is it that the Windows Mail app acts funny sometimes.  Here's what you'd do:</p>

<ol>
<li><p>Go to the modern desktop and type "Debuggable Package Manager" and launch it.</p>

<blockquote>
  <p><a href='http://blogorama.nerdworks.in/images/_img3.jpg' ><img src='http://blogorama.nerdworks.in/images/_img4.jpg'  alt="clip_image001" title="clip_image001" /></a></p>
</blockquote>

<p>This opens up a powershell window.</p></li>
<li><p>Run <strong>Get-AppxPackage</strong> to list the packages installed and use <strong>Where-Object</strong> to filter for what you're looking for. Since were interested in the mail app we run this:</p>

<pre><code>Get-AppxPackage | Where-Object PackageFullName -like "*commu*"
</code></pre></li>
<li><p>Note the value of the "PackageFullName" property and enable debugging by running this:</p>

<pre><code>Enable-AppxDebug microsoft.windowscommunicationsapps_17.0.1114.318_x64__8wekyb3d8bbwe
</code></pre></li>
<li><p>Now launch the app.  Then launch Visual Studio, hit <strong>Ctrl+Alt+P</strong> and select the instance of <strong>WWAHost.exe</strong> which looks like the app you're interested in.</p>

<blockquote>
  <p><a href='http://blogorama.nerdworks.in/images/_img31.png' ><img src='http://blogorama.nerdworks.in/images/_img32.png'  alt="image" title="image" /></a></p>
</blockquote></li>
<li><p>Debug away!</p>

<blockquote>
  <p><a href='http://blogorama.nerdworks.in/images/_img35.png' ><img src='http://blogorama.nerdworks.in/images/_img36.png'  alt="image" title="image" /></a></p>
</blockquote></li>
</ol>]]></description><link>http://blog1.nerdworks.in/debuggingexistingwindowsstorea/</link><guid isPermaLink="false">18c58b97-6a6e-4b9b-a2e2-6fb2a9f9000d</guid><dc:creator><![CDATA[Rajasekharan Vengalil]]></dc:creator><pubDate>Thu, 23 May 2013 07:29:02 GMT</pubDate></item><item><title><![CDATA[Screen scraping with your browser's JavaScript console]]></title><description><![CDATA[<p>I needed to experiment a bit with <a href='http://www.microsoft.com/en-us/download/details.aspx?id=36804&amp;WT.mc_id=rss_alldownloads_all' >language packs for IE 10</a> the other day and that involved downloading and installing all the available language packs. Unfortunately I couldn't find a single convenient file for download that'd install everything. The language packs were available as separate downloads for each supported language. Like this:</p>

<blockquote>
  <p><a href='http://blogorama.nerdworks.in/images/_img25.png' ><img src='http://blogorama.nerdworks.in/images/_img26.png'  alt="image" title="image" /></a></p>
</blockquote>

<p>This was a problem as I was in no mood to download each file individually and there were 100s of "download" buttons there. I figured I'd see if I can screen scrape the links from the DOM of this page and then write a little script to download all of them in one go. So I fired up an instance of IE and hit F12 to launch the developer tools and used the "Select element by click" button to quickly navigate to the markup associated with a "download" button.</p>

<blockquote>
  <p><a href='http://blogorama.nerdworks.in/images/_img1.gif' ><img src='http://blogorama.nerdworks.in/images/_img2.gif'  alt="select-element" title="select-element" /></a></p>
</blockquote>

<p>As you can tell, all the download buttons are basically anchor tags and the <em>href</em> attribute points to the MSU file for that particular language. Also, you'll note that each such anchor tag has a class called "download" applied on it. So I should be able to fetch all the links by simply iterating through all anchor tags which have the "download" class applied on them. I switched to the "Console" tab in the developer tools window and ran the following script:</p>

<pre><code>document.querySelectorAll("a.download")
</code></pre>

<p>And sure enough this produced a list of all the anchor tags I was interested in. I needed the URL however and not the DOM elements themselves. So I ran this next:</p>

<pre><code>Array.prototype.forEach.call(
    document.querySelectorAll("a.download"),
    function (a) {
        console.log(a.href);
    });
</code></pre>

<p>This produced a list of links such as this (snipped since there are quite a lot of them):</p>

<pre><code>http://download.microsoft.com/download/D/9/A/.../IE10-Windows6.1-LanguagePack-x64-zh-tw.msu 
http://download.microsoft.com/download/D/9/A/.../IE10-Windows6.1-LanguagePack-x64-zu-za.msu 
http://download.microsoft.com/download/D/9/A/.../IE10-Windows6.1-LanguagePack-x86-af-za.msu 
http://download.microsoft.com/download/D/9/A/.../IE10-Windows6.1-LanguagePack-x86-am-et.msu
</code></pre>

<p>If you're wondering why I had to iterate through each element in the list of nodes returned by <em>querySelectorAll</em> via <em>Array.prototype.forEach.call</em> then that's because what <em>querySelectorAll</em> returns isn't a JavaScript array object, i.e., it doesn't inherit from <em>Array.prototype</em>. It is instead a <em>NodeList</em> object which looks a lot like an array! It has numeric properties starting from 0 to N-1 where N is the number of elements returned and it has a <em>length</em> property as well which is equal to N. It turns out that all the <em>Array</em> methods are perfectly capable of dealing with such "array like" objects just as well as genuine, certified JavaScript arrays. Here's an example of what I am talking about:</p>

<pre><code>var notArray = {
    0: "This ",
    1: "is ",
    2: "not ",
    3: "really ",
    4: "an ",
    5: "array.",
    length: 6
};

console.log(Array.prototype.reduce.call(
    notArray,
    function (previous, current) {
        return previous + current;
    },
    ""));
</code></pre>

<p>This snippet prints the following text to the console:</p>

<pre><code>This is not really an array.
</code></pre>

<p>If you take another look at the list of URLs our script printed to the console, you'll notice from the file names that this list includes both x86 files and x64 files. I wanted only x64 files. So, I next changed the script to this:</p>

<pre><code>Array.prototype.forEach.call(
    document.querySelectorAll("a.download[href*=x64]"),
    function (a) {
        console.log(a.href);
    });
</code></pre>

<p>The selector syntax above looks for all anchor tags in the DOM which has a class called "download" applied where the <em>href</em> attribute's value contains the string "x64". I had first implemented this via another call to <em>Array.prototype.filter</em> before learning that CSS3 selector syntax already provides for it! Pretty nifty no? That's pretty much it. I wanted to run a download script for fetching all the files so I slightly modified the script to produce <em><a href='http://www.gnu.org/software/wget/' >wget</a></em> calls like so:</p>

<pre><code>Array.prototype.forEach.call(
    document.querySelectorAll("a.download[href*=x64]"),
    function (a) {
        console.log("wget " + a.href);
    });
</code></pre>

<p>And plonked the output into a batch file and ran it. Mission accomplished!</p>

<p>Now, it turned out that this particular page in question includes the jQuery library as well as can be seen when you pull up the files list from the "Script" tab in the developer console.</p>

<blockquote>
  <p><a href='http://blogorama.nerdworks.in/images/_img27.png' ><img src='http://blogorama.nerdworks.in/images/_img28.png'  alt="image" title="image" /></a></p>
</blockquote>

<p>I could have done the same thing I did above using a slightly terser syntax using jQuery as well. Here's how:</p>

<pre><code>$("a.download[href*=x64]").each(function () {
    console.log("wget " + this.href);
});
</code></pre>

<p>Not having to resort to the <em>Array.prototype</em> weirdness does make the code a lot cleaner doesn't it?</p>]]></description><link>http://blog1.nerdworks.in/screenscrapingwithyourbrowsers/</link><guid isPermaLink="false">d6e2bea4-87bc-4c9b-8768-0619f88a5752</guid><dc:creator><![CDATA[Rajasekharan Vengalil]]></dc:creator><pubDate>Sun, 05 May 2013 05:34:43 GMT</pubDate></item><item><title><![CDATA[Building an Instagram clone - Part 2]]></title><description><![CDATA[<p>In <a href='http://blogorama.nerdworks.in/entry-BuildinganInstagramclonePart1.aspx' >part 1</a> we took a look at some of the UI layout implementation details of the <a href='http://blogorama.nerdworks.in/arbit/InstaFuzz/' >InstaFuzz</a> app.  You can get the source code for the app from <a href='http://sdrv.ms/14B672O' >here</a> if you wish to run it locally.  In this installment we'll take a look at some of the other bits such as how drag/drop, File API, Canvas and Web Workers are used.</p>

<h3 id="dragdrop">Drag/Drop</h3>

<p>One of the things that <em>InstaFuzz</em> supports is the ability to drag and drop image files directly on to the big blackish/blue box. Support for this is enabled by handling the "drop" event on the CANVAS element. When a file is dropped onto an HTML element the browser fires the "drop" event on that element and passes in a <a href='http://msdn.microsoft.com/library/ie/ms535861.aspx' ><em>dataTransfer</em></a> object which contains a <a href='http://msdn.microsoft.com/en-US/library/ie/hh772716.aspx' ><em>files</em></a> property that contains a reference to the list of files that were dropped. Here's how this is handled in the app ("picture" is the ID of the CANVAS element on the page):</p>

<pre><code>var pic = $("#picture");
pic.bind("drop", function (e) {
    suppressEvent(e);
    var files = e.originalEvent.dataTransfer.files;
    // more code here to open the file
});
pic.bind("dragover", suppressEvent).bind("dragenter", suppressEvent);
function suppressEvent(e) {
    e.stopPropagation();
    e.preventDefault();
}
</code></pre>

<p>The <em>files</em> property is a collection of <a href='http://msdn.microsoft.com/en-US/library/ie/hh772305.aspx' ><em>File</em></a> objects that can then subsequently be used with the File API to access the file contents (covered in the next section). We also handle the <em>dragover</em> and <em>dragenter</em> events and basically prevent those events from propagating to the browser thereby preventing the browser from handling the file drop. IE for instance might unload the current page and attempt to open the file directly otherwise.</p>

<h3 id="fileapi">File API</h3>

<p>Once the file has been dropped, the app attempts to open the image and render it in the canvas. It does this by using the <a href='http://msdn.microsoft.com/en-us/library/ie/hh673542' (v=vs.85).aspx">File API</a>. The File API is a W3C specification that allows web apps to programmatically access files from the local file system in a secure fashion. In <em>InstaFuzz</em> we use the <a href='http://msdn.microsoft.com/en-us/library/ie/hh772310' (v=vs.85).aspx"><em>FileReader</em></a> object to read the file contents as a <a href='http://msdn.microsoft.com/en-us/library/ie/cc848897.aspx' >data URL</a> string like so using the <a href='http://msdn.microsoft.com/en-us/library/ie/hh772313' (v=vs.85).aspx"><em>readAsDataURL</em></a> method:</p>

<pre><code>var reader = new FileReader();
reader.onloadend = function (e2) {
    drawImageToCanvas(e2.target.result);
};
reader.readAsDataURL(files[0]);
</code></pre>

<p>Here, <em>files</em> is the collection of <em>File</em> objects retrieved from the function handling the "drop" event on the CANVAS element. Since we are interested only in a single file we simply pick the first file from the collection and ignore the rest if there are any. The actual file contents are loaded asynchronously and once the load completes, the <a href='http://msdn.microsoft.com/en-us/library/ie/hh772337' (v=vs.85).aspx"><em>onloadend</em></a> event is fired where we get the file contents as a data URL which we then subsequently draw on to the canvas.</p>

<h3 id="renderingthefilters">Rendering the filters</h3>

<p>Now the core functionality here is of course the application of the filters. In order to be able to apply the filter to the image we need a way to access the individual pixels from the image. And before we can access the pixels we need to have actually rendered the image on to our canvas. So let's first take a look at the code that renders the image that the user picked on to the canvas element.</p>

<h4 id="renderingimagesontothecanvas">Rendering images on to the canvas</h4>

<p>The canvas element supports the rendering of <em>Image</em> objects via the <a href='http://msdn.microsoft.com/en-us/library/ie/ff975414' (v=vs.85).aspx"><em>drawImage</em></a> method. To load up the image file in an <em>Image</em> instance, <em>InstaFuzz</em> uses the following utility routine:</p>

<pre><code>App.Namespace.define("InstaFuzz.Utils", {
    loadImage: function (url, complete) {
        var img = new Image();
        img.src = url;
        img.onload = function () {
            complete(img);
        };
    }
});
</code></pre>

<p>This allows the app to load up image objects from a URL using code such as the following:</p>

<pre><code>function drawImageToCanvas(url) {
    InstaFuzz.Utils.loadImage(url, function (img) {
        // save reference to source image
        sourceImage = img;

        mainRenderer.clearCanvas();
        mainRenderer.renderImage(img);

        // load image filter previews
        loadPreviews(img);
    });
}
</code></pre>

<p>Here, <em>mainRenderer</em> is an instance created from the <em>FilterRenderer</em> constructor function defined in <em>filter-renderer.js</em>. The app uses <em>FilterRenderer</em> objects to manage canvas elements - both in the preview pane as well as the main canvas element on the right. The <em>renderImage</em> method on the <em>FilterRenderer</em> has been defined like so:</p>

<pre><code>FilterRenderer.prototype.renderImage = function (img) {
    var imageWidth = img.width;
    var imageHeight = img.height;
    var canvasWidth = this.size.width;
    var canvasHeight = this.size.height;
    var width, height;

    if ((imageWidth / imageHeight) &gt;= (canvasWidth / canvasHeight)) {
        width = canvasWidth;
        height = (imageHeight * canvasWidth / imageWidth);
    } else {
        width = (imageWidth * canvasHeight / imageHeight);
        height = canvasHeight;
    }

    var x = (canvasWidth - width) / 2;
    var y = (canvasHeight - height) / 2;
    this.context.drawImage(img, x, y, width, height);
};
</code></pre>

<p>That might seem like a lot of code but all it does ultimately is to figure out the best way to render the image in the available screen area considering the aspect ratio of the image. The key piece of code that actually renders the image on the canvas occurs on the last line of the method. The <em>context</em> member refers to the 2D context acquired from the canvas object by calling its <a href='http://msdn.microsoft.com/en-us/library/ie/ff975238' (v=vs.85).aspx"><em>getContext</em></a> method.</p>

<h4 id="fetchingpixelsfromthecanvas">Fetching pixels from the canvas</h4>

<p>Now that the image has been rendered we will need access to the individual pixels in order to apply all the different filters that are available. This is easily acquired by calling <a href='http://msdn.microsoft.com/en-us/library/ie/ff975418' (v=vs.85).aspx"><em>getImageData</em></a> on the canvas's context object. Here's how <em>InstaFuzz</em> calls this from <em>instafuzz.js</em>.</p>

<pre><code>var imageData = renderer.context.getImageData(
    0, 0,
    renderer.size.width,
    renderer.size.height);
</code></pre>

<p>The object returned by <em>getImageData</em> provides access to the individual pixels via its <em>data</em> property which in turn is an array like object that contains a collection of byte values where each value represents the color rendered for a single channel of a single pixel. Each pixel is represented using 4 bytes that specify values for the red, green, blue and alpha channels. It also has a <a href='http://msdn.microsoft.com/en-us/library/ie/ff974921' (v=vs.85).aspx"><em>length</em></a> property that returns the length of the buffer. If you have a 2D co-ordinate you can easily transform that into an index into this array using code such as the following. The color intensity values of each channel ranges from 0 through 255. Here's the utility function from <em>filters.js</em> that accepts as input an image data object along with 2D coordinates for the pixel the caller is interested in and returns an object containing the color values:</p>

<pre><code>function getPixel(imageData, x, y) {
    var data = imageData.data, index = 0;

    // normalize x and y and compute index
    x = (x &lt; 0) ? (imageData.width + x) : x;
    y = (y &lt; 0) ? (imageData.height + y) : y;
    index = (x + y * imageData.width) * 4;

    return {
        r: data[index],
        g: data[index + 1],
        b: data[index + 2]
    };
}
</code></pre>

<h4 id="applyingthefilters">Applying the filters</h4>

<p>Now that we have access to the individual pixels, applying the filter is fairly straightforward. Here, for instance is the function that applies a weighted grayscale filter on the image. It simply picks intensities from the red, green and blue channels and sums them up after applying a multiplication factor on each channel and then assigns the result for all 3 channels.</p>

<pre><code>// "Weighted Grayscale" filter
Filters.addFilter({
    name: "Weighted Grayscale",
    apply: function (imageData) {
        var w = imageData.width, h = imageData.height;
        var data = imageData.data;
        var index;
        for (var y = 0; y &lt; h; ++y) {
            for (var x = 0; x &lt; w; ++x) {
                index = (x + y * imageData.width) * 4;
                var luminance = parseInt((data[index + 0] * 0.3) +
                                         (data[index + 1] * 0.59) +
                                         (data[index + 2] * 0.11));
                        data[index + 0] = data[index + 1] =
                    data[index + 2] = luminance;
            }

            Filters.notifyProgress(imageData, x, y, this);
        }

        Filters.notifyProgress(imageData, w, h, this);
    }
});
</code></pre>

<p>Once the filter has been applied we can have that reflected on the canvas by calling the <a href='http://msdn.microsoft.com/en-us/library/ie/ff975423' (v=vs.85).aspx"><em>putImageData</em></a> method passing in the modified image data object. While the weighted grayscale filter is fairly simple most of the other filters use an image processing technique known as <em>convolution</em>. The code for all the filters is available in <em>filters.js</em> and the convolution filters were ported from the C code available <a href='http://lodev.org/cgtutor/filtering.html' >here</a>.</p>

<h3 id="webworkers">Web Workers</h3>

<p>As you might imagine doing all this number crunching to apply the filters can potentially take a long time to complete. The <em>motion blur</em> filter for instance uses a 9x9 filter matrix for computing the new value for every single pixel and is in fact the most CPU intensive filter among them all. If we were to do all this computation on the UI thread of the browser then the app would essentially freeze every time a filter was being applied. To provide a responsive user experience the app delegates the core image processing tasks to a background script using the support for W3C <a href='http://msdn.microsoft.com/en-us/library/ie/hh673568' (v=vs.85).aspxhttp:/msdn.microsoft.com/en-us/library/ie/hh673568(v=vs.85">Web Workers</a>.aspx) in modern browsers.</p>

<p>Web workers allow web applications to have scripts run in a background task that executes in parallel along with the UI thread. Communication between the worker and the UI thread is accomplished by passing messages using the <a href='http://msdn.microsoft.com/library/ie/cc197015.aspx' ><em>postMessage</em></a> API. On both ends (i.e. the UI thread and the worker) this manifests as an event notification that you can handle. You can only pass "data" between workers and the UI thread, i.e., you cannot pass anything that has to do with the user interface - you cannot for instance, pass DOM elements to the worker from the UI thread.</p>

<p>In <em>InstaFuzz</em> the worker is implemented in the file <em>filter-worker.js</em>. All it does in the worker is handle the <a href='http://msdn.microsoft.com/library/ie/cc197057.aspx' ><em>onmessage</em></a> event and apply a filter and then pass the results back via <em>postMessage</em>. As it turns out, even though we cannot pass DOM elements (which means we cannot just hand a CANVAS element to the worker to have the filter applied) we can in fact pass the image data object as returned by the <em>getImageData</em> method that we discussed earlier. Here's the filter processing code from <em>filter-worker.js</em>:</p>

<pre><code>importScripts("ns.js", "filters.js");

var tag = null;
onmessage = function (e) {
    var opt = e.data;
    var imageData = opt.imageData;
    var filter;

    tag = opt.tag;
    filter = InstaFuzz.Filters.getFilter(opt.filterKey);

    var start = Date.now();
    filter.apply(imageData);
    var end = Date.now();

    postMessage({
        type: "image",
        imageData: imageData,
        filterId: filter.id,
        tag: tag,
        timeTaken: end - start
    });
}
</code></pre>

<p>The first line pulls in some script files that the worker depends on by calling <a href='http://msdn.microsoft.com/library/ie/hh772875.aspx' ><em>importScripts</em></a>. This is similar to including a JavaScript file in a HTML document using the SCRIPT tag. Then we set up a handler for the <em>onmessage</em> event in response to which we simply apply the filter in question and pass the result back to the UI thread by calling <em>postMessage</em>. Simple enough!</p>

<p>The code that initializes the worker is in <em>instafuzz.js</em> and looks like this:</p>

<pre><code>var worker = new Worker("js/filter-worker.js");
</code></pre>

<p>Not much is it? When a message is sent by the worker to the UI thread we handle it by specifying a handler for the <em>onmessage</em> event on the worker object. Here's how this is done in <em>InstaFuzz</em>:</p>

<pre><code>worker.onmessage = function (e) {
    var isPreview = e.data.tag;
    switch (e.data.type) {
        case "image":
            if (isPreview) {
                previewRenderers[e.data.filterId].
                    context.putImageData(
                        e.data.imageData, 0, 0);
            } else {
                mainRenderer.context.putImageData(
                    e.data.imageData, 0, 0);
            }

            break;
        // more code here
    }
};
</code></pre>

<p>The code should be fairly self-explanatory. It simply picks the image data object sent by the worker and applies it to the relevant canvas's context object causing the modified image to be rendered on screen. Scheduling a filter for conversion with the worker is equally simple. Here's the routine that performs this function in <em>InstaFuzz</em>:</p>

<pre><code>function scheduleFilter(filterId,
                        renderer,
                        img, isPreview,
                        resetRender) {
    if (resetRender) {
        renderer.clearCanvas();
        renderer.renderImage(img);
    }

    var imageData = renderer.context.getImageData(
        0, 0,
        renderer.size.width,
        renderer.size.height);

    worker.postMessage({
        imageData: imageData,
        width: imageData.width,
        height: imageData.height,
        filterKey: filterId,
        tag: isPreview
    });
}
</code></pre>

<h3 id="inconclusion">In conclusion</h3>

<p>We saw that fairly intricate user experiences are possible today with HTML5 technologies such as Canvas, Drag/Drop, File API and Web Workers. Support for all of these technologies is quite good in pretty much all modern browsers. One thing that we did not address here is the question of making the app compatible with older browsers. That, truth be told, is a non-trivial but necessary task that I will hopefully be able to talk about in a future article.</p>]]></description><link>http://blog1.nerdworks.in/buildinganinstagramclonepart2/</link><guid isPermaLink="false">da5e8c61-86a6-459f-95d8-db52f520ec60</guid><dc:creator><![CDATA[Rajasekharan Vengalil]]></dc:creator><pubDate>Fri, 19 Apr 2013 04:20:27 GMT</pubDate></item><item><title><![CDATA[Building an Instagram clone - Part 1]]></title><description><![CDATA[<h3 id="introduction">Introduction</h3>

<p>When I started out on this app I was only really just interested in seeing if the web platform had really evolved to a point where an app like the hugely popular <a href='http://instagram.com/' >Instagram</a> app could be built using just HTML, JavaScript and CSS. As it turns out we can in fact do exactly that. This article walks you through the technologies that make this possible and shows how it is entirely feasible today to build interoperable web applications that provide a great user experience no matter what brand of browser the user is running.</p>

<p>If you happen to be one of the two or so people who have not heard about <em>Instagram</em> then you might be pleased to hear that it is a hugely popular photo sharing and social networking service that allows you to take pictures, apply interesting digital filters on them and share them with the world to see. The service got so popular that it was <a href='http://finance.fortune.cnn.com/2012/04/09/breaking-facebook-buying-instagram-for-1-billion/' >acquired by Facebook</a> for a bag full of cash and stock in April of 2012.</p>

<p><a href='http://blogorama.nerdworks.in/arbit/InstaFuzz/' >InstaFuzz</a> is the name of the app I put together and while I don't expect to be acquired by Facebook or anybody else for a billion green it does however make the case that an app such as this one can be built using only standards compliant web technologies such as Canvas, File API, Drag/Drop, Web Workers, ES5 and CSS3 and still manage to run well on modern browsers such as Internet Explorer 10, Google Chrome and Firefox.</p>

<h3 id="abouttheapp">About the app</h3>

<p>If you'd like to take a look at the app, then here's where it is hosted at:</p>

<blockquote>
  <p><a href='http://blogorama.nerdworks.in/arbit/InstaFuzz/' >http://blogorama.nerdworks.in/arbit/InstaFuzz/</a></p>
</blockquote>

<p>You can download the source and run locally from <a href='http://sdrv.ms/14B672O' >here</a>.  While this is a Visual Studio 2012 project there really isn't any server code or anything like that.  You can use your favorite editor to look at the source and run it from the file system if you are so inclined.</p>

<p>As soon as you load it up, you're presented with a screen that looks like this:</p>

<blockquote>
  <p><a href='http://blogorama.nerdworks.in/images/_img1.jpg' ><img src='http://blogorama.nerdworks.in/images/_img2.jpg'  alt="clip_image002" title="clip_image002" /></a></p>
</blockquote>

<p>The idea is that you can load up a photograph into the app either by clicking on the big red "Add" button on the bottom left hand corner or drag and drop an image file into the blackish/blue area on the right. Once you do that you get something that looks like this:</p>

<blockquote>
  <p><a href='http://blogorama.nerdworks.in/images/_img15.png' ><img src='http://blogorama.nerdworks.in/images/_img16.png'  alt="clip_image003" title="clip_image003" /></a></p>
</blockquote>

<p>You'll note that a list of digital filters are listed on the left of the screen showing a preview of what the image would look like if you were to apply the said filter. Applying a filter is a simple matter of clicking on one of the filter previews on the left. Here's what it looks like after applying the "Weighted Grayscale" filter followed by a "Motion Blur". As you can tell filters are <em>additive</em> - as you keep clicking on filters, they are applied on top of what was applied earlier:</p>

<blockquote>
  <p><a href='http://blogorama.nerdworks.in/images/_img17.png' ><img src='http://blogorama.nerdworks.in/images/_img18.png'  alt="clip_image004" title="clip_image004" /></a></p>
</blockquote>

<p>Let's next take a look at how the UI layout has been put together.</p>

<h3 id="uilayout">UI Layout</h3>

<p>The HTML markup is actually so little that I can actually reproduce the contents of the BODY tag in its entirety here (excluding the SCRIPT includes):</p>

<pre><code>&lt;header&gt;
    &lt;div id="title"&gt;InstaFuzz&lt;/div&gt;
&lt;/header&gt;
&lt;section id="container"&gt;
    &lt;canvas id="picture" width="650" height="565"&gt;&lt;/canvas&gt;
    &lt;div id="controls"&gt;
        &lt;div id="filters-list"&gt;&lt;/div&gt;
        &lt;button id="loadImage"&gt;Add&lt;/button&gt;
        &lt;input type="file" id="fileUpload"
               style="display: none;"
               accept="image/gif, image/jpeg, image/png" /&gt;
    &lt;/div&gt;
&lt;/section&gt;

&lt;!-- Handlebar template for a filter UI button --&gt;
&lt;script id="filter-template" type="text/x-handlebars-template"&gt;
    &lt;div class="filter-container" data-filter-id="{{filterId}}"&gt;
        &lt;div class="filter-name"&gt;{{filterName}}&lt;/div&gt;
        &lt;canvas class="filter-preview" width="128" height="128"&gt;&lt;/canvas&gt;
    &lt;/div&gt;
&lt;/script&gt;
</code></pre>

<p>There's nothing much going on here. Pretty much everything should be standard fare. I will however draw attention to the fact that I am using the <a href='http://handlebarsjs.com/' >Handlebars</a> JavaScript templating system here for rendering the markup for the list of filters on the left of the screen. The template markup is declared in the HTML file (the SCRIPT tag in the snippet shown above) and then used from JavaScript. The template markup is then bound to a JavaScript object that supplies the values for handlebars expressions such as <em>{{filterId}}</em> and <em>{{filterName}}.</em> Here's the relevant piece of JS from the app with a bit of DOM manipulation help from <a href='http://jquery.com/' >jQuery</a>:</p>

<pre><code>var templHtml = $("#filter-template").html(),
    template = Handlebars.compile(templHtml),
    filtersList = $("#filters-list");
var context = {
    filterName: filter.name,
    filterId: index
};

filtersList.append(template(context));
</code></pre>

<p>As you can tell from the HTML markup all the filter preview boxes feature a CANVAS tag as does the big box on the right where the final output is rendered. We'll go into a bit more detail later on in the article as to how canvas technology is used to achieve these effects.</p>

<p>The app also uses <a href='http://msdn.microsoft.com/en-us/library/ie/ms530757' (v=vs.85).aspx">CSS3 @font-face</a> fonts to render the text in the header and the "Add" button. The fonts have been taken from the excellent <a href='http://fontsquirrel.com/' >Font Squirrel</a> site and here's what the declaration looks like:</p>

<pre><code>@font-face {
    font-family: 'TizaRegular';
    src: url('fonts/tiza/tiza-webfont.eot');
    src: url('fonts/tiza/tiza-webfont.eot?#iefix')
           format('embedded-opentype'),
         url('fonts/tiza/tiza-webfont.woff') format('woff'),
         url('fonts/tiza/tiza-webfont.ttf') format('truetype'),
         url('fonts/tiza/tiza-webfont.svg#TizaRegular') format('svg');
    font-weight: normal;
    font-style: normal;
}
</code></pre>

<p>This directive causes the user agent to embed the font in the page and make it available under the name assigned to the <em>font-family</em> rule which in this case is "TizaRegular". After this we can assign this font to any CSS <em>font-family</em> rule like how we normally do. In <em>InstaFuzz</em> I use the following rule to assign the font to the header element:</p>

<pre><code>font-family: TizaRegular, Cambria, Cochin, Georgia, Times,
   "Times New Roman", serif;
</code></pre>

<p>You might also have noticed that there is a subtle shadow being dropped on the page by the container element.</p>

<blockquote>
  <p><a href='http://blogorama.nerdworks.in/images/_img19.png' ><img src='http://blogorama.nerdworks.in/images/_img20.png'  alt="clip_image001[4]" title="clip_image001[4]" /></a></p>
</blockquote>

<p>This is made possible using the <a href='http://msdn.microsoft.com/library/ie/jj127322.aspx' >CSS3 box-shadow</a> rule and here's how it's used in <em>InstaFuzz</em>.</p>

<pre><code>-moz-box-shadow: 1px 0px 4px #000000, -1px -1px 4px #000000;
-webkit-box-shadow: 1px 0px 4px #000000, -1px -1px 4px #000000;
box-shadow: 1px 0px 4px #000000, -1px -1px 4px #000000;
</code></pre>

<p>This causes the browser to render a shadow around the relevant element. Each comma separated section in the value specifies the following attributes of the shadow:</p>

<ol>
<li><p>Horizontal offset</p></li>
<li><p>Vertical offset</p></li>
<li><p>Spread distance - positive values have the effect of softening the shadow</p></li>
<li><p>Shadow color</p></li>
</ol>

<p>One can specify multiple shadow values separated by comma as in fact has been done above. Note that I've also specified the shadow using vendor prefix syntax for Firefox and Chrome/Safari using the <em>moz</em> and <em>webkit</em> prefixes. This causes the shadow to continue to work in versions of those browsers where support for this capability was provided using the vendor prefixed version of the rule. Note that the W3C version of the rule - <em>box-shadow</em> - is specified last. This is done deliberately to ensure that in case the browser supports both the forms then only the W3C behavior is actually applied to the page.</p>

<p>One often finds that web developers either fail to include vendor prefixed version of a given CSS3 rule for all the browsers that support that rule and/or fail to include the W3C version as well. Often developers just put the <em>webkit</em> version of the rule ignoring other browsers and the W3C standard version. This causes two problems - [1] poor user experience for users who are using non-webkit browsers and [2] it ends up resulting in webkit becoming a de-facto standard for the web. Ideally we want W3C to be driving the future of the web and not one specific browser implementation. So here are some things to remember when playing with experimental implementations of CSS features:</p>

<ol>
<li><p>Use vendor prefixed versions of CSS rules by all means but remember to specify the rule for all supported browsers and not just the one that you happen to be testing the page in (if you're using <a href='http://www.microsoft.com/visualstudio/eng/products/visual-studio-express-products' >Visual Studio</a> to edit your CSS then you might be interested in the supremely excellent extension for Visual Studio called <a href='http://visualstudiogallery.msdn.microsoft.com/6ed4c78f-a23e-49ad-b5fd-369af0c2107f' ><em>Web Essentials</em></a> that makes the job of managing vendor prefixes about as simple as it can possibly get).</p></li>
<li><p>Remember to specify the W3C version of the rule as well.</p></li>
<li><p>Remember to order the occurrence of the rules so that the W3C version shows up last. This is to allow clients that support both the vendor prefixed version and the W3C version to use the W3C specified semantics for the rule.</p></li>
</ol>

<p>That's all for now.  In the <a href='http://blogorama.nerdworks.in/entry-BuildinganInstagramclonePart2.aspx' >next and final post</a> in this series we'll take a look at how the app supports drag/drop of files, the use of File API, how the filters themselves work and how we prevent the UI thread from freezing by delegating the core number crunching work to web workers.</p>]]></description><link>http://blog1.nerdworks.in/buildinganinstagramclonepart1/</link><guid isPermaLink="false">0a66e48b-f201-4e85-a4c3-81974b5a1705</guid><dc:creator><![CDATA[Rajasekharan Vengalil]]></dc:creator><pubDate>Wed, 17 Apr 2013 12:01:26 GMT</pubDate></item></channel></rss>