<?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>jd&#039;s &#187; C++</title>
	<atom:link href="http://disjunkt.com/jd/t/en/cplusplus/feed/" rel="self" type="application/rss+xml" />
	<link>http://disjunkt.com/jd</link>
	<description>/usr/[misc&#124;share]/*</description>
	<lastBuildDate>Thu, 09 Jan 2014 13:35:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>on the way to C++ (unix-)daemons &#8230; : FILE* to iostream</title>
		<link>http://disjunkt.com/jd/2010/en/cplusplus/on-the-way-to-cpp-unix-daemons-file-to-iostream-25/</link>
		<comments>http://disjunkt.com/jd/2010/en/cplusplus/on-the-way-to-cpp-unix-daemons-file-to-iostream-25/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 09:41:39 +0000</pubDate>
		<dc:creator>jd</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://disjunkt.com/jd/?p=25</guid>
		<description><![CDATA[turning FILE* into iostream]]></description>
			<content:encoded><![CDATA[<p>Writing a unix daemon using C++, the idea might seem odd. But it&#8217;s desirable in many ways&nbsp;:</p>
<ul>
<li>C++ provides a lot of methods which are inherently safe and neat regarding string treatments,</li>
<li><a href="http://en.wikipedia.org/wiki/Standard_Template_Library">STL</a><a href="http://www.sgi.com/tech/stl/">(sgi)</a> provides a lot of nice containers, particularly the associative map,</li>
<li>convenient simple text input/output using iostream &#8230;</li>
</ul>
<p>Now, coding a daemon implies a lot of very unix-native C details such as : properly detach from terminals, output some errors via syslog &#8230;</p>
<p>When one realises all those functionnalites have simple posix-C functions to take care of, a very convenient bridge from C to C++ is to bind a regular iostream to a FILE* descriptor.</p>
<p><strong>Example : reading from a pipe using istream</strong><br />
The below posix-C function :</p>
<pre>FILE *popen(const char *command, const char *type);</pre>
<p>provides a very simple way to fork and read to (or write from) another process.  The <a href="http://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_io.html">gnu iostream library documentation</a> mentions some methods usable to turn FILE* into stream, but I would not say the said documentation made it straighforward. So here&#8217;s an example of such a pipe :</p>
<pre>#include &lt;errno.h&gt;
#include &lt;string.h&gt;     // strerror

#include &lt;iostream&gt;
#include &lt;ext/stdio_filebuf.h&gt;  // __gnu_cxx::stdio_filebuf

using namespace std;

void filter_uggly_uppercase (istream &amp;in, ostream &amp;out) {
    char c;
    while (in &amp;&amp; in.get(c)) {
        if ((c&gt;='a') &amp;&amp; (c&lt;='z'))
            out &lt;&lt; (char)('A' - 'a' + c);
        else
            out &lt;&lt; c;
    }
}

int main (void) {
    const char *command = "find . -type f";

    FILE *f_pipe_in = popen (command, "r");
    if (f_pipe_in == NULL) {
        int e = errno;
        cerr &lt;&lt; "error at piping from " &lt;&lt; command &lt;&lt; " : " &lt;&lt; strerror (e) &lt;&lt; endl;
        return 1;
    }

    // let's build a buffer from the FILE* descriptor ...
    __gnu_cxx::stdio_filebuf&lt;char&gt; pipe_buf (f_pipe_in, ios_base::in);

    // there we are, a regular istream is build upon the buffer :
    istream stream_pipe_in (&amp;pipe_buf);

    // let's read from that pipe now, the usual way ...
    filter_uggly_uppercase (stream_pipe_in, cout);

    pclose (f_pipe_in);

    return 0;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://disjunkt.com/jd/2010/en/cplusplus/on-the-way-to-cpp-unix-daemons-file-to-iostream-25/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
