00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "copyright.h"
00015 #include "filehdr.h"
00016 #include "openfile.h"
00017 #include "system.h"
00018
00019 extern "C" {
00020 int bcopy(char*, char *, int);
00021 };
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 OpenFile::OpenFile(int sector)
00032 {
00033 hdr = new FileHeader;
00034 hdr->FetchFrom(sector);
00035 seekPosition = 0;
00036 }
00037
00038
00039
00040
00041
00042
00043 OpenFile::~OpenFile()
00044 {
00045 delete hdr;
00046 }
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 void
00057 OpenFile::Seek(int position)
00058 {
00059 seekPosition = position;
00060 }
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 int
00076 OpenFile::Read(char *into, int numBytes)
00077 {
00078 int result = ReadAt(into, numBytes, seekPosition);
00079 seekPosition += result;
00080 return result;
00081 }
00082
00083 int
00084 OpenFile::Write(char *into, int numBytes)
00085 {
00086 int result = WriteAt(into, numBytes, seekPosition);
00087 seekPosition += result;
00088 return result;
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 int
00118 OpenFile::ReadAt(char *into, int numBytes, int position)
00119 {
00120 int fileLength = hdr->FileLength();
00121 int i, firstSector, lastSector, numSectors;
00122 char *buf;
00123
00124 if ((numBytes <= 0) || (position >= fileLength))
00125 return 0;
00126 if ((position + numBytes) > fileLength)
00127 numBytes = fileLength - position;
00128 DEBUG('f', "Reading %d bytes at %d, from file of length %d.\n",
00129 numBytes, position, fileLength);
00130
00131 firstSector = divRoundDown(position, SectorSize);
00132 lastSector = divRoundDown(position + numBytes - 1, SectorSize);
00133 numSectors = 1 + lastSector - firstSector;
00134
00135
00136 buf = new char[numSectors * SectorSize];
00137 for (i = firstSector; i <= lastSector; i++)
00138 synchDisk->ReadSector(hdr->ByteToSector(i * SectorSize),
00139 &buf[(i - firstSector) * SectorSize]);
00140
00141
00142 bcopy(&buf[position - (firstSector * SectorSize)], into, numBytes);
00143 delete [] buf;
00144 return numBytes;
00145 }
00146
00147 int
00148 OpenFile::WriteAt(char *from, int numBytes, int position)
00149 {
00150 int fileLength = hdr->FileLength();
00151 int i, firstSector, lastSector, numSectors;
00152 bool firstAligned, lastAligned;
00153 char *buf;
00154
00155 if ((numBytes <= 0) || (position >= fileLength))
00156 return 0;
00157 if ((position + numBytes) > fileLength)
00158 numBytes = fileLength - position;
00159 DEBUG('f', "Writing %d bytes at %d, from file of length %d.\n",
00160 numBytes, position, fileLength);
00161
00162 firstSector = divRoundDown(position, SectorSize);
00163 lastSector = divRoundDown(position + numBytes - 1, SectorSize);
00164 numSectors = 1 + lastSector - firstSector;
00165
00166 buf = new char[numSectors * SectorSize];
00167
00168 firstAligned = (position == (firstSector * SectorSize));
00169 lastAligned = ((position + numBytes) == ((lastSector + 1) * SectorSize));
00170
00171
00172 if (!firstAligned)
00173 ReadAt(buf, SectorSize, firstSector * SectorSize);
00174 if (!lastAligned && ((firstSector != lastSector) || firstAligned))
00175 ReadAt(&buf[(lastSector - firstSector) * SectorSize],
00176 SectorSize, lastSector * SectorSize);
00177
00178
00179 bcopy(from, &buf[position - (firstSector * SectorSize)], numBytes);
00180
00181
00182 for (i = firstSector; i <= lastSector; i++)
00183 synchDisk->WriteSector(hdr->ByteToSector(i * SectorSize),
00184 &buf[(i - firstSector) * SectorSize]);
00185 delete [] buf;
00186 return numBytes;
00187 }
00188
00189
00190
00191
00192
00193
00194 int
00195 OpenFile::Length()
00196 {
00197 return hdr->FileLength();
00198 }
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees