diff options
author | Adam <you@example.com> | 2020-05-17 05:51:50 +0200 |
---|---|---|
committer | Adam <you@example.com> | 2020-05-17 05:51:50 +0200 |
commit | e611b132f9b8abe35b362e5870b74bce94a1e58e (patch) | |
tree | a5781d2ec0e085eeca33cf350cf878f2efea6fe5 /private/crt32/iostream | |
download | NT4.0-e611b132f9b8abe35b362e5870b74bce94a1e58e.tar NT4.0-e611b132f9b8abe35b362e5870b74bce94a1e58e.tar.gz NT4.0-e611b132f9b8abe35b362e5870b74bce94a1e58e.tar.bz2 NT4.0-e611b132f9b8abe35b362e5870b74bce94a1e58e.tar.lz NT4.0-e611b132f9b8abe35b362e5870b74bce94a1e58e.tar.xz NT4.0-e611b132f9b8abe35b362e5870b74bce94a1e58e.tar.zst NT4.0-e611b132f9b8abe35b362e5870b74bce94a1e58e.zip |
Diffstat (limited to 'private/crt32/iostream')
48 files changed, 6717 insertions, 0 deletions
diff --git a/private/crt32/iostream/cerrinit.cxx b/private/crt32/iostream/cerrinit.cxx new file mode 100644 index 000000000..5f5d75f5e --- /dev/null +++ b/private/crt32/iostream/cerrinit.cxx @@ -0,0 +1,28 @@ +/*** +*cerrinit.cxx - definitions and initialization for predefined stream cerr. +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions and initialization of predefined stream cerr. +* +*Revision History: +* 11 18-91 KRS Created. +* +*******************************************************************************/ +#include <cruntime.h> +#include <internal.h> +#include <iostream.h> +#include <fstream.h> +#pragma hdrstop + +// put contructors in special MS-specific XIFM segment +#pragma warning(disable:4074) // ignore init_seg warning +#pragma init_seg(compiler) + +#if ((!defined(_WINDOWS)) || defined(_QWIN)) +ostream_withassign cerr(new filebuf(2)); + +static Iostream_init __InitCerr(cerr,1); + +#endif diff --git a/private/crt32/iostream/cininit.cxx b/private/crt32/iostream/cininit.cxx new file mode 100644 index 000000000..7a8c6762d --- /dev/null +++ b/private/crt32/iostream/cininit.cxx @@ -0,0 +1,28 @@ +/*** +*cininit.cxx - definitions and initialization for predefined stream cin. +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions and initialization of predefined stream cin. +* +*Revision History: +* 11 18-91 KRS Created. +* +*******************************************************************************/ +#include <cruntime.h> +#include <internal.h> +#include <iostream.h> +#include <fstream.h> +#pragma hdrstop + +// put contructors in special MS-specific XIFM segment +#pragma warning(disable:4074) // ignore init_seg warning +#pragma init_seg(compiler) + +#if ((!defined(_WINDOWS)) || defined(_QWIN)) +istream_withassign cin(new filebuf(0)); + +static Iostream_init __InitCin(cin); + +#endif diff --git a/private/crt32/iostream/cloginit.cxx b/private/crt32/iostream/cloginit.cxx new file mode 100644 index 000000000..f1f0a064a --- /dev/null +++ b/private/crt32/iostream/cloginit.cxx @@ -0,0 +1,28 @@ +/*** +*cloginit.cxx - definitions and initialization for predefined stream clog. +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions and initialization of predefined stream clog. +* +*Revision History: +* 11 18-91 KRS Created. +* +*******************************************************************************/ +#include <cruntime.h> +#include <internal.h> +#include <iostream.h> +#include <fstream.h> +#pragma hdrstop + +// put contructors in special MS-specific XIFM segment +#pragma warning(disable:4074) // ignore init_seg warning +#pragma init_seg(compiler) + +#if ((!defined(_WINDOWS)) || defined(_QWIN)) +ostream_withassign clog(new filebuf(2)); + +static Iostream_init __InitClog(clog); + +#endif diff --git a/private/crt32/iostream/filebuf.cxx b/private/crt32/iostream/filebuf.cxx new file mode 100644 index 000000000..35980c4bb --- /dev/null +++ b/private/crt32/iostream/filebuf.cxx @@ -0,0 +1,460 @@ +/*** +*filebuf.cxx - core filebuf member functions +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains the core member functions for filebuf class. +* +*Revision History: +* 08-08-91 KRS Created. +* 08-20-91 KRS Added virtual xsgetn()/xsputn() functions. +* 08-21-91 KRS Fix circular reference between sync() and seekoff(). +* Close file in destructor only if we opened it! +* 09-06-91 KRS Fix ios::ate case in filebuf::open(). +* 09-09-91 KRS Add support for ios::binary in filebuf::open(). +* 09-10-91 KRS Remove virtual xsputn()/xsgetn(). +* 09-11-91 KRS Fix filebuf::seekoff() for ios::cur and in_avail(). +* 09-12-91 KRS Make sure close() always closes even if sync() fails. +* Fix seekoff call in filebuf::sync() and pbackfail(). +* 09-16-91 KRS Make virtual filebuf::setbuf() more robust. +* 09-19-91 KRS Add calls to delbuf(1) in constructors. +* 09-20-91 KRS C700 #4453: Improve efficiency in overflow(). +* 09-29-91 KRS Granularity split. Move fstream into separate file. +* 10-24-91 KRS Avoid virtual calls from virtual functions. +* 11-13-91 KRS Use allocate() properly in overflow() and underflow(). +* Fix constructor. +* 01-03-92 KRS Remove virtual keyword. Add function headers and PCH. +* 01-20-92 KRS In text mode, account for CR/LF pairs in sync(). +* 02-03-92 KRS Change for new compiler destructor behavior. +* 08-19-92 KRS Remove sh_compat for NT. +* 08-27-92 KRS Fix bug in close() introduced in MTHREAD work. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <string.h> +#include <stdio.h> +#include <fcntl.h> +#include <sys\types.h> +#include <io.h> +#include <fstream.h> +#pragma hdrstop + +#include <msdos.h> +#include <sys\stat.h> + +const int filebuf::openprot = 0644; // CONSIDER: meaning? + +const int filebuf::sh_none = 04000; // deny rw +const int filebuf::sh_read = 05000; // deny wr +const int filebuf::sh_write = 06000; // deny rd + +const int filebuf::binary = O_BINARY; +const int filebuf::text = O_TEXT; + +/*** +*filebuf::filebuf() - filebuf default constructor +* +*Purpose: +* Default constructor. +* +*Entry: +* +*******************************************************************************/ + filebuf::filebuf() +: streambuf() +{ + x_fOpened = 0; + x_fd = -1; +} + + +/*** +*filebuf::filebuf(filedesc fd) - filebuf constructor +* +*Purpose: +* Constructor. Initialize filebuf and attach to file descriptor. +* +*Entry: +* fd = file descriptor to attach to filebuf +* +*******************************************************************************/ + filebuf::filebuf(filedesc fd) +: streambuf() +{ + x_fOpened = 0; + x_fd=fd; +} + + +/*** +*filebuf::filebuf(filedesc fd, char* p, int len) - filebuf constructor +* +*Purpose: +* Constructor. Initialize filebuf and attach to file descriptor. +* +*Entry: +* fd = file descriptor to attach to filebuf +* p = user-supplied buffer +* len = length of buffer +* +*******************************************************************************/ + filebuf::filebuf(filedesc fd, char* p, int len) +: streambuf() +{ + filebuf::setbuf(p,len); + x_fOpened = 0; + x_fd=fd; +} + + +/*** +*filebuf::~filebuf() - filebuf destructor +* +*Purpose: +* Destructor. Close attached file only if we opened it. +* +*Entry: +* None. +* +*******************************************************************************/ + filebuf::~filebuf() +{ + lock(); // no need to unlock... + if (x_fOpened) + close(); // calls filebuf::sync() + else + filebuf::sync(); +} + + +/*** +*filebuf* filebuf::close() - close an attached file +* +*Purpose: +* Close attached file. +* +*Entry: +* None. +*Exit: +* Returns NULL if error, otherwise returns "this" pointer. +* +*******************************************************************************/ +filebuf* filebuf::close() +{ + int retval; + if (x_fd==-1) + return NULL; + + lock(); + retval = sync(); + + if ((_close(x_fd)==-1) || (retval==EOF)) + { + unlock(); + return NULL; + } + x_fd = -1; + unlock(); + return this; +} + +/*** +*virtual int filebuf::overflow(int c) - overflow virtual function +* +*Purpose: +* flush any characters in the reserve area and handle 'c'. +* +*Entry: +* c = character to output (if not EOF) +* +*Exit: +* Returns EOF if error, otherwise returns something else. +* +*Exceptions: +* Returns EOF if error. +* +*******************************************************************************/ +int filebuf::overflow(int c) +{ + if (allocate()==EOF) // make sure there is a reserve area + return EOF; + if (filebuf::sync()==EOF) // sync before new buffer created below + return EOF; + + if (!unbuffered()) + setp(base(),ebuf()); + + if (c!=EOF) + { + if ((!unbuffered()) && (pptr() < epptr())) // guard against recursion + sputc(c); + else + { + if (_write(x_fd,&c,1)!=1) + return(EOF); + } + } + return(1); // return something other than EOF if successful +} + +/*** +*virtual int filebuf::underflow() - underflow virtual function +* +*Purpose: +* return next character in get area, or get more characters from source. +* +*Entry: +* None. +* +*Exit: +* Returns current character in file. Does not advance get pointer. +* +*Exceptions: +* Returns EOF if error. +* +*******************************************************************************/ +int filebuf::underflow() +{ + int count; + unsigned char tbuf; + + if (in_avail()) + return (int)(unsigned char) *gptr(); + + if (allocate()==EOF) // make sure there is a reserve area + return EOF; + if (filebuf::sync()==EOF) + return EOF; + + if (unbuffered()) + { + if (_read(x_fd,(void *)&tbuf,1)<=0) + return EOF; + return (int)tbuf; + } + + if ((count=_read(x_fd,(void *)base(),blen())) <= 0) + return EOF; // reached EOF + setg(base(),base(),base()+count); + return (int)(unsigned char) *gptr(); // CONSIDER: possible recursion +} + + +/*** +*virtual streampos filebuf::seekoff() - seekoff virtual function +* +*Purpose: +* Seeks to given absolute or relative file offset. +* +*Entry: +* off = offset to seek to relative to beginning, end or current +* position in the file. +* dir = one of ios::beg, ios::cur, or ios::end +* +*Exit: +* Returns current file position after seek. +* +*Exceptions: +* Returns EOF if error. +* +*******************************************************************************/ +streampos filebuf::seekoff(streamoff off, ios::seek_dir dir, int) +{ + + int fdir; + long retpos; + switch (dir) { + case ios::beg : + fdir = SEEK_SET; + break; + case ios::cur : + fdir = SEEK_CUR; + break; + case ios::end : + fdir = SEEK_END; + break; + default: + // error + return(EOF); + } + + if (filebuf::sync()==EOF) + return EOF; + if ((retpos=_lseek(x_fd, off, fdir))==-1L) + return (EOF); + return((streampos)retpos); +} + +/* NOT IN SPEC. +int filebuf::pbackfail(int c) +{ + if (eback()<gptr()) return sputbackc(c); // CONSIDER: should never happen + + if (filebuf::seekoff( -1, ios::cur, ios::in)==EOF) + return EOF; + if (!unbuffered()) + filebuf::underflow(); + // assert c = underflow() + } + return(c); +} +*/ + +#if 0 +// NOT IN SPEC. +// UNDONE: consider removing this? +int filebuf::xsgetn(char * ptr, int n) +{ + int retval; + int oldmode; + + if (x_fd==-1) + return 0; + if (filebuf::sync()!=EOF) + { + // set to BINARY and save old mode. + oldmode = _setmode(x_fd,O_BINARY); + } + retval = sgetn(ptr,n); + filebuf::sync(); // put back any excess (ignore errors) + + if (oldmode==O_TEXT) // check != -1 + { + _setmode(x_fd,oldmode); + } + return retval; +} + +// NOT IN SPEC. +// UNDONE: consider removing this? +int filebuf::xsputn(const char * ptr, int n) +{ + int retval; + int oldmode; + + if (x_fd==-1) + return 0; + if (filebuf::sync()!=EOF) + { + // set to BINARY and save old mode. + oldmode = _setmode(x_fd,O_BINARY); + } + + retval = sputn(ptr,n); + filebuf::sync(); // flush output; + + if (oldmode==O_TEXT) // check != -1 + { + _setmode(x_fd,oldmode); + } + return retval; +} +#endif + + +/*** +*virtual int filebuf::sync() - synchronize buffers with external file postion. +* +*Purpose: +* Synchronizes buffer with external file, by flushing any output and/or +* discarding any unread input data. Discards any get or put area(s). +* +*Entry: +* None. +* +*Exit: +* Returns EOF if error, else 0. +* +*Exceptions: +* Returns EOF if error. +* +*******************************************************************************/ +int filebuf::sync() +{ + long count, nout; + char * p; + if (x_fd==-1) + return(EOF); +// lock(); // CONSIDER: do we need/want locking here? + if (!unbuffered()) + { + if ((count=out_waiting())!=0) + { + if ((nout =_write(x_fd,(void *) pbase(),(unsigned int)count)) != count) + { + // should set _pptr -= nout + pbump(-(int)nout); + memmove(pbase(), pbase()+nout, (int)(count-nout)); +// unlock(); + return(EOF); + } + } + setp(0,0); // empty put area + + if ((count=in_avail()) > 0) + { + // can't use seekoff here!! + if (_osfile[x_fd] & FTEXT) + { + // If text mode, need to account for CR/LF etc. + for (p = gptr(); p < egptr(); p++) + if (*p == '\n') + count++; + + // account for EOF if read, not counted by _read + // UNDONE: is this necessary / useful? + if ((_osfile[x_fd] & FEOFLAG)) + count++; +#if 0 + // UNDONE: is this correct? + if ((gptr()==eback()) && (_osfile[x_fd] & FCRLF)) + count--; +#endif + } + if (_lseek(x_fd, -count, SEEK_CUR)==-1L) + { +// unlock(); + return (EOF); + } + } + setg(0,0,0); // empty get area + } +// unlock(); + return(0); +} + +/*** +*virtual streambuf* filebuf::setbuf(char* ptr, int len) - set reserve area. +* +*Purpose: +* Synchronizes buffer with external file, by flushing any output and/or +* discarding any unread input data. Discards any get or put area(s). +* +*Entry: +* ptr = requested reserve area. If NULL, request is for unbuffered. +* len = size of reserve area. If <= 0, request is for unbuffered. +* +*Exit: +* Returns this pointer if request is honored, else NULL. +* +*Exceptions: +* Returns NULL if request is not honored. +* +*******************************************************************************/ +streambuf * filebuf::setbuf(char * ptr, int len) +{ + if (is_open() && (ebuf())) + return NULL; + if ((!ptr) || (len <= 0)) + unbuffered(1); + else + { + lock(); // consider: necessary? + setb(ptr, ptr+len, 0); + unlock(); + } + return this; +} diff --git a/private/crt32/iostream/filebuf1.cxx b/private/crt32/iostream/filebuf1.cxx new file mode 100644 index 000000000..6079d69e2 --- /dev/null +++ b/private/crt32/iostream/filebuf1.cxx @@ -0,0 +1,231 @@ +/*** +*filebuf1.cxx - non-core filebuf member functions. +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains optional member functions for filebuf class. +* +*Revision History: +* 09-21-91 KRS Created. Split off from fstream.cxx. +* 10-24-91 KRS C700 #4909: Typo/logic bug in setmode(). +* 11-06-91 KRS Add support for share mode in open(). Use _sopen(). +* 08-19-92 KRS Use _SH_DENYNO for default mode for NT. +* 03-02-93 SKS Avoid setting _O_TRUNC when noreplace is specified +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <string.h> +#include <stdlib.h> +#include <stdio.h> +#include <fcntl.h> +#include <share.h> +#include <sys\types.h> +#include <io.h> +#include <fstream.h> +#pragma hdrstop + +#include <sys\stat.h> + +/*** +*filebuf* filebuf::attach(filedesc fd) - filebuf attach function +* +*Purpose: +* filebuf attach() member function. Attach filebuf object to the +* given file descriptor previously obtained from _open() or _sopen(). +* +*Entry: +* fd = file descriptor. +* +*Exit: +* Returns this pointer or NULL if error. +* +*Exceptions: +* Returns NULL if fd = -1. +* +*******************************************************************************/ +filebuf* filebuf::attach(filedesc fd) +{ + if (x_fd!=-1) + return NULL; // error if already attached + + lock(); + x_fd = fd; + if ((fd!=-1) && (!unbuffered()) && (!ebuf())) + { + char * sbuf = new char[BUFSIZ]; + if (!sbuf) + { + unbuffered(1); + } + else + { + streambuf::setb(sbuf,sbuf+BUFSIZ,1); + } + } + unlock(); + return this; +} + +/*** +*filebuf* filebuf::open(const char* name, int mode, int share) - filebuf open +* +*Purpose: +* filebuf open() member function. Open a file and attach to filebuf +* object. +* +*Entry: +* name = file name string. +* mode = open mode: Combination of ios:: in, out, binary, nocreate, app, +* ate, noreplace and trunc. See spec. for details on behavior. +* share = share mode (optional). sh_compat, sh_none, sh_read, sh_write. +* +*Exit: +* Returns this pointer or NULL if error. +* +*Exceptions: +* Returns NULL if filebuf is already attached to an open file, or if +* invalid mode options, or if call to _sopen or filebuf::seekoff() fails. +* +*******************************************************************************/ +filebuf* filebuf::open(const char* name, int mode, int share) +{ + int dos_mode; + int smode; + if (x_fd!=-1) + return NULL; // error if already open +// translate mode argument + dos_mode = (mode & ios::binary) ? O_BINARY : O_TEXT; + if (!(mode & ios::nocreate)) + dos_mode |= O_CREAT; + if (mode & ios::noreplace) + dos_mode |= O_EXCL; + if (mode & ios::app) + { + mode |= ios::out; + dos_mode |= O_APPEND; + } + if (mode & ios::trunc) + { + mode |= ios::out; // IMPLIED + dos_mode |= O_TRUNC; + } + if (mode & ios::out) + { + if (mode & ios::in) + { + dos_mode |= O_RDWR; + } + else + { + dos_mode |= O_WRONLY; + } + // CONSIDER: a bit weaker treatment than in spec. + if (!(mode & (ios::in|ios::app|ios::ate|ios::noreplace))) + { + mode |= ios::trunc; // IMPLIED + dos_mode |= O_TRUNC; + } + } + else if (mode & ios::in) + dos_mode |= O_RDONLY; + else + return NULL; // error if not ios:in or ios::out + + smode = _SH_DENYNO; // default for NT + share &= (sh_read|sh_write|sh_none); // ignore other bits + if (share) // optimization openprot serves as default + { + switch (share) + { +/* case 03000 : Reserved for sh_compat */ + +// case sh_none : + case 04000 : + smode = _SH_DENYRW; + break; +// case sh_read : + case 05000 : + smode = _SH_DENYWR; + break; +// case sh_write : + case 06000 : + smode = _SH_DENYRD; + break; +// case (sh_read|sh_write) : + case 07000 : + smode = _SH_DENYNO; + break; + default : // unrecognized value same as default + break; + }; + } + + x_fd = _sopen(name, dos_mode, smode, S_IREAD|S_IWRITE); + if (x_fd==-1) + return NULL; + lock(); + x_fOpened = 1; + if ((!unbuffered()) && (!ebuf())) + { + char * sbuf = new char[BUFSIZ]; + if (!sbuf) + { + unbuffered(1); + } + else + { + streambuf::setb(sbuf,sbuf+BUFSIZ,1); + } + } + if (mode & ios::ate) + if (seekoff(0,ios::end,mode)==EOF) + { + close(); + unlock(); + return NULL; + } + unlock(); + return this; +} + +/*** +*int filebuf::setmode(int mode) - filebuf setmode function +* +*Purpose: +* filebuf setmode() member function. Set binary or text access mode. +* Calls _setmode(). +* +* MS-specific extension. +* +*Entry: +* mode = filebuf::binary or filebuf::text. +* +*Exit: +* Returns previous mode, or -1 error. +* +*Exceptions: +* Return -1 (EOF) if invalid argument or _setmode fails. +* +*******************************************************************************/ +int filebuf::setmode(int mode) +{ + int retval; + if ((mode!=filebuf::binary) && (mode!=filebuf::text)) + return -1; + + lock(); + if ((x_fd==-1) || (sync()==EOF)) + { + retval = -1; + } + else + { + retval = _setmode(x_fd,mode); + } + + unlock(); + return retval; +} diff --git a/private/crt32/iostream/fstream.cxx b/private/crt32/iostream/fstream.cxx new file mode 100644 index 000000000..8cecf6c8e --- /dev/null +++ b/private/crt32/iostream/fstream.cxx @@ -0,0 +1,261 @@ +/*** +*fstream.cxx - +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains the member functions for the fstream class. +* +*Revision History: +* 09-21-91 KRS Created. Split off from filebuf.cxx for granularity. +* 10-22-91 KRS C700 #4883: fix error status of fstream::open(). +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <string.h> +#include <stdio.h> +#include <fcntl.h> +#include <sys\types.h> +#include <io.h> +#include <fstream.h> +#pragma hdrstop + +#include <sys\stat.h> + +/*** +*fstream::fstream() - fstream default constructor +* +*Purpose: +* Default constructor for fstream objects. +* +*Entry: +* None. +* +*Exit: +* None. +* +*Exceptions: +* +*******************************************************************************/ + fstream::fstream() +: iostream(new filebuf) +{ + istream::delbuf(1); + ostream::delbuf(1); + // CONSIDER: do anything else? +} + +/*** +*fstream::fstream(const char * name, int mode, int prot) - fstream constructor +* +*Purpose: +* Constructor for fstream objects. Creates an associated filebuf object, +* opens a named file and attaches it to the new filebuf. +* +*Entry: +* name = filename to open. +* mode = see filebuf::open mode argument +* prot = see filebuf::open share argument +* +*Exit: +* None. +* +*Exceptions: +* Sets failbit if open fails. +* +*******************************************************************************/ + fstream::fstream(const char * name, int mode, int prot) +: iostream(new filebuf) +{ + istream::delbuf(1); + ostream::delbuf(1); + if (!(rdbuf()->open(name, mode, prot))) + { + istream::state = istream::failbit; + ostream::state = ostream::failbit; + } +} + +/*** +*fstream::fstream(filedesc fd) - fstream constructor +* +*Purpose: +* Constructor for fstream objects. Creates an associated filebuf object +* and attaches it to the given file descriptor. +* +*Entry: +* fd = file descriptor of file previously opened using _open or _sopen. +* +*Exit: +* None. +* +*Exceptions: +* +*******************************************************************************/ + fstream::fstream(filedesc _fd) +: iostream(new filebuf(_fd)) +{ + istream::delbuf(1); + ostream::delbuf(1); +} + +/*** +*fstream::fstream(filedesc fd, char * sbuf, int len) - fstream constructor +* +*Purpose: +* Constructor for fstream objects. Creates an associated filebuf object +* and attaches it to the given file descriptor. Filebuf object uses +* user-supplied buffer or is unbuffered if sbuf or len = 0. +* +*Entry: +* fd = file descriptor of file previously opened using _open or _sopen. +* sbuf = pointer to character buffer or NULL if request for unbuffered. +* len = lenght of character buffer sbuf or 0 if request for unbuffered. +* +*Exit: +* None. +* +*Exceptions: +* +*******************************************************************************/ + fstream::fstream(filedesc _fd, char * sbuf, int len) +: iostream(new filebuf(_fd, sbuf, len)) +{ + istream::delbuf(1); + ostream::delbuf(1); +} + +/*** +*fstream::~fstream() - fstream destructor +* +*Purpose: +* fstream destructor. +* +*Entry: +* None. +* +*Exit: +* None. +* +*Exceptions: +* +*******************************************************************************/ + fstream::~fstream() +{ + // CONSIDER: do anything else? +} + +/*** +*streambuf* fstream::setbuf(char * ptr, int len) - setbuf function +* +*Purpose: +* fstream setbuf function +* +*Entry: +* ptr = pointer to buffer or NULL for unbuffered. +* len = length of buffer or zero for unbuffered. +* +*Exit: +* Returns rdbuf() or NULL if error. +* +*Exceptions: +* If fstream is already open or if rdbuf()->setbuf fails, sets failbit +* and returns NULL. +* +*******************************************************************************/ +streambuf * fstream::setbuf(char * ptr, int len) +{ + if ((is_open()) || (!(rdbuf()->setbuf(ptr, len)))) + { + istream::clear(istream::state | istream::failbit); + ostream::clear(ostream::state | ostream::failbit); + return NULL; + } + return rdbuf(); +} + +/*** +*void fstream::attach(filedesc _fd) - attach member function +* +*Purpose: +* fstream attach member function. Just calls rdbuf()->attach(). +* +*Entry: +* _fd = file descriptor of previously opened file. +* +*Exit: +* None. +* +*Exceptions: +* Sets failbit if rdbuf()->attach fails. +* +*******************************************************************************/ +void fstream::attach(filedesc _fd) // CONSIDER: inline? +{ + if (!(rdbuf()->attach(_fd))) + { + istream::clear(istream::state | istream::failbit); + ostream::clear(ostream::state | ostream::failbit); + } +} + +/*** +*void fstream::open(const char * name, int mode, int prot) - fstream open() +* +*Purpose: +* Opens a named file and attaches it to the associated filebuf. +* Just calls rdbuf()->open(). +* +*Entry: +* name = filename to open. +* mode = see filebuf::open mode argument +* prot = see filebuf::open share argument +* +*Exit: +* None. +* +*Exceptions: +* Sets failbit if already open or rdbuf()->open() fails. +* +*******************************************************************************/ +void fstream::open(const char * name, int mode, int prot) +{ + if (is_open() || !(rdbuf()->open(name, mode, prot))) + { + istream::clear(istream::state | istream::failbit); + ostream::clear(ostream::state | ostream::failbit); + } +} + +/*** +*void fstream::close() - close member function +* +*Purpose: +* fstream close member function. Just calls rdbuf()->close(). +* Clears rdstate() error bits if successful. +* +*Entry: +* None. +* +*Exit: +* None. +* +*Exceptions: +* Sets failbit if rdbuf()->close fails. +* +*******************************************************************************/ +void fstream::close() +{ + if (rdbuf()->close()) + { + istream::clear(); + ostream::clear(); + } + else + { + istream::clear(istream::state | istream::failbit); + ostream::clear(ostream::state | ostream::failbit); + } +} diff --git a/private/crt32/iostream/ifstream.cxx b/private/crt32/iostream/ifstream.cxx new file mode 100644 index 000000000..e49e26d40 --- /dev/null +++ b/private/crt32/iostream/ifstream.cxx @@ -0,0 +1,240 @@ +/*** +*ifstream.cxx - ifstream member function definitions +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains the member functions for the ifstream class. +* +*Revision History: +* 09-21-91 KRS Created. Split up fstream.cxx for granularity. +* 10-22-91 KRS C700 #4883: fix error status for ifstream::open(). +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <string.h> +#include <stdio.h> +#include <fcntl.h> +#include <sys\types.h> +#include <io.h> +#include <fstream.h> +#pragma hdrstop + +#include <sys\stat.h> + +/*** +*ifstream::ifstream() - ifstream default constructor +* +*Purpose: +* Default constructor for ifstream objects. +* +*Entry: +* None. +* +*Exit: +* None. +* +*Exceptions: +* +*******************************************************************************/ + ifstream::ifstream() +: istream(new filebuf) +{ + delbuf(1); // schedule automatic deletion too... + // CONSIDER: do anything else? +} + +/*** +*ifstream::ifstream(const char * name, int mode, int prot) - ifstream ctor +* +*Purpose: +* Constructor for ifstream objects. Creates an associated filebuf object, +* opens a named file and attaches it to the new filebuf. +* +*Entry: +* name = filename to open. +* mode = see filebuf::open mode argument. ios::in is implied. +* prot = see filebuf::open share argument +* +*Exit: +* None. +* +*Exceptions: +* Sets failbit if open fails. +* +*******************************************************************************/ + ifstream::ifstream(const char * name, int mode, int prot) +: istream(new filebuf) +{ + delbuf(1); // schedule automatic deletion too... + if (!(rdbuf()->open(name, (mode|ios::in), prot))) + state |= ios::failbit; +} + +/*** +*ifstream::ifstream(filedesc fd) - ifstream constructor +* +*Purpose: +* Constructor for ifstream objects. Creates an associated filebuf object +* and attaches it to the given file descriptor. +* +*Entry: +* fd = file descriptor of file previously opened using _open or _sopen. +* +*Exit: +* None. +* +*Exceptions: +* +*******************************************************************************/ + ifstream::ifstream(filedesc _fd) +: istream(new filebuf(_fd)) +{ + delbuf(1); +} + +/*** +*ifstream::ifstream(filedesc fd, char * sbuf, int len) - ifstream constructor +* +*Purpose: +* Constructor for ifstream objects. Creates an associated filebuf object +* and attaches it to the given file descriptor. Filebuf object uses +* user-supplied buffer or is unbuffered if sbuf or len = 0. +* +*Entry: +* fd = file descriptor of file previously opened using _open or _sopen. +* sbuf = pointer to character buffer or NULL if request for unbuffered. +* len = lenght of character buffer sbuf or 0 if request for unbuffered. +* +*Exit: +* None. +* +*Exceptions: +* +*******************************************************************************/ + ifstream::ifstream(filedesc _fd, char * sbuf, int len) +: istream(new filebuf(_fd, sbuf, len)) +//: istream(new filebuf); +{ + delbuf(1); +} + +/*** +*ifstream::~ifstream() - ifstream destructor +* +*Purpose: +* ifstream destructor. +* +*Entry: +* None. +* +*Exit: +* None. +* +*Exceptions: +* +*******************************************************************************/ + ifstream::~ifstream() +{ + // CONSIDER: do anything else? +} + +/*** +*streambuf* ifstream::setbuf(char * ptr, int len) - setbuf function +* +*Purpose: +* ifstream setbuf function +* +*Entry: +* ptr = pointer to buffer or NULL for unbuffered. +* len = length of buffer or zero for unbuffered. +* +*Exit: +* Returns rdbuf() or NULL if error. +* +*Exceptions: +* If ifstream is already open or if rdbuf()->setbuf fails, sets failbit +* and returns NULL. +* +*******************************************************************************/ +streambuf * ifstream::setbuf(char * ptr, int len) +{ + if ((is_open()) || (!(rdbuf()->setbuf(ptr, len)))) + { + clear(state | ios::failbit); + return NULL; + } + // UNDONE: turn off output buffer?? + return rdbuf(); +} + +/*** +*void ifstream::attach(filedesc _fd) - attach member function +* +*Purpose: +* ifstream attach member function. Just calls rdbuf()->attach(). +* +*Entry: +* _fd = file descriptor of previously opened file. +* +*Exit: +* None. +* +*Exceptions: +* Sets failbit if rdbuf()->attach fails. +* +*******************************************************************************/ +void ifstream::attach(filedesc _fd) // CONSIDER: inline? +{ + if (!(rdbuf()->attach(_fd))) + clear(state | ios::failbit); +} + +/*** +*void ifstream::open(const char * name, int mode, int prot) - ifstream open() +* +*Purpose: +* Opens a named file and attaches it to the associated filebuf. +* Just calls rdbuf()->open(). +* +*Entry: +* name = filename to open. +* mode = see filebuf::open mode argument. ios::in is implied. +* prot = see filebuf::open share argument +* +*Exit: +* None. +* +*Exceptions: +* Sets failbit if already open or rdbuf()->open() fails. +* +*******************************************************************************/ +void ifstream::open(const char * name, int mode, int prot) +{ + if (is_open() || !(rdbuf()->open(name, (mode|ios::in), prot))) + clear(state | ios::failbit); +} + +/*** +*void ifstream::close() - close member function +* +*Purpose: +* ifstream close member function. Just calls rdbuf()->close(). +* Clears rdstate() error bits if successful. +* +*Entry: +* None. +* +*Exit: +* None. +* +*Exceptions: +* Sets failbit if rdbuf()->close fails. +* +*******************************************************************************/ +void ifstream::close() +{ + clear((rdbuf()->close()) ? 0 : (state | ios::failbit)); +} diff --git a/private/crt32/iostream/ios.cxx b/private/crt32/iostream/ios.cxx new file mode 100644 index 000000000..6fc3468dc --- /dev/null +++ b/private/crt32/iostream/ios.cxx @@ -0,0 +1,323 @@ +/*** +*ios.cxx - fuctions for ios class. +* +* Copyright (c) 1990-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Functions for ios class. +* +*Revision History: +* 09-10-90 WAJ Initial version. +* 07-02-91 KRS Initial version completed. +* 09-19-91 KRS Make use of delbuf() in destructor. +* 11-04-91 KRS Change init(). Add operator=. Fix constructors. +* 11-11-91 KRS Change xalloc() to conform to AT&T usage. +* 11-20-91 KRS Add copy constructor. +* 02-12-92 KRS Fix init of delbuf in ios::ios(streambuf*). +* 03-30-92 KRS Add MTHREAD lock init calls to constructors. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <iostream.h> +#pragma hdrstop + +const long ios::basefield = (ios::dec | ios::oct | ios::hex); +const long ios::adjustfield = (ios::left | ios::right | ios::internal); +const long ios::floatfield = (ios::scientific | ios::fixed); + +long ios::x_maxbit = 0x8000; // ios::openprot +int ios::x_curindex = -1; + +#ifdef MTHREAD +// UNDONE: enough? 15? +#define MAXINDEX 7 +long ios::x_statebuf[MAXINDEX+1] = { 0,0,0,0,0,0,0,0 }; // MAXINDEX * 0 +int ios::fLockcInit = 0; // nonzero = static lock initialized +RTL_CRITICAL_SECTION ios::x_lockc; +#else // MTHREAD +long * ios::x_statebuf = NULL; +#endif // MTHREAD + +/*** +*ios::ios() - default constructor. +* +*Purpose: +* Initializes an ios. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +ios::ios() +{ + bp = NULL; + state = ios::badbit; + + ispecial = 0; + ospecial = 0; + x_tie = (0); + x_flags = 0; + x_precision = 6; + x_fill = ' '; + x_width = 0; + x_delbuf = 0; + +#ifdef MTHREAD + LockFlg = 1; // default is no locking + _mtlockinit(lockptr()); + if (!fLockcInit) // UNDONE: find a cheaper way to do this + { + _mtlockinit(&x_lockc); + fLockcInit++; + } +#endif /* MTHREAD */ + +} + + + +/*** +*ios::ios( streambuf* pSB ) - constructor. +* +*Purpose: +* Initializes an ios. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +ios::ios( streambuf* pSB ) +{ +// this->ios(); + + bp = pSB; + state = (bp) ? 0 : ios::badbit; + + ispecial = 0; + ospecial = 0; + x_tie = (0); + x_flags = 0; + x_precision = 6; + x_fill = ' '; + x_width = 0; + x_delbuf = 0; + +#ifdef MTHREAD + LockFlg = 1; // default is no locking + _mtlockinit(lockptr()); + if (!fLockcInit) // UNDONE: find a cheaper way to do this + { + _mtlockinit(&x_lockc); + fLockcInit++; + } +#endif /* MTHREAD */ + +} + +/*** +*ios::ios(const ios& _strm) - copy constructor. +* +*Purpose: +* Copy constructor. +* +*Entry: +* _strm = ios to copy data members from. +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ +ios::ios(const ios& _strm) // copy constructor +{ + bp = NULL; + x_delbuf = 0; + +// init(_strm.rdbuf()); +// delbuf(_strm.delbuf()); + + *this = _strm; // invoke assignment operator +// state = (bp) ? 0 : ios::badbit; + +#ifdef MTHREAD + LockFlg = 1; // default is no locking + _mtlockinit(lockptr()); + if (!fLockcInit) // UNDONE: find a cheaper way to do this + { + _mtlockinit(&x_lockc); + fLockcInit++; + } +#endif /* MTHREAD */ + +} + + +/*** +*virtual ios::~ios() - default destructor. +* +*Purpose: +* Terminates an ios. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +ios::~ios() +{ + lock(); // make sure no one else is using it + if ((x_delbuf) && (bp)) + delete bp; + bp = NULL; + state = badbit; + +} + + +/*** +*void ios::init( streambuf* pSB ) - initializes ios +* +*Purpose: +* Initializes an ios. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +void ios::init( streambuf* pSB ) +{ + if (delbuf() && (bp)) // delete previous bp if necessary + delete bp; + bp = pSB; + if (bp) + state &= ~ios::badbit; + else + state |= ios::badbit; +} + + + +/*** +*ios& ios::operator=( const ios& _strm ) - copy an ios. +* +*Purpose: +* Copy an ios. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +ios& ios::operator=(const ios& _strm) +{ + x_tie = _strm.tie(); + x_flags = _strm.flags(); + x_precision = (char)_strm.precision(); + x_fill = _strm.fill(); + x_width = (char)_strm.width(); + + state = _strm.rdstate(); + if (!bp) + state |= ios::badbit; // adjust state for uninitialized bp + +// init(_strm.rdbuf()); +// delbuf(_strm.delbuf()); + + return *this; +} + +/*** +*int ios::xalloc() - ios xalloc member function +* +*Purpose: +* UNDONE: +* +*Entry: +* None. +* +*Exit: +* Returns index of of new entry in new buffer, or EOF if error. +* +*Exceptions: +* Returns EOF if OM error. +* +*******************************************************************************/ +int ios::xalloc() +{ +#ifdef MTHREAD + // buffer must be static if multithread, since thread can't keep track of + // validity of pointer otherwise + int index; + lockc(); + if (x_curindex >= MAXINDEX) + index = EOF; + else + { + index = ++x_curindex; +// x_statebuf[index] = 0; + } + unlockc(); + return index; +#else // MTHREAD + long * tptr; + int i; + + if (!(tptr=new long[x_curindex+2])) // allocate new buffer + return EOF; + + for (i=0; i <= x_curindex; i++) // copy old buffer, if any + tptr[i] = x_statebuf[i]; + + tptr[++x_curindex] = 0L; // init new entry, bump size + + if (x_statebuf) // delete old buffer, if any + delete x_statebuf; + x_statebuf = tptr; // and assign new buffer + return x_curindex; +#endif // MTHREAD +} + +/*** +*long ios::bitalloc() - ios bitalloc member function +* +*Purpose: +* Returns a unused bit mask for flags(). +* +*Entry: +* None. +* +*Exit: +* Returns next available bit maskf. +* +*Exceptions: +* +*******************************************************************************/ +long ios::bitalloc() // CONSIDER: make inline? +{ + long b; + lockc(); // lock to make sure mask in unique (MTHREAD) + b = (x_maxbit<<=1); + unlockc(); + return b; +} diff --git a/private/crt32/iostream/iostream.cxx b/private/crt32/iostream/iostream.cxx new file mode 100644 index 000000000..1ae765423 --- /dev/null +++ b/private/crt32/iostream/iostream.cxx @@ -0,0 +1,83 @@ +/*** +* iostream.cxx - definitions for iostream classes +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains the member function definitions for iostream classes. Also, +* precompile all header files used by iostream lib into iostream.pch. +* +*Revision History: +* 09-23-91 KRS Created. +* 11-13-91 KRS Rearranged. +* 11-20-91 KRS Added copy constructor and assignment operators. +* 01-23-92 KRS Merge pch.cxx into this file. +* +*******************************************************************************/ + +// NOTE: the follow must include ALL header files used by any of the iostream +// source files which we want built into iostream.pch. It is necessary +// to have the pch associated with exactly one of the library modules +// for efficient storage of Codeview info. + +#include <cruntime.h> +#include <internal.h> +#include <limits.h> +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <errno.h> +#include <ctype.h> +#include <share.h> +#include <fcntl.h> +#include <io.h> +// #include <sys\stat.h> // compile problems +#include <sys\types.h> +#include <float.h> +#include <iostream.h> +// #include <ios.h> +// #include <streamb.h> +// #include <istream.h> +// #include <ostream.h> +#include <fstream.h> +#include <strstrea.h> +#include <stdiostr.h> + +#pragma hdrstop // end of headers to precompile + +// CONSIDER: not used, is this required??? + iostream::iostream() +: istream(), ostream() +{ +// CONSIDER: do anything else? +} + + iostream::iostream(streambuf * _sb) +: istream(_sb), ostream(_sb) +{ +// CONSIDER: do anything else? +} + + iostream::iostream(const iostream& _strm) +: istream(_strm), ostream(_strm) +{ +// CONSIDER: do anything else? +} + +iostream::~iostream() +{ +// if both input and output share the same streambuf, but not the same ios, +// make sure only deleted once +if ((istream::bp==ostream::bp) && (&istream::bp!=&ostream::bp)) + istream::bp = NULL; // let ostream::ios::~ios() do it +// CONSIDER: do anything else? +} + +/* done inline +iostream& iostream::operator=(streambuf* _sb) +{ + istream::operator=(_sb); + ostream::operator=(_sb); + return *this; +} +*/ diff --git a/private/crt32/iostream/iostrini.cxx b/private/crt32/iostream/iostrini.cxx new file mode 100644 index 000000000..e6bb83ba2 --- /dev/null +++ b/private/crt32/iostream/iostrini.cxx @@ -0,0 +1,83 @@ +/*** +*iostrini.cxx - definition and initialization for predefined stream cout. +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definition and initialization of and predefined iostream cout. +* +*Revision History: +* 11-18-91 KRS Created. +* +*******************************************************************************/ +#include <cruntime.h> +#include <internal.h> +#include <iostream.h> +#include <fstream.h> +#pragma hdrstop + +// put contructors in special MS-specific XIFM segment +#pragma warning(disable:4074) // disable init_seg warning +#pragma init_seg(compiler) + +#if ((!defined(_WINDOWS)) || defined(_QWIN)) +ostream_withassign cout(new filebuf(1)); + +static Iostream_init __InitCout(cout,-1); + +#endif + +/*** +*Iostream_init::Iostream_init() - initialize predefined streams +* +*Purpose: +* For compatibility only. Not used. +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + Iostream_init::Iostream_init() { } // do nothing + +/*** +*Iostream_init::Iostream_init() - initialize predefined streams +* +*Purpose: +* Initializes predefined streams: cin, cout, cerr, clog; +*Entry: +* pstrm = cin, cout, cerr, or clog +* sflg = 1 if cerr (unit buffered) +* sflg = -1 if cout +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + Iostream_init::Iostream_init(ios& pstrm, int sflg) +{ +#if ((!defined(_WINDOWS)) || defined(_QWIN)) + pstrm.delbuf(1); + if (sflg>=0) // make sure not cout + pstrm.tie(&cout); + if (sflg>0) + pstrm.setf(ios::unitbuf); +#endif +} + +/*** +*Iostream_init::~Iostream_init() - destroy predefined streams on exit +* +*Purpose: +* Destroy predefined streams: cin, cout, cerr, clog; +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + Iostream_init::~Iostream_init() { } // do nothing + diff --git a/private/crt32/iostream/istrchar.cxx b/private/crt32/iostream/istrchar.cxx new file mode 100644 index 000000000..0896a3e9d --- /dev/null +++ b/private/crt32/iostream/istrchar.cxx @@ -0,0 +1,40 @@ +/*** +* istrchar.cxx - definitions for istream class operator>>(char&) +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions of member function for istream operator>>(char&). +* [AT&T C++] +* +*Revision History: +* 09-23-91 KRS Created. Split out from istream.cxx for granularity. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <iostream.h> +#pragma hdrstop + +// CONSIDER: validify all these maximum lengths... + +istream& istream::operator>>(char& c) +{ + int tchar; + if (ipfx(0)) + { + tchar=bp->sbumpc(); + if (tchar==EOF) + { + state |= ios::eofbit|ios::badbit; + } + else + { + c = (char)tchar; + } + isfx(); + } + return *this; +} diff --git a/private/crt32/iostream/istrdbl.cxx b/private/crt32/iostream/istrdbl.cxx new file mode 100644 index 000000000..a129d4d20 --- /dev/null +++ b/private/crt32/iostream/istrdbl.cxx @@ -0,0 +1,46 @@ +/*** +* istrdbl.cxx - definition for operator>>(double) member funct for istream class +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definition of operator>>(double) member function for istream class. +* [AT&T C++] +* +*Revision History: +* 09-26-91 KRS Created. Split off from istream.cxx for granularity. +* 12-30-92 KRS Fix indirection problem with **endptr. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <iostream.h> +#pragma hdrstop + +#pragma check_stack(on) // large buffer(s) + +// CONSIDER: validate this maximum length +#define MAXDBLSIZ 28 + +istream& istream::operator>>(double& n) +{ +_WINSTATIC char ibuffer[MAXDBLSIZ]; + char ** endptr = (char**)NULL; + if (ipfx(0)) + { + if (getdouble(ibuffer, MAXDBLSIZ)>0) + { + n = strtod(ibuffer, endptr); +#if 0 + if (**endptr) + { + //UNDONE: put back any unread characters, if possible + } +#endif + } + isfx(); + } +return *this; +} diff --git a/private/crt32/iostream/istream.cxx b/private/crt32/iostream/istream.cxx new file mode 100644 index 000000000..d5916001d --- /dev/null +++ b/private/crt32/iostream/istream.cxx @@ -0,0 +1,278 @@ +/*** +* istream.cxx - definitions for istream and istream_withassign classes +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions of member functions for istream and istream_withassign +* classes. +* [AT&T C++] +* +*Revision History: +* 07-15-91 KRS Created. +* 08-15-91 KRS Fix handling of 1-length strings in get(char*,int,int) +* 08-20-91 KRS Make read() not do text translation (for filebufs) +* 08-21-91 KRS Fix >>(streambuf *) to advance pointer properly. +* 08-22-91 KRS Fix octal error in getint(). +* 08-26-91 KRS Fix for Windows DLL's and set max. ibuffer[] lengths. +* 09-05-91 KRS Don't special-case 0x in getint(). Spec. conformance... +* 09-10-91 KRS Reinstate text translation (by default) for read(). +* 09-12-91 KRS Treat count as unsigned in get() and read(). +* 09-16-91 KRS Fix get(char *, int lim, char) for lim=0 case. +* 09-23-91 KRS Split up flie for granularity purposes. +* 10-21-91 KRS Make eatwhite() return void again. +* 10-24-91 KRS Move istream_withassign::operator=(streambuf*) here. +* 11-04-91 KRS Make constructors work with virtual base. +* Fix whitespace error handling in ipfx(). +* 11-20-91 KRS Add/fix copy constructor and assignment operators. +* 01-23-92 KRS C700 #5883: Fix behaviour of peek() to call ipfx(1). +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <ctype.h> +#include <iostream.h> +#pragma hdrstop + + istream::istream() +// : ios() +{ + x_flags |= ios::skipws; + x_gcount = 0; + _fGline = 0; + // CONSIDER: do anything else? +} + + istream::istream(streambuf* _inistbf) +// : ios() +{ + init(_inistbf); + + x_flags |= ios::skipws; + x_gcount = 0; + _fGline = 0; + // CONSIDER: do anything else? +} + + istream::istream(const istream& _istrm) +// : ios() +{ + init(_istrm.rdbuf()); + + x_flags |= ios::skipws; + x_gcount = 0; + _fGline = 0; + // CONSIDER: do anything else? +} + + istream::~istream() +// : ~ios() +{ + // CONSIDER: do anything else? +} + +// used by ios::sync_with_stdio() +istream& istream::operator=(streambuf * _sbuf) +{ +// consider: may be some redundency here, depending on spec. + if (delbuf() && rdbuf()) + delete rdbuf(); + bp = 0; + + this->ios::operator=(ios()); // initialize ios members + delbuf(0); // important! + init(_sbuf); // set up bp + + x_flags |= ios::skipws; // init istream members too + x_gcount = 0; +// _fGline = 0; // not necessary + + return *this; +} +int istream::ipfx(int need) +{ + lock(); + if (need) // reset gcount if unformatted input + x_gcount = 0; + if (state) // return 0 iff error condition + { + state |= ios::failbit; // solves cin>>buf problem + unlock(); + return 0; + } + if (x_tie && ((need==0) || (need > bp->in_avail()))) + { + x_tie->flush(); + } + lockbuf(); + if ((need==0) && (x_flags & ios::skipws)) + { + eatwhite(); + if (state) // eof or error + { + state |= ios::failbit; + unlockbuf(); + unlock(); + return 0; + } + } + // leave locked ; isfx() will unlock + return 1; // return nz if okay +} + +// formatted input functions + +istream& istream::operator>>(char * s) +{ + int c; + unsigned int i, lim; + if (ipfx(0)) + { + lim = (unsigned)(x_width-1); + x_width = 0; + if (!s) + { + state |= ios::failbit; + } + else + { + for (i=0; i< lim; i++) + { + c=bp->sgetc(); + if (c==EOF) + { + state |= ios::eofbit; + if (!i) + state |= ios::failbit|ios::badbit; + break; + } + else if (isspace(c)) + { + break; + } + else + { + s[i] = (char)c; + bp->stossc(); // advance pointer + } + } + if (!i) + state |= ios::failbit; + else + s[i] = '\0'; + } + isfx(); + } + return *this; +} + +#if 0 +istream& istream::operator>>(char& c) +{ + int tchar; + if (ipfx(0)) + { + tchar=bp->sbumpc(); + if (tchar==EOF) + { + state |= ios::eofbit|ios::badbit; + } + else + { + c = (char)tchar; + } + isfx(); + } + return *this; +} +#endif + +int istream::peek() +{ +int retval; + if (ipfx(1)) + { + retval = (bp->sgetc()); + isfx(); + } + else + retval = EOF; + return retval; +} + +istream& istream::putback(char c) +{ +// if (!bad()) // CONSIDER: just return if bad error? + if (good()) // this is how it's spec.ed out... + { + lockbuf(); + + if (bp->sputbackc(c)==EOF) + { + clear(state | ios::failbit); + } +// else +// clear(state & ~(ios::eof)); // no longer at end of file, if we were... + + unlockbuf(); + } + return *this; +} + +int istream::sync() +{ + int retval; + lockbuf(); + + if ((retval=bp->sync())==EOF) + { + clear(state | (ios::failbit|ios::badbit)); + } + + unlockbuf(); + return retval; +} + +void istream::eatwhite() +{ + int c; + lockbuf(); + c = bp->sgetc(); + for ( ; ; ) + { + if (c==EOF) + { + clear(state | ios::eofbit); + break; + } + if (isspace(c)) + { + c = bp->snextc(); + } + else + { + break; + } + } + unlockbuf(); +} + + istream_withassign::istream_withassign() +: istream() +{ + // CONSIDER: do anything else? +} + + istream_withassign::istream_withassign(streambuf* _is) +: istream(_is) +{ + // CONSIDER: do anything else? +} + + istream_withassign::~istream_withassign() +// : ~istream() +{ + // CONSIDER: do anything else? +} diff --git a/private/crt32/iostream/istream1.cxx b/private/crt32/iostream/istream1.cxx new file mode 100644 index 000000000..ff2bc8098 --- /dev/null +++ b/private/crt32/iostream/istream1.cxx @@ -0,0 +1,98 @@ +/*** +* istream1.cxx - non-core definitions for istream & istream_withassign classes +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions of non-core member functions for istream and +* istream_withassign classes. +* [AT&T C++] +* +*Revision History: +* 09-23-91 KRS Created. Split off from istream.cxx for granularity. +* 10-07-91 KRS Increment x_gcount in get(sb). +* 10-24-91 KRS Fix istream_withassign::operator=() functions. +* 11-20-91 KRS Make operator= inline. +* 03-30-92 KRS Add multithread locking. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <iostream.h> +#pragma hdrstop + +istream& istream::operator>>(streambuf* _sbuf) +{ + int c; + if (ipfx(0)) + { + while ((c=bp->sbumpc())!=EOF) + { + if (_sbuf->sputc(c)==EOF) + { + state |= ios::failbit; + } + } + isfx(); + } +return *this; +} + + +// unformatted input functions + +istream& istream::get( streambuf& sbuf, char delim) +{ + int c; + if (ipfx(1)) // resets x_gcount + { + while ((c = bp->sgetc())!=delim) + { + if (c==EOF) // stop if EOF encountered + { + state |= ios::eofbit; + break; + } + bp->stossc(); // advance get pointer + x_gcount++; // and increment count + + // store c into sbuf // UNDONE: is this what they meant??? + if (sbuf.sputc(c)==EOF) + state |= ios::failbit; + } + isfx(); + } + return *this; +} + +istream& istream::seekg(streampos _strmp) +{ + lockbuf(); + if (bp->seekpos(_strmp, ios::in)==EOF) + { + clear(state | failbit); + } + unlockbuf(); + return(*this); +} + +istream& istream::seekg(streamoff _strmf, seek_dir _sd) +{ + lockbuf(); + if (bp->seekoff(_strmf, _sd, ios::in)==EOF) + clear(state | failbit); + unlockbuf(); + return(*this); +} + +streampos istream::tellg() +{ + streampos retval; + lockbuf(); + if ((retval=bp->seekoff(streamoff(0), ios::cur, ios::in))==EOF) + clear(state | failbit); + unlockbuf(); + return(retval); +} diff --git a/private/crt32/iostream/istrflt.cxx b/private/crt32/iostream/istrflt.cxx new file mode 100644 index 000000000..79c04a11b --- /dev/null +++ b/private/crt32/iostream/istrflt.cxx @@ -0,0 +1,58 @@ +/*** +* istrflt.cxx - definitions for istream operator>>(float) member function +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions of operator>>(float) member function for istream class. +* [AT&T C++] +* +*Revision History: +* 09-26-91 KRS Created. Split out from istream.cxx for granularity. +* 12-30-92 KRS Fix indirection problem with **endptr. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <float.h> +#include <iostream.h> +#pragma hdrstop + +#pragma check_stack(on) // large buffer(s) + +// CONSIDER: validate this maximum length +#define MAXFLTSIZ 20 + +istream& istream::operator>>(float& n) +{ +_WINSTATIC char ibuffer[MAXFLTSIZ]; + double d; + char ** endptr = (char**)NULL; + if (ipfx(0)) + { + if (getdouble(ibuffer, MAXFLTSIZ)>0) + { + d = strtod(ibuffer, endptr); +#if 0 + if (**endptr) + { + //UNDONE: put back any unread characters, if possible + } +#endif + if (d > FLT_MAX) + n = FLT_MAX; + else if (d < -FLT_MAX) + n = -FLT_MAX; + else if ((d>0) && (d< FLT_MIN)) + n = FLT_MIN; + else if ((d<0) && (d> -FLT_MIN)) + n = - FLT_MIN; + else + n = (float) d; + } + isfx(); + } +return *this; +} diff --git a/private/crt32/iostream/istrgdbl.cxx b/private/crt32/iostream/istrgdbl.cxx new file mode 100644 index 000000000..e3bb18b2b --- /dev/null +++ b/private/crt32/iostream/istrgdbl.cxx @@ -0,0 +1,105 @@ +/*** +* istrgdbl.cxx - definitions for istream class core double routine +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions of member function for istream getdouble(). +* [AT&T C++] +* +*Revision History: +* 09-26-91 KRS Created. Split off from istream.cxx for granularity. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <ctype.h> +#include <iostream.h> +#pragma hdrstop + +/*** +*int istream::getdouble(char * buffer, int buflen) - get a double +* +*Purpose: +* Get a double from stream. +* +*Entry: +* char * buffer = area for number to be copied. +* int buflen = max. length of buffer +* +*Exit: +* Returns 0 if fatal error +* Otherwise, returns length of buffer filled. +* Sets ios::failbit on error forming number. +* If successful, buffer[] contains the number, followed by \0. +* +*Exceptions: +* +*******************************************************************************/ +int istream::getdouble(char * buffer, int buflen) // returns length +{ + int c; + int i = 0; + int fDigit = 0; // true if legal digit encountered + int fDecimal=0; // true if '.' encountered or no longer valid + int fExp=0; // true if 'E' or 'e' encounted + + if (ipfx(0)) + { + c=bp->sgetc(); + for (; i<buflen; buffer[i] = (char)c,c=bp->snextc(),i++) + { + if (c==EOF) + { + state |= ios::eofbit; + break; + } + if ((!i) || (fExp==1)) + { + if ((c=='-') || (c=='+')) + { + continue; + } + } + // UNDONE: use locale-dependent decimal point character!!! + if ((c=='.') && (!fExp) && (!fDecimal)) + { + fDecimal++; + continue; + } + if (((c=='E') || (c=='e')) && (!fExp)) + { + fDecimal++; // can't allow decimal now + fExp++; + continue; + } + if (!isdigit(c)) + break; + if (fExp) + fExp++; + else + fDigit++; + } + if (fExp==1) // E or e with no number after it + { + if (bp->sputbackc(buffer[i])!=EOF) + { + i--; + state &= ~(ios::eofbit); + } + else + { + state |= ios::failbit; // CONSIDER: correct? + } + } + if ((!fDigit) || (i==buflen)) + state |= ios::failbit; + + // buffer contains a valid number or '\0' + buffer[i] = '\0'; + isfx(); + } + // UNDONE: check for errors, return EOF + return i; +} diff --git a/private/crt32/iostream/istrget.cxx b/private/crt32/iostream/istrget.cxx new file mode 100644 index 000000000..7b099cda7 --- /dev/null +++ b/private/crt32/iostream/istrget.cxx @@ -0,0 +1,65 @@ +/*** +* istrget.cxx - definitions for istream class get() member functions +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions of get() member functions for istream class. +* [AT&T C++] +* +*Revision History: +* 09-26-91 KRS Created. Split off from istream.cxx for granularity. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <iostream.h> +#pragma hdrstop + +// unformatted input functions + +int istream::get() +{ + int c; + if (ipfx(1)) // resets x_gcount + { + if ((c=bp->sbumpc())==EOF) + state |= ios::eofbit; + else + x_gcount++; + isfx(); + return c; + } + return EOF; +} + +// signed and unsigned char make inline calls to this: +istream& istream::get( char& c) +{ + int temp; + if (ipfx(1)) // resets x_gcount + { + if ((temp=bp->sbumpc())==EOF) + state |= (ios::failbit|ios::eofbit); // UNDONE ?? + else + x_gcount++; + c = (char) temp; + isfx(); + } + return *this; +} + + +// called by signed and unsigned char versions +istream& istream::read(char * ptr, int n) +{ + if (ipfx(1)) // resets x_gcount + { + x_gcount = bp->sgetn(ptr, n); + if ((unsigned)x_gcount < (unsigned)n) + state |= (ios::failbit|ios::eofbit); + isfx(); + } + return *this; +} diff --git a/private/crt32/iostream/istrgetl.cxx b/private/crt32/iostream/istrgetl.cxx new file mode 100644 index 000000000..a8ce283cf --- /dev/null +++ b/private/crt32/iostream/istrgetl.cxx @@ -0,0 +1,70 @@ +/*** +* istrgetl.cxx - definitions for istream class getl() member function +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions of get and getline member functions istream class. +* [AT&T C++] +* +*Revision History: +* 09-26-91 KRS Created. Split off from istream.cxx for granularity. +* 01-23-92 KRS C700 #5880: Add cast to fix comparison in get(). +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <iostream.h> +#pragma hdrstop + +// unformatted input functions + +// signed and unsigned char make inline calls to this: +// all versions of getline also share this code: + +istream& istream::get( char *b, int lim, char delim) +{ + int c; + unsigned int i = 0; + if (ipfx(1)) // resets x_gcount + { + if (lim--) + { + while (i < (unsigned)lim) + { + c=bp->sgetc(); + if (c==EOF) + { + state |= ios::eofbit; + if (!i) + state |= ios::failbit; + break; + } + else if (c==(int)(unsigned char)delim) + { + if (_fGline) + { + x_gcount++; + bp->stossc(); // extract delim if called from getline + } + break; + } + else + { + if (b) + b[i] = (char)c; + bp->stossc(); // advance pointer + } + i++; + } + x_gcount += i; // set gcount() + } + isfx(); + lim++; // restore lim for test below + } + if ((b) && (lim)) // always null-terminate, if possible + b[i] = '\0'; + _fGline = 0; + return *this; +} diff --git a/private/crt32/iostream/istrgint.cxx b/private/crt32/iostream/istrgint.cxx new file mode 100644 index 000000000..0f4997483 --- /dev/null +++ b/private/crt32/iostream/istrgint.cxx @@ -0,0 +1,145 @@ +/*** +* istrgint.cxx - definitions for istream class core integer routines +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions of member function getint() for istream class. +* [AT&T C++] +* +*Revision History: +* 09-26-91 KRS Created. Split off from istream.cxx for granularity. +* 01-06-92 KRS Remove buflen argument. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <ctype.h> +#include <iostream.h> +#pragma hdrstop + +#define MAXLONGSIZE 16 + +/*** +*int istream::getint(char * buffer) - get an int +* +*Purpose: +* Get an int from stream. +* +*Entry: +* char * buffer = area for number to be copied. +* +*Exit: +* Returns base for conversion: (0, 2, 8, or 16). +* If successful, buffer[] contains the number, followed by \0. +* +*Exceptions: +* Sets ios::failbit on error forming number. +* Sets ios::badbit on error after failbit +* Sets ios::eofbit if at EOF at return +* +*******************************************************************************/ +int istream::getint(char * buffer) // returns length +{ + int base, i; + int c; + int fDigit = 0; + int bindex = 1; + + if (x_flags & ios::dec) + base = 10; + else if (x_flags & ios::hex) + base = 16; + else if (x_flags & ios::oct) + base = 8; + else + base = 0; + + if (ipfx(0)) + { + c=bp->sgetc(); + for (i = 0; i<MAXLONGSIZE; buffer[i] = (char)c,c=bp->snextc(),i++) + { + if (c==EOF) + { + state |= ios::eofbit; + break; + } + if (!i) + { + if ((c=='-') || (c=='+')) + { + bindex++; + continue; + } + } + if ((i==bindex) && (buffer[i-1]=='0')) + { + if (((c=='x') || (c=='X')) && ((base==0) || (base==16))) + { + base = 16; // simplifies matters + fDigit = 0; + continue; + } + else if (base==0) + { + base = 8; + } + } + + + // now simply look for a digit and set fDigit if found else break + + if (base==16) + { + if (!isxdigit(c)) + break; + } + else if ((!isdigit(c)) || ((base==8) && (c>'7'))) + break; + + fDigit++; + } + if (!fDigit) + { +#if 0 + if ((i==bindex+1) && ((buffer[bindex]=='x') || (buffer[bindex]=='X'))) + { + // found 0x but no number, so back up one and return '0' + if (bp->sputbackc(buffer[bindex])!=EOF) + { + i--; + state &= ~(ios::eofbit); + // fDigit++; + } + else + state |= (ios::failbit|ios::badbit); // CONSIDER: correct? + } + else +#endif + { + state |= ios::failbit; + while (i--) + { + if(bp->sputbackc(buffer[i])==EOF) + { + state |= ios::badbit; // CONSIDER: correct? + break; + } + else + state &= ~(ios::eofbit); + } + i=0; + } + } + // buffer contains a valid number or '\0' + buffer[i] = '\0'; + isfx(); + } + if (i==MAXLONGSIZE) + { + state |= ios::failbit; + } + return base; +} diff --git a/private/crt32/iostream/istrint.cxx b/private/crt32/iostream/istrint.cxx new file mode 100644 index 000000000..30c8eae48 --- /dev/null +++ b/private/crt32/iostream/istrint.cxx @@ -0,0 +1,71 @@ +/*** +* istrint.cxx - definitions for istream class operaotor>>(int) member functions +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions of operator>>(int) member function(s) for istream class. +* [AT&T C++] +* +*Revision History: +* 09-26-91 KRS Created. Split out from istream.cxx for granularity. +* 12-30-92 KRS Fix indirection problem with **endptr. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <limits.h> +#include <iostream.h> +#pragma hdrstop + +// CONSIDER: validify all this maximum length +#define MAXLONGSIZ 16 + +/*** +*istream& istream::operator>>(int& n) - extract int +* +*Purpose: +* Extract int value from stream +* +*Entry: +* n = value to update +* +*Exit: +* n updated, or ios::failbit & n=INT_MAX/INT_MIN on overflow/underflow +* +*Exceptions: +* Stream error on entry or value out of range +* +*******************************************************************************/ +istream& istream::operator>>(int& n) +{ +_WINSTATIC char ibuffer[MAXLONGSIZ]; + long value; + char ** endptr = (char**)NULL; + if (ipfx(0)) + { + value = strtol(ibuffer, endptr, getint(ibuffer)); + if (value>INT_MAX) + { + n = INT_MAX; + state |= ios::failbit; + } + else if (value<INT_MIN) + { + n = INT_MIN; + state |= ios::failbit; + } + else + n = (int) value; +#if 0 + if (**endptr) + { + // UNDONE: put back any unread characters, if possible + } +#endif + isfx(); + } +return *this; +} diff --git a/private/crt32/iostream/istrldbl.cxx b/private/crt32/iostream/istrldbl.cxx new file mode 100644 index 000000000..6059685b2 --- /dev/null +++ b/private/crt32/iostream/istrldbl.cxx @@ -0,0 +1,48 @@ +/*** +* istrldbl.cxx - definitions for istream class operator>>(long double) funct +* +* Copyright (c) 1991-1993, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions of operator>>(long double) member function for istream +* class. +* [AT&T C++] +* +*Revision History: +* 09-26-91 KRS Created. Split off from istream.cxx for granularity. +* 12-30-92 KRS Fix indirection problem with **endptr. +* 05-10-93 CFW Re-enable function, use strtod since no _strtold. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <iostream.h> +#pragma hdrstop + +#pragma check_stack(on) // large buffer(s) + +// CONSIDER: validate this maximum length +#define MAXLDBLSIZ 32 + +istream& istream::operator>>(long double& n) +{ +_WINSTATIC char ibuffer[MAXLDBLSIZ]; + char ** endptr = (char**)NULL; + if (ipfx(0)) + { + if (getdouble(ibuffer, MAXLDBLSIZ)>0) + { + n = (long double)strtod(ibuffer, endptr); +#if 0 + if (**endptr) + { + //UNDONE: put back any unread characters, if possible + } +#endif + } + isfx(); + } +return *this; +} diff --git a/private/crt32/iostream/istrlong.cxx b/private/crt32/iostream/istrlong.cxx new file mode 100644 index 000000000..1a701da0b --- /dev/null +++ b/private/crt32/iostream/istrlong.cxx @@ -0,0 +1,63 @@ +/*** +* istrlong.cxx - definitions for istream class operator>>(long) member function +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions of operator>>(long) member function(s) for istream class. +* [AT&T C++] +* +*Revision History: +* 09-26-91 KRS Created. Split off from istream.cxx for granularity. +* 01-06-92 KRS Added check of errno. +* 12-30-92 KRS Fix indirection problem with **endptr. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <limits.h> +#include <errno.h> +#include <iostream.h> +#pragma hdrstop + +// CONSIDER: validify this maximum length +#define MAXLONGSIZ 16 + +/*** +*istream& istream::operator>>(long& n) - extract long +* +*Purpose: +* Extract long value from stream +* +*Entry: +* n = value to update +* +*Exit: +* n updated, or ios::failbit & n=LONG_MAX/LONG_MIN on overflow/underflow +* +*Exceptions: +* Stream error on entry or value out of range +* +*******************************************************************************/ +istream& istream::operator>>(long& n) +{ +_WINSTATIC char ibuffer[MAXLONGSIZ]; + char ** endptr = (char**)NULL; + if (ipfx(0)) { + n = strtol(ibuffer, endptr, getint(ibuffer)); + if (errno==ERANGE) + { + state |= ios::failbit; + } +#if 0 + if (**endptr) + { + //UNDONE: put back any unread characters, if possible + } +#endif + isfx(); + } +return *this; +} diff --git a/private/crt32/iostream/istrshrt.cxx b/private/crt32/iostream/istrshrt.cxx new file mode 100644 index 000000000..e61a71dbf --- /dev/null +++ b/private/crt32/iostream/istrshrt.cxx @@ -0,0 +1,71 @@ +/*** +* istrshrt.cxx - definitions for istream class operator>>(short) function(s) +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions of operator>>(short) member function(s) for istream class. +* [AT&T C++] +* +*Revision History: +* 09-26-91 KRS Created. Split off from istream.cxx for granularity. +* 12-30-92 KRS Fix indirection problem with **endptr. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <limits.h> +#include <iostream.h> +#pragma hdrstop + +// CONSIDER: validify this maximum length +#define MAXLONGSIZ 16 + +/*** +*istream& istream::operator>>(short& n) - extract short +* +*Purpose: +* Extract short value from stream +* +*Entry: +* n = value to update +* +*Exit: +* n updated, or ios::failbit & n=SHRT_MAX/SHRT_MIN on overflow/underflow +* +*Exceptions: +* Stream error on entry or value out of range +* +*******************************************************************************/ +istream& istream::operator>>(short& n) +{ +_WINSTATIC char ibuffer[MAXLONGSIZ]; + long value; + char ** endptr = (char**)NULL; + if (ipfx(0)) + { + value = strtol(ibuffer, endptr, getint(ibuffer)); + if (value>SHRT_MAX) + { + n = SHRT_MAX; + state |= ios::failbit; + } + else if (value<SHRT_MIN) + { + n = SHRT_MIN; + state |= ios::failbit; + } + else + n = (short) value; +#if 0 + if (**endptr) + { + //UNDONE: put back any unread characters, if possible + } +#endif + isfx(); + } +return *this; +} diff --git a/private/crt32/iostream/istruint.cxx b/private/crt32/iostream/istruint.cxx new file mode 100644 index 000000000..9432b08f4 --- /dev/null +++ b/private/crt32/iostream/istruint.cxx @@ -0,0 +1,72 @@ +/*** +* istruint.cxx - definitions for istream class operaotor>>(unsigned int) funct +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions of operator>>(unsigned int) member function for istream +* class. +* [AT&T C++] +* +*Revision History: +* 09-26-91 KRS Created. Split out from istream.cxx for granularity. +* 01-06-92 KRS Improve error handling. +* 12-30-92 KRS Fix indirection problem with **endptr. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <limits.h> +#include <errno.h> +#include <iostream.h> +#pragma hdrstop + +// CONSIDER: validify all this maximum length +#define MAXLONGSIZ 16 + +/*** +*istream& istream::operator>>(unsigned int& n) - extract unsigned int +* +*Purpose: +* Extract unsigned int value from stream +* Valid range is INT_MIN to UINT_MAX. +* +*Entry: +* n = value to update +* +*Exit: +* n updated, or ios::failbit and n=UINT_MAX if error +* +*Exceptions: +* Stream error on entry or value out of range +* +*******************************************************************************/ +istream& istream::operator>>(unsigned int& n) +{ +_WINSTATIC char ibuffer[MAXLONGSIZ]; + unsigned long value; + char ** endptr = (char**)NULL; + if (ipfx(0)) { + value = strtoul(ibuffer, endptr, getint(ibuffer)); + + // CONSIDER: all but the first check is to emulate strtoul behavior + if (((value>UINT_MAX) && (value<=(ULONG_MAX-(-INT_MIN)))) + || ((value==ULONG_MAX) && (errno==ERANGE))) + { + n = UINT_MAX; + state |= ios::failbit; + } + else + n = (unsigned int) value; +#if 0 + if (**endptr) + { + //UNDONE: put back any unread characters, if possible + } +#endif + isfx(); + } +return *this; +} diff --git a/private/crt32/iostream/istrulng.cxx b/private/crt32/iostream/istrulng.cxx new file mode 100644 index 000000000..f57d2572a --- /dev/null +++ b/private/crt32/iostream/istrulng.cxx @@ -0,0 +1,65 @@ +/*** +* istrulng.cxx - definitions for istream class operator>>(unsigned long) funct +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions of operator>>(unsigned long) member function(s) for istream +* class. +* [AT&T C++] +* +*Revision History: +* 09-26-91 KRS Created. Split off from istream.cxx for granularity. +* 01-06-91 KRS Added check of errno. +* 12-30-92 KRS Fix indirection problem with **endptr. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <limits.h> +#include <errno.h> +#include <iostream.h> +#pragma hdrstop + + +// CONSIDER: validify this maximum length +#define MAXLONGSIZ 16 + +/*** +*istream& istream::operator>>(unsigned long& n) - extract unsigned long +* +*Purpose: +* Extract unsigned long value from stream +* +*Entry: +* n = value to update +* +*Exit: +* n updated, or ios::failbit and n=ULONG_MAX if error +* +*Exceptions: +* Stream error on entry or value out of range +* +*******************************************************************************/ +istream& istream::operator>>(unsigned long& n) +{ +_WINSTATIC char ibuffer[MAXLONGSIZ]; + char ** endptr = (char**)NULL; + if (ipfx(0)) { + n = strtoul(ibuffer, endptr, getint(ibuffer)); + if ((n==ULONG_MAX) && (errno==ERANGE)) + { + state |= ios::failbit; + } +#if 0 + if (**endptr) + { + //UNDONE: put back any unread characters, if possible + } +#endif + isfx(); + } +return *this; +} diff --git a/private/crt32/iostream/istrusht.cxx b/private/crt32/iostream/istrusht.cxx new file mode 100644 index 000000000..a464dedbb --- /dev/null +++ b/private/crt32/iostream/istrusht.cxx @@ -0,0 +1,73 @@ +/*** +* istrusht.cxx - definitions for istream class operator>>(unsigned short) funct +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Definitions of operator>>(unsigned short) member function for istream +* class. +* [AT&T C++] +* +*Revision History: +* 09-26-91 KRS Created. Split off from istream.cxx for granularity. +* 01-06-92 KRS Improved error handling. +* 12-30-92 KRS Fix indirection problem with **endptr. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <limits.h> +#include <errno.h> +#include <iostream.h> +#pragma hdrstop + +// CONSIDER: validify this maximum length +#define MAXLONGSIZ 16 + +/*** +*istream& istream::operator>>(unsigned short& n) - extract unsigned short +* +*Purpose: +* Extract unsigned short value from stream +* Valid range is SHRT_MIN to USHRT_MAX. +* +*Entry: +* n = value to update +* +*Exit: +* n updated, or ios::failbit and n=USHRT_MAX if error +* +*Exceptions: +* Stream error on entry or value out of range +* +*******************************************************************************/ +istream& istream::operator>>(unsigned short& n) +{ +_WINSTATIC char ibuffer[MAXLONGSIZ]; + unsigned long value; + char ** endptr = (char**)NULL; + if (ipfx(0)) + { + value = strtoul(ibuffer, endptr, getint(ibuffer)); + + // CONSIDER: all but the first check is to emulate strtoul behavior + if (((value>USHRT_MAX) && (value<=(ULONG_MAX-(-SHRT_MIN)))) + || ((value==ULONG_MAX) && (errno==ERANGE))) + { + n = USHRT_MAX; + state |= ios::failbit; + } + else + n = (unsigned short) value; +#if 0 + if (**endptr) + { + //UNDONE: put back any unread characters, if possible + } +#endif + isfx(); + } +return *this; +} diff --git a/private/crt32/iostream/makefile b/private/crt32/iostream/makefile new file mode 100644 index 000000000..9e18c9a05 --- /dev/null +++ b/private/crt32/iostream/makefile @@ -0,0 +1,7 @@ +# +# DO NOT EDIT THIS FILE!!! Edit .\sources. if you want to add a new source +# file to this component. This file merely indirects to the real make file +# that is shared by all the components of NT OS/2 +# +BLDCRT=1 +!INCLUDE $(NTMAKEENV)\makefile.def diff --git a/private/crt32/iostream/mtlock.c b/private/crt32/iostream/mtlock.c new file mode 100644 index 000000000..78201917d --- /dev/null +++ b/private/crt32/iostream/mtlock.c @@ -0,0 +1,943 @@ +/*** +*mtlock.c - Multi-thread locking routines +* +* Copyright (c) 1987-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains definitions for general-purpose multithread locking functions. +* _mtlockinit() +* _mtlock() +* _mtunlock() +* +*Revision History: +* 03-10-92 KRS Created from mlock.c. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <oscalls.h> +#include <internal.h> +#include <os2dll.h> +#include <rterr.h> +#include <stddef.h> +#include <limits.h> + +#ifdef MTHREAD + +void _CALLTYPE2 _mtlockinit( PRTL_CRITICAL_SECTION pLk) +{ + +#ifdef _CRUISER_ + if ( DOSCREATEMUTEXSEM(NULL, pLk, 0, 0) != 0) + _FATAL; +#else /* ndef _CRUISER_ */ +#ifdef _WIN32_ + /* + * Initialize the critical section. + */ + InitializeCriticalSection( pLk ); +#else /* ndef _WIN32_ */ +#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED! +#endif /* _WIN32_ */ +#endif /* _CRUISER_ */ + +} + +void _CALLTYPE2 _mtlock ( PRTL_CRITICAL_SECTION pLk) +{ +#ifdef _CRUISER_ + + if (DOSREQUESTMUTEXSEM(pLk,-1) != 0) + _FATAL; + +#else /* ndef _CRUISER_ */ + +#ifdef _WIN32_ + + /* + * Enter the critical section. + */ + EnterCriticalSection( pLk ); + +#else /* ndef _WIN32_ */ + +#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED! + +#endif /* _WIN32_ */ + +#endif /* _CRUISER_ */ + +} + +void _CALLTYPE2 _mtunlock ( PRTL_CRITICAL_SECTION pLk) +{ +#ifdef _CRUISER_ + /* + * Release the lock + */ + if (DOSRELEASEMUTEXSEM(pLk) != 0) + _FATAL; + +#else /* ndef _CRUISER_ */ + +#ifdef _WIN32_ + + /* + * leave the critical section. + */ + LeaveCriticalSection( pLk ); + +#else /* ndef _WIN32_ */ + +#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED! + +#endif /* _WIN32_ */ + +#endif /* _CRUISER_ */ + +} + +#endif /* MTHREAD */ + + + + + + + + + + + +/* history: mlock.c */ + +#if 0 +#ifdef DEBUG +#include <assert.h> +#endif + +/* + * Local routines + */ + +static void _CALLTYPE1 _lock_create (unsigned); + +#ifdef DEBUG +static struct _debug_lock * _CALLTYPE1 _lock_validate(int); +#endif + + +/* + * Global Data + */ + +#ifdef _CRUISER_ +/* + * Lock Table + * This table contains the semaphore handle for each lock (lock number + * is used as an index into the table). + */ + +unsigned _locktable[_TOTAL_LOCKS]; /* array of locks */ + +#else /* ndef _CRUISER_ */ + +#ifdef _WIN32_ +/* + * Lock Table + * This table contains the critical section management structure of each + * lock. + */ + +RTL_CRITICAL_SECTION _locktable[_TOTAL_LOCKS]; /* array of locks */ + +#else /* ndef _WIN32_ */ + +#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED! + +#endif /* _WIN32_ */ + +#endif /* _CRUISER_ */ + +/* + * Lock Bit Map + * This table contains one bit for each lock (i.e., each entry in the + * _locktable[] array). + * + * If bit = 0, lock has not been created/opened + * If bit = 1, lock has been created/opened + */ + +char _lockmap[(_TOTAL_LOCKS/CHAR_BIT)+1]; /* lock bit map */ + + +#ifdef _LOCKCOUNT +/* + * Total number of locks held + */ + +unsigned _lockcnt = 0; +#endif + + +#ifdef DEBUG +/* + * Lock Debug Data Table Segment + * Contains debugging data for each lock. + */ + +struct _debug_lock _debug_locktable[_TOTAL_LOCKS]; + +#endif + +#define _FATAL _amsg_exit(_RT_LOCK) + +/*** +* Bit map macros +* +*Purpose: +* _CLEARBIT() - Clear the specified bit +* _SETBIT() - Set the specified bit +* _TESTBIT() - Test the specified bit +* +*Entry: +* char a[] = character array +* unsigned b = bit number (0-based, range from 0 to whatever) +* unsigned x = bit number (0-based, range from 0 to 31) +* +*Exit: +* _CLEARBIT() = void +* _SETBIT() = void +* _TESTBIT() = 0 or 1 +* +*Exceptions: +* +*******************************************************************************/ + +/* + * Macros for use when managing a bit in a character array (e.g., _lockmap) + * a = character array + * b = bit number (0-based) + */ + +#define _CLEARBIT(a,b) \ + ( a[b>>3] &= (~(1<<(b&7))) ) + +#define _SETBIT(a,b) \ + ( a[b>>3] |= (1<<(b&7)) ) + +#define _TESTBIT(a,b) \ + ( a[b>>3] & (1<<(b&7)) ) + +/* + * Macros for use when managing a bit in an unsigned int + * x = bit number (0-31) + */ + +#define _BIT_INDEX(x) (1 << (x & 0x1F)) + + +/*** +*_mtinitlocks() - Initialize the semaphore lock data base +* +*Purpose: +* Initialize the mthread semaphore lock data base. +* +* NOTES: +* (1) Only to be called ONCE at startup +* (2) Must be called BEFORE any mthread requests are made +* +* Schemes for creating the mthread locks: +* +* Create the locks one at a time on demand the first +* time the lock is attempted. This is more complicated but +* is much faster than creating them all at startup time. +* These is currently the default scheme. +* +* Create and open the semaphore that protects the lock data +* base. +* +*Entry: +* <none> +* +*Exit: +* returns on success +* calls _amsg_exit on failure +* +*Exceptions: +* +*******************************************************************************/ + +void _CALLTYPE1 _mtinitlocks ( + void + ) +{ + + /* + * All we need to do is create the lock table lock + */ + + _lock_create(_LOCKTAB_LOCK); + + /* + * Make sure the assumptions we make in this source are correct. + * The following is a tricky way to validate sizeof() assumptions + * at compile time without generating any runtime code (can't + * use sizeof() in an #ifdef). If the assumption fails, the + * compiler will generate a divide by 0 error. + * + * This here only because it must be inside a subroutine. + */ + + ( (sizeof(char) == 1) ? 1 : (1/0) ); + ( (sizeof(int) == 4) ? 1 : (1/0) ); + +} + + +/*** +*_lock_create() - Create and open a lock +* +*Purpose: +* Create and open a mthread lock. +* +* NOTES: +* +* (1) The caller must have previously determined that the lock +* needs to be created/opened (and this hasn't already been done). +* +* (2) The caller must have aquired the _LOCKTAB_LOCK, if needed. +* (The only time this isn't required is at init time.) +* +*Entry: +* unsigned locknum = lock to create +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +static void _CALLTYPE1 _lock_create ( + unsigned locknum + ) +{ + +#ifdef DEBUG + /* + * See if the lock already exists; if so, die. + */ + + if (_TESTBIT(_lockmap, locknum)) + _FATAL; +#endif + + /* + * Convert the lock number into a lock address + * and create the semaphore. + */ +#ifdef _CRUISER_ + + if ( DOSCREATEMUTEXSEM(NULL, &_locktable[locknum], 0, 0) != 0) + _FATAL; + +#else /* ndef _CRUISER_ */ + +#ifdef _WIN32_ + + /* + * Convert the lock number into a lock address + * and initialize the critical section. + */ + InitializeCriticalSection( &_locktable[locknum] ); + +#else /* ndef _WIN32_ */ + +#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED! + +#endif /* _WIN32_ */ + +#endif /* _CRUISER_ */ + + /* + * Set the appropriate bit in the lock bit map. + */ + + _SETBIT(_lockmap, locknum); + +} + + +/*** +* _lock_stream, etc. - Routines to lock/unlock streams, files, etc. +* +*Purpose: +* _lock_stream = Lock a stdio stream +* _unlock_stream = Unlock a stdio stream +* _lock_file = Lock a lowio file +* _unlock_file = Unlock a lowio file +* +*Entry: +* stream/file identifier +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +void _CALLTYPE2 _lock_stream ( + int stream_id + ) +{ + _lock(stream_id+_STREAM_LOCKS); +} + +void _CALLTYPE2 _unlock_stream ( + int stream_id + ) +{ + _unlock(stream_id+_STREAM_LOCKS); +} + +void _CALLTYPE2 _lock_file ( + int fh + ) +{ + _lock(fh+_FH_LOCKS); +} + +void _CALLTYPE2 _unlock_file ( + int fh + ) +{ + _unlock(fh+_FH_LOCKS); +} + + +/*** +* _lock - Acquire a multi-thread lock +* +*Purpose: +* Note that it is legal for a thread to aquire _EXIT_LOCK1 +* multiple times. +* +*Entry: +* locknum = number of the lock to aquire +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +void _CALLTYPE2 _lock ( + int locknum + ) +{ + +#ifdef DEBUG + struct _debug_lock *deblock; + unsigned tidbit; +#endif + + /* + * Create/open the lock, if necessary + */ + + if (!_TESTBIT(_lockmap, locknum)) { + + _mlock(_LOCKTAB_LOCK); /*** WARNING: Recursive lock call ***/ + + /* if lock still doesn't exist, create it */ + + if (!_TESTBIT(_lockmap, locknum)) + _lock_create(locknum); + + _munlock(_LOCKTAB_LOCK); + + } + +#ifdef DEBUG + /* + * Validate the lock and get pointer to debug lock structure, etc. + */ + + deblock = _lock_validate(locknum); + +#ifdef _CRUISER_ + + tidbit = _BIT_INDEX(*_threadid); + +#else /* ndef _CRUISER_ */ + +#ifdef _WIN32_ + + /* + * Set tidbit to 2**(index of ptd[] entry). + * + * call non-locking form of _getptd to avoid recursing + */ + tidbit = _getptd_lk() - _ptd; /* index of _ptd[] entry */ + + tidbit = _BIT_INDEX(tidbit); + +#else /* ndef _WIN32_ */ + +#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED! + +#endif /* _WIN32_ */ + +#endif /* _CRUISER_ */ + + /* + * Make sure we're not trying to get lock we already have + * (except for _EXIT_LOCK1). + */ + + if (locknum != _EXIT_LOCK1) + if ((deblock->holder) & tidbit) + _FATAL; + + /* + * Set waiter bit for this thread + */ + + deblock->waiters |= tidbit; + +#endif /* DEBUG */ + + /* + * Get the lock + */ + +#ifdef _LOCKCOUNT + _lockcnt++; +#endif + +#ifdef _CRUISER_ + + if (DOSREQUESTMUTEXSEM(_locktable[locknum],-1) != 0) + _FATAL; + +#else /* ndef _CRUISER_ */ + +#ifdef _WIN32_ + + /* + * Enter the critical section. + */ + EnterCriticalSection( &_locktable[locknum] ); + +#else /* ndef _WIN32_ */ + +#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED! + +#endif /* _WIN32_ */ + +#endif /* _CRUISER_ */ + +#ifdef DEBUG + /* + * Clear waiter bit + */ + + deblock->waiters &= (~tidbit); + + /* + * Make sure there are no lock holders (unless this is + * _EXIT_LOCK1); then set holder bit and bump lock count. + */ + + assert(THREADINTS==1); + + if (locknum != _EXIT_LOCK1) + if ( (unsigned) deblock->holder != 0) + _FATAL; + + deblock->holder &= tidbit; + deblock->lockcnt++; + +#endif + +} + + +/*** +* _unlock - Release multi-thread lock +* +*Purpose: +* Note that it is legal for a thread to aquire _EXIT_LOCK1 +* multiple times. +* +*Entry: +* locknum = number of the lock to release +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +void _CALLTYPE2 _unlock ( + int locknum + ) +{ +#ifdef DEBUG + struct _debug_lock *deblock; + unsigned tidbit; +#endif + +#ifdef DEBUG + /* + * Validate the lock and get pointer to debug lock structure, etc. + */ + + deblock = _lock_validate(locknum); + +#ifdef _CRUISER_ + + tidbit = _BIT_INDEX(*_threadid); + +#else /* ndef _CRUISER_ */ + +#ifdef _WIN32_ + + /* + * Set tidbit to 2**(index of ptd[] entry). + */ + tidbit = _getptd_lk() - _ptd; /* index of _ptd[] entry */ + + tidbit = _BIT_INDEX(tidbit); + +#else /* ndef _WIN32_ */ + +#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED! + +#endif /* _WIN32_ */ + +#endif /* _CRUISER_ */ + + /* + * Make sure we hold this lock then clear holder bit. + * [Note: Since it is legal to aquire _EXIT_LOCK1 several times, + * it's possible the holder bit is already clear.] + */ + + if (locknum != _EXIT_LOCK1) + if (!((deblock->holder) & tidbit)) + _FATAL; + + deblock->holder &= (~tidbit); + + /* + * See if anyone else is waiting for this lock. + */ + + assert(THREADINTS==1); + + if ((unsigned) deblock->waiters != 0) + deblock->collcnt++; + +#endif + +#ifdef _CRUISER_ + + /* + * Release the lock + */ + if (DOSRELEASEMUTEXSEM(_locktable[locknum]) != 0) + _FATAL; + +#else /* ndef _CRUISER_ */ + +#ifdef _WIN32_ + + /* + * leave the critical section. + */ + LeaveCriticalSection( &_locktable[locknum] ); + +#else /* ndef _WIN32_ */ + +#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED! + +#endif /* _WIN32_ */ + +#endif /* _CRUISER_ */ + +#ifdef _LOCKCOUNT + _lockcnt--; +#endif + +} + + +/* + * Debugging code + */ + +#ifdef DEBUG + +/*** +*_lock_validate() - Validate a lock +* +*Purpose: +* Debug lock validations common to both lock and unlock. +* +*Entry: +* lock number +* +*Exit: +* ptr to lock's debug structure +* +*Exceptions: +* +*******************************************************************************/ + +static struct _debug_lock * _CALLTYPE1 _lock_validate ( + int locknum + ) +{ + /* + * Make sure lock is legal + */ + + if (locknum > _TOTAL_LOCKS) + _FATAL; + +#ifdef _CRUISER_ + + /* + * Make sure threadid is in range + */ + + if (*_threadid > MAXTHREADID) + _FATAL; + +#endif /* _CRUISER_ */ + + /* + * Return pointer to this lock's debug structure + */ + + return(&_debug_locktable[locknum]); + +} + + +/*** +*_fh_locknum() - Return the lock number for a file handle +* +*Purpose: +* +*Entry: +* int fh = file handle +* +*Exit: +* int locknum = corresponding lock number +* +*Exceptions: +* +*******************************************************************************/ + +int _CALLTYPE2 _fh_locknum ( + int fh + ) +{ + return(fh+_FH_LOCKS); +} + + +/*** +*_stream_locknum() - Return the lock number for a stream +* +*Purpose: +* +*Entry: +* int stream = stream number (i.e., offset of the stream +* in the _iob table) +* +*Exit: +* int locknum = corresponding lock number +* +*Exceptions: +* +*******************************************************************************/ + +int _CALLTYPE2 _stream_locknum ( + int stream + ) +{ + return(stream+_STREAM_LOCKS); +} + + +#ifdef _CRUISER_ + +/*** +*_check_lock() - Make sure a lock is in good shape +* +*Purpose: +* This routine checks to make sure that a lock is in a 'good' +* released state. That is, abort if any of the following +* are true: +* (1) lock is held +* (2) holder bits are set +* (3) waiter bits are set +* +* NOTE: This routine does NOT aquire the lock but simply looks +* at the lock data bases. +* +*Entry: +* locknum = number of the lock to aquire +* +*Exit: +* success = 0 +* failure = !0 +* +*Exceptions: +* +*******************************************************************************/ + +int _CALLTYPE2 _check_lock ( + int locknum + ) +{ + unsigned pid; + unsigned long tid; + unsigned cnt; + struct _debug_lock *deblock; + + /* + * See if lock exists. + */ + + if (_TESTBIT(_lockmap, locknum)) { + + /* + * Lock exists. Ask the OS about the lock and + * make sure lock isn't held. + */ + + if (DOSQUERYMUTEXSEM(_locktable[locknum], &pid, &tid, &cnt) != 0) + goto error_return; + + if (cnt != 0) + goto error_return; + } + + else { + + /* + * Lock doesn't exist, handle better be 0. + */ + + if (_locktable[locknum] != 0) + goto error_return; + } + + + /* + * Make sure there are no lock waiters or holders. + */ + + assert(THREADINTS==1); + + if ((deblock->waiters != 0) || (deblock->holder != 0)) + goto error_return; + + /* + * Good Return + */ + + return(0); + + /* + * Error return + */ + +error_return: + return(-1); + +} + +#endif /* _CRUISER_ */ + + +/*** +*_collide_cnt() - Return the collision count for a lock +* +*Purpose: +* +*Entry: +* int lock = lock number +* +*Exit: +* int count = collision count +* +*Exceptions: +* +*******************************************************************************/ + +int _CALLTYPE2 _collide_cnt ( + int locknum + ) +{ + return(_debug_locktable[locknum].collcnt); +} + + +/*** +*_lock_cnt() - Return the lock count for a lock +* +*Purpose: +* +*Entry: +* int lock = lock number +* +*Exit: +* int count = lock count +* +*Exceptions: +* +*******************************************************************************/ + +int _CALLTYPE2 _lock_cnt ( + int locknum + ) +{ + return(_debug_locktable[locknum].lockcnt); +} + + +/*** +*_lock_exist() - Check to see if a lock exists +* +*Purpose: +* Test lock bit map to see if the lock has +* been created or not. +* +*Entry: +* int lock = lock number +* +*Exit: +* int 0 = lock has NOT been created +* 1 = lock HAS been created +* +*Exceptions: +* +*******************************************************************************/ + +int _CALLTYPE2 _lock_exist ( + int locknum + ) +{ + if (_TESTBIT(_lockmap, locknum)) + return(1); + else + return(0); +} + +#endif +#endif diff --git a/private/crt32/iostream/ofstream.cxx b/private/crt32/iostream/ofstream.cxx new file mode 100644 index 000000000..cf915082b --- /dev/null +++ b/private/crt32/iostream/ofstream.cxx @@ -0,0 +1,239 @@ +/*** +*ofstream.cxx - +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains the member functions for the ofstream class. +* +*Revision History: +* 09-21-91 KRS Created. Split up fstream.cxx for granularity. +* 10-22-91 KRS C700 #4883: fix error status for ofstream::open(). +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <string.h> +#include <stdio.h> +#include <fcntl.h> +#include <sys\types.h> +#include <io.h> +#include <fstream.h> +#pragma hdrstop + +#include <sys\stat.h> + +/*** +*ofstream::ofstream() - ofstream default constructor +* +*Purpose: +* Default constructor for ofstream objects. +* +*Entry: +* None. +* +*Exit: +* None. +* +*Exceptions: +* +*******************************************************************************/ + ofstream::ofstream() +: ostream(new filebuf) +{ + delbuf(1); + // CONSIDER: do anything else? +} + +/*** +*ofstream::ofstream(const char * name, int mode, int prot) - ofstream ctor +* +*Purpose: +* Constructor for ofstream objects. Creates an associated filebuf object, +* opens a named file and attaches it to the new filebuf. +* +*Entry: +* name = filename to open. +* mode = see filebuf::open mode argument. ios::out is implied. +* prot = see filebuf::open share argument +* +*Exit: +* None. +* +*Exceptions: +* Sets failbit if open fails. +* +*******************************************************************************/ + ofstream::ofstream(const char * name, int mode, int prot) +: ostream(new filebuf) +{ + delbuf(1); + if (!(rdbuf()->open(name, (mode|ios::out), prot))) + state |= ios::failbit; +} + +/*** +*ofstream::ofstream(filedesc fd) - ofstream constructor +* +*Purpose: +* Constructor for ofstream objects. Creates an associated filebuf object +* and attaches it to the given file descriptor. +* +*Entry: +* fd = file descriptor of file previously opened using _open or _sopen. +* +*Exit: +* None. +* +*Exceptions: +* +*******************************************************************************/ + ofstream::ofstream(filedesc _fd) +: ostream(new filebuf(_fd)) +{ + delbuf(1); +} + +/*** +*ofstream::ofstream(filedesc fd, char * sbuf, int len) - ofstream constructor +* +*Purpose: +* Constructor for ofstream objects. Creates an associated filebuf object +* and attaches it to the given file descriptor. Filebuf object uses +* user-supplied buffer or is unbuffered if sbuf or len = 0. +* +*Entry: +* fd = file descriptor of file previously opened using _open or _sopen. +* sbuf = pointer to character buffer or NULL if request for unbuffered. +* len = lenght of character buffer sbuf or 0 if request for unbuffered. +* +*Exit: +* None. +* +*Exceptions: +* +*******************************************************************************/ + ofstream::ofstream(filedesc _fd, char * sbuf, int len) +: ostream(new filebuf(_fd, sbuf, len)) +{ + delbuf(1); +} + +/*** +*ofstream::~ofstream() - ofstream destructor +* +*Purpose: +* ofstream destructor. +* +*Entry: +* None. +* +*Exit: +* None. +* +*Exceptions: +* +*******************************************************************************/ + ofstream::~ofstream() +{ + // CONSIDER: do anything else? +} + +/*** +*streambuf* ofstream::setbuf(char * ptr, int len) - setbuf function +* +*Purpose: +* ofstream setbuf function +* +*Entry: +* ptr = pointer to buffer or NULL for unbuffered. +* len = length of buffer or zero for unbuffered. +* +*Exit: +* Returns rdbuf() or NULL if error. +* +*Exceptions: +* If ofstream is already open or if rdbuf()->setbuf fails, sets failbit +* and returns NULL. +* +*******************************************************************************/ +streambuf * ofstream::setbuf(char * ptr, int len) +{ + if ((is_open()) || (!(rdbuf()->setbuf(ptr, len)))) + { + clear(state | ios::failbit); + return NULL; + } + // UNDONE: turn off input buffer?? + return rdbuf(); +} + +/*** +*void ofstream::attach(filedesc _fd) - attach member function +* +*Purpose: +* ofstream attach member function. Just calls rdbuf()->attach(). +* +*Entry: +* _fd = file descriptor of previously opened file. +* +*Exit: +* None. +* +*Exceptions: +* Sets failbit if rdbuf()->attach fails. +* +*******************************************************************************/ +void ofstream::attach(filedesc _fd) // CONSIDER: inline? +{ + if (!(rdbuf()->attach(_fd))) + clear(state | ios::failbit); +} + +/*** +*void ofstream::open(const char * name, int mode, int prot) - ofstream open() +* +*Purpose: +* Opens a named file and attaches it to the associated filebuf. +* Just calls rdbuf()->open(). +* +*Entry: +* name = filename to open. +* mode = see filebuf::open mode argument. ios::out is implied. +* prot = see filebuf::open share argument +* +*Exit: +* None. +* +*Exceptions: +* Sets failbit if already open or rdbuf()->open() fails. +* +*******************************************************************************/ +void ofstream::open(const char * name, int mode, int prot) +{ + if (is_open() || !(rdbuf()->open(name, (mode|ios::out), prot))) + clear(state | ios::failbit); +} + +/*** +*void ofstream::close() - close member function +* +*Purpose: +* ofstream close member function. Just calls rdbuf()->close(). +* Clears rdstate() error bits if successful. +* +*Entry: +* None. +* +*Exit: +* None. +* +*Exceptions: +* Sets failbit if rdbuf()->close fails. +* +*******************************************************************************/ +void ofstream::close() +{ + clear((rdbuf()->close()) ? 0 : (state | ios::failbit)); +} diff --git a/private/crt32/iostream/ostrchar.cxx b/private/crt32/iostream/ostrchar.cxx new file mode 100644 index 000000000..3e8bb07a2 --- /dev/null +++ b/private/crt32/iostream/ostrchar.cxx @@ -0,0 +1,39 @@ +/*** +* ostrchar.cxx - definitions for ostream class operator<<(char) functions. +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains the member function definitions for ostream operator<<(char). +* +*Revision History: +* 09-23-91 KRS Created. Split out from ostream.cxx for granularity. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <iostream.h> +#pragma hdrstop + +// note: called inline by char and signed char versions: +ostream& ostream::operator<<(unsigned char c) +{ + if (opfx()) + { + if (x_width) + { + _WINSTATIC char outc[2]; + outc[0] = c; + outc[1] = '\0'; + writepad("",outc); + } + else if (bp->sputc(c)==EOF) + { + if (bp->overflow(c)==EOF) + state |= (badbit|failbit); // fatal error? + } + osfx(); + } + return *this; +} diff --git a/private/crt32/iostream/ostrdbl.cxx b/private/crt32/iostream/ostrdbl.cxx new file mode 100644 index 000000000..1d8f22060 --- /dev/null +++ b/private/crt32/iostream/ostrdbl.cxx @@ -0,0 +1,65 @@ +/*** +* ostrdbl.cxx - definitions for ostream class operator<<(double) functions +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains the member function definitions for ostream operator<<(double). +* +*Revision History: +* 09-23-91 KRS Created. Split out from ostream.cxx for granularity. +* 10-24-91 KRS Combine float version with double. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <stdio.h> +#include <float.h> +#include <iostream.h> +#pragma hdrstop + +#pragma check_stack(on) // large buffer(s) + +ostream& ostream::operator<<(double f) +{ +_WINSTATIC char obuffer[24]; +_WINSTATIC char fmt[8]; +_WINSTATIC char leader[4]; + char * optr = obuffer; + int x = 0; + + // x_floatused nonzero indicates called for float, not double + unsigned int curprecision = (x_floatused) ? FLT_DIG : DBL_DIG; + x_floatused = 0; // reset for next call + + curprecision = __min((unsigned)x_precision,curprecision); + + if (opfx()) { + if (x_flags & ios::showpos) + leader[x++] = '+'; + if (x_flags & ios::showpoint) + leader[x++] = '#'; // show decimal and trailing zeros + leader[x] = '\0'; + x = sprintf(fmt,"%%%s.%.0ug",leader,curprecision) - 1; + if ((x_flags & ios::floatfield)==ios::fixed) + fmt[x] = 'f'; + else + { + if ((x_flags & ios::floatfield)==ios::scientific) + fmt[x] = 'e'; + if (x_flags & uppercase) + fmt[x] = (char)toupper(fmt[x]); + } + + sprintf(optr,fmt,f); + x = 0; + if (*optr=='+' || *optr=='-') + leader[x++] = *(optr++); + leader[x] = '\0'; + writepad(leader,optr); + osfx(); + } + return *this; +} diff --git a/private/crt32/iostream/ostream.cxx b/private/crt32/iostream/ostream.cxx new file mode 100644 index 000000000..69eaefabe --- /dev/null +++ b/private/crt32/iostream/ostream.cxx @@ -0,0 +1,229 @@ +/*** +* ostream.cxx - definitions for ostream and ostream_withassign classes +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains the core member function definitions for ostream and +* ostream_withassign classes. +* +*Revision History: +* 07-01-91 KRS Created. +* 08-19-91 KRS Corrected my interpretation of the spec. for negative +* hex or octal integers. +* 08-20-91 KRS Replace 'clear(x)' with 'state |= x'. +* Skip text translation for write(). +* 08-26-91 KRS Modify to work with DLL's/MTHREAD. +* 09-05-91 KRS Fix opfx() to flush tied ostream, not current one. +* 09-09-91 KRS Remove sync_with_stdio() call from Iostream_init(). +* Reinstate text-translation (by default) for write(). +* 09-19-91 KRS Add opfx()/osfx() calls to put() and write(). +* Schedule destruction of predefined streams. +* 09-23-91 KRS Split up for granularity. +* 10-04-91 KRS Use bp->sputc, not put(), in writepad(). +* 10-24-91 KRS Added initialization of x_floatused. +* 11-04-91 KRS Make constructors work with virtual base. +* 11-20-91 KRS Add/fix copy constructor and assignment operators. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <iostream.h> +#pragma hdrstop + +int ostream::opfx() +{ + lock(); + if (state) + { + state |= ios::failbit; // CONSIDER??? + unlock(); + return 0; + } + if (x_tie) + { + x_tie->flush(); + } + lockbuf(); + return(1); // return non-zero +} + +void ostream::osfx() +{ + x_width = 0; + if (x_flags & unitbuf) + { + if (bp->sync()==EOF) + state = failbit | badbit; + } +#ifndef _WINDLL + if (x_flags & ios::stdio) + { + if (fflush(stdout)==EOF) + state |= failbit; + if (fflush(stderr)==EOF) + state |= failbit; + } +#endif + unlockbuf(); + unlock(); +} + +// note: called inline by char and signed char versions: +#if 0 +ostream& ostream::operator<<(unsigned char c) +{ + if (opfx()) + { + if (x_width) + { + _WINSTATIC char outc[2]; + outc[0] = c; + outc[1] = '\0'; + writepad("",outc); + } + else if (bp->sputc(c)==EOF) + { + if (bp->overflow(c)==EOF) + state |= (badbit|failbit); // fatal error? + } + osfx(); + } + return *this; +} +#endif + +// note: called inline by unsigned char * and signed char * versions: +ostream& ostream::operator<<(const char * s) +{ + if (opfx()) { + writepad("",s); + osfx(); + } + return *this; +} + +ostream& ostream::flush() +{ + lock(); + lockbuf(); + if (bp->sync()==EOF) + state |= ios::failbit; + unlockbuf(); + unlock(); + return(*this); +} + + ostream::ostream() +// : ios() +{ + x_floatused = 0; + // CONSIDER: do anything else? +} + + ostream::ostream(streambuf* _inistbf) +// : ios() +{ + init(_inistbf); + + x_floatused = 0; + // CONSIDER: do anything else? +} + + ostream::ostream(const ostream& _ostrm) +// : ios() +{ + init(_ostrm.rdbuf()); + + x_floatused = 0; + // CONSIDER: do anything else? +} + + ostream::~ostream() +// : ~ios() +{ + // CONSIDER: do anything else? +} + +// used in ios::sync_with_stdio() +ostream& ostream::operator=(streambuf * _sbuf) +{ +// consider: may be some redundency here, depending on spec. + + if (delbuf() && rdbuf()) + delete rdbuf(); + bp = 0; + + this->ios::operator=(ios()); // initialize ios members + delbuf(0); // important! + init(_sbuf); + +// x_floatused = 0; // not necessary + + return *this; +} + + + ostream_withassign::ostream_withassign() +: ostream() +{ + // CONSIDER: do anything else? +} + + ostream_withassign::ostream_withassign(streambuf* _os) +: ostream(_os) +{ + // CONSIDER: do anything else? +} + + ostream_withassign::~ostream_withassign() +// : ~ostream() +{ +; // CONSIDER: do anything else? +} + +ostream& ostream::writepad(const char * leader, const char * value) + + { +unsigned int len, leadlen; +long padlen; + leadlen = strlen(leader); + len = strlen(value); + padlen = (((unsigned)x_width) > (len+leadlen)) ? ((unsigned)x_width) - (len + leadlen) : 0; + if (!(x_flags & (left|internal))) // default is right-adjustment + { + while (padlen-- >0) + { + if (bp->sputc((unsigned char)x_fill)==EOF) + state |= (ios::failbit|ios::badbit); + } + } + if (leadlen) + { + if ((unsigned)bp->sputn(leader,leadlen)!=leadlen) + state |= (failbit|badbit); // CONSIDER: right error? + } + if (x_flags & internal) + { + while (padlen-- >0) + { + if (bp->sputc((unsigned char)x_fill)==EOF) + state |= (ios::failbit|ios::badbit); + } + } + if ((unsigned)bp->sputn(value,len)!=len) + state |= (failbit|badbit); // CONSIDER: right error? + if (x_flags & left) + { + while ((padlen--)>0) // left-adjust if necessary + { + if (bp->sputc((unsigned char)x_fill)==EOF) + state |= (ios::failbit|ios::badbit); + } + } + return (*this); + } diff --git a/private/crt32/iostream/ostream1.cxx b/private/crt32/iostream/ostream1.cxx new file mode 100644 index 000000000..5f486537e --- /dev/null +++ b/private/crt32/iostream/ostream1.cxx @@ -0,0 +1,70 @@ +/*** +* ostream1.cxx - definitions for ostream class non-core member functions +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains the non-core member function definitions for ostream class. +* +*Revision History: +* 07-01-91 KRS Created. Split out from ostream.cxx for granularity. +* 11-20-91 KRS Make operator= inline. +* 03-30-92 KRS Add multithread locks. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <iostream.h> +#pragma hdrstop + +ostream& ostream::seekp(streampos _strmp) +{ + lockbuf(); + + if (bp->seekpos(_strmp, ios::out)==EOF) + clear(state | failbit); + + unlockbuf(); + return(*this); +} + +ostream& ostream::seekp(streamoff _strmf, seek_dir _sd) +{ + lockbuf(); + + if (bp->seekoff(_strmf, _sd, ios::out)==EOF) + clear(state | failbit); + + unlockbuf(); + return(*this); +} + +streampos ostream::tellp() +{ + streampos retval; + lockbuf(); + + if ((retval=bp->seekoff(streamoff(0), ios::cur, ios::out))==EOF) + clear(state | failbit); + + unlockbuf(); + return(retval); +} + +ostream& ostream::operator<<(streambuf * instm) +{ + int c; + if (opfx()) + { + while ((c=instm->sbumpc())!=EOF) + if (bp->sputc(c) == EOF) + { + state |= failbit; // CONSIDER: is this redundent? + break; + } +// CONSIDER: not very efficient in many cases + osfx(); + } + return *this; +} diff --git a/private/crt32/iostream/ostrint.cxx b/private/crt32/iostream/ostrint.cxx new file mode 100644 index 000000000..09cb6490e --- /dev/null +++ b/private/crt32/iostream/ostrint.cxx @@ -0,0 +1,55 @@ +/*** +* ostrint.cxx - definitions for ostream class operator<<(int) member functions +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains the member function definitions for ostream operator<<(int). +* +*Revision History: +* 09-23-91 KRS Created. Split out from ostream.cxx for granularity. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdio.h> +#include <iostream.h> +#pragma hdrstop + +ostream& ostream::operator<<(int n) +{ +_WINSTATIC char obuffer[12]; +_WINSTATIC char fmt[4] = "%d"; +_WINSTATIC char leader[4] = "\0\0"; + if (opfx()) { + + if (n) + { + if (x_flags & (hex|oct)) + { + if (x_flags & hex) + { + if (x_flags & uppercase) + fmt[1] = 'X'; + else + fmt[1] = 'x'; + leader[1] = fmt[1]; // 0x or 0X (or \0X) + } + else + fmt[1] = 'o'; + if (x_flags & showbase) + leader[0] = '0'; + } + else if ((n>0) && (x_flags & showpos)) + { + leader[0] = '+'; + } + } + sprintf(obuffer,fmt,n); + writepad(leader,obuffer); + osfx(); + } + return *this; + +} diff --git a/private/crt32/iostream/ostrldbl.cxx b/private/crt32/iostream/ostrldbl.cxx new file mode 100644 index 000000000..0bb3fbfc7 --- /dev/null +++ b/private/crt32/iostream/ostrldbl.cxx @@ -0,0 +1,61 @@ +/*** +* ostrldbl.cxx - definitions for ostream class operator<<(long double) functs +* +* Copyright (c) 1991-1993, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains the member function definitions for ostream +* operator<<(long double). +* +*Revision History: +* 09-23-91 KRS Created. Split out from ostream.cxx for granularity. +* 10-24-91 KRS Minor robustness work. +* 05-10-93 CFW Re-enable function. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <stdio.h> +#include <float.h> +#include <iostream.h> +#pragma hdrstop + +#pragma check_stack(on) // large buffer(s) + +ostream& ostream::operator<<(long double f) +{ +_WINSTATIC char obuffer[28]; +_WINSTATIC char fmt[12]; +_WINSTATIC char leader[4]; + char * optr = obuffer; + int x = 0; + unsigned int curprecision = __min((unsigned)x_precision,LDBL_DIG); + if (opfx()) { + if (x_flags & ios::showpos) + leader[x++] = '+'; + if (x_flags & ios::showpoint) + leader[x++] = '#'; // show decimal and trailing zeros + leader[x] = '\0'; + x = sprintf(fmt,"%%%s.%.0uLg",leader,curprecision) - 1; + if ((x_flags & ios::floatfield)==ios::fixed) + fmt[x] = 'f'; + else + { + if ((x_flags & ios::floatfield)==ios::scientific) + fmt[x] = 'e'; + if (x_flags & uppercase) + fmt[x] = (char)toupper(fmt[x]); + } + + sprintf(optr,fmt,f); + x = 0; + if (*optr=='+' || *optr=='-') + leader[x++] = *(optr++); + leader[x] = '\0'; + writepad(leader,optr); + osfx(); + } + return *this; +} diff --git a/private/crt32/iostream/ostrlong.cxx b/private/crt32/iostream/ostrlong.cxx new file mode 100644 index 000000000..959c5fb94 --- /dev/null +++ b/private/crt32/iostream/ostrlong.cxx @@ -0,0 +1,55 @@ +/*** +* ostrlong.cxx - definitions for ostream class operator<<(long) member functions +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains the member function definitions for ostream operator<<(long). +* +*Revision History: +* 09-23-91 KRS Created. Split out from ostream.cxx for granularity. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdio.h> +#include <iostream.h> +#pragma hdrstop + +ostream& ostream::operator<<(long n) +{ +_WINSTATIC char obuffer[12]; +_WINSTATIC char fmt[4] = "%ld"; +_WINSTATIC char leader[4] = "\0\0"; + if (opfx()) { + + if (n) + { + if (x_flags & (hex|oct)) + { + if (x_flags & hex) + { + if (x_flags & uppercase) + fmt[2] = 'X'; + else + fmt[2] = 'x'; + leader[1] = fmt[2]; // 0x or 0X (or \0X) + } + else + fmt[2] = 'o'; + if (x_flags & showbase) + leader[0] = '0'; + } + else if ((n>0) && (x_flags & showpos)) + { + leader[0] = '+'; + } + } + sprintf(obuffer,fmt,n); + writepad(leader,obuffer); + osfx(); + } + return *this; + +} diff --git a/private/crt32/iostream/ostrptr.cxx b/private/crt32/iostream/ostrptr.cxx new file mode 100644 index 000000000..adbb51607 --- /dev/null +++ b/private/crt32/iostream/ostrptr.cxx @@ -0,0 +1,41 @@ +/*** +* ostrptr.cxx - definitions for ostream operator<<(const void*) member function +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Member function definition for ostream operator<<(const void*). +* +*Revision History: +* 09-23-91 KRS Created. Split out from ostream.cxx for granularity. +* 06-03-92 KRS CAV #1183: add 'const' to ptr output argument. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdio.h> +#include <iostream.h> +#pragma hdrstop + +ostream& ostream::operator<<(const void * ptr) +{ +_WINSTATIC char obuffer[12]; +_WINSTATIC char fmt[4] = "%p"; +_WINSTATIC char leader[4] = "0x"; + if (opfx()) + { + if (ptr) + { + if (x_flags & uppercase) + leader[1] = 'X'; +// else // initialized above by default +// leader[1] = 'x'; + } +//CONSIDER: can we use and depend on out %p conversion to do this??? + sprintf(obuffer,fmt,ptr); + writepad(leader,obuffer); + osfx(); + } + return *this; +} diff --git a/private/crt32/iostream/ostrput.cxx b/private/crt32/iostream/ostrput.cxx new file mode 100644 index 000000000..d1a51a5f8 --- /dev/null +++ b/private/crt32/iostream/ostrput.cxx @@ -0,0 +1,42 @@ +/*** +* ostrput.cxx - definitions for ostream classes put() and write() functions +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains the member function definitions for ostream put() and write(). +* +*Revision History: +* 09-23-91 KRS Created. Split out from ostream.cxx for granularity. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <iostream.h> +#pragma hdrstop + +// note: called inline by char and signed char versions: +ostream& ostream::put(unsigned char c) +{ + if (opfx()) // CONSIDER: needed?? + { + if (bp->sputc((int)c)==EOF) + state |= (failbit|badbit); // CONSIDER: right error? + osfx(); + } + return(*this); +} + +// note: called inline by unsigned char * and signed char * versions: +ostream& ostream::write(const char * s, int n) +{ + if (opfx()) // CONSIDER: needed?? + { +// Note: 'n' treated as unsigned + if (bp->sputn(s,n)!=n) + state |= (failbit|badbit); // CONSIDER: right error? + osfx(); + } + return(*this); +} diff --git a/private/crt32/iostream/ostrshrt.cxx b/private/crt32/iostream/ostrshrt.cxx new file mode 100644 index 000000000..e4818c15d --- /dev/null +++ b/private/crt32/iostream/ostrshrt.cxx @@ -0,0 +1,56 @@ +/*** +* ostrshrt.cxx - definitions for ostream class operator<<(short) member functions +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains the member function definitions for ostream operator<<(short). +* +*Revision History: +* 09-23-91 KRS Created. Split out from ostream.cxx for granularity. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <stdio.h> +#include <iostream.h> +#pragma hdrstop + +ostream& ostream::operator<<(short n) +{ +_WINSTATIC char obuffer[8]; // CONSIDER: assumes max int is 65535 +_WINSTATIC char fmt[4] = "%hd"; +_WINSTATIC char leader[4] = "\0\0"; + if (opfx()) { + + if (n) + { + if (x_flags & (hex|oct)) + { + if (x_flags & hex) + { + if (x_flags & uppercase) + fmt[2] = 'X'; + else + fmt[2] = 'x'; + leader[1] = fmt[2]; // 0x or 0X (or \0X) + } + else + fmt[2] = 'o'; + if (x_flags & showbase) + leader[0] = '0'; + } + else if ((n>0) && (x_flags & showpos)) + { + leader[0] = '+'; + } + } + sprintf(obuffer,fmt,n); + writepad(leader,obuffer); + osfx(); + } + return *this; + +} diff --git a/private/crt32/iostream/ostruint.cxx b/private/crt32/iostream/ostruint.cxx new file mode 100644 index 000000000..f528fcc63 --- /dev/null +++ b/private/crt32/iostream/ostruint.cxx @@ -0,0 +1,55 @@ +/*** +* ostruint.cxx - definitions for ostream class operator<<(unsigned int) funct +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains the member function definitions for ostream +* operator<<(unsigned int). +* +*Revision History: +* 09-23-91 KRS Created. Split out from ostream.cxx for granularity. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdio.h> +#include <iostream.h> +#pragma hdrstop + +ostream& ostream::operator<<(unsigned int n) +{ +_WINSTATIC char obuffer[12]; +_WINSTATIC char fmt[4] = "%u"; +_WINSTATIC char leader[4] = "\0\0"; + if (opfx()) { + if (n) + { + if (x_flags & (hex|oct)) + { + if (x_flags & hex) + { + if (x_flags & uppercase) + fmt[1] = 'X'; + else + fmt[1] = 'x'; + leader[1] = fmt[1]; // 0x or 0X (or \0X) + } + else + fmt[1] = 'o'; + if (x_flags & showbase) + leader[0] = '0'; + } + else if (x_flags & showpos) + { + leader[0] = '+'; + } + } + sprintf(obuffer,fmt,n); + writepad(leader,obuffer); + osfx(); + } + return *this; + +} diff --git a/private/crt32/iostream/ostrulng.cxx b/private/crt32/iostream/ostrulng.cxx new file mode 100644 index 000000000..d09a5c9de --- /dev/null +++ b/private/crt32/iostream/ostrulng.cxx @@ -0,0 +1,55 @@ +/*** +* ostrulng.cxx - definitions for ostream class operator<<(unsigned long) funct +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains the member function definition for ostream +* operator<<(unsigned long). +* +*Revision History: +* 09-23-91 KRS Created. Split out from ostream.cxx for granularity. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdio.h> +#include <iostream.h> +#pragma hdrstop + +ostream& ostream::operator<<(unsigned long n) +{ +_WINSTATIC char obuffer[12]; +_WINSTATIC char fmt[4] = "%lu"; +_WINSTATIC char leader[4] = "\0\0"; + if (opfx()) { + if (n) + { + if (x_flags & (hex|oct)) + { + if (x_flags & hex) + { + if (x_flags & uppercase) + fmt[2] = 'X'; + else + fmt[2] = 'x'; + leader[1] = fmt[2]; // 0x or 0X (or \0X) + } + else + fmt[2] = 'o'; + if (x_flags & showbase) + leader[0] = '0'; + } + else if (x_flags & showpos) + { + leader[0] = '+'; + } + } + sprintf(obuffer,fmt,n); + writepad(leader,obuffer); + osfx(); + } + return *this; + +} diff --git a/private/crt32/iostream/ostrusht.cxx b/private/crt32/iostream/ostrusht.cxx new file mode 100644 index 000000000..796ff3caf --- /dev/null +++ b/private/crt32/iostream/ostrusht.cxx @@ -0,0 +1,56 @@ +/*** +* ostrusht.cxx - definition for ostream class operator<<(unsigned short) funct +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Contains the member function definition for ostream +* operator<<(unsigned short). +* +*Revision History: +* 09-23-91 KRS Created. Split out from ostream.cxx for granularity. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <stdio.h> +#include <iostream.h> +#pragma hdrstop + +ostream& ostream::operator<<(unsigned short n) +{ +_WINSTATIC char obuffer[8]; +_WINSTATIC char fmt[4] = "%hu"; +_WINSTATIC char leader[4] = "\0\0"; + if (opfx()) { + if (n) + { + if (x_flags & (hex|oct)) + { + if (x_flags & hex) + { + if (x_flags & uppercase) + fmt[2] = 'X'; + else + fmt[2] = 'x'; + leader[1] = fmt[2]; // 0x or 0X (or \0X) + } + else + fmt[2] = 'o'; + if (x_flags & showbase) + leader[0] = '0'; + } + else if (x_flags & showpos) + { + leader[0] = '+'; + } + } + sprintf(obuffer,fmt,n); + writepad(leader,obuffer); + osfx(); + } + return *this; + +} diff --git a/private/crt32/iostream/sources b/private/crt32/iostream/sources new file mode 100644 index 000000000..7c58ffce1 --- /dev/null +++ b/private/crt32/iostream/sources @@ -0,0 +1,83 @@ +!IF 0 + +Copyright (c) 1992, Microsoft Corporation. All rights reserved. + +Module Name: + + sources. + +Abstract: + + This file specifies the target component being built and the list of + sources files needed to build that component. Also specifies optional + compiler switches and libraries that are unique for the component being + built. + + Set MAKEFILE. for BLDCRT=1 definition that forces use of MS C8 C++ compiler + for all platforms to compile these files. + +Author: + + karlsi 15-Jun-1992, Created. + +NOTE: Commented description of this file is in \nt\bak\bin\sources.tpl + +!ENDIF + +MAJORCOMP=crt +MINORCOMP=iostream + +TARGETNAME=iostream +TARGETPATH=..\obj +TARGETTYPE=LIBRARY +386_STDCALL=0 +_AXPACC_=0 + +!INCLUDE ..\crt32.def + +SOURCES=iostream.cxx \ + iostrini.cxx \ + cerrinit.cxx \ + cininit.cxx \ + cloginit.cxx \ + filebuf.cxx \ + filebuf1.cxx \ + fstream.cxx \ + ifstream.cxx \ + ios.cxx \ + istrchar.cxx \ + istrdbl.cxx \ + istream.cxx \ + istream1.cxx \ + istrflt.cxx \ + istrgdbl.cxx \ + istrget.cxx \ + istrgetl.cxx \ + istrgint.cxx \ + istrint.cxx \ + istrldbl.cxx \ + istrlong.cxx \ + istrshrt.cxx \ + istruint.cxx \ + istrulng.cxx \ + istrusht.cxx \ + mtlock.c \ + ofstream.cxx \ + ostrchar.cxx \ + ostrdbl.cxx \ + ostream.cxx \ + ostream1.cxx \ + ostrint.cxx \ + ostrldbl.cxx \ + ostrlong.cxx \ + ostrptr.cxx \ + ostrput.cxx \ + ostrshrt.cxx \ + ostruint.cxx \ + ostrulng.cxx \ + ostrusht.cxx \ + stdiostr.cxx \ + streamb.cxx \ + streamb1.cxx \ + strmbdbp.cxx \ + strstrea.cxx diff --git a/private/crt32/iostream/stdiostr.cxx b/private/crt32/iostream/stdiostr.cxx new file mode 100644 index 000000000..58c385a78 --- /dev/null +++ b/private/crt32/iostream/stdiostr.cxx @@ -0,0 +1,266 @@ +/*** +*stdiostr.cxx - +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* +*Revision History: +* +* 07-10-91 KRS Created. +* 08-26-91 KRS Switch out cout/cerr. etc. for Windows non-QuickWin. +* 09-09-91 KRS Modify sync_with_stdio() for filebuf defaults. +* 09-12-91 KRS Add stdiostream class. +* 09-19-91 KRS Use delbuf(1) in stdiostream constructor. +* 09-20-91 KRS C700 #4453: Improve efficiency in overflow(). +* 10-21-91 KRS Eliminate last use of default iostream constructor. +* 10-24-91 KRS Avoid virtual calls from virtual functions. +* 11-13-91 KRS Split out streambuf::dbp() into separate file. +* Improve default buffer handling in underflow/overflow. +* Fix bug in sync(). +* 01-20-92 KRS C700 #5803: account for CR/LF pairs in ssync(). +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <string.h> +#include <stdiostr.h> +#pragma hdrstop + +extern "C" { +#include <file2.h> +#include <msdos.h> +} +#include <dos.h> + + stdiobuf::stdiobuf(FILE * f) +: streambuf() +{ + unbuffered(1); // initially unbuffered + _str = f; +} + + stdiobuf::~stdiobuf() +// : ~streambuf() +{ + stdiobuf::sync(); // make sure buffer flushed +// _str = 0; +} + + int stdiobuf::setrwbuf(int readsize, int writesize) +{ + char * tbuf; + unbuffered(!(readsize+writesize)); + if (unbuffered()) + return(0); + + tbuf = new char[(readsize+writesize)]; + if (!tbuf) + return(EOF); + + setb( tbuf, tbuf + (readsize+writesize), 1); + + if (readsize) + { + setg(base(),base()+readsize,base()+readsize); + } + else + { + setg(0,0,0); + } + + if (writesize) + { + setp(base()+readsize,ebuf()); + } + else + { + setp(0,0); + } + + return(1); +} + +int stdiobuf::overflow(int c) { + long count, nout; + if (allocate()==EOF) // make sure there is a reserve area + return EOF; + if (!unbuffered() && epptr()) + { + if ((count = pptr() - pbase()) > 0) + { + nout=fwrite((void *) pbase(), 1, (int)count, _str); + pbump(-(int)nout); + if (nout != count) + { + memmove(pbase(),pbase()+nout,(int)(count-nout)); + return(EOF); + } + } + } + if ((!unbuffered()) && (!epptr())) + setp(base()+(blen()>>1),ebuf()); // hack: default to 2nd half + if (c!=EOF) + { + if ((!unbuffered()) && (pptr() < epptr())) // guard against recursion + sputc(c); + else + return fputc(c, _str); + } + return(1); // return something other than EOF if successful +} + +int stdiobuf::underflow() +{ + int count; + if (allocate()==EOF) // make sure there is a reserve area + return EOF; + if ((!unbuffered()) && (!egptr())) + setg(base(),(base()+(blen()>>1)),(base()+(blen()>>1))); // hack: default + + if (unbuffered() || (!egptr())) + return fgetc(_str); + if (gptr() >= egptr()) +// buffer empty, try for more + { + if (!(count = fread((void *)eback(), 1, (size_t)(egptr()-eback()), _str))) + return(EOF); // reach EOF, nothing read + setg(eback(),(egptr()-count),egptr()); // _gptr = _egptr - count + if (gptr()!=eback()) + { + // CONSIDER: expensive, is there a better sol'n? + memmove(gptr(), eback(), count); // overlapping memory! + } + } + return sbumpc(); // CONSIDER: possible recursion +} + +streampos stdiobuf::seekoff(streamoff off, ios::seek_dir dir, int) +{ + + int fdir; + long retpos; + switch (dir) { + case ios::beg : + fdir = SEEK_SET; + break; + case ios::cur : + fdir = SEEK_CUR; + break; + case ios::end : + fdir = SEEK_END; + break; + default: + // error + return(EOF); + } + +// CONSIDER: is this needed? + stdiobuf::overflow(EOF); + if (fseek(_str, off, fdir)) + return (EOF); + if ((retpos=ftell(_str))==-1L) + return(EOF); + return((streampos)retpos); +} + +int stdiobuf::pbackfail(int c) +{ + if (eback()<gptr()) return sputbackc((char)c); // CONSIDER: should never happen + + if (stdiobuf::seekoff( -1, ios::cur, ios::in)==EOF) + return EOF; + if (!unbuffered() && egptr()) + { + memmove((gptr()+1),gptr(),(egptr()-(gptr()+1))); + *gptr()=(char)c; + } + return(c); +} + +int stdiobuf::sync() +{ + long count; + char * p; + char flags; + if (!unbuffered()) + { + if (stdiobuf::overflow(EOF)==EOF) + return(EOF); + if ((count=in_avail())>0) + { + flags = _osfile[_fileno(_str)]; + if (flags & FTEXT) + { + // If text mode, need to account for CR/LF etc. + for (p = gptr(); p < egptr(); p++) + if (*p == '\n') + count++; + + // account for EOF if read, not counted by _read + // UNDONE: is this necessary? + if (_str->_flag & _IOCTRLZ) + count++; +#if 0 + // UNDONE: is this correct? + if ((gptr()==eback()) && (flags & FCRLF)) + count--; +#endif + } + if (stdiobuf::seekoff( -count, ios::cur, ios::in)==EOF) + return(EOF); + + setg(eback(),egptr(),egptr()); // empty get area (_gptr = _egptr;) + } + } + return(0); +} + + stdiostream::stdiostream(FILE * file) +: iostream(new stdiobuf(file)) +{ +istream::delbuf(1); +ostream::delbuf(1); +// CONSIDER: do anything else? +} + + stdiostream::~stdiostream() +{ +// CONSIDER: do anything else? +} + +// include here for better granularity + +int ios::sunk_with_stdio = 0; + +void ios::sync_with_stdio() +{ +#if ((!defined(_WINDOWS)) || defined(_QWIN)) + if (!sunk_with_stdio) // first time only + { +// printf("In sync_with_stdio\n"); + + cin = new stdiobuf(stdin); + cin.delbuf(1); + cin.setf(ios::stdio); + + cout = new stdiobuf(stdout); + cout.delbuf(1); + cout.setf(ios::stdio|ios::unitbuf); + ((stdiobuf*)(cout.rdbuf()))->setrwbuf(0,80); // UNDONE: size?? + + cerr = new stdiobuf(stderr); + cerr.delbuf(1); + cerr.setf(ios::stdio|ios::unitbuf); + ((stdiobuf*)(cerr.rdbuf()))->setrwbuf(0,80); // UNDONE: size?? + + clog = new stdiobuf(stderr); + clog.delbuf(1); + clog.setf(ios::stdio); + ((stdiobuf*)(clog.rdbuf()))->setrwbuf(0,BUFSIZ); + + sunk_with_stdio++; + } +#endif +} diff --git a/private/crt32/iostream/streamb.cxx b/private/crt32/iostream/streamb.cxx new file mode 100644 index 000000000..bcfb9a1eb --- /dev/null +++ b/private/crt32/iostream/streamb.cxx @@ -0,0 +1,514 @@ +/*** +*streamb.cxx - fuctions for streambuf class. +* +* Copyright (c) 1990-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Functions for streambuf class. +* +*Revision History: +* 09-10-90 WAJ Initial version. +* 07-02-91 KRS Initial version completed. +* 08-20-91 KRS Treat chars as unsigned; fix sgetn() function. +* 09-06-91 KRS Do a sync() in ~streambuf before destroying buffer. +* 11-18-91 KRS Split off stream1.cxx for input-specific code. +* 12-09-91 KRS Fix up xsputn/xsgetn usage. +* 03-03-92 KRS Added mthread lock init calls to constructors. +* 06-02-92 KRS CAV #1745: Don't confuse 0xFF with EOF in xsputn() +* call to overflow(). +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <string.h> +#include <stdlib.h> +#include <iostream.h> +#pragma hdrstop + + +#ifndef BUFSIZ +#define BUFSIZ 512 +#endif + +/*** +*streambuf::streambuf() - +* +*Purpose: +* Default constructor. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +streambuf::streambuf() +{ + _fAlloc = 0; + _fUnbuf = 0; + x_lastc = EOF; + _base = NULL; + _ebuf = NULL; + _pbase = NULL; + _pptr = NULL; + _epptr = NULL; + _eback = NULL; + _gptr = NULL; + _egptr = NULL; + +#ifdef MTHREAD + LockFlg = 1; // default is no locking + _mtlockinit(lockptr()); +#endif /* MTHREAD */ + +} + +/*** +*streambuf::streambuf(char* pBuf, int cbBuf) - +* +*Purpose: +* Constructor which specifies a buffer area. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +streambuf::streambuf( char* pBuf, int cbBuf ) +{ + _fAlloc = 0; + _fUnbuf = 0; + x_lastc = EOF; + _base = pBuf; + _ebuf = pBuf + (unsigned)cbBuf; + _pbase = NULL; + _pptr = NULL; + _epptr = NULL; + _eback = NULL; + _gptr = NULL; + _egptr = NULL; + + if( pBuf == NULL || cbBuf == 0 ){ + _fUnbuf = 1; + _base = NULL; + _ebuf = NULL; + } + +#ifdef MTHREAD + LockFlg = 1; // default is no locking + _mtlockinit(lockptr()); +#endif /* MTHREAD */ + +} + + +/*** +*virtual streambuf::~streambuf() - +* +*Purpose: +* Destructor. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +streambuf::~streambuf() +{ + lock(); // make sure no one else still using it +// x_lastc = EOF; + sync(); // make sure buffer empty before possibly destroying it + if( (_fAlloc) && (_base) ) + delete _base; +} + + +/*** +* virtual streambuf * streambuf::setbuf(char * p, int len) - +* +*Purpose: +* Offers the array at p with len bytes to be used as a reserve area. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +streambuf * streambuf::setbuf(char * p, int len) +{ + if (!_base) + { + if ((!p) || (!len)) + _fUnbuf = 1; // mark as unbuffered + else + { + _base = p; + _ebuf = p + (unsigned)len; + _fUnbuf = 0; + } + return (this); + } + return((streambuf *)NULL); +} + + +/*** +*virtual int streambuf::xsputn( char* pBuf, int cbBuf ) - +* +*Purpose: +* Tries to output cbBuf characters. Returns number of characters +* that were outputted. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +int streambuf::xsputn( const char* pBuf, int cbBuf ) +{ + int cbOut; +#if 0 + if (!this) + printf("xsputn: this = NULL\n"); +#endif + for (cbOut = 0; cbBuf--; cbOut++) + { + if ((_fUnbuf) || (_pptr >= _epptr)) + { +// UNDONE: can be optimized like sgetn !! + if (overflow((unsigned char)*pBuf)==EOF) // 0-extend 0xFF !=EOF + break; + } + else + { + *(_pptr++) = *pBuf; + } + pBuf++; + } + return cbOut; +} + +/*** +*virtual int streambuf::xsgetn( char* pBuf, int cbBuf ) - +* +*Purpose: +* Tries to input cbBuf characters. Returns number of characters +* that were read from streambuf. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +// #pragma intrinsic(memcpy,__min) + +int streambuf::xsgetn( char * pBuf, int cbBuf) +{ + int count; + int cbIn = 0; + if (_fUnbuf) + { + if (x_lastc==EOF) + x_lastc=underflow(); + + while (cbBuf--) + { + if (x_lastc==EOF) + break; + *(pBuf++) = (char)x_lastc; + cbIn++; + x_lastc=underflow(); + } + } + else + { + while (cbBuf) + { + if (underflow()==EOF) // make sure something to read + break; + count = __min(egptr() - gptr(),cbBuf); + if (count>0) + { + memcpy(pBuf,gptr(),count); + pBuf += count; + _gptr += count; + cbIn += count; + cbBuf -= count; + } + } + } + return cbIn; +} +// #pragma function(memcpy, __min) + +/*** +*virtual int streambuf::sync() - +* +*Purpose: +* Tries to flush all data in put area and give back any data in the +* get area (if possible), leaving both areas empty on exit. +* Default behavior is to fail unless buffers empty. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +int streambuf::sync() +{ + if ((gptr() <_egptr) || (_pptr > _pbase)) + { + return EOF; + } + return 0; +} + +#if 0 +/*** +*int streambuf::sputn( char* pBuf, int cbBuf ) - +* +*Purpose: +* Tries to output cbBuf characters. Returns number of characters +* that were output. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +int streambuf::sputn( const char* pBuf, int cbBuf ) +{ +// UNDONE: + return xsputn(pBuf, cbBuf); +} + +/*** +*int streambuf::sgetn( char* pBuf, int cbBuf ) - +* +*Purpose: +* Tries to input cbBuf characters. Returns number of characters +* that were read from streambuf. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +int streambuf::sgetn( char * pBuf, int cbBuf) +{ +// UNDONE: + return xsgetn(pBuf, cbBuf); +} +#endif + +/*** +*int streambuf::allocate() - +* +*Purpose: +* Tries to set up a Reserve Area. If one already exists, or if +* unbuffered, just returns 0. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +int streambuf::allocate() +{ + if ((_fUnbuf) || (_base)) + return 0; + if (doallocate()==EOF) return EOF; + + return(1); +} + +/*** +*virtual int streambuf::doallocate() - +* +*Purpose: +* Tries to set up a Reserve Area. Returns EOF if unsuccessful. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +int streambuf::doallocate() +{ + char * tptr; + if (!( tptr = new char[BUFSIZ])) + return(EOF); + setb(tptr, tptr + BUFSIZ, 1); + return(1); +} + +/*** +*void streambuf::unbuffered(int fUnbuf) - +* +*Purpose: +* Sets unbuffered status. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +// void streambuf::unbuffered(int fUnbuf) +// { +// _fUnbuf = fUnbuf; +// if ((_fUnbuf) && (_base)) delete _base; // UNDONE: not doc'd as such ?? +// } + +/*** +*void streambuf::setb(char * b, char * eb, int a = 0) - +* +*Purpose: +* Sets up reserve area. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + +void streambuf::setb(char * b, char * eb, int a ) +{ + if ((_fAlloc) && (_base)) delete _base; + _base = b; + _fAlloc = a; + _ebuf = eb; +} + +#if 0 +int streambuf::overflow(int d) +{ + printf("Error: *%p streambuf::overflow(%d) called!\n",this,d); + return EOF; +} + +int streambuf::underflow() +{ + printf("Error: *%p streambuf::underflow() called!\n",this); + return EOF; +} +#endif + +/*** +*virtual streampos streambuf::seekoff(streamoff off, ios::seekdir dir, int mode) +* +*Purpose: +* seekoff member function. seek forward or backward in the stream. +* Default behavior: returns EOF. +* +*Entry: +* off = offset (+ or -) to seek by +* dir = one of ios::beg, ios::end, or ios::cur. +* mode = ios::in or ios::out. +* +*Exit: +* Returns new file position or EOF if error or seeking not supported. +* +*Exceptions: +* Returns EOF if error. +* +*******************************************************************************/ +streampos streambuf::seekoff(streamoff,ios::seek_dir,int) +{ +return EOF; +} + +/*** +*virtual streampos streambuf::seekpos(streampos pos, int mode) - +* +*Purpose: +* seekoff member function. seek to absolute file position. +* Default behavior: returns seekoff(streamoff(pos), ios::beg, mode). +* +*Entry: +* pos = absolute offset to seek to +* mode = ios::in or ios::out. +* +*Exit: +* Returns new file position or EOF if error or seeking not supported. +* +*Exceptions: +* Returns EOF if error. +* +*******************************************************************************/ +streampos streambuf::seekpos(streampos pos,int mode) +{ +return seekoff(streamoff(pos), ios::beg, mode); +} + +/*** +*virtual int streambuf::pbackfail(int c) - handle failure of putback +* +*Purpose: +* pbackfail member function. Handle exception of pback function. +* Default behavior: returns EOF. See spec. for details. +* +* Note: the following implementation gives default behavior, thanks +* to the default seekoff, but also supports derived classes properly: +* +*Entry: +* c = character to put back +* +*Exit: +* Returns c if successful or EOF on error. +* +*Exceptions: +* Returns EOF if error. Behavior is undefined if c was not the +* previous character in the stream. +* +*******************************************************************************/ +int streambuf::pbackfail(int c) +{ + if (eback()<gptr()) return sputbackc((char)c); // CONSIDER: should never happen + + if (seekoff( -1, ios::cur, ios::in)==EOF) // always EOF for streambufs + return EOF; + if (!unbuffered() && egptr()) + { + memmove((gptr()+1),gptr(),(egptr()-(gptr()+1))); + *gptr()=(char)c; + } + return(c); +} diff --git a/private/crt32/iostream/streamb1.cxx b/private/crt32/iostream/streamb1.cxx new file mode 100644 index 000000000..d6899ea79 --- /dev/null +++ b/private/crt32/iostream/streamb1.cxx @@ -0,0 +1,165 @@ +/*** +*streamb1.cxx - non-core functions for streambuf class. +* +* Copyright (c) 1990-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* None-core functions for streambuf class. +* +*Revision History: +* 11-18-91 KRS Split off from streamb.cxx. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <iostream.h> +#pragma hdrstop + + +/*** +*int streambuf::snextc() - +* +*Purpose: +* Increments get_pointer and returns the character following the new +* get_pointer. +* +*Entry: +* None. +* +*Exit: +* Returns the next character or EOF. +* +*Exceptions: +* Returns EOF if error. +* +*******************************************************************************/ +int streambuf::snextc() +{ + if (_fUnbuf) + { + if (x_lastc==EOF) + underflow(); // skip 1st character + return x_lastc = underflow(); // return next character, or EOF + } + else + { + if ((!egptr()) || (gptr()>=egptr())) + underflow(); // make sure buffer + + if ((++_gptr) < egptr()) + return (int)(unsigned char) *gptr(); + return underflow(); // returns next character, or EOF + } +} + + +/*** +*int streambuf::sbumpc() - +* +*Purpose: +* Increments get_pointer and returns the character that the previous +* get_pointer pointed to. +* +*Entry: +* None. +* +*Exit: +* Returns current character before bumping get pointer. +* +*Exceptions: +* Returns EOF if error. +* +*******************************************************************************/ +int streambuf::sbumpc() +{ + int c; + if (_fUnbuf) // no buffer + { + if (x_lastc==EOF) + { + c = underflow(); + } + else + { + c = x_lastc; + x_lastc = EOF; + } + } + else + { + if( gptr() < egptr() ) + { + c = (int)(unsigned char)*(gptr()); + } + else + { + // UNDONE: possible recursion + c = underflow(); + } + _gptr++; + } + return c; +} + +/*** +*void streambuf::stossc() - advance get pointer +* +*Purpose: +* Advances the get pointer. Does not check for EOF. +* +*Entry: +* None. +* +*Exit: +* None. +* +*Exceptions: +* +*******************************************************************************/ +void streambuf::stossc() +{ + if (_fUnbuf) + { + if (x_lastc==EOF) + underflow(); // throw away current character + else + x_lastc=EOF; // discard current cached character + } + else + { + if (gptr() >= egptr()) + underflow(); + if (gptr() < egptr()) + _gptr++; + } +} + +/*** +*int streambuf::sgetc() - +* +*Purpose: +* Returns the character that the previous get_pointer points to. +* DOES NOT advance the get pointer. +* +*Entry: +* None. +* +*Exit: +* Returns current character or EOF if error. +* +*Exceptions: +* Returns EOF if error. +* +*******************************************************************************/ +int streambuf::sgetc() +{ + if (_fUnbuf) // no buffer + { + if (x_lastc==EOF) + x_lastc = underflow(); + return x_lastc; + } + else + return underflow(); +} diff --git a/private/crt32/iostream/strmbdbp.cxx b/private/crt32/iostream/strmbdbp.cxx new file mode 100644 index 000000000..2f209a67f --- /dev/null +++ b/private/crt32/iostream/strmbdbp.cxx @@ -0,0 +1,48 @@ +/*** +*strmbdbp.cxx - streambuf::dbp() debug routine +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* Dump debug info about streambuf to stdout. +* +*Revision History: +* +* 11-13-91 KRS Created. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <io.h> +#include <stdio.h> +#include <iostream.h> +#pragma hdrstop + +#pragma check_stack(on) // large buffer(s) + +void streambuf::dbp() +{ +#if ((!defined(_WINDOWS)) || defined(_QWIN)) +int olen; +_WINSTATIC char obuffer[256]; // CONSIDER: size? (198 bytes?) + if (unbuffered()) + olen = sprintf(obuffer, + "\nSTREAMBUF DEBUG INFO: this=%p, unbuffered\n", + (void *) this); + else + { + olen = sprintf(obuffer, + "\nSTREAMBUF DEBUG INFO: this=%p, _fAlloc=%d\n" + " base()=%p, ebuf()=%p, blen()=%d\n" + " pbase()=%p, pptr()=%p, epptr()=%p\n" + " eback()=%p, gptr()=%p, egptr()=%p\n", + (void *) this, (_fAlloc), + base(), ebuf(), blen(), + pbase(), pptr(), epptr(), + eback(), gptr(), egptr()); + } + _write(1,obuffer,olen); // direct write to stdout + // CONSIDER: add more info? +#endif +} diff --git a/private/crt32/iostream/strstrea.cxx b/private/crt32/iostream/strstrea.cxx new file mode 100644 index 000000000..fba8e0126 --- /dev/null +++ b/private/crt32/iostream/strstrea.cxx @@ -0,0 +1,440 @@ + +/*** +*strstream.cxx - definitions for strstreambuf, strstream +* +* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved. +* +*Purpose: +* This file defines the functions used by strstream and strstrembuf +* classes. +* +*Revision History: +* 08-14-91 KRS Initial version. +* 08-23-91 KRS Initial version completed. +* 09-03-91 KRS Fix typo in strstreambuf::seekoff(,ios::in,) +* 09-04-91 KRS Added virtual sync() to fix flush(). Fix underflow(). +* 09-05-91 KRS Change str() and freeze() to match spec. +* 09-19-91 KRS Add calls to delbuf(1) in constructors. +* 10-24-91 KRS Avoid virtual calls from virtual functions. +* +*******************************************************************************/ + +#include <cruntime.h> +#include <internal.h> +#include <stdlib.h> +#include <string.h> +#include <strstrea.h> +#pragma hdrstop + +/*** +*strstreambuf::strstreambuf() - default constructor for strstreambuf +* +*Purpose: +* Default constructor for class strstreambuf. +* +*Entry: +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ + strstreambuf::strstreambuf() +: streambuf() +{ +x_bufmin = x_dynamic = 1; +x_static = 0; +x_alloc = (0); +x_free = (0); +// CONSIDER: do anything else?? +} + +/*** +*strstreambuf::strstreambuf(int n) - constructor for strstreambuf +* +*Purpose: +* Constructor for class strstreambuf. Created in dynamic mode. +* +*Entry: +* n = minimum size for initial allocation. +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ +strstreambuf::strstreambuf(int n) +: streambuf() +{ +x_dynamic = 1; +x_static = 0; +x_alloc = (0); +x_free = (0); +setbuf(0,n); +// CONSIDER: do anything else?? +} + +/*** +*strstreambuf::strstreambuf(void* (*_a)(long), void (*_f)(void*)) - constructor for strstreambuf +* +*Purpose: +* Construct a strstreambuf in dynamic mode. Use specified allocator +* and deallocator instead of new and delete. +* +*Entry: +* *_a = allocator: void * (*_a)(long) +* *_f = deallocator: void (*_f)(void *) +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ +strstreambuf::strstreambuf(void* (*_a)(long), void (*_f)(void*)) +: streambuf() +{ +// CONSIDER: do anything else?? +x_dynamic = x_bufmin = 1; +x_static = 0; +x_alloc = _a; +x_free = _f; +} + +/*** +*strstreambuf::strstreambuf(char * ptr, int size, char * pstart = 0) - +* +*Purpose: +* Construct a strstreambuf in static mode. Buffer used is of 'size' +* bytes. If 'size' is 0, uses a null-terminated string as buffer. +* If negative, size is considered infinite. Get starts at ptr. +* If pstart!=0, put buffer starts at pstart. Otherwise, no output. +* +*Entry: +* char * ptr; pointer to buffer base() +* int size; size of buffer, or 0= use strlen to calculate size +* or if negative size is 'infinite'. +* char * pstart; pointer to put buffer of NULL if none. +* +*Exit: +* +*Exceptions: +* +*******************************************************************************/ +strstreambuf::strstreambuf( char * ptr, int size, char * pstart) +: streambuf() +{ + x_static = 1; + x_dynamic = 0; + char * pend; + + if (!size) + pend = ptr + strlen(ptr); + else if (size < 0) + { + // size = 32767 - (unsigned short)ptr; // UNDONE: fix for 32-bit + // UNDONE: bogus segment math + pend = (char*)-1L; + } + else + pend = ptr + size; + + setb(ptr, pend,0); +// CONSIDER: is this right? Not quite what spec. says... + if (pstart) + { + setg(ptr,ptr,pstart); + setp(pstart, pend); + } + else + { + setg(ptr,ptr,pend); + setp(0, 0); + } +} + +strstreambuf::~strstreambuf() +{ + if ((x_dynamic) && (base())) + { + if (x_free) + { + (*x_free)(base()); + } + else + { + delete base(); + } + } +// x_dynamic = 0; +// x_static = 1; +} + +void strstreambuf::freeze(int n) +{ + if (!x_static) + x_dynamic = (!n); +} + +char * strstreambuf::str() +{ +// x_static = 1; // CONSIDER: disallow further dynamic changes? + + x_dynamic = 0; // freeze(); + + return base(); +} + +int strstreambuf::doallocate() +{ + char * bptr; + int size; + size = __max(x_bufmin,blen()+1); + long offset = 0; + + if (x_alloc) + { + bptr = (char*)(*x_alloc)(size); + } + else + { + bptr = new char[size]; + } + if (!bptr) + return EOF; + + if (blen()) + { + memcpy(bptr, base(), blen()); + offset = bptr - base(); // amount to adjust pointers by + } + if (x_free) + { + (*x_free)(base()); + } + else + { + delete base(); + } + setb(bptr,bptr+size,0); // we handle deallocation + + // adjust get/put pointers too, if necessary + if (offset) + if (egptr()) + { + setg(eback()+offset,gptr()+offset,egptr()+offset); + } + if (epptr()) + { + size = pptr() - pbase(); + setp(pbase()+offset,epptr()+offset); + pbump(size); + } + return(1); +} + +streambuf * strstreambuf::setbuf( char *, int l) +{ + if (l) + x_bufmin = l; + return this; +} + +int strstreambuf::overflow(int c) +{ +/* +- if no room and not dynamic, give error +- if no room and dynamic, allocate (1 more or min) and store +- if and when the buffer has room, store c if not EOF +*/ + int temp; + if (pptr() >= epptr()) + { + if (!x_dynamic) + return EOF; + + if (strstreambuf::doallocate()==EOF) + return EOF; + + if (!epptr()) // init if first time through + { + setp(base() + (egptr() - eback()),ebuf()); + } + else + { + temp = pptr()-pbase(); + setp(pbase(),ebuf()); + pbump(temp); + } + } + + if (c!=EOF) + { + *pptr() = (char)c; + pbump(1); // UNDONE: + } + return(1); +} + +int strstreambuf::underflow() +{ + char * tptr; + if (gptr() >= egptr()) + { + // try to grow get area if we can... + if (egptr()<pptr()) + { + tptr = base() + (gptr()-eback()); + setg(base(),tptr,pptr()); + } + if (gptr() >= egptr()) + return EOF; + } + + return *gptr(); +} + +int strstreambuf::sync() +{ +// a strstreambuf is always in sync, by definition! +return 0; +} + +streampos strstreambuf::seekoff(streamoff off, ios::seek_dir dir, int mode) +{ +char * tptr; +long offset = EOF; // default return value + if (mode & ios::in) + { + strstreambuf::underflow(); // makes sure entire buffer available + switch (dir) { + case ios::beg : + tptr = eback(); + break; + case ios::cur : + tptr = gptr(); + break; + case ios::end : + tptr = egptr(); + break; + default: + return EOF; + } + tptr += off; + offset = tptr - eback(); + if ((tptr < eback()) || (tptr > egptr())) + { + return EOF; + } + gbump(tptr-gptr()); + } + if (mode & ios::out) + { + if (!epptr()) + { + if (strstreambuf::overflow(EOF)==EOF) // make sure there's a put buffer + return EOF; + } + switch (dir) { + case ios::beg : + tptr = pbase(); + break; + case ios::cur : + tptr = pptr(); + break; + case ios::end : + tptr = epptr(); + break; + default: + return EOF; + } + tptr += off; + offset = tptr - pbase(); + if (tptr < pbase()) + return EOF; + if (tptr > epptr()) + { + if (x_dynamic) + { + x_bufmin = __max(x_bufmin, (tptr-pbase())); + if (strstreambuf::doallocate()==EOF) + return EOF; + // _epptr = ebuf(); + } + else + return EOF; + } + pbump(tptr-pptr()); + } + return offset; // note: if both in and out set, returns out offset +} + + + istrstream::istrstream(char * pszStr) +: istream(new strstreambuf(pszStr,0)) +{ + delbuf(1); + // CONSIDER: do anything else? +} + + istrstream::istrstream(char * pStr, int len) +: istream(new strstreambuf(pStr,len)) +{ + delbuf(1); + // CONSIDER: do anything else? +// CONSIDER: do anything else? +} + + istrstream::~istrstream() +{ +// CONSIDER: do anything else? +} + + ostrstream::ostrstream() +: ostream(new strstreambuf) +{ + delbuf(1); + // CONSIDER: do anything else? +// CONSIDER: do anything else? +} + + ostrstream::ostrstream(char * str, int size, int mode) +: ostream(new strstreambuf(str,size,str)) +{ + delbuf(1); + if (mode & (ios::app|ios::ate)) + seekp(strlen(str),ios::beg); +// CONSIDER: needed? +// rdbuf()->setg(0,0,0); // no input allowed +// CONSIDER: do anything else? +} + + ostrstream::~ostrstream() +{ +// CONSIDER: do anything else? +} + + strstream::strstream() +: iostream(new strstreambuf) +{ + istream::delbuf(1); + ostream::delbuf(1); + // CONSIDER: do anything else? +} + + strstream::strstream(char * str, int size, int mode) +: iostream(new strstreambuf(str,size,str)) +{ +// UNDONE: not quite correct! + + istream::delbuf(1); + ostream::delbuf(1); + if ((mode & ostream::out) && (mode & (ostream::app|ostream::ate))) + seekp(strlen(str),ostream::beg); +// rdbuf()->setg(rdbuf()->base(),rdbuf()->base(),rdbuf()->ebuf()); // UNDONE: how is input handled??? +// CONSIDER: do anything else? +} + + strstream::~strstream() +{ +// CONSIDER: do anything else? +} |