Fixed several obvious omissions. Allow specifying a start address.

git-svn-id: svn://svn.cc65.org/cc65/trunk@570 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz
2000-12-09 10:10:07 +00:00
parent b2b7fb4b33
commit 61a1fa52c4
7 changed files with 89 additions and 11 deletions

View File

@@ -69,6 +69,7 @@ void LoadCode (const char* Name, unsigned long StartAddress)
/* Load the code from the given file */
{
unsigned Count, MaxCount;
long Size;
FILE* F;
@@ -80,18 +81,36 @@ void LoadCode (const char* Name, unsigned long StartAddress)
/* Open the file */
F = fopen (Name, "rb");
if (F == 0) {
Error ("Cannot open `%s': %s", Name, strerror (errno));
Error ("Cannot open `%s': %s", Name, strerror (errno));
}
/* Seek to the end to get the size of the file */
if (fseek (F, 0, SEEK_END) != 0) {
Error ("Cannot seek on file `%s': %s", Name, strerror (errno));
}
Size = ftell (F);
rewind (F);
/* Check if the size is larger than what we can read */
if (Size == 0) {
Error ("File `%s' contains no data", Name);
}
if (Size > MaxCount) {
Warning ("File `%s' is too large, ignoring %ld bytes",
Name, Size - MaxCount);
} else if (MaxCount > Size) {
MaxCount = (unsigned) Size;
}
/* Read from the file and remember the number of bytes read */
Count = fread (CodeBuf + StartAddress, 1, MaxCount, F);
if (ferror (F)) {
Error ("Error reading from `%s': %s", Name, strerror (errno));
}
if (Count == 0) {
Error ("File `%s' contains no data", Name);
if (ferror (F) || Count != MaxCount) {
Error ("Error reading from `%s': %s", Name, strerror (errno));
}
/* Close the file */
fclose (F);
/* Set the buffer variables */
CodeStart = PC = StartAddress;
CodeEnd = CodeStart + Count - 1; /* CodeEnd is inclusive */