source: translator/examples/iostream.c @ 0b8cd722

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 0b8cd722 was 134b86a, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

add compiler flag to driver, update examples, fix unnamed bit fields

  • Property mode set to 100644
File size: 1.6 KB
RevLine 
[51b7345]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>
[134b86a]10//#include <string.h>
11//#include <ctype.h>
12typedef long unsigned int size_t;
13size_t strlen(const char *s);
[51b7345]14}
15
16forall( dtype ostype | ostream( ostype ) )
[134b86a]17ostype * ?<<?( ostype *os, char c ) {
18    return write( os, &c, 1 );
[51b7345]19}
20
21forall( dtype ostype | ostream( ostype ) )
[134b86a]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 ) );
[51b7345]26}
27
28forall( dtype ostype | ostream( ostype ) )
[134b86a]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 ) );
[51b7345]38}
39
40forall( dtype istype | istream( istype ) )
[134b86a]41istype * ?>>?( istype *is, char *cp ) {
42    return read( is, cp, 1 );
[51b7345]43}
44
45forall( dtype istype | istream( istype ) )
[134b86a]46istype * ?>>?( istype *is, int *ip ) {
47    char cur;
[51b7345]48 
[134b86a]49    // skip some whitespace
50    do {
51        is >> &cur;
52        if( fail( is ) || eof( is ) ) return is;
53    } while( !( cur >= '0' && cur <= '9' ) );
[51b7345]54 
[134b86a]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    }
[51b7345]62 
[134b86a]63    unread( is, cur );
64    return is;
[51b7345]65}
Note: See TracBrowser for help on using the repository browser.