Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

fileclasses.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++ -*-
00002    
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2003 Koblenz University
00006    $Id: fileclasses.cpp,v 1.5 2003/09/10 00:30:10 tomhoward Exp $
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; version 2 of the License.
00011   
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016  
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020 */
00021 
00022 #include "fileclasses.h"
00023 #include <cstring>
00024 
00025 namespace salt
00026 {
00027 /*static long fsize(FILE *f)
00028 {
00029     long savepos, end;
00030     
00031     savepos = ftell(f);
00032     fseek(f, 0, SEEK_END);
00033     end = ftell(f);
00034     fseek(f, savepos, SEEK_SET);
00035     
00036     return end;
00037 }*/
00038 }
00039 
00040 using namespace salt;
00041 
00042 
00043 //------------------------------------------------------------------------------------------------
00044 // MemFile
00045 //------------------------------------------------------------------------------------------------
00046 
00047 MemFile::MemFile(const char *fn, char *mode)
00048 {
00049     if(fn==NULL)
00050     {
00051         mHandle     = NULL;
00052         mCharHandle = NULL;
00053         mSize       = 0;
00054         mPosition   = 0;
00055     }
00056     else
00057     {
00058         Open(fn, mode);
00059     }
00060 }
00061 
00062 MemFile::MemFile(FILE *f)
00063 {
00064   fseek(f,0,SEEK_END);
00065   mSize = ftell(f);
00066   rewind(f);
00067 
00068     mHandle     = new unsigned char[mSize];
00069     mCharHandle = (unsigned char*)mHandle;
00070     mPosition   = 0;
00071     fread(mHandle,1,mSize,f);
00072 }
00073 
00074 MemFile::MemFile(RFile *f)
00075 {
00076     mSize=f->Size();
00077     mHandle=new unsigned char[mSize];
00078     mCharHandle=(unsigned char*)mHandle;
00079     f->Read(mHandle,mSize);
00080     mPosition=0;
00081 }
00082 
00083 MemFile::~MemFile()
00084 {
00085     if(mHandle!=NULL)
00086         Destroy();
00087 }
00088 
00089 bool MemFile::Open(const char *fn, char *mode)
00090 {
00091     if(fn==NULL)
00092         return false;
00093     else
00094     {
00095         StdFile* f = new StdFile(fn, mode);
00096         if(f->GetHandle())
00097         {
00098             mSize       = f->Size();
00099             mCharHandle = new unsigned char[mSize];
00100             mHandle     = mCharHandle;
00101             mPosition   = 0;
00102             f->Read(mHandle, mSize);
00103             delete f;
00104             return true;
00105         }
00106         else
00107         {
00108             delete f;
00109             return false;
00110         }
00111     }
00112 }
00113 
00114 bool MemFile::Open(void *buffer, long s)
00115 {
00116     mHandle     = buffer;
00117     mCharHandle = (unsigned char*)mHandle;
00118     mSize       = s;
00119     mPosition   = 0;
00120     return true;
00121 }
00122 
00123 void MemFile::Close()
00124 {
00125     Destroy();
00126 }
00127 
00128 void MemFile::Destroy()
00129 {
00130     if(mHandle!=NULL)
00131     {
00132       delete mCharHandle;
00133         mHandle     = NULL;
00134         mCharHandle = NULL;
00135         mSize       = 0;
00136         mPosition   = 0;
00137     }
00138 }
00139 
00140 int MemFile::Eof()
00141 {
00142     return (mPosition>=mSize);
00143 }
00144 
00145 long MemFile::Tell()
00146 {
00147     return mPosition;
00148 }
00149 
00150 int MemFile::GetPos(long *pos)
00151 {
00152     *pos=Tell();
00153     return 1;
00154 }
00155 
00156 int MemFile::Seek(long offset, int origin)
00157 {
00158     switch(origin){
00159         case SEEK_SET:
00160             return (mPosition=offset);
00161             break;
00162         case SEEK_END:
00163             return (mPosition=mSize-1-offset);
00164             break;
00165         default:
00166             return (mPosition+=offset);
00167             break;
00168     }
00169 }
00170 
00171 void MemFile::Rewind()
00172 {
00173     mPosition=0;
00174 }
00175 
00176 long MemFile::Size()
00177 {
00178     return mSize;
00179 }
00180 
00181 char* MemFile::Gets(char* buffer, int n)
00182 {
00183     int i       = 0;
00184     bool done   = false;
00185 
00186     if(Eof()) return NULL;
00187 
00188     while(!done)
00189     {
00190         buffer[i]=mCharHandle[mPosition];
00191         mPosition++;
00192         i++;
00193         if((i==n)||(buffer[i-1]=='\n')||(Eof())) done=true;
00194     }
00195     buffer[i]=0;
00196     return buffer;
00197 }
00198 
00199 int MemFile::Getc()
00200 {
00201     mPosition++;
00202     if((mPosition-1)>=mSize)
00203         return EOF;
00204     else
00205         return (int)(mCharHandle[(mPosition-1)]);
00206 }
00207 
00208 size_t MemFile::Read(void *buffer, size_t size, size_t count)
00209 {
00210     long oldPos = mPosition;
00211 
00212     mPosition += (size*count);
00213     if(mPosition >= mSize)
00214     {
00215         memcpy(buffer, &mCharHandle[oldPos], (mSize-oldPos));
00216         return (mSize-oldPos);
00217     }
00218     else
00219     {
00220         memcpy(buffer, &mCharHandle[oldPos], size*count);
00221         return (count);
00222     }
00223 }
00224 
00225 //------------------------------------------------------------------------------------------------
00226 // StdFile
00227 //------------------------------------------------------------------------------------------------
00228 
00229 StdFile::StdFile(FILE* h)
00230 {
00231     //Make sure object is free
00232     Destroy();
00233     //Assign new mHandle
00234     mHandle=h;
00235 }
00236 
00237 StdFile::StdFile(const char* fn,char* mode)
00238 {
00239     mHandle=NULL;
00240     //open file
00241     Open(fn,mode);
00242 }
00243 
00244 StdFile::~StdFile()
00245 {
00246     //Free up memory
00247     Destroy();
00248 }
00249 
00250 void StdFile::Destroy()
00251 {
00252     //Close file
00253     Close();
00254 }
00255 
00256 void StdFile::Close()
00257 {
00258     //Close file
00259     if(mHandle!=NULL)
00260         fclose((FILE*)mHandle);
00261 
00262     mHandle = NULL;
00263 }
00264 
00265 bool StdFile::Open(const char* fileName,char* mode)
00266 {
00267     //Open fn with mode mode
00268     if(fileName==NULL)
00269         return false;
00270     else
00271     {
00272         mHandle=fopen(fileName, mode);
00273         if(mHandle==NULL)
00274             return false;
00275         else
00276             return true;
00277     }
00278 }
00279 
00280 int StdFile::Eof()
00281 {
00282     return feof((FILE*)mHandle);
00283 }
00284 
00285 long StdFile::Tell()
00286 {
00287     return ftell((FILE*)mHandle);
00288 }
00289 
00290 long StdFile::Size()
00291 {
00292   long pos = ftell((FILE*)mHandle);
00293   fseek((FILE*)mHandle,0,SEEK_END);
00294   long size = ftell((FILE*)mHandle);
00295   fseek((FILE*)mHandle,pos,SEEK_SET);
00296 return size;
00297 }
00298 
00299 int StdFile::GetPos(long*/* pos*/)
00300 {
00301   return ftell((FILE*)mHandle);
00302 }
00303 
00304 void StdFile::Rewind()
00305 {
00306     rewind((FILE*)mHandle);
00307 }
00308 
00309 char* StdFile::Gets(char* buffer, int n)
00310 {
00311     return fgets(buffer, n, (FILE*)mHandle);
00312 }
00313 
00314 int StdFile::Getc()
00315 {
00316     return fgetc((FILE*)mHandle);
00317 }
00318 
00319 int StdFile::Puts(const char*s)
00320 {
00321     return fputs(s, (FILE*)mHandle);
00322 }
00323 
00324 int StdFile::Putc(int c)
00325 {
00326     return fputc(c, (FILE*)mHandle);
00327 }
00328 
00329 int StdFile::Seek(long offset, int origin)
00330 {
00331     return fseek((FILE*)mHandle, offset, origin);
00332 }
00333 
00334 size_t StdFile::Read(void *buffer,size_t mSize,size_t count)
00335 {
00336     return fread(buffer, mSize, count, (FILE*)mHandle);
00337 } 
00338 
00339 size_t StdFile::Write(void *buffer,size_t mSize,size_t count)
00340 {
00341     return fwrite(buffer, mSize, count, (FILE*)mHandle);
00342 }
00343 
00344 void* StdFile::GetHandle()
00345 {
00346     return (void*) mHandle;
00347 }

Generated on Thu Apr 6 15:25:37 2006 for rcssserver3d by  doxygen 1.4.4