ArchiveFile.cpp
Go to the documentation of this file.00001 //*** ArchiveFile.cpp *** 00002 00003 #include "ArchiveFile.h" 00004 #include "StaticBuffer.h" 00005 #include "Platform.h" 00006 #include "Platform_FileSystem.h" 00007 #include "Platform_FileSystem_File.h" 00008 #include "Debug.h" 00009 00010 //*** Constructor *** 00011 00012 ArchiveFile::ArchiveFile(Platform_FileSystem_File* file, int offset, int size, bool sharedFile): 00013 file_(file), 00014 buffer_(0), 00015 position_(0), 00016 offset_(offset), 00017 size_(size), 00018 sharedFile_(sharedFile) 00019 { 00020 00021 } 00022 00023 00024 //*** Constructor *** 00025 00026 ArchiveFile::ArchiveFile(const StaticBuffer* memoryBuffer, int offset, int size): 00027 file_(0), 00028 buffer_(memoryBuffer), 00029 position_(0), 00030 offset_(offset), 00031 size_(size), 00032 sharedFile_(false) 00033 { 00034 00035 } 00036 00037 00038 //*** Copy Constructor *** 00039 00040 ArchiveFile::ArchiveFile(const ArchiveFile* archiveFile): 00041 file_(archiveFile->file_), 00042 buffer_(archiveFile->buffer_), 00043 position_(archiveFile->position_), 00044 offset_(archiveFile->offset_), 00045 size_(archiveFile->size_), 00046 sharedFile_(archiveFile->sharedFile_) 00047 { 00048 if (!sharedFile_ && file_) 00049 { 00050 00051 file_=Platform::GetPlatform_FileSystem()->CreateFileObject(archiveFile->file_->GetPath()); 00052 file_->Open(); 00053 } 00054 } 00055 00056 00057 //*** Destructor *** 00058 00059 ArchiveFile::~ArchiveFile() 00060 { 00061 if (file_ && !sharedFile_) 00062 { 00063 delete file_; 00064 } 00065 00066 } 00067 00068 00069 //*** Create *** 00070 00071 bool ArchiveFile::Open() const 00072 { 00073 position_=0; 00074 return true; 00075 } 00076 00077 00078 //*** Close *** 00079 00080 bool ArchiveFile::Close() const 00081 { 00082 position_=-1; 00083 return true; 00084 } 00085 00086 00087 //*** GetSize *** 00088 00089 int ArchiveFile::GetSize() const 00090 { 00091 return size_; 00092 } 00093 00094 00095 //*** Seek *** 00096 00097 int ArchiveFile::Seek(int offset, SeekOrigin origin) const 00098 { 00099 switch (origin) 00100 { 00101 case SEEK_FROM_START: 00102 { 00103 position_=offset; 00104 return position_; 00105 } break; 00106 00107 case SEEK_FROM_CURRENT: 00108 { 00109 position_+=offset; 00110 return position_; 00111 } break; 00112 00113 case SEEK_FROM_END: 00114 { 00115 position_=size_+offset; 00116 return position_; 00117 } break; 00118 } 00119 00120 Assert(false,"Invalid seek origin"); 00121 return -1; 00122 } 00123 00124 00125 //*** Tell *** 00126 00127 int ArchiveFile::Tell() const 00128 { 00129 return position_; 00130 } 00131 00132 00133 //*** READMACRO *** 00134 00135 // This macro defines the code that is to be executed for all of the overloaded Read methods 00136 // The code is identical for all the different Read methods, only the input parameters change, 00137 // so it makes more sense to use a macro rather than copy-paste. This macro is undefined as 00138 // soon as it is not needed anymore 00139 00140 #define READMACRO() \ 00141 /* Calculate the total number of bytes to be read */ \ 00142 int totalSize=sizeof(*value)*count; \ 00143 /* Make sure we're not trying to read more bytes than there is */ \ 00144 if (position_+totalSize>size_) \ 00145 { \ 00146 count=(size_-position_)/sizeof(*value); \ 00147 totalSize=sizeof(*value)*count; \ 00148 } \ 00149 /* Read the actual data */ \ 00150 if (buffer_) \ 00151 { \ 00152 buffer_->SetPosition(offset_+position_); \ 00153 buffer_->Read(value,count); \ 00154 } \ 00155 if (file_) \ 00156 { \ 00157 file_->SetPosition(offset_+position_); \ 00158 file_->Read(value,count); \ 00159 } \ 00160 /* Update the current position */ \ 00161 position_+=totalSize; \ 00162 /* Return the number of elements written */ \ 00163 return count; \ 00164 00165 00166 //** Read methods */ 00167 00168 int ArchiveFile::Read(char* value, int count) const 00169 { 00170 READMACRO(); 00171 } 00172 00173 int ArchiveFile::Read(short* value, int count) const 00174 { 00175 READMACRO(); 00176 } 00177 00178 int ArchiveFile::Read(int* value, int count) const 00179 { 00180 READMACRO(); 00181 } 00182 00183 int ArchiveFile::Read(long* value, int count) const 00184 { 00185 READMACRO(); 00186 } 00187 00188 int ArchiveFile::Read(unsigned char* value, int count) const 00189 { 00190 READMACRO(); 00191 } 00192 00193 int ArchiveFile::Read(unsigned short* value, int count) const 00194 { 00195 READMACRO(); 00196 } 00197 00198 int ArchiveFile::Read(unsigned int* value, int count) const 00199 { 00200 READMACRO(); 00201 } 00202 00203 int ArchiveFile::Read(unsigned long* value, int count) const 00204 { 00205 READMACRO(); 00206 } 00207 00208 int ArchiveFile::Read(float* value, int count) const 00209 { 00210 READMACRO(); 00211 } 00212 00213 int ArchiveFile::Read(double* value, int count) const 00214 { 00215 READMACRO(); 00216 } 00217 00218 int ArchiveFile::Read(bool* value, int count) const 00219 { 00220 READMACRO(); 00221 } 00222 00223 // No need for the read macro anymore, so undefine it 00224 #undef READMACRO
Reproduction/republishing of any material on this site without permission is strictly prohibited.
