source: src/examples/iostream.c @ b18b0b5

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 b18b0b5 was cf16f94, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

create temporary return variable for return expressions, remove unnecessary code after temporary-return-variable change, fix missing lvalue qualifiers, change stream operator to |

  • Property mode set to 100644
File size: 2.9 KB
RevLine 
[86bd7c1f]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// iostream.c --
8//
9// Author           : Richard C. Bilson
10// Created On       : Wed May 27 17:56:53 2015
11// Last Modified By : Peter A. Buhr
[cf16f94]12// Last Modified On : Mon Dec  7 23:08:02 2015
13// Update Count     : 24
[86bd7c1f]14//
[51b7345]15
16#include "iostream.h"
17extern "C" {
18#include <stdio.h>
[839ccbb]19#include <string.h>                                                                             // strlen
[51b7345]20}
21
22forall( dtype ostype | ostream( ostype ) )
[cf16f94]23ostype * ?|?( ostype *os, char c ) {
[86bd7c1f]24        return write( os, &c, 1 );
[cf16f94]25} // ?|?
[51b7345]26
27forall( dtype ostype | ostream( ostype ) )
[cf16f94]28ostype * ?|?( ostype *os, int i ) {
[e56cfdb0]29        char buffer[32];                                                                        // larger than the largest integer
[86bd7c1f]30        sprintf( buffer, "%d", i );
31        return write( os, buffer, strlen( buffer ) );
[cf16f94]32} // ?|?
[51b7345]33
34forall( dtype ostype | ostream( ostype ) )
[cf16f94]35ostype * ?|?( ostype *os, double d ) {
[e56cfdb0]36        char buffer[32];                                                                        // larger than the largest double
[86bd7c1f]37        sprintf( buffer, "%g", d );
38        return write( os, buffer, strlen( buffer ) );
[cf16f94]39} // ?|?
[134b86a]40
41forall( dtype ostype | ostream( ostype ) )
[cf16f94]42ostype * ?|?( ostype *os, const char *cp ) {
[86bd7c1f]43        return write( os, cp, strlen( cp ) );
[cf16f94]44} // ?|?
[51b7345]45
[e56cfdb0]46forall( dtype ostype | ostream( ostype ) )
[cf16f94]47ostype * ?|?( ostype *os, const void *p ) {
[e56cfdb0]48        char buffer[32];                                                                        // larger than the largest pointer
49        sprintf( buffer, "%p", p );
50        return write( os, buffer, strlen( buffer ) );
[cf16f94]51} // ?|?
52
53
54forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) ) 
55retostype * ?|?( ostype *os, retostype * (*manip)(ostype*) ) {
56  return manip(os);
57}
58
59forall( dtype ostype | ostream( ostype ) ) 
60ostype * endl( ostype * os ) {
61  os | "\n";
62  // flush
63  return os;
64} // endl
[e56cfdb0]65
66forall( type elt_type | writeable( elt_type ),
67                type iterator_type | iterator( iterator_type, elt_type ),
68                dtype os_type | ostream( os_type ) )
69void write( iterator_type begin, iterator_type end, os_type *os ) {
70        void print( elt_type i ) {
[cf16f94]71                os | i | ' ';
[e56cfdb0]72        }
73        for_each( begin, end, print );
[cf16f94]74} // ?|?
[e56cfdb0]75
76forall( type elt_type | writeable( elt_type ),
77                type iterator_type | iterator( iterator_type, elt_type ),
78                dtype os_type | ostream( os_type ) )
79void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
[cf16f94]80        void print( elt_type i ) { os | i | ' '; }
[e56cfdb0]81        for_each_reverse( begin, end, print );
[cf16f94]82} // ?|?
[e56cfdb0]83
84
[51b7345]85forall( dtype istype | istream( istype ) )
[cf16f94]86istype * ?|?( istype *is, char *cp ) {
[86bd7c1f]87        return read( is, cp, 1 );
[cf16f94]88} // ?|?
[51b7345]89
90forall( dtype istype | istream( istype ) )
[cf16f94]91istype * ?|?( istype *is, int *ip ) {
[86bd7c1f]92        char cur;
[51b7345]93 
[86bd7c1f]94        // skip some whitespace
95        do {
[cf16f94]96                is | &cur;
[86bd7c1f]97                if ( fail( is ) || eof( is ) ) return is;
98        } while ( !( cur >= '0' && cur <= '9' ) );
[51b7345]99 
[86bd7c1f]100        // accumulate digits
101        *ip = 0;
102        while ( cur >= '0' && cur <= '9' ) {
103                *ip = *ip * 10 + ( cur - '0' );
[cf16f94]104                is | &cur;
[86bd7c1f]105                if ( fail( is ) || eof( is ) ) return is;
106        }
[51b7345]107 
[86bd7c1f]108        unread( is, cur );
109        return is;
[cf16f94]110} // ?|?
[86bd7c1f]111
112// Local Variables: //
113// tab-width: 4 //
114// compile-command: "cfa iostream.c" //
115// End: //
Note: See TracBrowser for help on using the repository browser.