source: src/libcfa/iostream.c @ 9d7b3ea

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 9d7b3ea was 6ba0659, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

major rewrite of I/O library and update example programs to use new I/O

  • Property mode set to 100644
File size: 4.4 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
[6ba0659]12// Last Modified On : Wed Feb 17 14:19:56 2016
13// Update Count     : 76
[86bd7c1f]14//
[51b7345]15
[d3b7937]16#include "iostream"
17
[51b7345]18extern "C" {
19#include <stdio.h>
[839ccbb]20#include <string.h>                                                                             // strlen
[52f85e0]21#include <float.h>                                                                              // DBL_DIG, LDBL_DIG
[d3b7937]22#include <complex.h>                                                                    // creal, cimag
[51b7345]23}
24
25forall( dtype ostype | ostream( ostype ) )
[cf16f94]26ostype * ?|?( ostype *os, char c ) {
[86bd7c1f]27        return write( os, &c, 1 );
[cf16f94]28} // ?|?
[51b7345]29
30forall( dtype ostype | ostream( ostype ) )
[cf16f94]31ostype * ?|?( ostype *os, int i ) {
[784deab]32        char buffer[32];
33        return write( os, buffer, sprintf( buffer, "%d", i ) );
34} // ?|?
35
36forall( dtype ostype | ostream( ostype ) )
37ostype * ?|?( ostype *os, unsigned int i ) {
38        char buffer[32];
39        return write( os, buffer, sprintf( buffer, "%u", i ) );
40} // ?|?
41
42forall( dtype ostype | ostream( ostype ) )
43ostype * ?|?( ostype *os, long int i ) {
44        char buffer[32];
45        return write( os, buffer, sprintf( buffer, "%ld", i ) );
46} // ?|?
47
48forall( dtype ostype | ostream( ostype ) )
49ostype * ?|?( ostype *os, long long int i ) {
50        char buffer[32];
51        return write( os, buffer, sprintf( buffer, "%lld", i ) );
52} // ?|?
53
54forall( dtype ostype | ostream( ostype ) )
55ostype * ?|?( ostype *os, unsigned long int i ) {
56        char buffer[32];
57        return write( os, buffer, sprintf( buffer, "%lu", i ) );
58} // ?|?
59
60forall( dtype ostype | ostream( ostype ) )
61ostype * ?|?( ostype *os, unsigned long long int i ) {
62        char buffer[32];
63        return write( os, buffer, sprintf( buffer, "%llu", i ) );
[cf16f94]64} // ?|?
[51b7345]65
[d3b7937]66forall( dtype ostype | ostream( ostype ) )
67ostype * ?|?( ostype *os, float f ) {
68        char buffer[32];
69        return write( os, buffer, sprintf( buffer, "%g", f ) );
70} // ?|?
71
[51b7345]72forall( dtype ostype | ostream( ostype ) )
[cf16f94]73ostype * ?|?( ostype *os, double d ) {
[784deab]74        char buffer[32];
[52f85e0]75        return write( os, buffer, sprintf( buffer, "%.*lg", DBL_DIG, d ) );
[cf16f94]76} // ?|?
[134b86a]77
78forall( dtype ostype | ostream( ostype ) )
[784deab]79ostype * ?|?( ostype *os, long double d ) {
80        char buffer[32];
[52f85e0]81        return write( os, buffer, sprintf( buffer, "%.*Lg", LDBL_DIG, d ) );
[cf16f94]82} // ?|?
[51b7345]83
[d3b7937]84forall( dtype ostype | ostream( ostype ) )
85ostype * ?|?( ostype *os, float _Complex c ) {
[5721a6d]86        return os | crealf( c ) | (cimagf( c ) < 0 ? "" : "+") | cimagf( c ) | 'i';
[d3b7937]87} // ?|?
88
89forall( dtype ostype | ostream( ostype ) )
90ostype * ?|?( ostype *os, double _Complex c ) {
[5721a6d]91        return os | creal( c ) | (cimag( c ) < 0 ? "" : "+") | cimag( c ) | 'i';
[d3b7937]92} // ?|?
93
94forall( dtype ostype | ostream( ostype ) )
95ostype * ?|?( ostype *os, long double _Complex c ) {
[5721a6d]96        return os | creall( c ) | (cimagl( c ) < 0 ? "" : "+") | cimagl( c ) | 'i';
[d3b7937]97} // ?|?
98
[e56cfdb0]99forall( dtype ostype | ostream( ostype ) )
[cf16f94]100ostype * ?|?( ostype *os, const void *p ) {
[784deab]101        char buffer[32];
102        return write( os, buffer, sprintf( buffer, "%p", p ) );
103} // ?|?
104
105forall( dtype ostype | ostream( ostype ) )
106ostype * ?|?( ostype *os, const char *cp ) {
107        return write( os, cp, strlen( cp ) );
[cf16f94]108} // ?|?
109
110
111forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) ) 
112retostype * ?|?( ostype *os, retostype * (*manip)(ostype*) ) {
[6ba0659]113  return manip( os );
[cf16f94]114}
115
116forall( dtype ostype | ostream( ostype ) ) 
117ostype * endl( ostype * os ) {
[6ba0659]118        os | "\n";
119        flush( os );
120        return os;
[cf16f94]121} // endl
[e56cfdb0]122
[6ba0659]123//---------------------------------------
124
125forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ),
[e56cfdb0]126                dtype os_type | ostream( os_type ) )
127void write( iterator_type begin, iterator_type end, os_type *os ) {
[6ba0659]128        void print( elt_type i ) { os | i | ' '; }
[e56cfdb0]129        for_each( begin, end, print );
[cf16f94]130} // ?|?
[e56cfdb0]131
[6ba0659]132forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ),
[e56cfdb0]133                dtype os_type | ostream( os_type ) )
134void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
[cf16f94]135        void print( elt_type i ) { os | i | ' '; }
[e56cfdb0]136        for_each_reverse( begin, end, print );
[cf16f94]137} // ?|?
[e56cfdb0]138
[6ba0659]139//---------------------------------------
[e56cfdb0]140
[51b7345]141forall( dtype istype | istream( istype ) )
[cf16f94]142istype * ?|?( istype *is, char *cp ) {
[86bd7c1f]143        return read( is, cp, 1 );
[cf16f94]144} // ?|?
[51b7345]145
146forall( dtype istype | istream( istype ) )
[cf16f94]147istype * ?|?( istype *is, int *ip ) {
[6ba0659]148        return get( is, ip );
[cf16f94]149} // ?|?
[86bd7c1f]150
151// Local Variables: //
152// tab-width: 4 //
153// compile-command: "cfa iostream.c" //
154// End: //
Note: See TracBrowser for help on using the repository browser.