1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/* this handles all file IO, it is written to be as portable as I could make it
*/
#include "includes.h"
/* error family, members and descriptions for those errors
* generated here
*/
#define LF 10
#define CR 13
FILE *OpenReadFile(char *fname,int bufferSize)
/* passed a pointer to a file name, open a file for reading (in untranslated mode)
* return a FILE, or NULL if there was an error
* If NULL is returned, GetError can be called to determine what went wrong
*/
{
FILE
*theFile;
if( (theFile=(FILE *)fopen(fname,"r")) ) /* open the file */
{
if(bufferSize)
{
if(setvbuf(theFile,NULL,_IOFBF,bufferSize)!=0)
{
SetStdCLibError(); /* set high level error information */
fclose(theFile); /* could not set buf for the file, so close it and complain */
theFile=(FILE *)NULL;
}
}
else
{
if(setvbuf(theFile,NULL,_IONBF,0)!=0)
{
SetStdCLibError(); /* set high level error information */
fclose(theFile); /* could not set buf for the file, so close it and complain */
theFile=(FILE *)NULL;
}
}
}
else
{
SetStdCLibError(); /* set high level error information */
}
return(theFile);
}
FILE *OpenWriteFile(char *fname,int bufferSize)
/* passed a pointer to a file name, open a file for writing (untranslated mode)
* return a FILE, or null if there was an error
* If NULL is returned, GetError can be called to determine what went wrong
*/
{
FILE
*theFile;
if( (theFile=(FILE *)fopen(fname,"w")) ) /* open the file */
{
if(bufferSize)
{
if(setvbuf(theFile,NULL,_IOFBF,bufferSize)!=0)
{
SetStdCLibError(); /* set high level error information */
fclose(theFile); /* could not set buf for the file, so close it and complain */
theFile=(FILE *)NULL;
}
}
else
{
if(setvbuf(theFile,NULL,_IONBF,0)!=0)
{
SetStdCLibError(); /* set high level error information */
fclose(theFile); /* could not set buf for the file, so close it and complain */
theFile=(FILE *)NULL;
}
}
}
else
{
SetStdCLibError(); /* set high level error information */
}
return(theFile);
}
bool CloseFile(FILE *fp)
/* passed a pointer to a file, close it
* return false if there is a problem
* If false is returned, GetError can be called to determine what went wrong
*/
{
fclose(fp); /* get rid of the file */
return(true);
}
bool ReadFile(FILE *fp,UBYTE *dest,ULONG count,ULONG *numRead)
/* passed a pointer to an open file, extract a string of bytes from the file
* and return it at dest
* fill numRead with the actual number of bytes read
* pass back a bool that tells if all was well during read
* it will be false for a read error
* If false is returned, GetError can be called to determine what went wrong
* it is allowed to attempt to read past the end of file, numRead will just
* return less than count.
*/
{
int
numToRead,numJustRead;
bool
fail,done;
(*numRead)=0; /* no bytes read so far */
fail=done=false;
while(!done&&count)
{
numToRead=Min(count,INT_MAX); /* dont pass an int that is too big */
if((numJustRead=fread(&(dest[(*numRead)]),1,numToRead,fp))!=numToRead)
{
done=true; /* no matter what, we are done now */
if(ferror(fp)) /* if there was an error, report it */
{
SetStdCLibError(); /* set high level error information */
fail=true;
}
}
(*numRead)+=(ULONG)numJustRead; /* add in amount just read */
count-=(ULONG)numJustRead; /* this many less bytes to go */
}
return(!fail);
}
bool WriteFile(FILE *fp,UBYTE *src,ULONG count,ULONG *numWritten)
/* passed a pointer to an open file, send the string of bytes given
* out to it. If there is an error, return false
* If false is returned, GetError can be called to determine what went wrong
*/
{
int
numToWrite,numJustWritten;
bool
fail;
(*numWritten)=0; /* no bytes written so far */
fail=false;
while(!fail&&count)
{
numToWrite=Min(count,INT_MAX); /* dont pass an int that is too big */
if((numJustWritten=fwrite(&(src[(*numWritten)]),1,numToWrite,fp))!=numToWrite)
{
SetStdCLibError(); /* set high level error information */
fail=true; /* no matter what, we have failed now */
}
(*numWritten)+=(ULONG)numJustWritten; /* add in amount just written */
count-=(ULONG)numJustWritten; /* this many less bytes to go */
}
return(!fail);
}
bool GetLine(FILE *theFile,char *line,int lineLength,bool *atEOF,bool *overFlow)
/* read a line from the given file into the array line
* if there is a read error, return false
* lineLength is considered to be the maximum number of characters
* to read into the line (including the 0 terminator)
* if the line fills before a CR is hit, overFlow will be set true, and the rest of the
* line will be read, but not stored
* if the EOF is hit, atEOF is set true
*/
{
int
i;
UBYTE
c;
bool
hadError;
ULONG
numRead;
bool
stopReading;
i=0; /* index into output line */
stopReading=hadError=(*atEOF)=(*overFlow)=false;
while(!stopReading)
{
if(ReadFile(theFile,&c,(ULONG)1,&numRead)) /* get character from input */
{
if(numRead) /* if something was read, check it, and add to output line */
{
if(c=='\n'||c=='\0') /* found termination? */
{
stopReading=true; /* yes, output line is complete */
}
else
{
if(c!=LF&&c!=CR) /* see if the character should be added to the line */
{
if(i<lineLength-1) /* make sure there is room to store it */
{
line[i++]=c; /* store it if there is room */
}
else
{
(*overFlow)=true; /* complain of overflow, but continue to the end */
}
}
}
}
else
{
stopReading=true;
(*atEOF)=true; /* tell caller that EOF was encountered */
}
}
else
{
stopReading=true;
hadError=true;
}
}
if(lineLength)
{
line[i]='\0'; /* terminate the line if possible */
}
return(!hadError); /* return error status */
}
|