home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_11 / nelson / iohandle.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-12  |  2.4 KB  |  86 lines

  1. /* ----------------------------------------------------
  2.  *  Listing 4
  3.  *
  4.  *  iohandle.cpp
  5.  *  Base class for handle-based IOCTL for character
  6.  *  devices and files
  7.  * ------------------------------------------------- */
  8.  
  9. #include <io.h>         //open()
  10. #include <fcntl.h>
  11. #include "iohandle.h"
  12.  
  13. #ifndef NDEBUG
  14. #include <iostream.h>
  15.  
  16. void IoctlHandle::IoctlError( int errval )
  17. {
  18.     cerr << "*** class IoctlHandle: DOS error #"
  19.          << errval << " encountered, subfunction #"
  20.          << hex << _iregs.x.ax << dec << endl;
  21. }
  22. #endif      // NDEBUG
  23.  
  24. IoctlHandle::~IoctlHandle()     {
  25.     if( _pathused )     //close() handle
  26.             close( _handle );   //if path specified.
  27. }
  28. IoctlHandle::IoctlHandle( int handle )      {
  29.     _info = handle_info( handle );
  30.     _handle = _dos_error ? -1 : handle;
  31.     _pathused = 0;
  32. }
  33. IoctlHandle::IoctlHandle( const char *path )    {
  34.     int fh;
  35.     if( (fh = open( path, O_RDWR )) != -1 )   {
  36.             _info = handle_info( fh );
  37.             _handle = fh;
  38.             _pathused = 1;
  39.             return;
  40.     }
  41.     _handle = -1;
  42.     _pathused = 0;
  43. }
  44. IoctlHandle *IoctlHandle::Init( int fhandle )   {
  45.    /* Return a pointer to new IoctlHandle object    */
  46.     IoctlHandle *obj = new IoctlHandle( fhandle);
  47.     if( obj->_handle == -1 )  {    //bad fhandle
  48.             delete obj;
  49.             return (IoctlHandle *) 0;
  50.     }
  51.     return obj;
  52. }
  53. IoctlHandle *IoctlHandle::Init(const char *path )   {
  54.    /* Return a pointer to a new IoctlHandle object */
  55.     IoctlHandle *obj = new IoctlHandle( path);
  56.     if( obj->_handle == -1 )   {   //bad path
  57.             delete obj;
  58.             return (IoctlHandle *) 0;
  59.     }
  60.     return obj;
  61. }
  62. unsigned IoctlHandle::handle_info( int handle ) {
  63.    /* Get device information word */
  64.     _iregs.x.bx = handle;
  65.     int21_44h(get_handle_info);
  66.     return _oregs.x.dx;
  67. }
  68. int IoctlHandle::inputStatus(void)      {
  69.    /* Return -1 if handle ready for input, else 0 */
  70.     _iregs.x.bx = _handle;
  71.     int21_44h(input_status);
  72.     return (int) _oregs.h.al;
  73. }
  74. int IoctlHandle::outputStatus( void)    {
  75.    /* Return -1 if handle ready for output, else 0 */
  76.     _iregs.x.bx = _handle;
  77.     int21_44h(output_status);
  78.     return (int) _oregs.h.al;
  79. }
  80. int IoctlHandle::isRemote(void)    {
  81.     _iregs.x.bx = _handle;
  82.     int21_44h( handle_remote );
  83.     return _oregs.x.dx & remote;  //!0 if remote
  84. }
  85. /* ----- End of File ------------------------------- */
  86.