tag:blogger.com,1999:blog-31243370824720864722024-02-08T04:43:36.031-08:00The Renaissance ProgrammerAdventures in many languagesMatt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.comBlogger17125tag:blogger.com,1999:blog-3124337082472086472.post-60895331634492817912019-10-09T14:44:00.000-07:002019-10-09T14:47:03.738-07:00UNIXem versions 1.9.2 - 1.11.4 pushed to GitHub<span style="font-family: "times" , "times new roman" , serif;"><b>UNIXem</b> - <b>Unix</b>-<b>em</b>ulation library for Windows - is a </span><span style="background-color: white; color: #24292e; font-family: "times" , "times new roman" , serif; font-size: 16px;">small and simple library that provides emulation of several popular Unix API functions on the Windows platform. Its primary purpose is to assist Windows programmers who are porting to Unix or are writing multi-platform code.</span><br />
<span style="font-family: "times" , "times new roman" , serif;"><br /></span>
<span style="font-family: "times" , "times new roman" , serif;">Prior to version 1.9.2 it was available on the (currently moribund; soon to be turned around) Synesis Software website at <a href="http://synesis.com.au/software/unixem.html">http://synesis.com.au/software/unixem.html</a>; henceforth newer versions will be provided at <a href="https://github.com/synesissoftware/UNIXem">https://github.com/synesissoftware/UNIXem</a>.</span><br />
<span style="font-family: "times" , "times new roman" , serif;"><br /></span>
<span style="font-family: "times" , "times new roman" , serif;">A few days ago I received a pull request notification on UNIXem, to correct a syntax header in one of the headers. This prompted me to update and reconcile the GitHub repo with the internal source control. This has now been published, and all updates over the last 8+ years are now available. The significant changes are:</span><br />
<span style="font-family: "times" , "times new roman" , serif;"><br /></span>
<br />
<ul>
<li><span style="font-family: "times" , "times new roman" , serif;">added </span><span style="font-family: "courier new" , "courier" , monospace;">mkdtemp()</span><span style="font-family: "times" , "times new roman" , serif;">;</span></li>
<li><span style="font-family: "times" , "times new roman" , serif;">added </span><span style="font-family: "courier new" , "courier" , monospace;">mkstemp()</span><span style="font-family: "times" , "times new roman" , serif;">;</span></li>
<li><span style="font-family: "times" , "times new roman" , serif;">added </span><span style="font-family: "courier new" , "courier" , monospace;">unixem_timegm()</span><span style="font-family: "times" , "times new roman" , serif;"> / </span><span style="font-family: "courier new" , "courier" , monospace;">timegm()</span><span style="font-family: "times" , "times new roman" , serif;">;</span></li>
<li><span style="font-family: "times" , "times new roman" , serif;">modified </span><span style="font-family: "courier new" , "courier" , monospace;">glob()</span><span style="font-family: "times" , "times new roman" , serif;">:</span></li>
<ul>
<li><span style="font-family: "courier new" , "courier" , monospace;">GLOB_ONLYFILE</span><span style="font-family: "times" , "times new roman" , serif;"> => </span><span style="font-family: "courier new" , "courier" , monospace;">GLOB_ONLYREG</span><span style="font-family: "times" , "times new roman" , serif;">;</span></li>
<li><span style="font-family: "courier new" , "courier" , monospace;">glob()</span><span style="font-family: "times" , "times new roman" , serif;"> now preserves dots directories when included in leaf-most pattern part;</span></li>
</ul>
<li><span style="font-family: "times" , "times new roman" , serif;">renamed </span><span style="font-family: "courier new" , "courier" , monospace;">setenv()</span><span style="font-family: "times" , "times new roman" , serif;"> => </span><span style="font-family: "courier new" , "courier" , monospace;">unixem_setenv()</span><span style="font-family: "times" , "times new roman" , serif;">, </span><span style="font-family: "courier new" , "courier" , monospace;">unsetenv()</span><span style="font-family: "times" , "times new roman" , serif;"> => </span><span style="font-family: "courier new" , "courier" , monospace;">unixem_unsetenv()</span><span style="font-family: "times" , "times new roman" , serif;">;</span></li>
<li><span style="font-family: "times" , "times new roman" , serif;">fixed defect in </span><span style="font-family: "courier new" , "courier" , monospace;">unixem_setenv()</span><span style="font-family: "times" , "times new roman" , serif;">;</span></li>
<li><span style="font-family: "times" , "times new roman" , serif;">built library name changes from "<b>unixem(.X)</b>" to "<b>unixem(.X).core</b>", e.g. </span><span style="font-family: "times" , "times new roman" , serif;"><span style="font-family: "courier new" , "courier" , monospace;">unixem.lib</span><span style="font-family: "times" , "times new roman" , serif;"> => </span><span style="font-family: "courier new" , "courier" , monospace;">unixem.core.lib</span><span style="font-family: "times" , "times new roman" , serif;">, </span><span style="font-family: "courier new" , "courier" , monospace;">unixem.1.lib</span><span style="font-family: "times" , "times new roman" , serif;"> => </span><span style="font-family: "courier new" , "courier" , monospace;">unixem.1.core.lib</span><span style="font-family: "times" , "times new roman" , serif;">, </span>;</span></li>
<li>removed <b style="font-family: Times, "Times New Roman", serif;">include/sys/select.h</b><span style="font-family: "times" , "times new roman" , serif;"> (which was vestigial);</span></li>
<li>build/project files:</li>
<ul>
<li>added VC++11, VC++12, and VC++14 support;</li>
<li>moved VC++6 project file from <b>build/vc6</b> to <b>projects/core/vc6</b>;</li>
<li>added VC++ 10 project files, in <b>projects/core/vc10</b>;</li>
<li>added VC++ 10+ props file(s), in <b>projects/vcprops</b>.</li>
</ul>
</ul>
Matt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.com0tag:blogger.com,1999:blog-3124337082472086472.post-6935269965634389842019-04-10T03:07:00.000-07:002019-04-10T03:07:48.991-07:00CLASP & libCLImate : different language implementations convergingI'm working on <b>CLASP</b> and <b>libCLImate</b> for <b>Ruby</b>, <b>Go</b>, <b>.NET</b> at the moment, and they're getting close to a 1.0 release, particularly <b>CLASP</b>.<br />
<br />
There've been two recent releases of <b><a href="https://github.com/synesissoftware/CLASP.Ruby">CLASP.Ruby</a></b>:<br />
<br />
<ul>
<li><b>0.17</b> adds a <b><span style="font-family: "courier new" , "courier" , monospace;">CLASP::Arguments.load()</span></b> method, which takes a <span style="font-family: "courier new" , "courier" , monospace;">Hash</span> or YAML-containing file (actually an <span style="font-family: "courier new" , "courier" , monospace;">IO</span> type), containing definitions of the flag/option/alias specifications;</li>
<li><b>0.18</b> changes the name of the concept from <i>alias</i> to <i>specification</i>, removing the ambiguity that's been bugging me for years.</li>
</ul>
<div>
The same changes will be made in the <b>Go</b> and <b>.NET</b> versions shortly. The <b>Python</b> and <b>Node</b> versions will change in the medium term.</div>
<div>
<br /></div>
<div>
Once all the other variants are done, the original <b>C/C++</b> library will be done.</div>
<div>
<br /></div>
Matt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.com0tag:blogger.com,1999:blog-3124337082472086472.post-7485242023423342172019-03-29T18:18:00.004-07:002019-03-29T18:18:46.299-07:00Lots of new things to Go at ...The <a href="https://golang.org/">Go</a> work is marching on, with the occasionally publicly publishable parts, the most recent of which are:<br />
<br />
<ul>
<li><b><a href="https://github.com/synesissoftware/ANGoLS">ANGoLS</a></b> (Algorithms Not found in Go Language Standard library) first release to the world;</li>
<li><b><a href="https://github.com/synesissoftware/STEGoL">STEGoL</a></b> (Simple Testing Enhancements for Go Language) first release to the world;</li>
<li><b><a href="https://github.com/synesissoftware/CLASP.Go">CLASP.Go</a></b> <a href="https://github.com/synesissoftware/CLASP.Go/blob/master/CHANGES.md">v0.14.2</a>, much improved and working seamlessly with libCLImate.Go, of which:</li>
<li><b><a href="https://github.com/synesissoftware/libCLImate.Go">libCLImate.Go</a></b> <a href="https://github.com/synesissoftware/libCLImate.Go/blob/master/CHANGES.md">v0.4.1</a>, again much improved.</li>
</ul>
<div>
<b>ANGoLS</b> and <b>STEGoL</b> are new, and so are suffering from the Matt Wilson-disease of poor/missing documentation, but <b>CLASP.Go</b> and <b>libCLImate.Go</b> are now replete with information and examples. Checkout them out. Feedback welcomed.</div>
<div>
<br /></div>
<div>
Watch this space - and the Synesis Twitter and Synesis GitHub accounts - for more Go stuff in the coming weeks. <b>libpath.Go</b> and <b>recls.Go</b> are imminent. <b>Pantheios.Go</b> will be released by mid-year. (And there'll likely be plenty of webservice-related libraries, too, given that's the current focus of Go development here ...)</div>
<div>
<br /></div>
Matt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.com0tag:blogger.com,1999:blog-3124337082472086472.post-41886190671003130652019-03-18T17:14:00.000-07:002019-03-18T17:14:31.887-07:00A bunch of CLASPs<div>
<b>CLASP</b> - <b>C</b>ommand-<b>L</b>ine <b>A</b>rgument <b>S</b>orting and <b>P</b>arsing - is a suite of libraries for sophisticated parsing of command-line arguments and presenting the parsed elements - flags, options, values - in a usable form. The original <b><a href="https://github.com/synesissoftware/CLASP">C/C++</a></b> library was released in 2011; variants for other languages have been released from time to time since.</div>
<div>
<br /></div>
<div>
Of necessity (but also enjoying it), currently I'm building systems in a bunch of different technologies, including <b>Go</b>, <b>Javascript</b> (<b>NodeB</b>), <b>Python</b>, and <b>Ruby</b>. As such, I have new/updated versions of the <b>CLASP</b> variants for these languages:</div>
<div>
<br /></div>
<div>
<ul>
<li><b>CLASP.Go</b> - <a href="https://github.com/synesissoftware/CLASP.Go/releases/tag/0.11.1">version 0.11.2</a> is the first public release. Project page is <a href="https://github.com/synesissoftware/CLASP.Go">https://github.com/synesissoftware/CLASP.Go</a>;</li>
<li><b>CLASP.js</b> - <a href="https://www.npmjs.com/package/clasp-js">version 0.2.1</a> is the first public release, installed via <span style="font-family: Courier New, Courier, monospace;"><b>npm install clasp-js</b></span>. Project page is <a href="https://github.com/synesissoftware/CLASP.js">https://github.com/synesissoftware/CLASP.js</a>;</li>
<li><b>CLASP.Python</b> - <a href="https://pypi.org/project/pyclasp/">version 0.3.0.1</a> is the first public release, installed via <span style="font-family: Courier New, Courier, monospace;"><b>pip install pyclasp</b></span> . Project page is https://github.com/synesissoftware/CLASP.Python;</li>
<li><b>CLASP.Ruby</b> - <a href="https://rubygems.org/gems/clasp-ruby/versions/0.15.1">version 0.15.1</a> is the latest release, installed via <span style="font-family: Courier New, Courier, monospace;"><b>gem install clasp-ruby</b></span>. Project page is <a href="https://github.com/synesissoftware/CLASP.Ruby">https://github.com/synesissoftware/CLASP.Ruby</a>.</li>
</ul>
<div>
<br /></div>
<div>
Each of the newly-released projects - <b>Go</b>, <b>js</b>, <b>Python</b> - contains (on GitHub) an EXAMPLES.md file, with links to examples. <b>CLASP.Ruby</b> will provide examples in a forthcoming release.</div>
</div>
Matt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.com0tag:blogger.com,1999:blog-3124337082472086472.post-3770553430711609642019-02-15T18:20:00.002-08:002019-02-15T18:37:01.360-08:00Simple cat in CoffeeScriptFurther to the post "<a href="http://blog.renaissanceprogrammer.com/2015/08/cat-in-node.html">Simple cat in Node</a>", here's a version in <b><a href="https://coffeescript.org/">CoffeeScript</a></b>.<br />
<br />
<span style="color: #6aa84f; font-family: "courier new" , "courier" , monospace;">#! /usr/bin/env coffee</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;">fs = require(<span style="caret-color: rgb(204, 0, 0); color: #cc0000;">'fs'</span>)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">readline = require(<span style="caret-color: rgb(204, 0, 0); color: #cc0000;">'readline'</span>)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">paths = process.argv.slice(<span style="caret-color: rgb(204, 0, 0); color: #cc0000;">2</span>)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;">cat = (path) -></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>stm = <span style="caret-color: rgb(255, 0, 255); color: magenta;">if</span> path <span style="caret-color: rgb(255, 0, 255); color: magenta;">then</span> fs.createReadStream(path) <span style="caret-color: rgb(255, 0, 255); color: magenta;">else</span> process.stdin</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>readline.createInterface(</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>input: stm</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>).on <span style="caret-color: rgb(204, 0, 0); color: #cc0000;">'line'</span>, (line) -></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>process.stdout.write line + </span><span style="caret-color: rgb(204, 0, 0); color: #cc0000; font-family: "courier new" , "courier" , monospace;">'\n'</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="caret-color: rgb(255, 0, 255); color: magenta; font-family: "courier new" , "courier" , monospace;">return</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace; white-space: pre;"> </span><span style="caret-color: rgb(255, 0, 255); color: magenta; font-family: "courier new" , "courier" , monospace;">return</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"><span style="caret-color: rgb(255, 0, 255); color: magenta;">if</span> 0 == paths.length</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>cat null</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="caret-color: rgb(255, 0, 255); color: magenta; font-family: "courier new" , "courier" , monospace;">return</span><br />
<div>
</div>
<span style="caret-color: rgb(255, 0, 255); color: magenta; font-family: "courier new", courier, monospace;">else</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="caret-color: rgb(106, 168, 79); color: #6aa84f;">// process each one at a time</span></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>paths.forEach (path) -></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>cat path</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="caret-color: rgb(255, 0, 255); color: magenta; font-family: "courier new" , "courier" , monospace;">return</span><br />
<div>
<br /></div>
<div>
<br /></div>
As you can see, it's very simple in structure to the JavaScript version; just simpler and with fewer parentheses. It can be further simplified by removing all the return statements, as every one is redundant in this case:<br />
<br />
<span style="color: #6aa84f; font-family: "courier new" , "courier" , monospace;">#! /usr/bin/env coffee</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;">fs = require(<span style="caret-color: rgb(204, 0, 0); color: #cc0000;">'fs'</span>)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">readline = require(<span style="caret-color: rgb(204, 0, 0); color: #cc0000;">'readline'</span>)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">paths = process.argv.slice(<span style="caret-color: rgb(204, 0, 0); color: #cc0000;">2</span>)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;">cat = (path) -></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>stm = <span style="caret-color: rgb(255, 0, 255); color: magenta;">if</span> path <span style="caret-color: rgb(255, 0, 255); color: magenta;">then</span> fs.createReadStream(path) <span style="caret-color: rgb(255, 0, 255); color: magenta;">else</span> process.stdin</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>readline.createInterface(</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>input: stm</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>).on <span style="caret-color: rgb(204, 0, 0); color: #cc0000;">'line'</span>, (line) -></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>process.stdout.write line + </span><span style="caret-color: rgb(204, 0, 0); color: #cc0000; font-family: "courier new" , "courier" , monospace;">'\n'</span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"><span style="caret-color: rgb(255, 0, 255); color: magenta;">if</span> 0 == paths.length</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>cat null</span><br />
<span style="caret-color: rgb(255, 0, 255); color: magenta; font-family: "courier new", courier, monospace;">else</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"> <span style="caret-color: rgb(106, 168, 79); color: #6aa84f;">// process each one at a time</span></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>paths.forEach (path) -></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>cat path</span><br />
<br />
<div>
<br /></div>
<br />
<br />Matt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.com0tag:blogger.com,1999:blog-3124337082472086472.post-15758491991396918662016-06-18T17:55:00.000-07:002016-06-18T17:55:02.645-07:00The Strobl InterjectionThe other day I was giving a <a href="https://en.wikipedia.org/wiki/Ruby_(programming_language)">Ruby</a> programming tutorial to the team at <a href="https://www.linkedin.com/company/razor-risk-technologies">Razor Risk</a>, since we're introducing a suite of tools to effect automated wholesale code transmogrification of our codebase, in part to bring up-to-date the very large C++ codebase with <a href="https://en.wikipedia.org/wiki/C%2B%2B11">C++ 11</a> / <a href="https://en.wikipedia.org/wiki/C%2B%2B14">C++ 14</a>.
The element in question resides within a chunk of Ruby code that is a pattern I've been using for well over a decade, involving two loops:<br />
<br />
<ul>
<li>An outer loop, that searches a given directory for a given set of patterns, using the <a href="https://github.com/synesissoftware/recls.Ruby">recls.Ruby</a> library; and</li>
<li>An inner loop, that processes the contents of a given file on a per-line basis.</li>
</ul>
<div>
In each case, we're applying <span style="font-family: "courier new" , "courier" , monospace;">each</span>.</div>
<div>
<br /></div>
<div>
I've been applying this pattern for almost as long as I've been using Ruby, and it looks something like this:</div>
<div>
<br /></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><span style="color: #38761d;">Recls</span>::<span style="color: #38761d;">FileSearch</span>.new(root_dir, patterns, <span style="color: #38761d;">Recls</span>::<span style="color: #38761d;">RECURSIVE</span> | <span style="color: #38761d;">Recls</span>::<span style="color: #38761d;">FILE</span>).each <span style="color: #b45f06;">do</span> |<span style="color: #3d85c6;">fe</span>|</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> IO::readlines(fe.path).each_with_index </span><span style="color: #b45f06; font-family: '"courier new"', '"courier"', monospace;">do</span><span style="font-family: '"courier new"', '"courier"', monospace;"> </span><span style="font-family: 'courier new', courier, monospace;">|</span><span style="color: #3d85c6; font-family: 'courier new', courier, monospace;">line</span><span style="font-family: 'courier new', courier, monospace;">, </span><span style="color: #3d85c6; font-family: 'courier new', courier, monospace;">index0</span><span style="font-family: 'courier new', courier, monospace;">|</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> index = 1 + index0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> line = line.chomp</span></div>
<div>
<br /></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="color: #b45f06;">if</span> line =~ / . . . some regex that finds something interesting .../</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> . . . do something interesting to log or change the line, ...</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="color: #b45f06;">end</span></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="color: #b45f06;">end</span></span></div>
<div>
<span style="color: #b45f06; font-family: "courier new" , "courier" , monospace;">end</span></div>
<div>
<br />
The first two lines of the inner-loop have been part of my idiom forever, and are explained thusly:<br />
<br />
<ul>
<li>Since humans and <a href="https://en.wikipedia.org/wiki/Integrated_development_environment">IDEs</a> use 1-based numbering, I always obtain a 1-based <span style="font-family: Courier New, Courier, monospace;">index</span> from the 0-based one, which I name <span style="font-family: Courier New, Courier, monospace;">index0</span>, obtained as the second parameter to <span style="font-family: Courier New, Courier, monospace;">each_with_index()</span>'s block. So far, so good;</li>
<li>Since lines come with their end-of-line sequence - usually <span style="font-family: Courier New, Courier, monospace;">"\n"</span> - this needs to be <span style="font-family: Courier New, Courier, monospace;">chomp</span>ed off. Since it's bad practice to change objects that are held by other objects (in this case it doesn't matter, but making a habit of good practice is, er, good practice) I don't call <span style="font-family: Courier New, Courier, monospace;">chomp!</span> to change the actual line, but instead obtain a <span style="font-family: Courier New, Courier, monospace;">chomp</span>ed copy from it.</li>
</ul>
<div>
All seems quiet on the western front. Alas, as I was showing this to the team, little did I know I was about to be hoist on my own petard.</div>
<div>
<br /></div>
<div>
Some of the things I'm always banging on about wrt the C++ codebase include being totally unambiguous and making absolutely everything immutable that can possibly be so. One of the consequences of these two exhortations to good practice is that variables should be initialised (rather than declared and then assigned) and should be made immutable (by using const), as in:</div>
<div>
<br /></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> size_t const number_to_process = container.size() - offset_length;</span></div>
<div>
<br /></div>
<div>
rather than, say, </div>
<div>
<br /></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> size_t number_to_process;</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"><br /></span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> number_to_process = container.size();</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> number_to_process -= offset_length;</span></div>
<div>
<br /></div>
<div>
In the former case the compiler will stop you from mistakenly changing or reusing the variable; in the latter all bets are off.</div>
<div>
<br /></div>
<div>
Anyway, one of our implementation consultants, Bernard Strobl, has a keen interest in programming and pays a lot of attention during code reviews and tutorial sessions. As is his wont, Bernie jumped immediately and with great relish upon my transgression of good practice in my reuse of the variable <span style="font-family: Courier New, Courier, monospace;">line</span>. Notwithstanding my love of my own opinion and my ability to lever bombast at high speed with almost no warm-up, when you're wrong, you're wrong.</div>
<div>
<br /></div>
<div>
And so I learned something, or at least remembered something that now informs a modified idiom:</div>
<div>
<br /></div>
<div>
<div>
<br /></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><span style="color: #38761d;">Recls</span>::<span style="color: #38761d;">FileSearch</span>.new(root_dir, patterns, <span style="color: #38761d;">Recls</span>::<span style="color: #38761d;">RECURSIVE</span> | <span style="color: #38761d;">Recls</span>::<span style="color: #38761d;">FILE</span>).each <span style="color: #b45f06;">do</span> |<span style="color: #3d85c6;">fe</span>|</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> IO::readlines(fe.path).each_with_index </span><span style="color: #b45f06; font-family: '"courier new"', '"courier"', monospace;">do</span><span style="font-family: '"courier new"', '"courier"', monospace;"> </span><span style="font-family: 'courier new', courier, monospace;">|</span><span style="background-color: yellow; color: #3d85c6; font-family: 'courier new', courier, monospace;">line0</span><span style="font-family: 'courier new', courier, monospace;">, </span><span style="color: #3d85c6; font-family: 'courier new', courier, monospace;">index0</span><span style="font-family: 'courier new', courier, monospace;">|</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> index = 1 + index0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> line = <span style="background-color: yellow;">line0</span>.chomp</span></div>
<div>
<br /></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="color: #b45f06;">if</span> line =~ / . . . some regex that finds something interesting .../</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> . . . do something interesting to log or change the line, ...</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="color: #b45f06;">end</span></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="color: #b45f06;">end</span></span></div>
<div>
<span style="color: #b45f06; font-family: "courier new" , "courier" , monospace;">end</span></div>
<div>
<br />
As of version 0.4.12, our code transmogrification tool suite now contains corrected forms for every tool, and the commit bears the message "The Strobl Interjection" lest I forget my error.</div>
</div>
<div>
<br /></div>
<div>
Thanks, Bernie! :-)</div>
</div>
Matt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.com0tag:blogger.com,1999:blog-3124337082472086472.post-22846917486919601032016-06-13T02:32:00.003-07:002016-06-13T02:32:35.466-07:00Important semantic effects of small syntax errors : C++ conditional expressionHere's a typo that had dramatic effects:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">if(processed_count =! container.size())</span><br />
<span style="font-family: Courier New, Courier, monospace;">{</span><br />
<span style="font-family: Courier New, Courier, monospace;"> . . .</span><br />
<br />
Result: the code always thought that the whole container had been processed, even when it wasn't.<br />
<br />
Had the codebase been running at maximum warnings, and treating warnings as errors, this would never have happened. Those compilation conditions are now active.<br />
<br />
<b>Always compile at maximum warnings and treat warnings as errors.</b><br />
<b><br /></b>Matt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.com0tag:blogger.com,1999:blog-3124337082472086472.post-76181359693094357712016-05-21T13:18:00.000-07:002016-05-21T16:14:01.939-07:00Revisibility in Node declarationsOne of the things that strikes me about Node.js is how <span style="font-family: "courier new" , "courier" , monospace;">var</span>iable declarations are done messily in the canon. Consider:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"> var express = require('express'),</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> app = express();</span><br />
<br />
For some reason, Node programmers (or is that JS programmers) don't want to use too many <span style="font-family: "courier new" , "courier" , monospace;">var</span> keywords. Perhaps there is a shortage.<br />
<br />
Problem is, when it comes time to add in a new variable, we're up against <b><i><a href="http://synesis.com.au/publishing/software-anatomies/anatomy-of-a-cli-program-written-in-c.html#sidebar-revisibility">revisibility</a> </i></b>problems. To add another variable <span style="font-family: "courier new" , "courier" , monospace;">thing</span>,<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"> var express = require('express'),</span><br />
<span style="background-color: #f4cccc; font-family: "courier new" , "courier" , monospace;"><span style="background-color: #f4cccc;"> app = express()</span><b style="background-color: #ea9999;">,</b></span><br />
<span style="background-color: #f4cccc; font-family: "courier new" , "courier" , monospace;"><b style="background-color: #ea9999;"> thing = 'thing'</b><span style="background-color: #f4cccc;">;</span></span><br />
<br />
involves changing <i>two</i> lines, because of the need to add a comma at the end of the second line to continue the variable declarations.<br />
<br />
This is just nuts. Working with extant code - reading it, changing it - is always more important than writing it. The remedy is simple: use one <span style="font-family: "courier new" , "courier" , monospace;">var</span> per line (as has been advocated strongly for pretty much every other C-family language since the dawn of time). Now, changing<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"> var express = require('express');</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> var app = express();</span><br />
<br />
to<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"> var express = require('express');</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> var app = express();</span><br />
<span style="background-color: #ea9999;"><b style="font-family: 'courier new', courier, monospace;"> var thing = 'thing';</b></span><br />
<br />
is at maximum revisibility, and your <i><b><a href="http://synesis.com.au/publishing/software-anatomies/anatomy-of-a-cli-program-written-in-c.html#sidebar-revisibility">revisiology</a></b></i> (which is something you will do a lot if you are writing code professionally, or in teams, or in professional teams) will be much eased.<br />
<br />Matt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.com0tag:blogger.com,1999:blog-3124337082472086472.post-11831889294127920922015-12-19T20:00:00.003-08:002015-12-19T20:00:46.265-08:00Important semantic effects of small syntax errors : Ruby regexHere's a recent typo that had interestingly important effects:<br />
<br />
Meant to write<br />
<br />
<span style="font-family: Courier New, Courier, monospace;"> if line =~ /<i>some-reg-expression</i>/</span><br />
<br />
Actually wrote:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;"> if line = ~/<i>some-reg-expression</i>/</span><br />
<br />
Result: found nothing!<br />
<br />
FYI, the type of <span style="font-family: Courier New, Courier, monospace;">~/<i>some-reg-expression</i>/</span> is <span style="font-family: Courier New, Courier, monospace;">NilClass</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<br />Matt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.com0tag:blogger.com,1999:blog-3124337082472086472.post-35489238982773358112015-09-28T03:35:00.001-07:002015-09-28T03:35:32.524-07:00Synesis Software C/C++ libraries updatedThere've been a bunch of <a href="http://twitter.com/synesissoftware"><b>#synesissoftware</b></a> libraries released in the last two weeks (necessitated because I'm going to be very busy on something new very soon), including:<div>
<br /></div>
<div>
<ul>
<li><a href="https://sourceforge.net/projects/stlsoft/files/STLSoft%201.9/"><b>STLSoft</b> 1.9.121 released on SourceForge</a>. <a href="http://github.com/synesissoftware/STLSoft-1.9"><b>STLSoft</b> 1.9 current working version released on GitHub</a>;</li>
<li><a href="https://sourceforge.net/projects/fastformat/files/fastformat/"><b>FastFormat</b> 0.7.1 released on SourceForge</a>. <a href="http://github.com/synesissoftware/FastFormat"><b>FastFormat</b> current working version released on GitHub</a>;</li>
<li><a href="https://sourceforge.net/projects/pantheios/files/Pantheios%20%28C%20and%20Cxx%29/"><b>Pantheios</b> 1.0.1 beta 215 released on SourceForge</a>. <a href="http://github.com/synesissoftware/Pantheios"><b>Pantheios</b> current working version released on GitHub</a>;</li>
<li><a href="http://github.com/synesissoftware/Pantheios.Extras.DiagUtil"><b>Pantheios.Extras.DiagUtil</b> current working version released on GitHub</a>;</li>
<li><a href="http://github.com/synesissoftware/Pantheios.Extras.Main"><b>Pantheios.Extras.Main</b> current working version released on GitHub</a>;</li>
<li><a href="http://github.com/synesissoftware/libCLImate"><b>libCLImate</b> current working version released on GitHub</a>;</li>
<li><a href="http://github.com/synesissoftware/CLASP"><b>CLASP</b> current working version released on GitHub</a>.</li>
</ul>
<div>
<br /></div>
</div>
Matt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.com0tag:blogger.com,1999:blog-3124337082472086472.post-62487205430937828502015-09-09T06:39:00.000-07:002015-09-09T06:42:53.649-07:00C/C++ libraries on the moveJust a quick update to note:<br />
<br />
<br />
<ul>
<li>Latest STLSoft 1.9 - <a href="https://sourceforge.net/projects/stlsoft/files/STLSoft%201.9/">STLSoft 1.9.120</a> - released. (STLSoft 1.12 will be released by end of year;)</li>
<li>Main (i.e. C & C++) <a href="https://github.com/synesissoftware/CLASP"><b>CLASP</b></a> (<b>C</b>ommand-<b>l</b>ine <b>A</b>rgument <b>S</b>orting and <b>P</b>arsing) library released onto <a href="https://github.com/synesissoftware">Synesis Software's GitHub repo</a>. Other language implementations of CLASP, including for Go, Node.js, .NET, and Ruby, will be released soon, and the interesting polymathematicals discussed in this blog.</li>
</ul>
<div>
<br /></div>
Matt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.com0tag:blogger.com,1999:blog-3124337082472086472.post-30436963285711111602015-09-07T00:25:00.001-07:002015-09-07T05:04:32.043-07:00Back in the Groove ...Like so many of my other activities - blogs, open-source libraries, magazine articles & columns, books, consulting company - this blog has been left fallow for far too long, due to pressures of work and focus on other things (my stalled startup).<br />
<br />
Now, having got my startup curiosity out of my system, I'm getting back in the groove, and will be sorting out the <a href="http://www.synesis.com.au/">Synesis Software website</a>, open-source libs, and all publishing activities over the next few months.<br />
<br />
For now, as far as the Renaissance Programmer goes, there are new posts about <a href="http://blog.renaissanceprogrammer.com/2015/08/cat-in-node.html">implementing the cat system tool in Node.js</a> and <a href="http://blog.renaissanceprogrammer.com/2015/07/small-but-powerful-ui-highlight-decorations-in-WinForms-with-closures.html">closures in C#</a>, and an update to <a href="http://blog.renaissanceprogrammer.com/2011/09/getting-current-time-and-displaying-in-HH-MM-SS-mmm.html">the post on time-formatting</a> with items on Objective-C, JavaScript (for Node aficionados), PowerShell, and Swift. There'll be a lot more to come on Go, JavaScript and Swift over the coming months, and I intend to post several times a week from here on in.<br />
<br />
<br />Matt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.com0tag:blogger.com,1999:blog-3124337082472086472.post-43628783982895850292015-08-29T05:34:00.000-07:002015-09-07T02:30:07.861-07:00Small but powerful UI highlight decorations in WinForms with closuresI've been working so much recently with languages for which <a href="https://en.wikipedia.org/wiki/Closure_(computer_programming)">closures</a> is the norm - <b>Node</b> (which, if you haven't heard yet, is a useful JavaScript platform that obviates the need for a browser), <b>Go</b>, <b>Ruby</b> - that it's easy to think of this as the natural way to program.<br />
<br />
In this post, and a follow-up to come in a couple of days, I'll talk about use of closures in languages for which it's not so natural. Next time, I'll talk about <a href="https://en.wikipedia.org/wiki/C%2B%2B11#Lambda_functions_and_expressions">C++'s support</a>, which is only very recent (in consideration of its age) with the advent of C++ 11 / 14; I'll also show how they can be synthesised with some locality (but a lot of messing around) in "old" C++ (C++'98 and before).<br />
<br />
First, <b>C#</b>, which has <a href="https://msdn.microsoft.com/en-us/library/bb397687.aspx">great support in recent forms</a> of the language, but didn't start out that way.<br />
<br />
A while back I was helping out a buddy with a WinForms scientific application, which involved a large and sophisticated amount of user input, spread over numerous forms (separated by a two-level nesting of tabs). The challenge was that inputs in many parts of each form depended on the presence and on the values of input in other parts of each form, and there were also such dependencies between parts of different forms.<br />
<br />
Dealing with invalid user input follows the established troika of <a href="http://accu.org/index.php/journals/1681"><b>detect</b>, <b>report</b>, <b>respond (</b>just as it does for dealing with failure)</a>. For a UI program in the activity of eliciting user input, detection and response are pretty run of the mill:<br />
<ul>
<li><b>Detection</b>, is simply determining whether the given input is (in)valid, such as negative numbers or alpha/punct in a numeric field. This may be done when a control is being populated, when focus leaves the input control, or when a button is pressed (whether to dismiss a dialogue or to fill out a further portion of the form). Doing so on button pressing is the easiest, and that was the case here;</li>
<li><b>Response</b>, for user input, is, of course, in the purview of the user. The key to eliciting a good response (and resulting in good user experience) is good <b>reporting</b>.</li>
</ul>
From experience - think MFC programming a <i>long</i> time ago - I've found it useful to report invalid input in forms to users by two simultaneous actions:<br />
<ul>
<li>set the focus to the control containing the "offending" values (which may include just missing values); and</li>
<li>change the control background to an eye-catching colour, and then set it back when focus leaves the control (presumably with a correct value, set there by the now, ahem, focused user).</li>
</ul>
<div>
Back in the days of Windows UI programming in C/C++, whether straight to the Win32 API, using MFC, ATL, or whatever, setting up such functionality involved a lot of coding and the involvement of multiple components. (Dare one remind gentle readers about control reflection ... ???)</div>
<br />
In WinForms, it's straightforward enough to go to a control - just call <span style="font-family: Courier New, Courier, monospace;">ctrl.Focus()</span> - and to set the background colour - just assign to <span style="font-family: Courier New, Courier, monospace;">ctrl.BackColor</span> - but how do we get it to change back when the focus leaves the control without involving the rest of the program?<br />
<br />
Here's where C#"s closures come to the rescue. Rather than any more waffle, let me just present the solution (coded for <span style="color: #6aa84f; font-family: Courier New, Courier, monospace;">TextBox</span>) as it appears in the utility code:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;"><span style="color: magenta;">public static</span> <span style="color: #6aa84f;">void</span> FocusAndHighlight(<span style="color: #6aa84f;">TextBox</span> tbx, <span style="color: #6aa84f;">Color</span> highlightColour)</span><br />
<span style="font-family: Courier New, Courier, monospace;">{</span><br />
<span style="color: #274e13; font-family: Courier New, Courier, monospace;"> // Have to declare these two as variables, since they</span><br />
<span style="color: #274e13; font-family: Courier New, Courier, monospace;"> // will be "closed over" by the lamdba (aka closure)</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;"> <span style="color: #6aa84f;">Color</span> originalClr = tbx.BackColor;</span><br />
<span style="font-family: Courier New, Courier, monospace;"> <span style="color: #6aa84f;">EventHandler</span> handler = null;</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #274e13; font-family: Courier New, Courier, monospace;"> // Note that handler has to be initialised and then</span><br />
<span style="color: #274e13; font-family: Courier New, Courier, monospace;"> // separately assigned</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;"> handler = (<span style="color: #6aa84f;">object</span> sender, <span style="color: #6aa84f;">EventArgs</span> e) =></span><br />
<span style="font-family: Courier New, Courier, monospace;"> {</span><br />
<span style="font-family: Courier New, Courier, monospace;"> tbx.BackColor = originalClr;</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;"> tbx.Leave -= handler;</span><br />
<span style="font-family: Courier New, Courier, monospace;"> };</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;"> tbx.Leave += handler;</span><br />
<span style="font-family: Courier New, Courier, monospace;"> tbx.BackColor = highlightColour;</span><br />
<span style="font-family: Courier New, Courier, monospace;"> tbx.Focus();</span><br />
<span style="font-family: Courier New, Courier, monospace;">}</span><br />
<br />
I trust the logic here is pretty straightforward:<br />
<br />
<ul>
<li>remember the original background colour of the control;</li>
<li>create a handler, using lambda syntax, that returns the background colour to its original and attach it to the control's <span style="font-family: Courier New, Courier, monospace;">Leave</span> event;</li>
<li>set the background colour to the required highlight;</li>
<li>set focus to the control.</li>
</ul>
<div>
The key to making this work is to declare two local variables - <span style="font-family: Courier New, Courier, monospace;">originalClr</span>, which stores the original colour, and <span style="font-family: Courier New, Courier, monospace;">handler</span>, which allows the handler to be removed from the control's <span style="font-family: Courier New, Courier, monospace;">Leave</span> events, returning it to its original state entirely when the focus is left. </div>
<div>
<br /></div>
<div>
Declaring the variables <i>is essential</i>, and this is something you must remember when working with closures in C# (and in many other languages): <i><a href="http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx">closures closes around variables, not values</a></i>.</div>
<br />
With this component, getting the user's attention and taking them to where they need to be is a single call:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;"><span style="color: magenta;">if</span>(0 == tbx.Text.Length)</span><br />
<span style="font-family: Courier New, Courier, monospace;">{</span><br />
<span style="font-family: Courier New, Courier, monospace;"> ControlUtil.FocusAndHighlight(tbx, <span style="color: #6aa84f;">Color</span>.Yellow);</span><br />
<span style="font-family: Courier New, Courier, monospace;">}</span><br />
<br />
<div style="font-family: Consolas; font-size: 9.5px; min-height: 11px;">
<br /></div>
<br />Matt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.com0tag:blogger.com,1999:blog-3124337082472086472.post-14774373132046232192015-08-26T04:41:00.000-07:002019-02-15T18:23:30.543-08:00Simple cat in Node<br />
I have a longstanding interest in writing system tools in different languages. In this post, I'll look at implementing a simple <a href="https://en.wikipedia.org/wiki/Cat_(Unix)"><b>cat</b></a> written in <b>JavaScript</b> and executed on <b>Node</b>.<br />
<div>
<br /></div>
<div>
The implementation is pretty succinct:</div>
<div>
<br /></div>
<div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<span style="color: #6aa84f; font-family: "courier new" , "courier" , monospace;">#! /usr/bin/env node</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="color: magenta; font-family: "courier new" , "courier" , monospace;">const</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> fs = require(<span style="color: #cc0000;">'fs'</span>)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">, readline = require(<span style="color: #cc0000;">'readline'</span>)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">, paths = process.argv.slice(<span style="color: #cc0000;">2</span>)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">, cat = function(path) {</span><br />
<div style="min-height: 13px;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="caret-color: rgb(255, 0, 255); color: magenta;">const</span> stm = path ? fs.createReadStream(path) : process.stdin;</span><br />
<div style="min-height: 13px;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<span style="font-family: "courier new" , "courier" , monospace;"> readline.createInterface({</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> input: stm,</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }).on(</span><span style="color: #cc0000; font-family: "courier new" , "courier" , monospace;">'line'</span><span style="font-family: "courier new" , "courier" , monospace;">, function(line) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> process.stdout.write(line + <span style="color: #cc0000;">"\n"</span>);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> });</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">;</span><br />
<div style="min-height: 13px;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div style="min-height: 13px;">
<span style="font-family: "courier new" , "courier" , monospace;"><span style="color: magenta;">if</span>(0 == paths.length) {</span></div>
<div style="min-height: 13px;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<span style="font-family: "courier new" , "courier" , monospace;"> cat(null);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">} <span style="color: magenta;">else</span> {</span><br />
<div style="min-height: 13px;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="color: #6aa84f;">// process each one at a time</span></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> paths.forEach(function(path) {</span><br />
<div style="min-height: 13px;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<span style="font-family: "courier new" , "courier" , monospace;"> cat(path);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> });</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span><br />
<div>
<br /></div>
<div>
We start with requiring two standard components: <b><a href="https://nodejs.org/api/fs.html">fs</a></b> and <b><a href="https://nodejs.org/api/readline.html">readline</a></b> (which I won't explain further here, because they're standard, so you probably should know them, and because the docs are great, so you can get to know them easily if you need to). Both of these are not going to be reassigned, so I define them <span style="font-family: "courier new" , "courier" , monospace;"><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const">const</a></span>.<br />
<div>
<br /></div>
</div>
</div>
<div>
Also defined in this way is the <span style="font-family: "courier new" , "courier" , monospace;">paths</span> variable, which obtains all the arguments given on the command-line (excepting the script name, of course), and a <span style="font-family: "courier new" , "courier" , monospace;">cat()</span> function, which takes a single parameter, <span style="font-family: "courier new" , "courier" , monospace;">path</span>. If <span style="font-family: "courier new" , "courier" , monospace;">path</span> is null, <span style="font-family: "courier new" , "courier" , monospace;">cat()</span> uses the standard input stream (<span style="font-family: "courier new" , "courier" , monospace;"><a href="https://nodejs.org/api/process.html#process_process_stdin">process.stdin()</a></span>); if not, a stream is created on the file (via <span style="font-family: "courier new" , "courier" , monospace;"><a href="https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options">fs.createReadStream()</a></span>). (Naturally, if the file does not exist or cannot be accessed for reading by the calling process, an exception will be thrown.)</div>
<div>
<br /></div>
<div>
Inside <span style="font-family: "courier new" , "courier" , monospace;">cat()</span> we invoke <span style="font-family: "courier new" , "courier" , monospace;"><a href="https://nodejs.org/api/readline.html#readline_readline_createinterface_options">readline.createInterface()</a></span>, specifying our discriminated stream for input and standard output stream for output, and then specify a handler for the <b>'line'</b> event that writes out the line.</div>
<div>
<br /></div>
<div>
With <span style="font-family: "courier new" , "courier" , monospace;">cat()</span> so defined, we invoke according to the number of arguments we're given. If we're given one or more arguments, then invoke <span style="font-family: "courier new" , "courier" , monospace;">cat()</span> for each; otherwise we pass <span style="font-family: "courier new" , "courier" , monospace;">null</span> to read from standard input.</div>
<div>
<br /></div>
<div>
Note that this may have a concurrency vulnerability, insofar as the contents from multiple paths may be interleaved when the <b>fs</b> components' buffering limit is exceeded, due to Node's concurrency model, but I have yet to experience it in testing. Just don't treat this as production code.</div>
Matt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.com0tag:blogger.com,1999:blog-3124337082472086472.post-52585487825508245002011-09-22T14:29:00.000-07:002015-09-07T04:12:24.121-07:00Getting the current time and displaying in HH:MM:SS.mmmTask: <b>How to elicit the current time and display it, in 24-hr format HH:MM:SS.mmm</b>.<br/>
<br/>
Answers in
<b><a href="#section-Bash">Bash</a></b>,
<b><a href="#section-C">C</a></b>,
<b><a href="#section-C#">C#</a></b>,
<b><a href="#section-Go">Go</a></b>,
<b><a href="#section-Java">Java</a></b>,
<b><a href="#section-JavaScript">JavaScript</a></b>,
<b><a href="#section-Objective-C">Objective-C</a></b>,
<b><a href="#section-PowerShell">PowerShell</a></b>,
<b><a href="#section-Python">Python</a></b>,
<b><a href="#section-Ruby">Ruby</a></b>.
<br/>
<br/>
<b>Caveat</b>: no failure testing is included in the samples. Things fail. Production code should take account of failure.
<br/>
<br/>
<h2 id="section-Bash" name="section-Bash"><b><u>Bash:</u></b></h2>
The solution for bash could hardly be simpler.<br/>
<br/>
<div style="color: #5330e1;">
<span style="font-family: Courier New, Courier, monospace;"><span style="color: black;">date +</span><span style="color: #ce7924;">'</span><span style="color: #c33720;">%H:%M:%S.%3N</span><span style="color: #ce7924;">'</span></span></div>
<br/>
The only problem is, it doesn't work on OSX. In that case, you might consider the Python or Ruby approach, or compile the C/UNIX option and execute that.
<br/>
<br/>
<h2 id="section-C" name="section-C"><b><u>C:</u></b></h2>
Unfortunately, <em>there is no way of getting milliseconds using standard C</em>. The best we can do is just H+M+S:<br/>
<br/>
<div style="color: #d53bd3;">
<span style="font-family: Courier New, Courier, monospace;">#include <span style="color: #c33720;"><stdio.h></span></span>
</div>
<div style="color: #d53bd3;">
<span style="font-family: Courier New, Courier, monospace;">#include <span style="color: #c33720;"><stdlib.h></span></span>
</div>
<div style="color: #d53bd3;">
<span style="font-family: Courier New, Courier, monospace;">#include <span style="color: #c33720;"><time.h></span></span>
</div>
<div style="min-height: 13px;">
<span style="font-family: Courier New, Courier, monospace;"><br/></span></div>
<span style="font-family: Courier New, Courier, monospace;"><span style="color: #34bd26;">int</span> main()</span><br/>
<span style="font-family: Courier New, Courier, monospace;">{</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> <span style="color: #34bd26;">time_t</span> now;</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> <span style="color: #c33720;">struct</span> <span style="color: #34bd26;">tm</span>* local;</span><br/>
<div style="min-height: 13px;">
<span style="font-family: Courier New, Courier, monospace;"><br/></span></div>
<span style="font-family: Courier New, Courier, monospace;"> time(&now, </span><span style="color: #c33720; font-family: 'Courier New', Courier, monospace;">NULL</span><span style="font-family: 'Courier New', Courier, monospace;">);</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> local = localtime(&now);</span><br/>
<div style="min-height: 13px;">
<span style="font-family: Courier New, Courier, monospace;"><br/></span></div>
<span style="font-family: Courier New, Courier, monospace;"> printf(<span style="color: #c33720;">"</span><span style="color: #d53bd3;">%02d</span><span style="color: #c33720;">:</span><span style="color: #d53bd3;">%02d</span><span style="color: #c33720;">:</span><span style="color: #d53bd3;">%02d\n</span><span style="color: #c33720;">"</span>, local->tm_hour, local->tm_min, local->tm_sec);</span><br/>
<br/>
<div style="color: #c33720;"><span style="font-family: Courier New, Courier, monospace;"><span style="color: black;"> </span><span style="color: magenta;">return</span><span style="color: black;"> </span>EXIT_SUCCESS<span style="color: black;">;</span></span></div>
<span style="font-family: 'Courier New', Courier, monospace;">}</span><br/>
<div>
<br/></div>
<div>
In non-standard ways we have a number of options: UNIX-specific, Windows-specific, APR.</div>
<br/>
<h3><u>C / UNIX:</u></h3>
<br/>
<div>
UNIX provides the system function <span style="font-family: Courier New, Courier, monospace;">gettimeofday()</span>, which obtains the time (equivalent to <span style="font-family: Courier New, Courier, monospace;">time()</span>) along with microseconds. Hence:
</div>
<br/>
<div style="color: #d53bd3;">
<span style="font-family: Courier New, Courier, monospace;">#include <span style="color: #c33720;"><sys/time.h></span></span>
</div>
<div style="color: black; min-height: 13px;">
<span style="font-family: Courier New, Courier, monospace;"><br/></span>
</div>
<div style="color: #d53bd3;">
<span style="font-family: Courier New, Courier, monospace;">#include <span style="color: #c33720;"><stdio.h></span></span>
</div>
<div style="color: #d53bd3;">
<span style="font-family: Courier New, Courier, monospace;">#include <span style="color: #c33720;"><stdlib.h></span></span>
</div>
<div style="color: #d53bd3;">
<span style="font-family: Courier New, Courier, monospace;">#include <span style="color: #c33720;"><time.h></span></span>
</div>
<div style="min-height: 13px;">
</div>
<span style="font-family: Courier New, Courier, monospace;"><span style="color: #34bd26;">int</span> main()</span><br/>
<span style="font-family: Courier New, Courier, monospace;">{</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> <span style="color: #c33720;">struct</span> <span style="color: #34bd26;">timeval</span> now;</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> <span style="color: #c33720;">struct</span> <span style="color: #34bd26;">tm</span>* local;</span><br/>
<div style="min-height: 13px;">
<span style="font-family: Courier New, Courier, monospace;"><br/></span></div>
<span style="font-family: Courier New, Courier, monospace;"> gettimeofday(&now, <span style="color: #c33720;">NULL</span>);</span><br/>
<span style="font-family: 'Courier New', Courier, monospace;"> local = localtime(&now.tv_sec);</span><br/>
<div style="min-height: 13px;">
<span style="font-family: Courier New, Courier, monospace;"><br/></span></div>
<span style="font-family: Courier New, Courier, monospace;"> printf(<span style="color: #c33720;">"</span><span style="color: #d53bd3;">%02d</span><span style="color: #c33720;">:</span><span style="color: #d53bd3;">%02d</span><span style="color: #c33720;">:</span><span style="color: #d53bd3;">%02d</span><span style="color: #c33720;">.</span><span style="color: #d53bd3;">%03d\n</span><span style="color: #c33720;">"</span>, local->tm_hour, local->tm_min, local->tm_sec, now.tv_usec / <span style="color: #c33720;">1000</span>);</span><br/>
<br/>
<div style="color: #c33720;"><span style="font-family: Courier New, Courier, monospace;"><span style="color: black;"> </span><span style="color: magenta;">return</span><span style="color: black;"> </span>EXIT_SUCCESS<span style="color: black;">;</span></span></div>
<span style="font-family: 'Courier New', Courier, monospace;">}</span><br/>
<br/>
<h3><u>C / Windows:</u></h3>
<br/>
<div>
Windows provides the system function <span style="font-family: Courier New, Courier, monospace;">GetLocalTime()</span>, which obtains the time in a rich structure <span style="font-family: Courier New, Courier, monospace;">SYSTEMTIME</span>) that includes microseconds. Hence:
</div>
<br/>
<div style="color: #d53bd3;">
<span style="font-family: Courier New, Courier, monospace;">#include <span style="color: #c33720;"><windows.h></span></span>
</div>
<div style="color: black; min-height: 13px;">
<span style="font-family: Courier New, Courier, monospace;"><br/></span>
</div>
<div style="color: #d53bd3;">
<span style="font-family: Courier New, Courier, monospace;">#include <span style="color: #c33720;"><stdio.h></span></span>
</div>
<div style="color: #d53bd3;">
<span style="font-family: Courier New, Courier, monospace;">#include <span style="color: #c33720;"><stdlib.h></span></span>
</div>
<div style="color: #d53bd3;">
<span style="font-family: Courier New, Courier, monospace;">#include <span style="color: #c33720;"><time.h></span></span>
</div>
<div style="min-height: 13px;">
</div>
<div style="min-height: 13px;">
</div>
<span style="font-family: Courier New, Courier, monospace;"><span style="color: #34bd26;">int</span> main()</span><br/>
<span style="font-family: Courier New, Courier, monospace;">{</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> <span style="color: #34bd26;">SYSTEMTIME</span> now;</span><br/>
<span style="font-family: Courier New, Courier, monospace;"><br/></span>
<span style="font-family: Courier New, Courier, monospace;"> GetLocalTime(&now);</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> </span><br/>
<span style="font-family: 'Courier New', Courier, monospace;"> printf(</span><span style="color: #c33720; font-family: 'Courier New', Courier, monospace;">"</span><span style="color: #d53bd3; font-family: 'Courier New', Courier, monospace;">%02d</span><span style="color: #c33720; font-family: 'Courier New', Courier, monospace;">:</span><span style="color: #d53bd3; font-family: 'Courier New', Courier, monospace;">%02d</span><span style="color: #c33720; font-family: 'Courier New', Courier, monospace;">:</span><span style="color: #d53bd3; font-family: 'Courier New', Courier, monospace;">%02d</span><span style="color: #c33720; font-family: 'Courier New', Courier, monospace;">.</span><span style="color: #d53bd3; font-family: 'Courier New', Courier, monospace;">%03d\n</span><span style="color: #c33720; font-family: 'Courier New', Courier, monospace;">"</span><span style="font-family: 'Courier New', Courier, monospace;">, now.wHour, </span><span style="font-family: 'Courier New', Courier, monospace;">now.wMinute</span><span style="font-family: 'Courier New', Courier, monospace;">, </span><span style="font-family: 'Courier New', Courier, monospace;">now.wSecond, </span><span style="font-family: 'Courier New', Courier, monospace;">now.wMilliseconds</span><span style="font-family: 'Courier New', Courier, monospace;">);</span><br/>
<br/>
<div style="color: #c33720;"><span style="font-family: Courier New, Courier, monospace;"><span style="color: black;"> </span><span style="color: magenta;">return</span><span style="color: black;"> </span>EXIT_SUCCESS<span style="color: black;">;</span></span></div>
<span style="font-family: 'Courier New', Courier, monospace;">}</span>
<br/>
<br/>
<h3><u>C / APR:</u></h3>
<br/>
<div>
APR provides the <span style="font-family: Courier New, Courier, monospace;">apr_time_exp_t</span> type, which is equivalent to <span style="font-family: Courier New, Courier, monospace;">struct tm</span> as well as a <span style="font-family: Courier New, Courier, monospace;">tm_usec</span> member from which we can derive the milliseconds. Hence:
</div>
<br/>
<div style="color: #d53bd3;">
<span style="font-family: Courier New, Courier, monospace;">#include <span style="color: #c33720;"><apr-1/apr_time.h></span></span>
</div>
<div style="color: black; min-height: 13px;">
<span style="font-family: Courier New, Courier, monospace;"><br/></span>
</div>
<div style="color: #d53bd3;">
<span style="font-family: Courier New, Courier, monospace;">#include <span style="color: #c33720;"><stdio.h></span></span>
</div>
<div style="color: #d53bd3;">
<span style="font-family: Courier New, Courier, monospace;">#include <span style="color: #c33720;"><stdlib.h></span></span>
</div>
<div style="color: #d53bd3;">
<span style="font-family: Courier New, Courier, monospace;">#include <span style="color: #c33720;"><time.h></span></span>
</div>
<div style="min-height: 13px;">
</div>
<span style="font-family: Courier New, Courier, monospace;"><span style="color: #34bd26;">int</span> main()</span><br/>
<span style="font-family: Courier New, Courier, monospace;">{</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> <span style="color: #34bd26;">apr_time_t</span> now;</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> <span style="color: #34bd26;">apr_time_exp_t</span> local;</span><br/>
<br/>
<span style="font-family: Courier New, Courier, monospace;"> now = apr_time_now();</span><br/>
<br/>
<span style="font-family: Courier New, Courier, monospace;"> apr_time_exp_lt(&local, now);</span><br/>
<br/>
<span style="font-family: Courier New, Courier, monospace;"> printf(<span style="color: #c33720;">"</span><span style="color: #d53bd3;">%02d</span><span style="color: #c33720;">:</span><span style="color: #d53bd3;">%02d</span><span style="color: #c33720;">:</span><span style="color: #d53bd3;">%02d</span><span style="color: #c33720;">.</span><span style="color: #d53bd3;">%03d\n</span><span style="color: #c33720;">"</span>, local.tm_hour, local.tm_min, local.tm_sec, local.tm_usec / <span style="color: #c33720;">1000</span>);</span><br/>
<br/>
<div style="color: #c33720;"><span style="font-family: Courier New, Courier, monospace;"><span style="color: black;"> </span><span style="color: magenta;">return</span><span style="color: black;"> </span>EXIT_SUCCESS<span style="color: black;">;</span></span></div>
<span style="font-family: 'Courier New', Courier, monospace;">}</span><br/>
<br/>
<br/>
<h2 id="section-C#" name="section-C#"><u><b>C#:</b></u></h2>
<br/>
<span style="font-family: Courier New, Courier, monospace;"><span style="color: magenta;">using</span> <span style="color: #6aa84f;">System</span>;</span><br/>
<span style="font-family: Courier New, Courier, monospace;"><span style="color: magenta;">class</span> <span style="color: #6aa84f;">Program</span></span><br/>
<span style="font-family: Courier New, Courier, monospace;">{</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> <span style="color: magenta;">static</span> <span style="color: #6aa84f;">void</span> Main()</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> {</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> <span style="color: #6aa84f;">DateTime</span> dt = <span style="color: #6aa84f;">DateTime</span>.Now;</span><br/>
<span style="font-family: Courier New, Courier, monospace;"><br/></span>
<span style="font-family: Courier New, Courier, monospace;"> <span style="color: #6aa84f;">Console.Out</span>.WriteLine(</span><br/>
<span style="font-family: Courier New, Courier, monospace;"><span style="color: #cc0000;"> "{0:d2}:{1:d2}:{2:d2}.{3:d3}"</span></span><br/>
<span style="font-family: Courier New, Courier, monospace;"> , dt.Hour</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> , dt.Minute</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> , dt.Second</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> , dt.Millisecond</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> );</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> }</span><br/>
<span style="font-family: Courier New, Courier, monospace;">}</span><br/>
<br/>
Even though <span style="font-family: Courier New, Courier, monospace;">System.DateTime.Now</span> is a property, it acts as a method in that it gets a new value every time it is involved. This is an all-too-common gotcha when repeated calls are made in the same region on the assumption that this property is property like in returning the same value.
<br/>
<br/>
<h2 id="section-Go" name="section-Go"><u><b>Go:</b></u></h2>
<br/>
<span style="font-family: Courier New, Courier, monospace;"><span style="color: magenta;">package</span> main</span><br/>
<div style="min-height: 13px;">
<span style="font-family: Courier New, Courier, monospace;"><br/></span></div>
<span style="font-family: Courier New, Courier, monospace;"><span style="color: magenta;">import</span> (</span><br/>
<div style="min-height: 13px;">
<span style="font-family: Courier New, Courier, monospace;"><br/></span></div>
<span style="font-family: Courier New, Courier, monospace;"> "fmt"</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> "time"</span><br/>
<span style="font-family: Courier New, Courier, monospace;">)</span><br/>
<div style="min-height: 13px;">
<span style="font-family: Courier New, Courier, monospace;"><br/></span></div>
<span style="font-family: Courier New, Courier, monospace;"><span style="color: magenta;">func</span> main() {</span><br/>
<div style="min-height: 13px;">
<span style="font-family: Courier New, Courier, monospace;"><br/></span></div>
<span style="font-family: Courier New, Courier, monospace;"> t := <span style="color: #6aa84f;">time</span>.Now()</span><br/>
<div style="min-height: 13px;">
<span style="font-family: Courier New, Courier, monospace;"><br/></span></div>
<span style="font-family: Courier New, Courier, monospace;"> <span style="color: #6aa84f;">fmt</span>.Printf(</span><br/>
<span style="font-family: Courier New, Courier, monospace;"><span style="color: #cc0000;"> "%02d:%02d:%02d.%03d\n"</span></span><br/>
<span style="font-family: Courier New, Courier, monospace;"> , t.Hour()</span><br/>
<span style="font-family: 'Courier New', Courier, monospace;"> </span> <span style="font-family: Courier New, Courier, monospace;">, t.Minute()</span><br/>
<span style="font-family: 'Courier New', Courier, monospace;"> </span> <span style="font-family: Courier New, Courier, monospace;">, t.Second()</span><br/>
<span style="font-family: 'Courier New', Courier, monospace;"> </span> <span style="font-family: Courier New, Courier, monospace;">, t.UnixNano()%1e6/1e3</span><br/>
<span style="font-family: 'Courier New', Courier, monospace;"> </span> <span style="font-family: Courier New, Courier, monospace;">)</span><br/>
<span style="font-family: Courier New, Courier, monospace;">}</span><br/>
<br/>
<br/>
<h2 id="section-Java" name="section-Java"><u><b>Java:</b></u></h2>
<br/>
<div>
<span style="font-family: Courier New, Courier, monospace;"><span style="color: magenta;">import</span> <span style="color: #6aa84f;">java.text.SimpleDateFormat</span>;</span><br/>
<span style="font-family: Courier New, Courier, monospace;"><span style="color: magenta;">import</span> <span style="color: #6aa84f;">java.util.Date</span>;</span><br/>
<span style="font-family: Courier New, Courier, monospace;"><span style="color: magenta;">class</span> <span style="color: #6aa84f;">Program</span></span><br/>
<span style="font-family: Courier New, Courier, monospace;">{</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> <span style="color: magenta;">public static</span> <span style="color: #6aa84f;">void</span> Main(<span style="color: #6aa84f;">String</span>[] args)</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> {</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> <span style="color: #6aa84f;">SimpleDateFormat</span> fmt = <span style="color: #6aa84f;">new </span><span style="color: #6aa84f;">SimpleDateFormat</span>(<span style="color: #c33720;">"HH:mm:ss.SSS"</span>);</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> <span style="color: #6aa84f;">Date</span> now = <span style="color: #6aa84f;">new </span><span style="color: #6aa84f;">Date</span>();</span><br/>
<br/>
<span style="font-family: Courier New, Courier, monospace;"> <span style="color: #6aa84f;">System</span>.out.println(fmt.format(now));</span><br/>
<span style="font-family: Courier New, Courier, monospace;"> }</span><br/>
<span style="font-family: Courier New, Courier, monospace;">}</span><br/>
</div>
<br/>
<br/>
<h2 id="section-JavaScript" name="section-JavaScript"><u><b>JavaScript:</b></u></h2>
<br/>
<div>
The built-in <span style="font-family: Courier New, Courier, monospace;">Date</span> type has a method <span style="font-family: Courier New, Courier, monospace;">toISOString()</span> that returns a string of the form YYYY-mm-DDTHH:MM:SS.mmmZ, to which a simple regex can extract the H+M+S+ms:
</div>
<br/>
<div>
<span style="font-family: 'Courier New', Courier, monospace;">console.log((<span style="color: magenta;">new</span> <span style="color: #6aa84f;">Date</span>()).toISOString().match(<span style="color: #c33720;">/(\d\d\:\d\d\:\d\d\.\d\d\d)/</span>)[0]);</span>
</div>
<br/>
<br/>
<h2 id="section-Objective-C" name="section-Objective-C"><u><b>Objective-C:</b></u></h2>
<br/>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="color: #6aa84f;">NSDate</span> *now = [<span style="color: #6aa84f;">NSDate</span> date];</span>
<br/>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="color: #6aa84f;">NSDateFormatter</span> *formatter = [[<span style="color: #6aa84f;">NSDateFormatter</span> alloc] init];</span>
<br/>
<span style="font-family: 'Courier New', Courier, monospace;">[formatter setDateFormat:<span style="color: #c33720;">@"HH:mm:ss.SSS"</span>];</span>
<br/>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="color: #6aa84f;">NSString</span> *s = [formatter stringFromDate:now];</span>
<br/>
<span style="font-family: 'Courier New', Courier, monospace;">NSLog(s);</span>
<br/>
<br/>
<h2 id="section-PowerShell" name="section-PowerShell"><u><b>Powershell:</b></u></h2>
<div style="-webkit-text-stroke-width: 0px; color: black; font-family: Times; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<span style="font-family: 'Courier New', Courier, monospace;">
Get-Date -format HH:mm:ss.fff
</span>
</div>
<br/>
<br/>
<h2 id="section-Python" name="section-Python"><u><b>Python:</b></u></h2>
<br/>
<div>
<span style="font-family: Courier New, Courier, monospace;"><span style="color: magenta;">import</span><span style="color: #5330e1;"> datetime</span></span>
</div>
<br/>
<div>
<span style="font-family: Courier New, Courier, monospace;"><span style="color: #34bbc7;">print </span>datetime.datetime.now().strftime(<span style="color: #c33720;">'%H:%M:%S.%f'</span>)[:-<span style="color: #c33720;">3</span>]</span>
</div>
<br/>
<br/>
<div>
<span style="font-family: inherit;">Python's </span><span style="font-family: Courier New, Courier, monospace;">strftime()</span><span style="font-family: inherit;">'s </span><span style="font-family: Courier New, Courier, monospace;">%f</span><span style="font-family: inherit;"> yields microseconds, hence the slicing off of the last three characters.</span>
</div>
<br/>
<br/>
<h2 id="section-Ruby" name="section-Ruby"><u><b>Ruby:</b></u></h2>
<br/>
<div>
<span style="font-family: Courier New, Courier, monospace;">puts <span style="color: #34bd26;">Time</span>.now.strftime(<span style="color: #d53bd3;">'</span><span style="color: #c33720;">%H:%M:%S.%L</span><span style="color: #d53bd3;">'</span>)</span>
</div>
<br/>
<br/>
<br/>
<br/>
<h2 id="section-Swift" name="section-Swift"><u><b>Swift:</b></u></h2>
<br/>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="color: magenta;">let </span>now = <span style="color: #6aa84f;">NSDate</span>()</span>
<br/>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="color: magenta;">var </span>formatter = <span style="color: #6aa84f;">NSDateFormatter</span>()</span>
<br/>
<span style="font-family: 'Courier New', Courier, monospace;">formatter.dateFormat = <span style="color: #c33720;">"HH:mm:ss.SSS"</span></span>
<br/>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="color: magenta;">let </span>s = formatter.stringFromDate(now)</span>
<br/>
<span style="font-family: 'Courier New', Courier, monospace;">println(s)</span>
<br/>
<br/>
<br/>
Matt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.com0tag:blogger.com,1999:blog-3124337082472086472.post-10751210081867361622009-12-19T03:18:00.000-08:002015-09-05T18:40:42.800-07:00Formatting a value in hexQ: Given an integer value, say 42, how do we print its hex value in the following formats in C, C++, C#, Ruby:<br />
<br />
1. <span style="font-family: "Courier New",Courier,monospace;"> "i=2a;"</span><br />
2. <span style="font-family: "Courier New",Courier,monospace;"> "i= 2a;"</span><br />
3. <span style="font-family: "Courier New",Courier,monospace;"> "i=2a ;"</span><br />
4. <span style="font-family: "Courier New",Courier,monospace;"> "i=002a;"</span><br />
5. <span style="font-family: "Courier New",Courier,monospace;"> "i=0x2a;"</span><br />
6. <span style="font-family: "Courier New",Courier,monospace;"> "i=0x002a;"</span><br />
7. <span style="font-family: "Courier New",Courier,monospace;"> "i=2A;"</span><br />
8. <span style="font-family: "Courier New",Courier,monospace;"> "i= 2A;"</span><br />
9. <span style="font-family: "Courier New",Courier,monospace;"> "i=2A ;"</span><br />
10. <span style="font-family: "Courier New",Courier,monospace;"> "i=002A;"</span><br />
11. <span style="font-family: "Courier New",Courier,monospace;"> "i=0X2A;"</span><br />
12. <span style="font-family: "Courier New",Courier,monospace;"> "i=0X002A;"</span><br />
<br />
(The semi-colon is to allow us to easily see the extent of the number field.)<br />
<br />
<br />
<span style="font-size: large;"><u><b>C:</b></u></span><br />
<br />
Using the C standard Streams library's <span style="font-family: "Courier New",Courier,monospace;">printf()</span>:<br />
<br />
1. <span style="font-family: "Courier New",Courier,monospace;">printf("i=%x;\n", i);</span><br />
2. <span style="font-family: "Courier New",Courier,monospace;">printf("i=%4x;\n", i);</span><br />
3. <span style="font-family: "Courier New",Courier,monospace;">printf("i=%-4x;\n", i);</span><br />
4. <span style="font-family: "Courier New",Courier,monospace;">printf("i=%04x;\n", i);</span><br />
5. <span style="font-family: "Courier New",Courier,monospace;">printf("i=0x%x;\n", i);</span><br />
6. <span style="font-family: "Courier New",Courier,monospace;">printf("i=0x%04x;\n", i);</span><br />
7. <span style="font-family: "Courier New",Courier,monospace;">printf("i=%X;\n", i);</span><br />
8. <span style="font-family: "Courier New",Courier,monospace;">printf("i=%4X;\n", i);</span><br />
9. <span style="font-family: "Courier New",Courier,monospace;">printf("i=%-4X;\n", i);</span><br />
10. <span style="font-family: "Courier New",Courier,monospace;">printf("i=%04X;\n", i);</span><br />
11. <span style="font-family: "Courier New",Courier,monospace;">printf("i=0X%X;\n", i);</span><br />
12. <span style="font-family: "Courier New",Courier,monospace;">printf("i=0X%04X;\n", i);</span><br />
<br />
<br />
<span style="font-size: large;"><u><b>C++:</b></u></span><br />
<br />
<b>Using the C++ standard IOStreams library:</b><br />
<br />
1. <span style="font-family: "Courier New",Courier,monospace;">std::cout << "i=" << std::setbase(16) << i << ";" << std::endl;</span><br />
2. <span style="font-family: "Courier New",Courier,monospace;">std::cout << "i=" << std::setbase(16) << std::setw(4) << std::setfill(' ') << i << ";" << std::endl;</span><br />
3. <span style="font-family: "Courier New",Courier,monospace;">don't know</span><br />
4. <span style="font-family: "Courier New",Courier,monospace;">std::cout << "i=" << std::setbase(16) << std::setw(4) << std::setfill('0') << i << ";" << std::endl;</span><br />
5. <span style="font-family: "Courier New",Courier,monospace;">std::cout << "i=0x" << std::setbase(16) << i << ";" << std::endl;</span><br />
6. <span style="font-family: "Courier New",Courier,monospace;">std::cout << "i=0x" << std::setbase(16) << std::setw(4) << std::setfill('0') << i << ";" << std::endl;</span><br />
7. <span style="font-family: "Courier New",Courier,monospace;">std::cout << "i=" << std::setbase(16) << std::uppercase << i << ";" << std::endl;</span><br />
8. <span style="font-family: "Courier New",Courier,monospace;">std::cout << "i=" << std::setbase(16) << std::uppercase << std::setw(4) << std::setfill(' ') << i << ";" << std::endl;</span><br />
9. <span style="font-family: "Courier New",Courier,monospace;">don't know</span><br />
10. <span style="font-family: "Courier New",Courier,monospace;">std::cout << "i=" << std::setbase(16) << std::uppercase << std::setw(4) << std::setfill('0') << i << ";" << std::endl;</span><br />
11. <span style="font-family: "Courier New",Courier,monospace;">std::cout << "i=0X" << std::setbase(16) << std::uppercase << i << ";" << std::endl;</span><br />
12. <span style="font-family: "Courier New",Courier,monospace;">std::cout << "i=0X" << std::setbase(16) << std::uppercase << std::setw(4) << std::setfill('0') << i << ";" << std::endl;</span><br />
<br />
<br />
<b>Using FastFormat's Format:</b><br />
<br />
1. <span style="font-family: "Courier New",Courier,monospace;">ff::fmtln(std::cout, "i={0};", ff::to_x(i));</span><br />
2. <span style="font-family: "Courier New",Courier,monospace;">ff::fmtln(std::cout, "i={0};", ff::to_x(i, 4));</span><br />
3. <span style="font-family: "Courier New",Courier,monospace;">ff::fmtln(std::cout, "i={0};", ff::to_x(i, -4));</span><br />
4. <span style="font-family: "Courier New",Courier,monospace;">ff::fmtln(std::cout, "i={0};", ff::to_x(i, 4, 4));</span><br />
5. <span style="font-family: "Courier New",Courier,monospace;">ff::fmtln(std::cout, "i=0x{0};", ff::to_x(i));</span><br />
6. <span style="font-family: "Courier New",Courier,monospace;">ff::fmtln(std::cout, "i=0x{0};", ff::to_x(i, 4, 4));</span><br />
7. <span style="font-family: "Courier New",Courier,monospace;">ff::fmtln(std::cout, "i={0};", ff::to_X(i));</span><br />
8. <span style="font-family: "Courier New",Courier,monospace;">ff::fmtln(std::cout, "i={0};", ff::to_X(i, 4));</span><br />
9. <span style="font-family: "Courier New",Courier,monospace;">ff::fmtln(std::cout, "i={0};", ff::to_X(i, -4));</span><br />
10. <span style="font-family: "Courier New",Courier,monospace;">ff::fmtln(std::cout, "i={0};", ff::to_X(i, 4, 4));</span><br />
11. <span style="font-family: "Courier New",Courier,monospace;">ff::fmtln(std::cout, "i=0X{0};", ff::to_X(i));</span><br />
12. <span style="font-family: "Courier New",Courier,monospace;">ff::fmtln(std::cout, "i=0X{0};", ff::to_X(i, 4, 4));</span><br />
<br />
<br />
<b>Using FastFormat's Write:</b><br />
<br />
1. <span style="font-family: "Courier New",Courier,monospace;">ff::writeln(std::cout, "i=", ff::to_x(i), ";");</span><br />
2. <span style="font-family: "Courier New",Courier,monospace;">ff::writeln(std::cout, "i=", ff::to_x(i, 4), ";");</span><br />
3. <span style="font-family: "Courier New",Courier,monospace;">ff::writeln(std::cout, "i=", ff::to_x(i, -4), ";");</span><br />
4. <span style="font-family: "Courier New",Courier,monospace;">ff::writeln(std::cout, "i=", ff::to_x(i, 4, 4), ";");</span><br />
5. <span style="font-family: "Courier New",Courier,monospace;">ff::writeln(std::cout, "i=0x", ff::to_x(i), ";");</span><br />
6. <span style="font-family: "Courier New",Courier,monospace;">ff::writeln(std::cout, "i=0x", ff::to_x(i, 4, 4), ";");</span><br />
7. <span style="font-family: "Courier New",Courier,monospace;">ff::writeln(std::cout, "i=", ff::to_X(i), ";");</span><br />
8. <span style="font-family: "Courier New",Courier,monospace;">ff::writeln(std::cout, "i=", ff::to_X(i, 4), ";");</span><br />
9. <span style="font-family: "Courier New",Courier,monospace;">ff::writeln(std::cout, "i=", ff::to_X(i, -4), ";");</span><br />
10. <span style="font-family: "Courier New",Courier,monospace;">ff::writeln(std::cout, "i=", ff::to_X(i, 4, 4), ";");</span><br />
11. <span style="font-family: "Courier New",Courier,monospace;">ff::writeln(std::cout, "i=0X", ff::to_X(i), ";");</span><br />
12. <span style="font-family: "Courier New",Courier,monospace;">ff::writeln(std::cout, "i=0X", ff::to_X(i, 4, 4), ";");</span><br />
<br />
<b>Using Boost.Format:</b><br />
<br />
1. <span style="font-family: "Courier New",Courier,monospace;">std::cout << boost::format("i=%x;") % i << std::endl;</span><br />
2. <span style="font-family: "Courier New",Courier,monospace;">std::cout << boost::format("i=%4x;") % i << std::endl;</span><br />
3. <span style="font-family: "Courier New",Courier,monospace;">std::cout << boost::format("i=%-4x;") % i << std::endl;</span><br />
4. <span style="font-family: "Courier New",Courier,monospace;">std::cout << boost::format("i=%04x;") % i << std::endl;</span><br />
5. <span style="font-family: "Courier New",Courier,monospace;">std::cout << boost::format("i=0x%x;") % i << std::endl;</span><br />
6. <span style="font-family: "Courier New",Courier,monospace;">std::cout << boost::format("i=0x%04x;") % i << std::endl;</span><br />
7. <span style="font-family: "Courier New",Courier,monospace;">std::cout << boost::format("i=%X;") % i << std::endl;</span><br />
8. <span style="font-family: "Courier New",Courier,monospace;">std::cout << boost::format("i=%4X;") % i << std::endl;</span><br />
9. <span style="font-family: "Courier New",Courier,monospace;">std::cout << boost::format("i=%-4X;") % i << std::endl;</span><br />
10. <span style="font-family: "Courier New",Courier,monospace;">std::cout << boost::format("i=%04X;") % i << std::endl;</span><br />
11. <span style="font-family: "Courier New",Courier,monospace;">std::cout << boost::format("i=0X%X;") % i << std::endl;</span><br />
12. <span style="font-family: "Courier New",Courier,monospace;">std::cout << boost::format("i=0X%04X;") % i << std::endl;</span><br />
<br />
<br />
<span style="font-size: large;"><u><b>C#:</b></u></span><br />
<br />
1. <span style="font-family: "Courier New",Courier,monospace;">Console.Out.WriteLine("i={0:x};", i);</span><br />
2. <span style="font-family: "Courier New",Courier,monospace;">Console.Out.WriteLine("i={0,4:x};", i);</span><br />
3. <span style="font-family: "Courier New",Courier,monospace;">Console.Out.WriteLine("i={0,-4:x};", i);</span><br />
4. <span style="font-family: "Courier New",Courier,monospace;">Console.Out.WriteLine("i={0:x4};", i);</span><br />
5. <span style="font-family: "Courier New",Courier,monospace;">Console.Out.WriteLine("i=0x{0:x};", i);</span><br />
6. <span style="font-family: "Courier New",Courier,monospace;">Console.Out.WriteLine("i=0x{0:x4};", i);</span><br />
7. <span style="font-family: "Courier New",Courier,monospace;">Console.Out.WriteLine("i={0:X};", i);</span><br />
8. <span style="font-family: "Courier New",Courier,monospace;">Console.Out.WriteLine("i={0,4:X};", i);</span><br />
9. <span style="font-family: "Courier New",Courier,monospace;">Console.Out.WriteLine("i={0,-4:X};", i);</span><br />
10. <span style="font-family: "Courier New",Courier,monospace;">Console.Out.WriteLine("i={0:X4};", i);</span><br />
11. <span style="font-family: "Courier New",Courier,monospace;">Console.Out.WriteLine("i=0X{0:X};", i);</span><br />
12. <span style="font-family: "Courier New",Courier,monospace;">Console.Out.WriteLine("i=0X{0:X4};", i);</span><br />
<br />
<span style="font-size: large;"><u><b>Ruby:</b></u></span><br />
<br />
<b>Using puts & %:</b><br />
<br />
1. <span style="font-family: "Courier New",Courier,monospace;">puts "i=%x;" % i</span><br />
2. <span style="font-family: "Courier New",Courier,monospace;">puts "i=%4x;" % i</span><br />
3. <span style="font-family: "Courier New",Courier,monospace;">puts "i=%-4x;" % i</span><br />
4. <span style="font-family: "Courier New",Courier,monospace;">puts "i=%04x;" % i</span><br />
5. <span style="font-family: "Courier New",Courier,monospace;">puts "i=0x%x;" % i</span><br />
6. <span style="font-family: "Courier New",Courier,monospace;">puts "i=0x%04x;" % i</span><br />
7. <span style="font-family: "Courier New",Courier,monospace;">puts "i=%X;" % i</span><br />
8. <span style="font-family: "Courier New",Courier,monospace;">puts "i=%4X;" % i</span><br />
9. <span style="font-family: "Courier New",Courier,monospace;">puts "i=%-4X;" % i</span><br />
10. <span style="font-family: "Courier New",Courier,monospace;">puts "i=%04X;" % i</span><br />
11. <span style="font-family: "Courier New",Courier,monospace;">puts "i=0X%X;" % i</span><br />
12. <span style="font-family: "Courier New",Courier,monospace;">puts "i=0X%04X;" % i</span><br />
<br />
<b>Using interpolation & to_s:</b><br />
<br />
1. <span style="font-family: "Courier New",Courier,monospace;">puts "i=#{i.to_s(16)};"</span><br />
2. <span style="font-family: "Courier New",Courier,monospace;">puts "i=#{i.to_s(16).rjust(4)};"</span><br />
3. <span style="font-family: "Courier New",Courier,monospace;">puts "i=#{i.to_s(16).ljust(4)};"</span><br />
4. <span style="font-family: "Courier New",Courier,monospace;">puts "i=#{i.to_s(16).rjust(4, '0')};"</span><br />
5. <span style="font-family: "Courier New",Courier,monospace;">puts "i=0x#{i.to_s(16)};"</span><br />
6. <span style="font-family: "Courier New",Courier,monospace;">puts "i=0x#{i.to_s(16).rjust(4, '0')};"</span><br />
7. <span style="font-family: "Courier New",Courier,monospace;">puts "i=#{i.to_s(16).upcase};"</span><br />
8. <span style="font-family: "Courier New",Courier,monospace;">puts "i=#{i.to_s(16).rjust(4).upcase};"</span><br />
9. <span style="font-family: "Courier New",Courier,monospace;">puts "i=#{i.to_s(16).ljust(4).upcase};"</span><br />
10. <span style="font-family: "Courier New",Courier,monospace;">puts "i=#{i.to_s(16).rjust(4, '0').upcase};"</span><br />
11. <span style="font-family: "Courier New",Courier,monospace;">puts "i=0X#{i.to_s(16).upcase};"</span><br />
12. <span style="font-family: "Courier New",Courier,monospace;">puts "i=0X#{i.to_s(16).rjust(4, '0').upcase};"</span>Matt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.com0tag:blogger.com,1999:blog-3124337082472086472.post-82732300083872435572009-12-19T03:17:00.000-08:002015-09-07T21:39:42.040-07:00Welcome to "The Renaissance Programmer"A <i>renaissance programmer</i> is one who can express him/herself well in a number of languages; aka, a <a href="http://en.wikipedia.org/wiki/Polymath"><i>polymath</i></a>. I have an interest in multiple languages and technologies, and have been programming in C, C#, C++, COM, Java, Objective-C, Python, and Ruby for a long time and, more recently, am enjoying learning Go, Node.js, and Swift.<br />
<br />
This blog will contain various multi-language solutions, large and small.Matt Wilsonhttp://www.blogger.com/profile/08032564940029177259noreply@blogger.com0