home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 8
/
amigaformatcd08.iso
/
in_the_mag
/
html_tutorial
/
iostream.h
< prev
next >
Wrap
C/C++ Source or Header
|
1996-09-03
|
4KB
|
201 lines
// Faked iostream.h to use less memory
#ifndef __IOSTREAM_H
#define __IOSTREAM_H
#include <stdio.h>
#undef putchar
#undef getchar
inline int isdigit( char c )
{
return (c>='0' && c<='9');
}
class ios {
public:
ios( FILE* );
ios( char name[], char mode[] );
enum Selector { dec, hex, oct, skipws, app };
void putchar( const char );
char getchar(void);
bool eof( void );
void ungetch( const char );
private:
FILE *stream; // Last character read
};
ios::ios( FILE* f )
{
stream = f;
}
ios::ios( char name[], char mode[] )
{
stream = fopen( name, mode );
}
void ios::putchar( const char c )
{
putc(c, stdout);
}
char ios::getchar(void)
{
return getc(stdin);
}
void ios::ungetch(const char c)
{
::ungetc(c,stdin);
}
bool ios::eof( void )
{
return (bool) feof(stream);
}
class ostream : public ios {
public:
ostream(FILE* f) : ios(f) { the_base = dec; the_width = 0; }
~ostream() { fclose( stdout ); }
ostream& operator << ( Selector );
ostream& operator << ( char );
ostream& operator << ( int );
ostream& operator << ( char * );
ostream& operator >> ( char& );
bool eof( void );
protected:
void write_the_base( int number, int the_base, int the_width);
void prints( char *str, int the_width );
int strlen( char *str );
private:
Selector the_base;
int the_width;
};
ostream& ostream::operator << ( char c )
{
putchar( c );
return *this;
}
ostream& ostream::operator << ( int i )
{
switch ( the_base )
{
case dec: write_the_base( i , 10, the_width ); break;
case oct: write_the_base( i , 8 , the_width ); break;
case hex: write_the_base( i , 16, the_width ); break;
}
return *this;
}
ostream& ostream::operator << ( char *str )
{
while ( *str ) putchar ( *str++ );
return *this;
}
ostream& ostream::operator << ( ios::Selector sel )
{
the_base = sel;
return *this;
}
void ostream::write_the_base( int number, int the_base, int the_width)
{
bool negative = false;
if ( number < 0 )
{
number = -number; --the_width; negative = true;
}
if ( number >= the_base )
{
write_the_base( number / the_base , the_base , --the_width );
number = number % the_base;
} else {
int i;
for( i=1; i<the_width; i++ ) putchar(' ');
if (negative == true)
{
putchar('-'); negative = false;
}
}
putchar( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[number] );
}
void ostream::prints( char *str, int the_width )
{
int l = strlen( str );
int i;
for( i=1; i<=the_width-l; i++ ) putchar(' ');
while ( *str )
{
putchar( *str++ );
}
}
int ostream::strlen( char *str )
{
int i=0;
while( *str++ ) i++;
return i;
}
class istream : public ios {
public:
istream(FILE *f) : ios(f) { }
~istream() { }
istream& operator >> ( char& );
istream& operator >> ( int& );
istream& operator >> ( char [] );
};
istream& istream::operator >> ( char& c )
{
c = getchar();
return *this;
}
istream& istream::operator >> ( int& i )
{
char c = getchar();
int num = 0;
while ( ! isdigit(c) ) c = getchar();
while ( isdigit(c) ) {
num = num * 10 + (c-'0');
c = getchar();
}
ungetch(c); i = num;
return *this;
}
ostream cout(stdout), cerr(stderr);
istream cin(stdin);
#endif
/*
void main()
{
int countdown=10;
cout << "Decimal" << "\t" << "Octal" << "\t" << "Hex" << "\n";
while ( countdown > 0 )
{
cout << ios::dec << countdown << "\t";
cout << ios::hex << countdown << "\t";
cout << ios::oct << countdown << "\t";
cout << "\n";
if ( countdown == 3 )
{
cout << "Ignition" << "\n";
}
countdown--;
}
cout << "Blast Off" << "\n";
}
*/