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

<channel>
	<title>John&#039;s Blog &#187; c++</title>
	<atom:link href="http://john.nachtimwald.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://john.nachtimwald.com</link>
	<description>My little blog</description>
	<lastBuildDate>Sat, 31 Jul 2010 17:28:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Qt Remove Directory and Its Contents</title>
		<link>http://john.nachtimwald.com/2010/06/08/qt-remove-directory-and-its-contents/</link>
		<comments>http://john.nachtimwald.com/2010/06/08/qt-remove-directory-and-its-contents/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 12:47:28 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[qt]]></category>

		<guid isPermaLink="false">http://john.nachtimwald.com/?p=391</guid>
		<description><![CDATA[When dealing with directories, Qt has a large number of functions to make manipulating them easy. However, it does not include a way to delete a non-empty directory. This little omission is easily solved. Following is a recursive function that will delete a directory along with all of it&#8217;s contents. This will delete depth first. [...]]]></description>
			<content:encoded><![CDATA[<p>When dealing with directories, Qt has a large number of functions to make manipulating them easy. However, it does not include a way to delete a non-empty directory. This little omission is easily solved.</p>
<p>Following is a recursive function that will delete a directory along with all of it&#8217;s contents. This will delete depth first. Meaning it will recurse into sub-directories and only start deleting once the directory has no sub-directories. Changing QDir::DirsFirst to QDir::DirsLast will change this into a breadth first search.</p>
<p>fileutils.h</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#ifndef FILEUTILS_H</span>
<span style="color: #339900;">#define FILEUTILS_H</span>
&nbsp;
<span style="color: #339900;">#include &lt;QString&gt;</span>
&nbsp;
<span style="color: #0000ff;">class</span> FileUtils
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">bool</span> removeDir<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> QString <span style="color: #000040;">&amp;</span>dirName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #339900;">#endif // FILEUTILS_H</span></pre></div></div>

<p>fileutils.cpp</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;QDir&gt;</span>
<span style="color: #339900;">#include &lt;QFile&gt;</span>
<span style="color: #339900;">#include &lt;QFileInfo&gt;</span>
<span style="color: #339900;">#include &lt;QFileInfoList&gt;</span>
&nbsp;
<span style="color: #339900;">#include &quot;fileutils.h&quot;</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">/*!
   Delete a directory along with all of its contents.
&nbsp;
   \param dirName Path of directory to remove.
   \return true on success; false on error.
*/</span>
<span style="color: #0000ff;">bool</span> FileUtils<span style="color: #008080;">::</span><span style="color: #007788;">removeDir</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> QString <span style="color: #000040;">&amp;</span>dirName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">bool</span> result <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
    QDir dir<span style="color: #008000;">&#40;</span>dirName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>dir.<span style="color: #007788;">exists</span><span style="color: #008000;">&#40;</span>dirName<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        Q_FOREACH<span style="color: #008000;">&#40;</span>QFileInfo info, dir.<span style="color: #007788;">entryInfoList</span><span style="color: #008000;">&#40;</span>QDir<span style="color: #008080;">::</span><span style="color: #007788;">NoDotAndDotDot</span> <span style="color: #000040;">|</span> QDir<span style="color: #008080;">::</span><span style="color: #007788;">System</span> <span style="color: #000040;">|</span> QDir<span style="color: #008080;">::</span><span style="color: #007788;">Hidden</span>  <span style="color: #000040;">|</span> QDir<span style="color: #008080;">::</span><span style="color: #007788;">AllDirs</span> <span style="color: #000040;">|</span> QDir<span style="color: #008080;">::</span><span style="color: #007788;">Files</span>, QDir<span style="color: #008080;">::</span><span style="color: #007788;">DirsFirst</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>info.<span style="color: #007788;">isDir</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                result <span style="color: #000080;">=</span> removeDir<span style="color: #008000;">&#40;</span>info.<span style="color: #007788;">absoluteFilePath</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
                result <span style="color: #000080;">=</span> QFile<span style="color: #008080;">::</span><span style="color: #0000dd;">remove</span><span style="color: #008000;">&#40;</span>info.<span style="color: #007788;">absoluteFilePath</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>result<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0000ff;">return</span> result<span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        result <span style="color: #000080;">=</span> dir.<span style="color: #007788;">rmdir</span><span style="color: #008000;">&#40;</span>dirName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> result<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://john.nachtimwald.com/2010/06/08/qt-remove-directory-and-its-contents/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ Derived Classes and Object Destruction</title>
		<link>http://john.nachtimwald.com/2010/05/29/c-derived-classes-and-object-destruction/</link>
		<comments>http://john.nachtimwald.com/2010/05/29/c-derived-classes-and-object-destruction/#comments</comments>
		<pubDate>Sat, 29 May 2010 15:56:09 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[c++]]></category>

		<guid isPermaLink="false">http://john.nachtimwald.com/?p=385</guid>
		<description><![CDATA[While working on lebookread I realized that the the destructor for my reader classes would never be called. lebook read has a base class (FormatReader) that exports all of the necessary functionality for use by applications using the library. All of the readers are a subclass of FormatReader. The library will find the appropriate reader [...]]]></description>
			<content:encoded><![CDATA[<p>While working on lebookread I realized that the the destructor for my reader classes would never be called. lebook read has a base class (FormatReader) that exports all of the necessary functionality for use by applications using the library. All of the readers are a subclass of FormatReader. The library will find the appropriate reader for the specified ebook create a reader object and return it as a FormatReader pointer.</p>
<p>When you are dealing with a pointer <i>p</i> of type <i>base</i> that points to an object of type <i>derived</i> you need to take special care. To have the destruction of <i>p</i> call the derived and base destructor the base class must have a virtual destructor. Otherwise only the base class&#8217;s destructor will be called. This is one of those little things to look out for when dealing with C++.</p>
<p>Here is some example code to demonstrate the above.</p>
<p>main.cpp</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #339900;">#include &quot;base.h&quot;</span>
<span style="color: #339900;">#include &quot;deriv.h&quot;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span><span style="color: #000040;">**</span> argv<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Base *b = new Base();&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;delete b;&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    Base <span style="color: #000040;">*</span>b <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Base<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; --- &quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">delete</span> b<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; --- &quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;b destroyed&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Deriv *d = new Deriv();&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;delete d&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    Deriv <span style="color: #000040;">*</span>d <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Deriv<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; --- &quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">delete</span> d<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; --- &quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;d destroyed&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Base *c = new Deriv();&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;delete c&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    Base <span style="color: #000040;">*</span>c <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Deriv<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; --- &quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">delete</span> c<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; --- &quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;c destroyed&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#ifndef BASE_H</span>
<span style="color: #339900;">#define BASE_H</span>
&nbsp;
<span style="color: #0000ff;">class</span> Base
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    ~Base<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #339900;">#endif /* BASE_H */</span></pre></div></div>

<p>base.cpp</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #339900;">#include &quot;base.h&quot;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
Base<span style="color: #008080;">::</span>~Base<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;base dest&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>deriv.h</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#ifndef DERIV_H</span>
<span style="color: #339900;">#define DERIV_H</span>
&nbsp;
<span style="color: #339900;">#include &quot;base.h&quot;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Deriv <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Base
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    ~Deriv<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #339900;">#endif /* DERIV_H */</span></pre></div></div>

<p>deriv.cpp</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #339900;">#include &quot;deriv.h&quot;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
Deriv<span style="color: #008080;">::</span>~Deriv<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;deriv dest&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The output of this will be:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>a.out 
Base <span style="color: #000000; font-weight: bold;">*</span>b = new Base<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
delete b;
 <span style="color: #660033;">---</span> 
base dest
 <span style="color: #660033;">---</span> 
b destroyed
&nbsp;
Deriv <span style="color: #000000; font-weight: bold;">*</span>d = new Deriv<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
delete d
 <span style="color: #660033;">---</span> 
deriv dest
base dest
 <span style="color: #660033;">---</span> 
d destroyed
&nbsp;
Base <span style="color: #000000; font-weight: bold;">*</span>c = new Deriv<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
delete c
 <span style="color: #660033;">---</span> 
base dest
 <span style="color: #660033;">---</span> 
c destroyed</pre></div></div>

<p>Notice that when destroying c only the Base&#8217;s destructor is called. To fix this and have both Base and Deriv&#8217;s destructors called just make Base&#8217;s destructor virtual.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#ifndef BASE_H</span>
<span style="color: #339900;">#define BASE_H</span>
&nbsp;
<span style="color: #0000ff;">class</span> Base
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    <span style="color: #0000ff;">virtual</span> ~Base<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #339900;">#endif /* BASE_H */</span></pre></div></div>

<p>This simple change will cause the output to become:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>a.out 
Base <span style="color: #000000; font-weight: bold;">*</span>b = new Base<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
delete b;
 <span style="color: #660033;">---</span> 
base dest
 <span style="color: #660033;">---</span> 
b destroyed
&nbsp;
Deriv <span style="color: #000000; font-weight: bold;">*</span>d = new Deriv<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
delete d
 <span style="color: #660033;">---</span> 
deriv dest
base dest
 <span style="color: #660033;">---</span> 
d destroyed
&nbsp;
Base <span style="color: #000000; font-weight: bold;">*</span>c = new Deriv<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
delete c
 <span style="color: #660033;">---</span> 
deriv dest
base dest
 <span style="color: #660033;">---</span> 
c destroyed</pre></div></div>

<p>Now when destroying c the destructor for both Base and Deriv are called.</p>
<p>To compile</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">g++</span> base.cpp deriv.cpp main.cpp</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://john.nachtimwald.com/2010/05/29/c-derived-classes-and-object-destruction/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>lebookread</title>
		<link>http://john.nachtimwald.com/2010/05/16/lebookread/</link>
		<comments>http://john.nachtimwald.com/2010/05/16/lebookread/#comments</comments>
		<pubDate>Mon, 17 May 2010 01:52:56 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[lebookread]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[epub]]></category>
		<category><![CDATA[palmdoc]]></category>
		<category><![CDATA[pdb]]></category>
		<category><![CDATA[qt]]></category>
		<category><![CDATA[rb]]></category>
		<category><![CDATA[ztxt]]></category>

		<guid isPermaLink="false">http://john.nachtimwald.com/?p=358</guid>
		<description><![CDATA[I have been taking a short break from blogging again. The pressure at work has only increased and is eating into a lot of my time. I haven&#8217;t been motivated to work on personal projects because well they are work. However, this has recently changed a bit. I&#8217;ve started a Qt based library for reading [...]]]></description>
			<content:encoded><![CDATA[<p>I have been taking a short break from blogging again. The pressure at work has only increased and is eating into a lot of my time. I haven&#8217;t been motivated to work on personal projects because well they are work. However, this has recently changed a bit.</p>
<p>I&#8217;ve started a Qt based library for reading ebooks in a generic manner. It is called <a href="https://launchpad.net/lebookread">lebookread</a>! It is it&#8217;s early stages. So far I have it supporting epub, palmdoc pdb, ztxt pdb, tcr, and rb files. I plan to support ereader pdb, mobi, and plucker files in the near future.</p>
<p>The main goal of this project is to make reading ebooks easy for Qt based projects. I&#8217;ve chose to write the library in C++. This is also my first attempt at writing a library and it shows. I hope that it will be used by <a href="http://code.google.com/p/sigil/">Sigil</a>.</p>
<p>The real motivation of writing lebook read is I really want a good light weight ebook reader. The current offering have issues. I want something that is a bit more advanced in it&#8217;s rendering than <a href="http://www.fbreader.org/">FBReader</a>. I also didn&#8217;t want anything with as large a dependency list as <a href="http://calibre-ebook.com/">calibre</a>. So, I plan on using lebookread to write my own ebook viewer.</p>
]]></content:encoded>
			<wfw:commentRss>http://john.nachtimwald.com/2010/05/16/lebookread/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sending WM_DELETE_WINDOW client messages</title>
		<link>http://john.nachtimwald.com/2009/11/08/sending-wm_delete_window-client-messages/</link>
		<comments>http://john.nachtimwald.com/2009/11/08/sending-wm_delete_window-client-messages/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 14:31:58 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[KDocker]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[x11]]></category>
		<category><![CDATA[xlib]]></category>

		<guid isPermaLink="false">http://john.nachtimwald.com/?p=263</guid>
		<description><![CDATA[In my X11 Intercept Window Close Event post I left out one very important piece of information. How to actually close the embedded window after intercepting the WM_DELETE_WINDOW message. The best thing to do is send our own WM_DELETE_WINDOW message to the embedded window. This will keep the embedded window embedded until it actually closes. [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://john.nachtimwald.com/2009/11/01/x11-intercept-window-close-event/">X11 Intercept Window Close Event</a> post I left out one very important piece of information. How to actually close the embedded window after intercepting the WM_DELETE_WINDOW message. The best thing to do is send our own WM_DELETE_WINDOW message to the embedded window. This will keep the embedded window embedded until it actually closes. Meaning any question dialogs that might stop the window from closing (Gedit asking to save for instance) can be answered and it keeps the window embedded if the user decides not to close.</p>
<p>In the previous post we overrode the WM_DELETE_WINDOW message. All we need to do is send it on to the embedded window. This will keep it embedded until it actually closes. Sending a this message entails setting the type to ClinetMessage, message type to the WM_PROTOCOLS Atom, the first l data value to the WM_DELETE_WINDOW Atom, and the second l data value to CurrentTime. The format of course if 32. Send the message to the embedded window and it will try to close.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;QX11Info&gt;</span>
<span style="color: #339933;">#include &lt;X11/XLib.h&gt;</span>
&nbsp;
Display <span style="color: #339933;">*</span>display <span style="color: #339933;">=</span> QX11Info<span style="color: #339933;">::</span><span style="color: #202020;">display</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
XEvent ev<span style="color: #339933;">;</span>
&nbsp;
memset<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>ev<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span> <span style="color: #009900;">&#40;</span>ev<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
ev.<span style="color: #202020;">xclient</span>.<span style="color: #202020;">type</span> <span style="color: #339933;">=</span> ClientMessage<span style="color: #339933;">;</span>
ev.<span style="color: #202020;">xclient</span>.<span style="color: #202020;">window</span> <span style="color: #339933;">=</span> window<span style="color: #339933;">;</span>
ev.<span style="color: #202020;">xclient</span>.<span style="color: #202020;">message_type</span> <span style="color: #339933;">=</span> XInternAtom<span style="color: #009900;">&#40;</span>display<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;WM_PROTOCOLS&quot;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ev.<span style="color: #202020;">xclient</span>.<span style="color: #202020;">format</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">32</span><span style="color: #339933;">;</span>
ev.<span style="color: #202020;">xclient</span>.<span style="color: #202020;">data</span>.<span style="color: #202020;">l</span><span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> XInternAtom<span style="color: #009900;">&#40;</span>display<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;WM_DELETE_WINDOW&quot;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ev.<span style="color: #202020;">xclient</span>.<span style="color: #202020;">data</span>.<span style="color: #202020;">l</span><span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> CurrentTime<span style="color: #339933;">;</span>
XSendEvent<span style="color: #009900;">&#40;</span>display<span style="color: #339933;">,</span> window<span style="color: #339933;">,</span> False<span style="color: #339933;">,</span> NoEventMask<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>ev<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://john.nachtimwald.com/2009/11/08/sending-wm_delete_window-client-messages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>X11 Intercept Window Close Event</title>
		<link>http://john.nachtimwald.com/2009/11/01/x11-intercept-window-close-event/</link>
		<comments>http://john.nachtimwald.com/2009/11/01/x11-intercept-window-close-event/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 22:33:45 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[KDocker]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[x11]]></category>
		<category><![CDATA[xlib]]></category>

		<guid isPermaLink="false">http://john.nachtimwald.com/?p=255</guid>
		<description><![CDATA[***Note Nov, 8 2009: a few additions have been made to the code samples to make them more complete. Specifically subscribing to X11 events. A feature request for KDocker was made a few days ago for docking when closed. Basically the person wants the window to iconify when the X button on the window decoration [...]]]></description>
			<content:encoded><![CDATA[<p>***Note Nov, 8 2009: a few additions have been made to the code samples to make them more complete. Specifically subscribing to X11 events.</p>
<p>A feature request for KDocker was made a few days ago for <a href="https://bugs.launchpad.net/kdocker/+bug/460338">docking when closed</a>. Basically the person wants the window to iconify when the X button on the window decoration is clicked. It&#8217;s something I&#8217;ve been thinking about for quite some time but it&#8217;s not the easiest feature to implement. <a href="http://www.forwardbias.in/">Girish</a> thankfully pointed me onto the right path when he suggested looking into using a QX11EmbedContainer. I&#8217;ve gotten it working and it will be in the 4.3 release.</p>
<p>There isn&#8217;t a whole lot of documentation out there on achieving this so I&#8217;m going to detail how I&#8217;ve implemented the feature in KDocker. Do be awhare that there are a few short comings. Versions &lt;= 4.2 will iconify all windows associated with the docked one. Meaning Gimp and Audacious (XMMS) which have multiple windows will not have the extra windows iconify when the main one is. Also, decorationless windows like audacious (possibly Google Chrome) are no longer movable by clicking on the top of the window. Alt+Left Mouse Click still works and is currently the only way to move them.</p>
<p>The basic overview of the implementation is: Take the user started application window and get info about it (title, size, location, window decorations &#8230;). Create a container window. Use <a href="http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html">XEmbed</a> to put the window into the container. Set the container properties to those of the window. Intercept the WM_DELETE_WINDOW message. Forward other changes like title and icon from the window to the container.</p>
<p>Getting info about the window requires Xlib calls. I&#8217;m using the following to get the minimum size, current size, position, width, height and decorations. window is the window id for the application window. The decoration code requires mwmutil.h which can be found in libmotif and LessTif.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">Display <span style="color: #000040;">*</span>display <span style="color: #000080;">=</span> QX11Info<span style="color: #008080;">::</span><span style="color: #007788;">display</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
XSizeHints sizeHint<span style="color: #008080;">;</span>
<span style="color: #0000ff;">long</span> dummy<span style="color: #008080;">;</span>
Window root<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> x, y<span style="color: #008080;">;</span>
<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> width, height, border, depth<span style="color: #008080;">;</span>
&nbsp;
XGetWMNormalHints<span style="color: #008000;">&#40;</span>display, window, <span style="color: #000040;">&amp;</span>sizeHint, <span style="color: #000040;">&amp;</span>dummy<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
XGetGeometry<span style="color: #008000;">&#40;</span>display, window, <span style="color: #000040;">&amp;</span>root, <span style="color: #000040;">&amp;</span>x, <span style="color: #000040;">&amp;</span>y, <span style="color: #000040;">&amp;</span>width, <span style="color: #000040;">&amp;</span>height, <span style="color: #000040;">&amp;</span>border, <span style="color: #000040;">&amp;</span>depth<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
Atom wm_hints_atom <span style="color: #000080;">=</span> XInternAtom<span style="color: #008000;">&#40;</span>display, _XA_MOTIF_WM_HINTS, <span style="color: #0000ff;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>wm_data<span style="color: #008080;">;</span>
Atom wm_type<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> wm_format<span style="color: #008080;">;</span>
<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> wm_nitems, wm_bytes_after<span style="color: #008080;">;</span>
&nbsp;
XGetWindowProperty<span style="color: #008000;">&#40;</span>display, window, wm_hints_atom, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">sizeof</span> <span style="color: #008000;">&#40;</span>MotifWmHints<span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> <span style="color: #0000dd;">sizeof</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span><span style="color: #008000;">&#41;</span>, <span style="color: #0000ff;">false</span>, AnyPropertyType, <span style="color: #000040;">&amp;</span>wm_type, <span style="color: #000040;">&amp;</span>wm_format, <span style="color: #000040;">&amp;</span>wm_nitems, <span style="color: #000040;">&amp;</span>wm_bytes_after, <span style="color: #000040;">&amp;</span>wm_data<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Next create the container. I&#8217;m using QX11EmbedContainer because it is is a Qt provided XEmbed container widget.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">QX11EmbedContainer <span style="color: #000040;">*</span>container <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> QX11EmbedContainer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
container<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>embedClient<span style="color: #008000;">&#40;</span>window<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
container<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>show<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>We need to tell X that we want to receive certain events about the container and embedded window. If we don&#8217;t set the appropriate masks the events we are interested in will never be sent to our X11EventFilter.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">long</span> mask_container <span style="color: #000080;">=</span> StructureNotifyMask <span style="color: #000040;">|</span> PropertyChangeMask <span style="color: #000040;">|</span> VisibilityChangeMask <span style="color: #000040;">|</span> FocusChangeMask<span style="color: #008080;">;</span>
<span style="color: #0000ff;">long</span> mask_embed <span style="color: #000080;">=</span> PropertyChangeMask<span style="color: #008080;">;</span>
&nbsp;
XWindowAttributes attr_container<span style="color: #008080;">;</span>
XWindowAttributes attr_embed<span style="color: #008080;">;</span>
&nbsp;
XGetWindowAttributes<span style="color: #008000;">&#40;</span>display, m_container<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>winId<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #000040;">&amp;</span>attr_container<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
XGetWindowAttributes<span style="color: #008000;">&#40;</span>display, window, <span style="color: #000040;">&amp;</span>attr_embed<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>attr_container.<span style="color: #007788;">your_event_mask</span> <span style="color: #000040;">&amp;</span> mask_container<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> mask_container<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    XSelectInput<span style="color: #008000;">&#40;</span>display, m_container<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>winId<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, attr_container.<span style="color: #007788;">your_event_mask</span> <span style="color: #000040;">|</span> mask_container<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>attr_container.<span style="color: #007788;">your_event_mask</span> <span style="color: #000040;">&amp;</span> mask_embed<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> mask_embed<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    XSelectInput<span style="color: #008000;">&#40;</span>display, window, attr_embed.<span style="color: #007788;">your_event_mask</span> <span style="color: #000040;">|</span> mask_embed<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now we want to register WM_DELETE_WINDOW so that instead of closing it will be directed to us as a client message. We will check for the message in the X11EventFilter and take appropriate action. This will only work because we have reparented the window by embedding it into the container we control. Simply setting this on the window will not work.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">Atom wm_delete <span style="color: #000080;">=</span> XInternAtom<span style="color: #008000;">&#40;</span>display, <span style="color: #FF0000;">&quot;WM_DELETE_WINDOW&quot;</span>, False<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
XSetWMProtocols<span style="color: #008000;">&#40;</span>display, container<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>winId<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #000040;">&amp;</span>wm_delete, <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Take all the info we obtained from the window and apply it to our container. This way it will look and behave the same as if it was not embedded.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">m_container<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setMinimumSize<span style="color: #008000;">&#40;</span>m_sizeHint.<span style="color: #007788;">min_width</span>, m_sizeHint.<span style="color: #007788;">min_height</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
m_container<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setGeometry<span style="color: #008000;">&#40;</span>x, y, width, height<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>wm_type <span style="color: #000080;">==</span> None<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    MotifWmHints hints<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">memset</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>hints, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">sizeof</span> <span style="color: #008000;">&#40;</span>hints<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    hints.<span style="color: #007788;">flags</span> <span style="color: #000080;">=</span> MWM_HINTS_DECORATIONS<span style="color: #008080;">;</span>
    hints.<span style="color: #007788;">decorations</span> <span style="color: #000080;">=</span> MWM_DECOR_ALL<span style="color: #008080;">;</span>
    XChangeProperty<span style="color: #008000;">&#40;</span>display, m_container<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>winId<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, wm_hints_atom, wm_hints_atom, <span style="color: #0000dd;">32</span>, PropModeReplace, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span> hints, <span style="color: #0000dd;">sizeof</span> <span style="color: #008000;">&#40;</span>MotifWmHints<span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> <span style="color: #0000dd;">sizeof</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
    MotifWmHints <span style="color: #000040;">*</span>hints<span style="color: #008080;">;</span>
    hints <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>MotifWmHints <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> wm_data<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span><span style="color: #008000;">&#40;</span>hints<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>flags <span style="color: #000040;">&amp;</span> MWM_HINTS_DECORATIONS<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        hints<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>flags <span style="color: #000040;">|</span><span style="color: #000080;">=</span> MWM_HINTS_DECORATIONS<span style="color: #008080;">;</span>
        hints<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>decorations <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    XChangeProperty<span style="color: #008000;">&#40;</span>display, m_container<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>winId<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, wm_hints_atom, wm_hints_atom, <span style="color: #0000dd;">32</span>, PropModeReplace, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> hints, <span style="color: #0000dd;">sizeof</span> <span style="color: #008000;">&#40;</span>MotifWmHints<span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> <span style="color: #0000dd;">sizeof</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
XFree<span style="color: #008000;">&#40;</span>wm_data<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Thats it for constructing the window but there are a few important pieces we still need to worry about. Within QApplication&#8217;s x11EventFilter we need to processes some events.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">bool</span> x11EventFilter<span style="color: #008000;">&#40;</span>XEvent <span style="color: #000040;">*</span>ev<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    XAnyEvent <span style="color: #000040;">*</span>event <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>XAnyEvent <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> ev<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>event<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>window <span style="color: #000080;">==</span> m_container<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>winId<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>ev<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>type <span style="color: #000080;">==</span> ClientMessage <span style="color: #000040;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span>ulong<span style="color: #008000;">&#41;</span> ev<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>xclient.<span style="color: #007788;">data</span>.<span style="color: #007788;">l</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> XInternAtom<span style="color: #008000;">&#40;</span>QX11Info<span style="color: #008080;">::</span><span style="color: #007788;">display</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">&quot;WM_DELETE_WINDOW&quot;</span>, False<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>m_iconifyOnClose<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                iconifyWindow<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
                close<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>event<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>window <span style="color: #000080;">==</span> window<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>event<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>type <span style="color: #000080;">==</span> PropertyNotify<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">return</span> propertyChangeEvent<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>XPropertyEvent <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> event<span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>atom<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">bool</span> propertyChangeEvent<span style="color: #008000;">&#40;</span>Atom property<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    Display <span style="color: #000040;">*</span>display <span style="color: #000080;">=</span> QX11Info<span style="color: #008080;">::</span><span style="color: #007788;">display</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">static</span> Atom WM_NAME <span style="color: #000080;">=</span> XInternAtom<span style="color: #008000;">&#40;</span>display, <span style="color: #FF0000;">&quot;WM_NAME&quot;</span>, True<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>property <span style="color: #000080;">==</span> WM_NAME<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        updateTitle<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> updateTitle<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    Display <span style="color: #000040;">*</span>display <span style="color: #000080;">=</span> QX11Info<span style="color: #008080;">::</span><span style="color: #007788;">display</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>windowName <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    QString title<span style="color: #008080;">;</span>
&nbsp;
    XFetchName<span style="color: #008000;">&#40;</span>display, window, <span style="color: #000040;">&amp;</span>windowName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    title <span style="color: #000080;">=</span> windowName<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>windowName<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        XFree<span style="color: #008000;">&#40;</span>windowName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    container<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setWindowTitle<span style="color: #008000;">&#40;</span>title<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://john.nachtimwald.com/2009/11/01/x11-intercept-window-close-event/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>KDocker 4.2 released</title>
		<link>http://john.nachtimwald.com/2009/09/27/kdocker-4-2-released/</link>
		<comments>http://john.nachtimwald.com/2009/09/27/kdocker-4-2-released/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 14:13:30 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[KDocker]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[qt]]></category>
		<category><![CDATA[release]]></category>

		<guid isPermaLink="false">http://john.nachtimwald.com/?p=228</guid>
		<description><![CDATA[This release includes bug fixes as usual. Some new features: bash completion, iconify on focus lost option and Italian translation thanks to Alessio Cassibba. There is also a small change to the behavior when activating the tray icon. If the window is not visible it will become active and if it is active it will [...]]]></description>
			<content:encoded><![CDATA[<p>This release includes bug fixes as usual. Some new features: bash completion, iconify on focus lost option and Italian translation thanks to Alessio Cassibba. There is also a small change to the behavior when activating the tray icon. If the window is not visible it will become active and if it is active it will iconify. You can get it at the <a href="https://launchpad.net/kdocker">launchpad page</a>.</p>
<p>Somehow I forget to mention the 4.1 release last week&#8230; I don&#8217;t think I&#8217;m going to be getting into the habit of making a release each week but there is still more I have planned. Mostly refactoring, and not to much in the way of new features.</p>
]]></content:encoded>
			<wfw:commentRss>http://john.nachtimwald.com/2009/09/27/kdocker-4-2-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>KDocker 4.0</title>
		<link>http://john.nachtimwald.com/2009/09/16/kdocker-4-0/</link>
		<comments>http://john.nachtimwald.com/2009/09/16/kdocker-4-0/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 11:43:12 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[KDocker]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[qt]]></category>

		<guid isPermaLink="false">http://john.nachtimwald.com/?p=224</guid>
		<description><![CDATA[Recently I&#8217;ve become the maintainer of the KDocker project. KDocker is a Qt application what allows you dock any application into the system tray. It currently supports any X Windows system. What I&#8217;ve done for the 4.0 release is, move the project to launchpad (Girish, the creator of the project is locked out of the [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve become the maintainer of the KDocker project. KDocker is a Qt application what allows you dock any application into the system tray. It currently supports any X Windows system. What I&#8217;ve done for the 4.0 release is, move the project to launchpad (Girish, the creator of the project is locked out of the Source Forge page) and port/re-write the entire app to use Qt 4.</p>
<p>The port/re-write to Qt 4 is complete and I&#8217;ve released it. The version has jumped from 1.3 to 4.0 to better illustrate the the severity of this change. Also, it takes it to a similar version scheme to Qt and KDE.</p>
<p>There are a few things to note about this release and KDocker in general. For this release session management and auto starting have been removed. This is mainly because modern desktop environments support both of these very well. Also, I believe that environments that do not support this are better of using a dedicated application instead of having the functionality rolled into a docker. Another point to note is KDocker 4.0 as well as the older version are pure Qt and Xlib applications. They do not depend on KDE.</p>
<p>The new project location is at <a href="https://launchpad.net/kdocker">https://launchpad.net/kdocker</a> and you can download the 4.0 release at <a href="https://launchpad.net/kdocker/+download">https://launchpad.net/kdocker/+download</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://john.nachtimwald.com/2009/09/16/kdocker-4-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>eBook Adding Empty Lines At End of File</title>
		<link>http://john.nachtimwald.com/2008/12/22/ebook-adding-empty-lines-at-end-of-file/</link>
		<comments>http://john.nachtimwald.com/2008/12/22/ebook-adding-empty-lines-at-end-of-file/#comments</comments>
		<pubDate>Tue, 23 Dec 2008 01:56:05 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[ebook]]></category>
		<category><![CDATA[qt]]></category>

		<guid isPermaLink="false">http://john.nachtimwald.com/?p=50</guid>
		<description><![CDATA[Continuing my work to clean up my eBooks I&#8217;ve written another little tool to help. I like for my eBooks to have two blank lines at the end of the file. The only major caveat of this one is it assumes Unix end of lines. Meaning a single \n character. In order for this to [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing my work to clean up my eBooks I&#8217;ve written another little tool to help. I like for my eBooks  to have two blank lines at the end of the file.</p>
<p>The only major caveat of this one is it assumes Unix end of lines. Meaning a single \n character. In order for this to work correctly use of the dos2unix tool is necessary for files that use a different new line format.</p>
<p>fix_end_ebook_txt.cpp</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">/*
Copyright (c) 2008 John Schember 
&nbsp;
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the &quot;Software&quot;), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
&nbsp;
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
&nbsp;
THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">/*
Ensures that there are 3 newline characters at the end of the file (two blank
lines after the last of the text). This assumes Unix \n line characters. Please
use dos2unix before running to ensure that the end of line characters are
correct.
*/</span>
&nbsp;
<span style="color: #339900;">#include &lt;QFile&gt;</span>
<span style="color: #339900;">#include &lt;QString&gt;</span>
<span style="color: #339900;">#include &lt;QTextStream&gt;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">**</span>argv<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// Stream to write errors to the console.</span>
    QTextStream errStream<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">stderr</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Store for the contents of the ebook.</span>
    QString content<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// We need an ebook file to work on.</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>argc <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        errStream <span style="color: #000080;">&lt;&lt;</span> QObject<span style="color: #008080;">::</span><span style="color: #007788;">tr</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Error: No input file&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    QFile ebook<span style="color: #008000;">&#40;</span>argv<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>ebook.<span style="color: #007788;">open</span><span style="color: #008000;">&#40;</span>QIODevice<span style="color: #008080;">::</span><span style="color: #007788;">ReadWrite</span> <span style="color: #000040;">|</span> QIODevice<span style="color: #008080;">::</span><span style="color: #007788;">Text</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        errStream <span style="color: #000080;">&lt;&lt;</span> QObject<span style="color: #008080;">::</span><span style="color: #007788;">tr</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Error: Could not open&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #666666;">// We use a QTextStream to actually work on the file.</span>
    QTextStream ioStream<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>ebook<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// We want to see what the last 3 characters are at the end of the file.</span>
    ioStream.<span style="color: #007788;">seek</span><span style="color: #008000;">&#40;</span>ebook.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> <span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    content <span style="color: #000080;">=</span> ioStream.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Move to the end of the file because we want to add newlines (\n&amp;#39;s) to</span>
    <span style="color: #666666;">// the end.</span>
    ioStream.<span style="color: #007788;">seek</span><span style="color: #008000;">&#40;</span>ebook.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// We want 3 newline (\n) characters at the end of the file. Add them until</span>
    <span style="color: #666666;">// they total 3.</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span> <span style="color: #000040;">-</span> content.<span style="color: #007788;">count</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        ioStream <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #000080;">&lt;&lt;</span> flush<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    ebook.<span style="color: #007788;">close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://john.nachtimwald.com/2008/12/22/ebook-adding-empty-lines-at-end-of-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>eBook Paragraph Formating</title>
		<link>http://john.nachtimwald.com/2008/12/21/ebook-paragraph-formating/</link>
		<comments>http://john.nachtimwald.com/2008/12/21/ebook-paragraph-formating/#comments</comments>
		<pubDate>Sun, 21 Dec 2008 20:56:59 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[ebook]]></category>
		<category><![CDATA[qt]]></category>

		<guid isPermaLink="false">http://john.nachtimwald.com/?p=35</guid>
		<description><![CDATA[Today I wrote two simple programs to help me clean up my ebooks. I prefer to keep my ebook collection as plain text files with paragraphs separated by a blank line. The first program reflows the paragraphs to put each on a single line. The second removes extraneous whitespace from the file. The reflow is [...]]]></description>
			<content:encoded><![CDATA[<p>Today I wrote two simple programs to help me clean up my ebooks. I prefer to keep my ebook collection as plain text files with paragraphs separated by a blank line. The first program reflows the paragraphs to put each on a single line. The second removes extraneous whitespace from the file.</p>
<p>The reflow is the more intensive of the two. I ran it on the largest ebook I have, Project Gutenberg&#8217;s War and Peace by Leo Tolstoy. The file is 3.1 MB.</p>
<p>Time to run: 7m35.494s.<br />
Memory usage: 13.1 MB according to gnome-system-monitor.</p>
<p>Right now I&#8217;m loading the entire book into memory and using QStrings to work on it. Memory usage is about 4.5 x the size of the book. Thankfully plain text ebooks are fairly small. Later I&#8217;m going to look into optimizing it for size and hopefully speed.</p>
<p>Without further ado here are the two. They are MIT licensed and use the Qt tool kit.</p>
<p>fix_paragraphs_ebook_txt.cpp</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">/*
Copyright (c) 2008 John Schember 
&nbsp;
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the &quot;Software&quot;), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
&nbsp;
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
&nbsp;
THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">/*
Reflows txt file ebook paragraphs. Paragraphs should be separated by a blank
line. Takes paragraphs that have hard breaks and puts all lines onto a single
line.
&nbsp;
For Example:
&nbsp;
INPUT
&nbsp;
This is a multi line paragraph. It comprises
a few lines but has hard
breaks.
&nbsp;
Now for the second
borken apart paragraph.
&nbsp;
OUTPUT
&nbsp;
This is a multi line paragraph. It comprises a few lines but has hard breaks.
&nbsp;
Now for the second broken apart paragraph.
*/</span>
&nbsp;
<span style="color: #339900;">#include &lt;QFile&gt;</span>
<span style="color: #339900;">#include &lt;QRegExp&gt;</span>
<span style="color: #339900;">#include &lt;QString&gt;</span>
<span style="color: #339900;">#include &lt;QTextStream&gt;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span><span style="color: #000040;">**</span> argv<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// Stream to write errors to the console.</span>
    QTextStream errStream<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">stderr</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Regular expression to search for broken paragraphs. Works by looking</span>
    <span style="color: #666666;">// for char newline char. A proper ebook should have paragraphs separated</span>
    <span style="color: #666666;">// by a blank line meaning char newline newline char.</span>
    QRegExp re<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;[^<span style="color: #000099; font-weight: bold;">\n</span>]<span style="color: #000099; font-weight: bold;">\n</span>[^<span style="color: #000099; font-weight: bold;">\n</span>]&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Store for the contents of the ebook.</span>
    QString content<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// We need an ebook file to work on.</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>argc <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        errStream <span style="color: #000080;">&lt;&lt;</span> QObject<span style="color: #008080;">::</span><span style="color: #007788;">tr</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Error: No input file&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    QFile ebook<span style="color: #008000;">&#40;</span>argv<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>ebook.<span style="color: #007788;">open</span><span style="color: #008000;">&#40;</span>QIODevice<span style="color: #008080;">::</span><span style="color: #007788;">ReadWrite</span> <span style="color: #000040;">|</span> QIODevice<span style="color: #008080;">::</span><span style="color: #007788;">Text</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        errStream <span style="color: #000080;">&lt;&lt;</span> QObject<span style="color: #008080;">::</span><span style="color: #007788;">tr</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Error: Could not open&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #666666;">// We use a QTextStream to actually work on the file.</span>
    QTextStream ioStream<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>ebook<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">// Read the entire file contents into memory.</span>
    content <span style="color: #000080;">=</span> ioStream.<span style="color: #007788;">readAll</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>content.<span style="color: #007788;">contains</span><span style="color: #008000;">&#40;</span>re<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #666666;">// Remove the newline when there is a match with the regular expression.</span>
        content <span style="color: #000080;">=</span> content.<span style="color: #007788;">replace</span><span style="color: #008000;">&#40;</span>content.<span style="color: #007788;">indexOf</span><span style="color: #008000;">&#40;</span>re<span style="color: #008000;">&#41;</span><span style="color: #000040;">+</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">1</span>, <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #666666;">// Truncate the ebook so we don&amp;#39;t end up with the original contents after</span>
    <span style="color: #666666;">// our modified contents.</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>ebook.<span style="color: #007788;">resize</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        errStream <span style="color: #000080;">&lt;&lt;</span> QObject<span style="color: #008080;">::</span><span style="color: #007788;">tr</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Error: Could not truncate file&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #666666;">// Store the modified content back on disk.</span>
    ioStream.<span style="color: #007788;">seek</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    ioStream <span style="color: #000080;">&lt;&lt;</span> content<span style="color: #008080;">;</span>
&nbsp;
    ebook.<span style="color: #007788;">close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>remove_extra_whitespace_ebook_txt.cpp</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">/*
Copyright (c) 2008 John Schember 
&nbsp;
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the &quot;Software&quot;), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
&nbsp;
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
&nbsp;
THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">/*
Removes extraneous whitespace in a txt file ebook. This will remove every
'\t', '\v', '\f', '\r', and will replace multiple occurrences ' ' with a single
one.
&nbsp;
For Example:
&nbsp;
INPUT
&nbsp;
      This     is a bad                          line.
&nbsp;
Now for  the     second     borken line.
&nbsp;
OUTPUT
&nbsp;
This is a bad line.
&nbsp;
Now for the second borken line.
&nbsp;
*/</span>
&nbsp;
<span style="color: #339900;">#include </span>
<span style="color: #339900;">#include </span>
<span style="color: #339900;">#include </span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">**</span>argv<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// Stream to write errors to the console.</span>
    QTextStream errStream<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">stderr</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Store for the contents of the ebook.</span>
    QString content<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// We need an ebook file to work on.</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>argc <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        errStream <span style="color: #000080;">&lt;&lt;</span> QObject<span style="color: #008080;">::</span><span style="color: #007788;">tr</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Error: No input file&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    QFile ebook<span style="color: #008000;">&#40;</span>argv<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>ebook.<span style="color: #007788;">open</span><span style="color: #008000;">&#40;</span>QIODevice<span style="color: #008080;">::</span><span style="color: #007788;">ReadWrite</span> <span style="color: #000040;">|</span> QIODevice<span style="color: #008080;">::</span><span style="color: #007788;">Text</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        errStream <span style="color: #000080;">&lt;&lt;</span> QObject<span style="color: #008080;">::</span><span style="color: #007788;">tr</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Error: Could not open&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #666666;">// We use a QTextStream to actually work on the file.</span>
    QTextStream ioStream<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>ebook<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Read every line and remove the extras we don&amp;#39;t want.</span>
    <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>ioStream.<span style="color: #007788;">atEnd</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        content <span style="color: #000040;">+</span><span style="color: #000080;">=</span> ioStream.<span style="color: #007788;">readLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>.<span style="color: #007788;">simplified</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #666666;">// Truncate the ebook so we don&amp;#39;t end up with the original contents after</span>
    <span style="color: #666666;">// our modified contents.</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>ebook.<span style="color: #007788;">resize</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        errStream <span style="color: #000080;">&lt;&lt;</span> QObject<span style="color: #008080;">::</span><span style="color: #007788;">tr</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Error: Could not truncate file&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #666666;">// Store the modified content back on disk.</span>
    ioStream.<span style="color: #007788;">seek</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    ioStream <span style="color: #000080;">&lt;&lt;</span> content<span style="color: #008080;">;</span>
&nbsp;
    ebook.<span style="color: #007788;">close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://john.nachtimwald.com/2008/12/21/ebook-paragraph-formating/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
