source: src/examples/iostream.c @ 843054c2

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 843054c2 was 843054c2, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

licencing: seventh groups of files

  • Property mode set to 100644
File size: 1.6 KB
Line 
1// "cfa -c -o iostream.o iostream.c"
2// "cfa -v -E iostream.c > iostream_out.c"
3// "cfa -CFA iostream.c > iostream_out.c"
4// "cfa iostream_out.c"
5// "gcc32 iostream_out.c LibCfa/libcfa.a"
6
7#include "iostream.h"
8extern "C" {
9#include <stdio.h>
10//#include <string.h>
11//#include <ctype.h>
12typedef long unsigned int size_t;
13size_t strlen(const char *s);
14}
15
16forall( dtype ostype | ostream( ostype ) )
17ostype * ?<<?( ostype *os, char c ) {
18    return write( os, &c, 1 );
19}
20
21forall( dtype ostype | ostream( ostype ) )
22ostype * ?<<?( ostype *os, int i ) {
23    char buffer[20];      // larger than the largest integer
24    sprintf( buffer, "%d", i );
25    return write( os, buffer, strlen( buffer ) );
26}
27
28forall( dtype ostype | ostream( ostype ) )
29ostype * ?<<?( ostype *os, double d ) {
30    char buffer[32];      // larger than the largest double
31    sprintf( buffer, "%g", d );
32    return write( os, buffer, strlen( buffer ) );
33}
34
35forall( dtype ostype | ostream( ostype ) )
36ostype * ?<<?( ostype *os, const char *cp ) {
37    return write( os, cp, strlen( cp ) );
38}
39
40forall( dtype istype | istream( istype ) )
41istype * ?>>?( istype *is, char *cp ) {
42    return read( is, cp, 1 );
43}
44
45forall( dtype istype | istream( istype ) )
46istype * ?>>?( istype *is, int *ip ) {
47    char cur;
48 
49    // skip some whitespace
50    do {
51        is >> &cur;
52        if ( fail( is ) || eof( is ) ) return is;
53    } while ( !( cur >= '0' && cur <= '9' ) );
54 
55    // accumulate digits
56    *ip = 0;
57    while ( cur >= '0' && cur <= '9' ) {
58        *ip = *ip * 10 + ( cur - '0' );
59        is >> &cur;
60        if ( fail( is ) || eof( is ) ) return is;
61    }
62 
63    unread( is, cur );
64    return is;
65}
Note: See TracBrowser for help on using the repository browser.