ptypes

ptypes Mercurial Source Tree


Root/doc/streams.examples.html

<html><!-- #BeginTemplate "/Templates/tmpl.dwt" --><!-- DW6 -->
<head>
<!-- #BeginEditable "doctitle" --> 
<title>PTypes: streams: examples</title>
<!-- #EndEditable --> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="styles.css">
</head>
<body bgcolor="#FFFFFF" leftmargin="40" marginwidth="40">
<p><a href="../index.html"><img src="title-21.png" width="253" height="39" alt="C++ Portable Types Library (PTypes) Version 2.1" border="0"></a> 
<hr size="1" noshade>
<!-- #BeginEditable "body" --> 
<p class="hpath"><a href="index.html">Top</a>: <a href="streams.html">Streams</a>: 
Examples </p>
<p><b>Example 1.</b> This simple program creates a new file and writes a string 
to it.</p>
<blockquote> 
<pre>#include <pstreams.h>

USING_PTYPES

int main() 
{
    <span class="comment">// the outfile object is declared and constructed outside 
    // the try...catch clause, since the exception object
    // contains a reference to the stream that caused the error.
    // besides, stream constructors and destructors in PTypes 
    // never throw exceptions.</span>
    outfile f(fname);

    f.set_bufsize(1024);             <span class="comment">// the default value in this version is 8192</span>

    try 
    {
        f.open();
        f.put("This is a test file.");
        f.close();
    }
    catch (estream* e) 
    {
        perr.putf("File error: %s\n", e->get_message());
        delete e;
    }
    return 0;
}

</pre>
</blockquote>
<p><b>Example 2.</b> This program reads a C source, extracts identifiers and builds 
a usage dictionary. It does not understand C comments and string literals though, 
but can be easily improved to understand them too.</p>
<blockquote> 
<pre>#include <ptypes.h>
#include <pstreams.h>

USING_PTYPES

const cset letters("_A-Za-z");
const cset digits("0-9");
const cset identchars = letters + digits;
const cset otherchars = !letters;

void main(int argc, char* argv[]) 
{
    tstrlist<void*> dic(SL_SORTED | SL_CASESENS);

    infile f(argv[1]);

    try 
    {
        f.open();

        while (!f.get_eof()) 
        {
            char c = f.preview();

            <span class="comment">// a C identifier begins with a letter</span>
            if (c & letters)
            {
                <span class="comment">// ... and may contain letters and digits</span>
                string ident = f.token(identchars);
                int i;
                if (!dic.search(ident, i))
                    dic.ins(i, ident, 0);
            }

            else
                <span class="comment">// ignore everything else</span>
                f.skiptoken(otherchars);
        }

    }

    catch (estream* e) 
    {
        pout.putf("Error: %s\n", e->get_message());
        delete e;
    }

    <span class="comment">// now print the dictionary</span>
    for (int i = 0; i < dic.get_count(); i++)
        pout.putline(dic.getkey(i));
}
</pre>
</blockquote>
<p class="seealso">See also: <a href="streams.iobase.html">iobase</a>, <a href="streams.instm.html">instm</a>, 
<a href="streams.infile.html">infile</a>, <a href="streams.outstm.html">outstm</a>, 
<a href="streams.outfile.html">outfile</a>, <a href="streams.errors.html">Error 
handling</a></p>
<!-- #EndEditable -->
<hr size="1">
<a href="../index.html" class="ns">PTypes home</a>
</body>
<!-- #EndTemplate --></html>
Source at commit 8edbcdac0d39 created 11 years 8 months ago.
By Nathan Adams, initial commit

Archive Download this file

Branches

Tags

Page rendered in 0.76696s using 11 queries.