00001 /* 00002 This product contains certain software code or other information 00003 ("AT&T Software") proprietary to AT&T Corp. ("AT&T"). The AT&T 00004 Software is provided to you "AS IS". YOU ASSUME TOTAL RESPONSIBILITY 00005 AND RISK FOR USE OF THE AT&T SOFTWARE. AT&T DOES NOT MAKE, AND 00006 EXPRESSLY DISCLAIMS, ANY EXPRESS OR IMPLIED WARRANTIES OF ANY KIND 00007 WHATSOEVER, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 00008 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, WARRANTIES OF 00009 TITLE OR NON-INFRINGEMENT OF ANY INTELLECTUAL PROPERTY RIGHTS, ANY 00010 WARRANTIES ARISING BY USAGE OF TRADE, COURSE OF DEALING OR COURSE OF 00011 PERFORMANCE, OR ANY WARRANTY THAT THE AT&T SOFTWARE IS "ERROR FREE" OR 00012 WILL MEET YOUR REQUIREMENTS. 00013 00014 Unless you accept a license to use the AT&T Software, you shall not 00015 reverse compile, disassemble or otherwise reverse engineer this 00016 product to ascertain the source code for any AT&T Software. 00017 00018 (c) AT&T Corp. All rights reserved. AT&T is a registered trademark of AT&T Corp. 00019 00020 *********************************************************************** 00021 00022 History: 00023 00024 24/11/99 - initial release by Hartmut Liefke, liefke@seas.upenn.edu 00025 Dan Suciu, suciu@research.att.com 00026 */ 00027 00028 //************************************************************************** 00029 //************************************************************************** 00030 00031 // This module contains the main functions for reading a file 00032 00033 #ifndef FILE_HPP 00034 #define FILE_HPP 00035 00036 #include <stdio.h> 00037 #ifdef WIN32 00038 #include <fcntl.h> 00039 #include <io.h> 00040 #endif 00041 00042 #include "Error.hpp" 00043 extern int errno; 00044 00045 class CFile 00046 { 00047 FILE *file; // The file handle 00048 char *savefilename; // We save the file name 00049 00050 protected: 00051 unsigned filepos; // Current file position 00052 char iseof; // Did we reach the end of the file? 00053 00054 00055 public: 00056 00057 CFile() 00058 { 00059 file=NULL; 00060 } 00061 00062 char OpenFile(char *filename) 00063 // Opens a file (if filename==NULL, then the standard input is opened) 00064 // Returns 1, if okay, otherwise 0 00065 { 00066 if(filename==NULL) 00067 { 00068 file=stdin; 00069 #ifdef WIN32 00070 _setmode(_fileno(stdin),_O_BINARY); 00071 #endif 00072 } 00073 else 00074 { 00075 if(*filename==0) // Empty file name ? 00076 return 0; 00077 00078 file=fopen(filename,"rb"); 00079 00080 if(file==NULL) // We couldn't open the file? 00081 return 0; 00082 } 00083 filepos=0; 00084 iseof=0; 00085 00086 savefilename=filename; 00087 return 1; 00088 } 00089 00090 unsigned GetFilePos() { return filepos;} 00091 // Returns the current position in the file 00092 00093 unsigned ReadBlock(char *dest,unsigned bytecount) 00094 // Reads a data block into the memory at 'dest'. The maximum size is 'bytecount' 00095 // The function returns the number of bytes read or -1, if something fails 00096 // If the result is smaller than bytecount, the end of the file has been reached 00097 // and flag eof is set to 1. 00098 { 00099 if(iseof) 00100 return 0; 00101 00102 // let's try to reach 'bytecount' bytes 00103 unsigned bytesread=(unsigned)fread(dest,1,bytecount,file); 00104 00105 if(bytesread<bytecount) 00106 // We didn't read all of them? 00107 { 00108 if(feof(file)) 00109 iseof=1; 00110 else 00111 { 00112 if(ferror(file)) 00113 { 00114 char tmpstr[300]; 00115 if(savefilename!=NULL) 00116 sprintf(tmpstr,"Could not read from file '%s' (Error %lu)!",savefilename,errno); 00117 else 00118 sprintf(tmpstr,"Could not read from standard input (Error %lu)!",errno); 00119 Error(tmpstr); 00120 Exit(); 00121 } 00122 } 00123 } 00124 filepos+=bytesread; 00125 return bytesread; 00126 } 00127 00128 void CloseFile() 00129 // Closes the file 00130 { 00131 if((file==NULL)||(file==stdout)) 00132 return; 00133 00134 fclose(file); 00135 file=NULL; 00136 } 00137 }; 00138 00139 #endif