Main Page   Compound List   File List   Compound Members   File Members  

OpenFile Class Reference

#include <openfile.h>

List of all members.

Public Methods

 OpenFile (int sector)
 ~OpenFile ()
void Seek (int position)
int Read (char *into, int numBytes)
int Write (char *from, int numBytes)
int ReadAt (char *into, int numBytes, int position)
int WriteAt (char *from, int numBytes, int position)
int Length ()

Private Attributes

FileHeaderhdr
int seekPosition


Constructor & Destructor Documentation

OpenFile::OpenFile int    sector
 

Definition at line 31 of file openfile.cc.

References FileHeader::FetchFrom, hdr, and seekPosition.

00032 { 
00033     hdr = new FileHeader;
00034     hdr->FetchFrom(sector);
00035     seekPosition = 0;
00036 }

OpenFile::~OpenFile  
 

Definition at line 43 of file openfile.cc.

References hdr.

00044 {
00045     delete hdr;
00046 }


Member Function Documentation

int OpenFile::Length  
 

Definition at line 195 of file openfile.cc.

References FileHeader::FileLength, and hdr.

00196 { 
00197     return hdr->FileLength(); 
00198 }

int OpenFile::Read char *    into,
int    numBytes
 

Definition at line 76 of file openfile.cc.

References ReadAt, and seekPosition.

Referenced by FileRead, and Print.

00077 {
00078    int result = ReadAt(into, numBytes, seekPosition);
00079    seekPosition += result;
00080    return result;
00081 }

int OpenFile::ReadAt char *    into,
int    numBytes,
int    position
 

Definition at line 118 of file openfile.cc.

References bcopy, FileHeader::ByteToSector, DEBUG, divRoundDown, FileHeader::FileLength, hdr, and SectorSize.

Referenced by AddrSpace::AddrSpace, Directory::FetchFrom, BitMap::FetchFrom, Read, and WriteAt.

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;                               // check request
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     // read in all the full and partial sectors that we need
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     // copy the part we want
00142     bcopy(&buf[position - (firstSector * SectorSize)], into, numBytes);
00143     delete [] buf;
00144     return numBytes;
00145 }

void OpenFile::Seek int    position
 

Definition at line 57 of file openfile.cc.

References seekPosition.

00058 {
00059     seekPosition = position;
00060 }       

int OpenFile::Write char *    from,
int    numBytes
 

Definition at line 84 of file openfile.cc.

References seekPosition, and WriteAt.

Referenced by Copy, and FileWrite.

00085 {
00086    int result = WriteAt(into, numBytes, seekPosition);
00087    seekPosition += result;
00088    return result;
00089 }

int OpenFile::WriteAt char *    from,
int    numBytes,
int    position
 

Definition at line 148 of file openfile.cc.

References bcopy, FileHeader::ByteToSector, DEBUG, divRoundDown, FileHeader::FileLength, hdr, ReadAt, and SectorSize.

Referenced by Write, Directory::WriteBack, and BitMap::WriteBack.

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;                               // check request
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 // read in first and last sector, if they are to be partially modified
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 // copy in the bytes we want to change 
00179     bcopy(from, &buf[position - (firstSector * SectorSize)], numBytes);
00180 
00181 // write modified sectors back
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 }


Member Data Documentation

FileHeader* OpenFile::hdr [private]
 

Definition at line 90 of file openfile.h.

Referenced by Length, OpenFile, ReadAt, WriteAt, and ~OpenFile.

int OpenFile::seekPosition [private]
 

Definition at line 91 of file openfile.h.

Referenced by OpenFile, Read, Seek, and Write.


The documentation for this class was generated from the following files:
Generated on Mon Feb 10 09:54:57 2003 for nachos by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002
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