home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / io / binfile.h < prev    next >
C/C++ Source or Header  |  1995-04-08  |  6KB  |  189 lines

  1. #ifndef _BINFILE_H_
  2. #define _BINFILE_H_
  3.  
  4.  
  5.  
  6.  
  7. /*
  8.  *
  9.  *          Copyright (C) 1994, M. A. Sridhar
  10.  *  
  11.  *
  12.  *     This software is Copyright M. A. Sridhar, 1994. You are free
  13.  *     to copy, modify or distribute this software  as you see fit,
  14.  *     and to use  it  for  any  purpose, provided   this copyright
  15.  *     notice and the following   disclaimer are included  with all
  16.  *     copies.
  17.  *
  18.  *                        DISCLAIMER
  19.  *
  20.  *     The author makes no warranties, either expressed or implied,
  21.  *     with respect  to  this  software, its  quality, performance,
  22.  *     merchantability, or fitness for any particular purpose. This
  23.  *     software is distributed  AS IS.  The  user of this  software
  24.  *     assumes all risks  as to its quality  and performance. In no
  25.  *     event shall the author be liable for any direct, indirect or
  26.  *     consequential damages, even if the  author has been  advised
  27.  *     as to the possibility of such damages.
  28.  *
  29.  */
  30.  
  31.  
  32. // This is an abstract interface to a binary file, essentially a wrapper around
  33. // the platform's file system calls. The strategy for deciding when to
  34. // open and close the file is determined by the implementation of this
  35. // class, and cannot be relied upon by the user of this class.
  36. //
  37. // The ``core'' methods for reading and writing unsigned char arrays and
  38. // for seeking to a given position, seeking to end of file and changing
  39. // file size are declared virtual; the default implementations for all
  40. // other methods are provided in terms of the core methods. Thus a
  41. // derived class need only override the core methods.
  42.  
  43.  
  44.  
  45. #if defined(__GNUC__)
  46. #pragma interface
  47. #endif
  48.  
  49. #include "base/string.h"
  50. #include "base/bytstrng.h"
  51. #include "base/stream.h"
  52.  
  53.  
  54. class CL_EXPORT CL_BinaryFile: public CL_Stream {
  55.  
  56. public:
  57.  
  58.     CL_BinaryFile (const char* pathName, bool create = FALSE);
  59.     // Constructor: instantiate a BinaryFile object with the given path
  60.     // name. If the second parameter is TRUE, create the file, overwriting
  61.     // any existing one. If it is FALSE, and the file does not exist,
  62.     // issue a runtime error.
  63.  
  64.     ~CL_BinaryFile();
  65.     // Destructor.
  66.  
  67.     // ----------------- Error state methods ---------------------
  68.  
  69.     inline bool Valid() const;
  70.     // Tell whether the state of the file is ok.
  71.  
  72.     CL_String ErrorString () const;
  73.     // Return the (platform-specific) error message string associated
  74.     // with the most recent error.
  75.     
  76.  
  77.     // ----------------- Read operations -------------------------
  78.  
  79.     virtual long Read (uchar* buffer, long num_bytes) const; 
  80.     // Read from current position. Returns number of bytes read, 0 on eof, -1
  81.     // on error.
  82.  
  83.     bool Read (CL_Object& obj) const
  84.         {return CL_Stream::Read (obj);};
  85.     // Override the inherited method; forward the call to parent class.
  86.     // This is to prevent the "hides virtual" warning.
  87.     
  88.     bool Read (CL_ObjectPtr& obj) const
  89.         {return CL_Stream::Read (obj);};
  90.     // Override the inherited method; forward the call to parent class.
  91.     // This is to prevent the "hides virtual" warning.
  92.     
  93.     // ----------------- Write operations ------------------------
  94.  
  95.     virtual bool Write (uchar* buffer, long num_bytes);
  96.     // Write at current position.
  97.  
  98.     bool Write (const CL_Object& p)
  99.         {return CL_Stream::Write (p);};
  100.     // Override the inherited method; forward the call to parent class.
  101.     // This is to prevent the "hides virtual" warning.
  102.     
  103.     bool Write (CL_ObjectPtr p)
  104.         {return CL_Stream::Write (p);};
  105.     // Override the inherited method; forward the call to parent class.
  106.     // This is to prevent the "hides virtual" warning.
  107.     
  108.     
  109.     // ----------------- Seek operations -------------------------
  110.     
  111.     virtual bool SeekTo (CL_Offset position) const;
  112.     // Change the current position. Returns TRUE on success.
  113.  
  114.     virtual bool SeekToEnd () const;
  115.  
  116.     virtual bool SeekRelative (long change) const;
  117.     
  118.     virtual bool ChangeSize (CL_Offset size);
  119.     // Change the size of file
  120.  
  121.     // ---------------------- Querying ----------------------
  122.  
  123.     virtual bool Eof () const;
  124.     // Are we at the end of the file?
  125.     
  126.     virtual long Offset () const;
  127.     // Return the current position
  128.     
  129.     CL_String FileName() const;
  130.     // Return the pathname of the file (the name it was initialized with)
  131.     
  132.     virtual long Size () const;
  133.     // Return the size of the file in bytes
  134.  
  135.     // ------------------ Static functions ------------------------------
  136.  
  137.     static bool Exists (const char *pathName);
  138.     // Return 1 if the file with given name exists, 0 if not.
  139.  
  140.     static bool Create (const char* pathName);     
  141.     // Create the file; truncate any existing file with
  142.     // the same name. Return TRUE on success, FALSE on failure.
  143.  
  144.     // ------------------ Basic functions ------------------------------
  145.     
  146.     const char* ClassName() const {return "CL_BinaryFile";};
  147.  
  148.     CL_ClassId ClassId() const { return _CL_BinaryFile_CLASSID;};
  149.  
  150.     // ------------------- End public protocol ------------------------ 
  151.  
  152. protected:
  153.  
  154.     //  Instance variables:
  155.  
  156.     CL_String    _pathName;     // Path name
  157.     int          _fd;           // The file descriptor
  158.     CL_String    _errorString;
  159.  
  160.     // Protected methods:
  161.     
  162.     virtual bool _Open ();
  163.  
  164.     virtual void _Close ();
  165.  
  166.     void         _MakeErrorString (const char*) const;
  167.  
  168. };
  169.  
  170.  
  171.  
  172.  
  173. inline bool CL_BinaryFile::Valid() const
  174. {
  175.     return _errorString.Size() == 0;
  176. }
  177.  
  178.  
  179.  
  180.  
  181.  
  182. inline CL_String CL_BinaryFile::FileName () const
  183. {
  184.     return _pathName;
  185. }
  186.  
  187.  
  188. #endif
  189.