home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: SysTools / SysTools.zip / sysba021.zip / SRC.ZIP / sysbar2 / ffile.h next >
C/C++ Source or Header  |  2000-06-20  |  4KB  |  113 lines

  1. /*
  2.  
  3.   C++ class library for file streams
  4.     version 1.00
  5.  
  6.   ..................................................................
  7.  
  8.   Copyright (c) 1995  Dmitry I. Platonoff
  9.                       All rights reserved
  10.  
  11.                     dplatonoff@canada.com
  12.  
  13.   ..................................................................
  14.  
  15.   LICENSE
  16.   ~~~~~~~
  17.   Redistribution and use in source and binary forms, with or without
  18.   modification, are permitted provided that the following conditions
  19.   are met:
  20.  
  21.   1. Redistributions of source code must retain the above copyright
  22.      notice, this list of conditions and the following disclaimer.
  23.  
  24.   2. Redistributions in binary form must reproduce the above
  25.      copyright notice, this list of conditions and the following
  26.      disclaimer in the documentation and/or other materials provided
  27.      with the distribution.
  28.  
  29.   3. Redistributions of any form whatsoever, as well as all
  30.      advertising materials mentioning features or use of this
  31.      software (if any), must include the following acknowledgment:
  32.      "This product includes software developed by Dmitry I. Platonoff".
  33.  
  34.   4. The names "SysBar/2" and "Dmitry I. Platonoff" must not be
  35.      used to endorse or promote products derived from this software
  36.      without prior written permission. For such permission, please
  37.      contact dplatonoff@canada.com.
  38.  
  39.   5. Products derived from this software may not be called
  40.      "SysBar/2" nor may "Dmitry I. Platonoff" appear in their
  41.      contributor lists without prior written permission.
  42.  
  43.   ..................................................................
  44.  
  45.   DISCLAIMER
  46.   ~~~~~~~~~~
  47.   THIS SOFTWARE IS PROVIDED BY THE AUTHOR OR CONTRIBUTORS "AS IS"
  48.   AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  49.   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  50.   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  51.   AUTHOR OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  52.   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  53.   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  54.   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55.   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  56.   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  57.   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  58.   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59.  
  60. */
  61.  
  62. #if !defined(__ffile_h)
  63. #define __ffile_h
  64.  
  65. #include <stdio.h>
  66. #include <io.h>
  67.  
  68. const int iFFileMaxString = 256;
  69.  
  70. // Here is the class that represents a standard C file stream,
  71. //  but has some features, e.g. auto-closing at destruction
  72. class ffile
  73. {
  74. protected:
  75.   unsigned uLinesGone;  //line counter (text files only)
  76.  
  77. public:
  78.  
  79.   FILE* f;  //pointer to FILE structure
  80.   long lFileSize;  //file size (for read-only mode)
  81.   char sBuffer[iFFileMaxString];  //buffer for text lines (text files only)
  82.  
  83.   ffile( char *pszFilename, const char* pszMode )  //Constructor (parameters
  84.   {                                               //are the same as for fopen)
  85.     if ( f = fopen( pszFilename, pszMode ) )
  86.     {
  87.       lFileSize = filelength( fileno( f ) );
  88.       uLinesGone = 0;
  89.     }
  90.   }
  91.  
  92.   ~ffile() { if ( f ) fclose( f ); }  //destructor that closes file
  93.  
  94.   FILE* operator()() { return f; }  //"()" operator - for use FILE
  95.                                                        //structure outside the class
  96.   long GetPosition( void ) { return ftell( f ); }  //position in file
  97.  
  98.   int ReadString( void )  //method to read a single line from a text file
  99.   {
  100.     if ( feof( f ) || ! fgets( sBuffer, iFFileMaxString, f ) ) return -1;
  101.     else { uLinesGone++; return 0; }
  102.   }
  103.  
  104.   unsigned GetCurrLine( void ) { return uLinesGone; }  //current line number
  105.  
  106.   void Close( void ) { if ( fclose( f ) == 0 ) f = NULL; }
  107. };
  108.  
  109.  
  110. #endif
  111.  
  112.  
  113.