<?xml version="1.0" encoding="UTF-8"?>
<rss version='2.0' 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">
  <channel>
    <title>Zdeslav Vojkovic</title>
    <description>freelance software dev</description>
    <link>https://zdeslav.silvrback.com/feed</link>
    <atom:link href="https://zdeslav.silvrback.com/feed" rel="self" type="application/rss+xml"/>
    <category domain="zdeslav.silvrback.com">Content Management/Blog</category>
    <language>en-us</language>
      <pubDate>Wed, 29 Apr 2009 21:47:00 +0100</pubDate>
    <managingEditor>zdeslav.vojkovic@gmail.com (Zdeslav Vojkovic)</managingEditor>
      <item>
        <guid>http://vojkovic.net/converting-pascal-case-to-sentences-using-regular-expression#6436</guid>
          <pubDate>Wed, 29 Apr 2009 21:47:00 +0100</pubDate>
        <link>http://vojkovic.net/converting-pascal-case-to-sentences-using-regular-expression</link>
        <title>Converting Pascal case to sentences using regular expression</title>
        <description></description>
        <content:encoded><![CDATA[<p>Here&#39;s another reminder to myself: a nifty one-liner regex which transforms Pascal/camel case string into a sentence. I always write it from scratch so I decided to put it here. It might also be a good idea to write it as an extension method for string class.</p>
<div class="highlight"><pre><span></span><span class="k">static</span> <span class="kt">string</span> <span class="nf">PascalCaseToSentence</span><span class="p">(</span><span class="kt">string</span> <span class="n">input</span><span class="p">)</span>
<span class="p">{</span>
   <span class="k">return</span> <span class="n">Regex</span><span class="p">.</span><span class="n">Replace</span><span class="p">(</span><span class="n">input</span><span class="p">,</span> <span class="s">&quot;.[A-Z]&quot;</span><span class="p">,</span> 
                        <span class="n">m</span> <span class="p">=&gt;</span> <span class="n">m</span><span class="p">.</span><span class="n">ToString</span><span class="p">()[</span><span class="m">0</span><span class="p">]</span> <span class="p">+</span> <span class="s">&quot; &quot;</span> <span class="p">+</span> 
                             <span class="kt">char</span><span class="p">.</span><span class="n">ToLower</span><span class="p">(</span><span class="n">m</span><span class="p">.</span><span class="n">ToString</span><span class="p">()[</span><span class="m">1</span><span class="p">]));</span>
<span class="p">}</span>
</pre></div>
<p>Following line</p>
<div class="highlight"><pre><span></span><span class="n">Console</span><span class="p">.</span><span class="n">Out</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">PascalCaseToSentence</span><span class="p">(</span><span class="s">&quot;MyVeryLongSentence&quot;</span><span class="p">));</span>
</pre></div>
<p>will result in:</p>
<div class="highlight"><pre><span></span>My very long sentence
</pre></div>]]></content:encoded>
      </item>
      <item>
        <guid>http://vojkovic.net/using-timestamps-in-batch-files#6206</guid>
          <pubDate>Tue, 23 Sep 2008 23:20:00 +0100</pubDate>
        <link>http://vojkovic.net/using-timestamps-in-batch-files</link>
        <title>Using timestamps in batch files</title>
        <description> </description>
        <content:encoded><![CDATA[<p>After failing many times before, I have finally found out how to create user-friendly timestamps in batch scripts. For many people this may be yesterday&#39;s news, but I am posting it here as a reminder to myself and in hope that it might help someone else.<br>
Windows command shell (sometimes erroneously called DOS prompt) provides <code>%DATE%</code> and <code>%TIME%</code> environment variables which, unsurprisingly, return current date and time. On the other hand, it is possible to extract a group of characters from any environment variable using following syntax:</p>
<div class="highlight"><pre><span></span>%VARIABLE:~START,LENGTH%
</pre></div>
<p>where <code>VARIABLE</code> is the name of the environment variable, <code>START</code> is the zero-based index of the first character to be retrieved and <code>LENGTH</code> is the length of the string to be retrieved. E.g. <code>%USERNAME:~1,3%</code> would return second, third and fourth letter of the current user&#39;s name.<br>
So in order to create a time stamp, we define a temporary environment variable with value defined by parts of  <code>%DATE%</code>  and <code>%TIME%</code>. Then we use this variable to create file names. Please note that the exact indices will depend on your time format settings. In a batch file or a script it would look like this:</p>
<div class="highlight"><pre><span></span><span class="nb">set</span> <span class="nv">backup_time</span><span class="o">=</span>%date:~10,4%_%date:~7,2%_%date:~4,2%
<span class="nb">set</span> <span class="nv">backup_detailed</span><span class="o">=</span>%backup_time%__%time:~0,2%_%time:~3,2%_%time:~6,2%
md d:<span class="se">\z</span>zz_%backup_time%
md d:<span class="se">\z</span>zz_%backup_detailed%
</pre></div>
<p>Output of the batch file is:</p>
<div class="highlight"><pre><span></span>D:\tmp&gt;set backup_time=2008_09_23
D:\tmp&gt;set backup_detailed=2008_09_23__15_50_31
D:\tmp&gt;md d:\zzz_2008_09_23
D:\tmp&gt;md d:\zzz_2008_09_23__15_50_31
</pre></div>]]></content:encoded>
      </item>
      <item>
        <guid>http://vojkovic.net/automatic-memory-management-myth#6201</guid>
          <pubDate>Thu, 17 Apr 2008 22:47:00 +0100</pubDate>
        <link>http://vojkovic.net/automatic-memory-management-myth</link>
        <title>Automatic memory management myth</title>
        <description>and why GC is not enough</description>
        <content:encoded><![CDATA[<p><em>NOTE: C+11 standard introduces new smart pointer types to replace <code>std::auto_ptr&lt;&gt;</code> and boost versions.</em></p>

<p>At a recent interview, a job candidate ticked me off when we reached one of the topics which is very dear to my heart. We are mostly C++ shop, but many of our job applicants come from .NET background. So during the interview we came to the topic of explicit memory management vs. automatic memory management (a.k.a. garbage collection), and the candidate (with substantial C++ experience) started ranting about how troublesome explicit memory management is, with all this extra caution required to call <code>delete</code> for every <code>new</code>, as opposed to simplicity of platforms which support garbage collection.</p>

<p>Well, if you happen to agree with this guy, then you are not doing it right :)</p>

<p>Although <a href="http://en.wikipedia.org/wiki/Boehm_garbage_collector">GC can be done in C++</a>, that is not the point: the main issue here is that many people are still writing C code which they compile with C++ compiler.</p>

<p>Modern C++ is very different beast from C, and as such provides different patterns for common problems. The concept of <a href="http://en.wikipedia.org/wiki/Smart_pointer">smart pointers</a> alleviates much of the manual work that is otherwise needed to handle allocations correctly. The STL comes with <a href="http://en.wikipedia.org/wiki/Auto_ptr"><code>std::auto_ptr</code></a>, which makes it trivial to ensure the proper deallocation in face of exceptions or normal scope exit. </p>

<p>If you need to handle an array, STL is your friend again: there is <code>std::vector</code>, and it also takes care of transparent resizing/reallocation, so you don&#39;t have to worry about it.</p>

<p>What is (currently) lacking in STL is an implementation of smart pointer with shared semantics. However, there is plenty of such implementations, most notably the excellent <a href="http://www.boost.org/doc/libs/1_35_0/libs/smart_ptr/shared_ptr.htm"><code>boost::shared_ptr</code></a>, which is a reference counted implementation that makes sure that the allocated object is deleted when the last reference to it goes away. It will also become a part of <a href="http://en.wikipedia.org/wiki/C%2B%2B0x">new C++ standard</a>, and some vendors <a href="http://msdn2.microsoft.com/en-us/library/bb982026.aspx">support it already</a>.</p>

<p>One issue where GC (at least when implemented with <a href="http://www.brpreiss.com/books/opus5/html/page424.html">mark-and-sweep</a> algorithms, like in Java or .NET) has an advantage over explicit memory management is the problem of circular references, i.e. when two objects hold a reference to each other. This is really a problem for reference counted implementations, but most cases can be handled by judiciously using <a href="http://www.boost.org/doc/libs/1_35_0/libs/smart_ptr/weak_ptr.htm">weak pointers</a>, and such an implementation is provided by <a href="http://www.boost.org/doc/libs/1_35_0/libs/smart_ptr/weak_ptr.htm"><code>boost::weak_ptr</code></a>, which will also be included in new C++ standard.</p>

<p>If you think that this problem is a &#39;deal breaker&#39; to prefer platforms with GC, bear in mind that the similar problem also exists even there, and that there is a very good reason why Java and .NET both provide <code>WeakReference</code> class (e.g. see <a href="http://www-128.ibm.com/developerworks/java/library/j-jtp11225/">here</a> for one such problem).</p>

<p>What I find particularly annoying is the fact that the only resource which is deemed to be important enough to be automatically handled is memory. For everything else, like database connections, kernel or GDI objects, clients have to explicitly call <code>Dispose()</code>, and it seems that most developers on managed platforms have no problems with that. A coworker who recently switched from VB to .NET found out hard way that you really have to call <code>Dispose()</code> on your bitmaps. What happens when you have to share such an object between multiple clients (yes, I know that there are idioms to avoid this resource sharing)? Who calls <code>Dispose()</code>? Can you be sure that it has not already been disposed? </p>

<p>GC is a nice and useful thing, but it is not a silver bullet, and has its own problems.</p>

<p>Deterministic finalization is what makes it possible in C++ to treat all resources equally: whether it is a memory, a bitmap, a COM object or database connection, with a simple wrapper around it, you can rest assured that the object will be properly released as soon as it is not referenced anymore. Actually, smart pointers are only a special case of what is one of the most powerful concepts in C++, although a bit unfortunately named: <a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization">RAII</a></p>

<p>If you are a C++ programmer, please take some time to learn C++ idioms. It will make you a better programmer, and your code a better code.</p>

<p>When you program in C++, make sure it really is C++ and not C.</p>
]]></content:encoded>
      </item>
  </channel>
</rss>