Root/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | < 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 209d6fa3805c created 11 years 11 months ago. By Nathan Adams, Updating tparray to use variants as array because tpodlist was causing variant data corruption |
---|