Make your own free website on Tripod.com
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

file.cpp

Go to the documentation of this file.
00001 /*
00002 PeRdr - PE file disassembler
00003 Copyright (C) 1999-2002 Frediano Ziglio
00004 -----
00005 
00006 This program is free software; you can redistribute it and/or modify
00007 it under the terms of the GNU General Public License as published by
00008 the Free Software Foundation; either version 2 of the License, or
00009 (at your option) any later version.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with this program; if not, write to the Free Software
00018 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 -----
00020 
00021 INFORMATION
00022   www: http://freddy77.tripod.com/perdr/
00023   e-mail: freddy77@angelfire.com
00024 */
00025 #include "global.h"
00026 #ifdef HAVE_HDRSTOP
00027 #pragma hdrstop
00028 #endif
00029 
00030 #include <stdexcept>
00031 #include "file.hpp"
00032 
00033 using namespace std;
00034 
00035 CFile::CFile(const char* filename,const char* mode):
00036   file(NULL)
00037 {
00038   file = fopen(filename,mode);
00039   if (file == NULL)
00040     throw runtime_error("Unable to open file");
00041 }
00042 
00043 void CFile::Close()
00044 {
00045   if (file)
00046     fclose(file);
00047   file = NULL;
00048 }
00049 
00050 void CFile::Seek(long offset,int whence) const
00051 {
00052   if ( fseek(file,offset,whence)!=0 )
00053     throw runtime_error("Unable to seek");
00054 }
00055 
00056 long CFile::Tell() const
00057 {
00058   long pos = ftell(file);
00059   if ( pos ==  -1L )
00060     throw runtime_error("Seek error");
00061   return pos;
00062 }
00063 
00064 char *CFile::ReadString() const
00065 {
00066   char *pbegin = new char[16];
00067   char *p      = pbegin;
00068   char *pend   = pbegin + 16;
00069   int c;
00070   for (;;)
00071   {
00072     c = fgetc(file);
00073     if (c==EOF)
00074       throw runtime_error("Error reading string");
00075     if (p==pend) {
00076       int size = (pend-pbegin);
00077       pend = new char[size+16];
00078       memcpy(pend,pbegin,size*sizeof(char));
00079       delete [] pbegin;
00080       pbegin = pend;
00081       pend = (p = pbegin + size) + 16;
00082     }
00083     *p++ = (char)c;
00084     if (c==0)
00085       return pbegin;
00086   }
00087 }
00088 
00089 // !!! problem with portability ?? See charBit and uint16 conversion
00090 char *CFile::ReadStringFlag(unsigned flag) const
00091 {
00092   unsigned char charBit = flag & strUcs2 ? 1:0;
00093   char *pbegin = new char[16<<charBit];
00094   char *p      = pbegin;
00095   char *pend   = pbegin + (16<<charBit);
00096   int c,cc;
00097   unsigned len = ~0u;
00098 
00099   uint32_t ui32;
00100   uint16_t ui16;
00101   uint8_t ui8;
00102 
00103   switch (flag & strLenMask)
00104   {
00105   case strLen8:  RawRead(&ui8, sizeof(ui8) ); len = ui8;  break;
00106   case strLen16: RawRead(&ui16,sizeof(ui16)); len = ui16; break;
00107   case strLen32: RawRead(&ui32,sizeof(ui32)); len = ui32; break;
00108   }
00109 
00110   cc = 0;
00111   for (;;--len)
00112   {
00113     // read char
00114     c = fgetc(file);
00115     if (c==EOF)
00116       throw runtime_error("Error reading string");
00117     if (charBit)
00118     {
00119       cc = fgetc(file);
00120       if (cc==EOF)
00121         throw runtime_error("Error reading string");
00122     }
00123 
00124     // extend buffer if needed
00125     if (p==pend) {
00126       int size = (pend-pbegin);
00127       pend = new char[size+(16<<charBit)];
00128       memcpy(pend,pbegin,size*sizeof(char));
00129       delete [] pbegin;
00130       pbegin = pend;
00131       pend = (p = pbegin + size) + (16<<charBit);
00132     }
00133 
00134     // check end string
00135     if (len == 0)
00136     {
00137       *p++ = 0;
00138       if (charBit)
00139         *p++ = 0;
00140       break;
00141     }
00142 
00143     // write char
00144     *p++ = (char)c;
00145     if (charBit)
00146       *p++ = (char)cc;
00147 
00148     if ( (flag&strLenMask) == 0 )
00149     {
00150       if ( c==0 && cc==0 )
00151         break;
00152     }
00153   }
00154 
00155   return pbegin;
00156 }
00157 
00158 void CFile::RawRead(void *ptr,size_t size) const
00159 {
00160   if ( fread(ptr,1,size,file) != size )
00161     throw runtime_error("Unable to read from file");
00162 }
00163 
00164 void CFile::RawWrite(const void *ptr,size_t size)
00165 {
00166   if ( fwrite(ptr,1,size,file) != size )
00167     throw runtime_error("Unable to write to file");
00168 }
00169 
00170 CFile::~CFile()
00171 {
00172   Close();
00173 }

Generated on Mon Jan 13 22:20:34 2003 for perdr by doxygen1.2.15