Index: src/libcfa/fstream
===================================================================
--- src/libcfa/fstream	(revision 52f85e0c522e3914bc6c3fa98cf315bdf8c09347)
+++ src/libcfa/fstream	(revision a73d57a0f560d4d17a58b395e9f01b5bdef94eed)
@@ -10,6 +10,6 @@
 // Created On       : Wed May 27 17:56:53 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Wed Jan 27 23:47:41 2016
-// Update Count     : 3
+// Last Modified On : Wed Feb 17 14:02:01 2016
+// Update Count     : 22
 //
 
@@ -19,25 +19,27 @@
 #include "iostream"
 
-typedef struct ofstream ofstream;
+// implement context ostream
+struct ofstream;
 
-// implement context ostream
-ofstream *write( ofstream *, const char *, streamsize_type );
-int fail( ofstream * );
+int fail( ofstream * os );
+int flush( ofstream * os );
+void open( ofstream ** os, const char * name, const char * mode );
+void close( ofstream * os );
+ofstream * write( ofstream * os, const char * data, streamsize_type size );
 
-ofstream *ofstream_stdout();
-ofstream *ofstream_stderr();
-ofstream *ofstream_fromfile( const char *name );
-void ofstream_close( ofstream *os );
-
-typedef struct ifstream ifstream;
+extern ofstream * sout, * serr;
 
 // implement context istream
-ifstream *read( ifstream *, char *, streamsize_type );
-ifstream *unread( ifstream *, char );
-int fail( ifstream * );
-int eof( ifstream * );
+struct ifstream;
 
-ifstream *ifstream_stdin();
-ifstream *ifstream_fromfile( const char *name );
+int fail( ifstream * is );
+int eof( ifstream * is );
+void open( ifstream ** is, const char * name, const char * mode );
+void close( ifstream * is );
+ifstream * get( ifstream * is, int * data );
+ifstream * read( ifstream * is, char * data, streamsize_type size );
+ifstream * ungetc( ifstream * is, char c );
+
+extern ifstream *sin;
 
 #endif // __FSTREAM_H__
Index: src/libcfa/fstream.c
===================================================================
--- src/libcfa/fstream.c	(revision 52f85e0c522e3914bc6c3fa98cf315bdf8c09347)
+++ src/libcfa/fstream.c	(revision a73d57a0f560d4d17a58b395e9f01b5bdef94eed)
@@ -10,6 +10,6 @@
 // Created On       : Wed May 27 17:56:53 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Tue Jan 26 17:12:59 2016
-// Update Count     : 6
+// Last Modified On : Wed Feb 17 14:03:05 2016
+// Update Count     : 76
 //
 
@@ -23,102 +23,120 @@
 struct ofstream {
 	FILE *file;
-	int fail;
 };
 
-ofstream *write( ofstream *os, const char *data, streamsize_type size ) {
-	if ( ! os->fail ) {
-		fwrite( data, size, 1, os->file );
-		os->fail = ferror( os->file );
+#define IO_MSG "I/O error "
+
+int fail( ofstream * os ) {
+	return ferror( os->file );
+} // fail
+
+int flush( ofstream * os ) {
+	return fflush( os->file );
+} // flush
+
+void open( ofstream ** os, const char * name, const char * mode ) {
+	FILE *t = fopen( name, mode );
+	if ( t == 0 ) {										// do not change unless successful
+		perror( IO_MSG "open output" );
+		exit( EXIT_FAILURE );
+	} // if
+	(*os)->file = t;
+} // open
+
+void close( ofstream * os ) {
+	if ( os->file == stdout || os->file == stderr ) return;
+
+	if ( fclose( os->file ) == EOF ) {
+		perror( IO_MSG "close output" );
+	} // if 
+} // close
+
+ofstream * write( ofstream * os, const char * data, streamsize_type size ) {
+	if ( fail( os ) ) {
+		fprintf( stderr, "attempt write I/O on failed stream\n" );
+		exit( EXIT_FAILURE );
+	} // if
+
+	if ( fwrite( data, 1, size, os->file ) != size ) {
+		perror( IO_MSG "write" );
+		exit( EXIT_FAILURE );
 	} // if
 	return os;
 } // write
 
-int fail( ofstream *os ) {
-	return os->fail;
-} // fail
+static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) };
+ofstream *sout = &soutFile;
+static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) };
+ofstream *serr = &serrFile;
 
-static ofstream *make_ofstream() {
-	ofstream *new_stream = malloc( sizeof( ofstream ) );
-	new_stream->fail = 0;
-	return new_stream;
-} // make_ofstream
-
-ofstream *ofstream_stdout() {
-	ofstream *stdout_stream = make_ofstream();
-	stdout_stream->file = stdout;
-	return stdout_stream;
-} // ofstream_stdout
-
-ofstream *ofstream_stderr() {
-	ofstream *stderr_stream = make_ofstream();
-	stderr_stream->file = stderr;
-	return stderr_stream;
-} // ofstream_stderr
-
-ofstream *ofstream_fromfile( const char *name ) {
-	ofstream *file_stream = make_ofstream();
-	file_stream->file = fopen( name, "w" );
-	file_stream->fail = file_stream->file == 0;
-	return file_stream;
-}
-
-void ofstream_close( ofstream *os ) {
-	if ( os->file != stdout && os->file != stderr ) {
-		os->fail = fclose( os->file );
-	}
-	free( os );
-}
+//---------------------------------------
 
 struct ifstream {
 	FILE *file;
-	int fail;
-	int eof;
 };
 
-ifstream *read( ifstream *is, char *data, streamsize_type size ) {
-	if ( ! is->fail && ! is->eof ) {
-		fread( data, size, 1, is->file );
-		is->fail = ferror( is->file );
-		is->eof = feof( is->file );
-	}
+int fail( ifstream * is ) {
+	return ferror( is->file );
+} // fail
+
+int eof( ifstream * is ) {
+	return feof( is->file );
+} // eof
+
+ifstream * get( ifstream * is, int * data ) {
+	if ( fscanf( is->file, "%d", data ) == EOF ) {
+		if ( ferror( is->file ) ) {
+			fprintf( stderr, "invalid int read\n" );
+			exit( EXIT_FAILURE );
+		} // if
+	} // if
 	return is;
-}
+} // read
+
+ifstream * read( ifstream * is, char * data, streamsize_type size ) {
+	if ( fail( is ) ) {
+		fprintf( stderr, "attempt read I/O on failed stream\n" );
+		exit( EXIT_FAILURE );
+	} // if
+
+	if ( fread( data, size, 1, is->file ) == 0 ) {
+		perror( IO_MSG "read" );
+		exit( EXIT_FAILURE );
+	} // if
+	return is;
+} // read
   
-ifstream *unread( ifstream *is, char c ) {
-	if ( ! is->fail ) {
-		if ( ! EOF == ungetc( c, is->file ) ) {
-			is->fail = 1;
-		}
-	}
+ifstream *ungetc( ifstream * is, char c ) {
+	if ( fail( is ) ) {
+		fprintf( stderr, "attempt ungetc I/O on failed stream\n" );
+		exit( EXIT_FAILURE );
+	} // if
+
+	if ( ungetc( c, is->file ) == EOF ) {
+		perror( IO_MSG "ungetc" );
+		exit( EXIT_FAILURE );
+	} // if
 	return is;
-}
+} // ungetc
 
-int fail( ifstream *is ) {
-	return is->fail;
-}
+void open( ifstream ** is, const char * name, const char * mode ) {
+	FILE *t = fopen( name, mode );
+	if ( t == 0 ) {										// do not change unless successful
+		perror( IO_MSG "open input" );
+		exit( EXIT_FAILURE );
+	} // if
+	(*is)->file = t;
+} // open
 
-int eof( ifstream *is ) {
-	return is->eof;
-}
+void close( ifstream * is ) {
+	if ( is->file == stdin ) return;
 
-static ifstream *make_ifstream() {
-	ifstream *new_stream = malloc( sizeof( ifstream ) );
-	new_stream->fail = 0;
-	new_stream->eof = 0;
-	return new_stream;
-}
+	if ( fclose( is->file ) == EOF ) {
+		perror( IO_MSG "close input" );
+	} // if 
+} // close
 
-ifstream *ifstream_stdin() {
-	ifstream *stdin_stream = make_ifstream();
-	stdin_stream->file = stdin;
-	return stdin_stream;
-}
-
-ifstream *ifstream_fromfile( const char *name ) {
-	ifstream *file_stream = make_ifstream();
-	file_stream->file = fopen( name, "r" );
-	file_stream->fail = file_stream->file == 0;
-	return file_stream;
-}
+static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
+ifstream *sin = &sinFile;
 
 // Local Variables: //
Index: src/libcfa/iostream
===================================================================
--- src/libcfa/iostream	(revision 52f85e0c522e3914bc6c3fa98cf315bdf8c09347)
+++ src/libcfa/iostream	(revision a73d57a0f560d4d17a58b395e9f01b5bdef94eed)
@@ -10,6 +10,6 @@
 // Created On       : Wed May 27 17:56:53 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Fri Jan 29 15:50:36 2016
-// Update Count     : 29
+// Last Modified On : Wed Feb 17 14:04:24 2016
+// Update Count     : 32
 //
 
@@ -22,8 +22,8 @@
 
 context ostream( dtype ostype ) {
-	ostype *write( ostype *, const char *, streamsize_type );
 	int fail( ostype * );
+	int flush( ostype * );
+	ostype * write( ostype *, const char *, streamsize_type );
 };
-
 context writeable( type T ) {
 	forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, T );
@@ -52,21 +52,18 @@
 
 // writes the range [begin, end) to the given stream
-forall( type elt_type | writeable( elt_type ),
-		type iterator_type | iterator( iterator_type, elt_type ),
-		dtype os_type | ostream( os_type ) )
+forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ), dtype os_type | ostream( os_type ) )
 void write( iterator_type begin, iterator_type end, os_type *os );
 
-forall( type elt_type | writeable( elt_type ),
-		type iterator_type | iterator( iterator_type, elt_type ),
-		dtype os_type | ostream( os_type ) )
+forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ), dtype os_type | ostream( os_type ) )
 void write_reverse( iterator_type begin, iterator_type end, os_type *os );
 
-//******************************************************************************
+//---------------------------------------
 
 context istream( dtype istype ) {
-	istype *read( istype *, char *, streamsize_type );
-	istype *unread( istype *, char );
 	int fail( istype * );
 	int eof( istype * );
+	istype * get( istype *, int * );
+	istype * read( istype *, char *, streamsize_type );
+	istype * ungetc( istype *, char );
 };
 
Index: src/libcfa/iostream.c
===================================================================
--- src/libcfa/iostream.c	(revision 52f85e0c522e3914bc6c3fa98cf315bdf8c09347)
+++ src/libcfa/iostream.c	(revision a73d57a0f560d4d17a58b395e9f01b5bdef94eed)
@@ -10,6 +10,6 @@
 // Created On       : Wed May 27 17:56:53 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Wed Feb 10 15:48:46 2016
-// Update Count     : 66
+// Last Modified On : Wed Feb 17 14:19:56 2016
+// Update Count     : 76
 //
 
@@ -111,26 +111,24 @@
 forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) ) 
 retostype * ?|?( ostype *os, retostype * (*manip)(ostype*) ) {
-  return manip(os);
+  return manip( os );
 }
 
 forall( dtype ostype | ostream( ostype ) ) 
 ostype * endl( ostype * os ) {
-  os | "\n";
-  // flush
-  return os;
+	os | "\n";
+	flush( os );
+	return os;
 } // endl
 
-forall( type elt_type | writeable( elt_type ),
-		type iterator_type | iterator( iterator_type, elt_type ),
+//---------------------------------------
+
+forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ),
 		dtype os_type | ostream( os_type ) )
 void write( iterator_type begin, iterator_type end, os_type *os ) {
-	void print( elt_type i ) {
-		os | i | ' ';
-	}
+	void print( elt_type i ) { os | i | ' '; }
 	for_each( begin, end, print );
 } // ?|?
 
-forall( type elt_type | writeable( elt_type ),
-		type iterator_type | iterator( iterator_type, elt_type ),
+forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ),
 		dtype os_type | ostream( os_type ) )
 void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
@@ -139,4 +137,5 @@
 } // ?|?
 
+//---------------------------------------
 
 forall( dtype istype | istream( istype ) )
@@ -147,22 +146,5 @@
 forall( dtype istype | istream( istype ) )
 istype * ?|?( istype *is, int *ip ) {
-	char cur;
-  
-	// skip some whitespace
-	do {
-		is | &cur;
-		if ( fail( is ) || eof( is ) ) return is;
-	} while ( !( cur >= '0' && cur <= '9' ) );
-  
-	// accumulate digits
-	*ip = 0;
-	while ( cur >= '0' && cur <= '9' ) {
-		*ip = *ip * 10 + ( cur - '0' );
-		is | &cur;
-		if ( fail( is ) || eof( is ) ) return is;
-	}
-  
-	unread( is, cur );
-	return is;
+	return get( is, ip );
 } // ?|?
 
