Logo
~Sockets~
~Examples~
~Contact~


File Class Reference
[File handling]

IFile implementation of a disk file. More...

#include <File.h>

Inheritance diagram for File:
Collaboration diagram for File:

List of all members.


Public Member Functions

 File ()
 File (FILE *)
 File (const std::string &path, const std::string &mode)
 convenience: calls fopen()
 ~File ()
bool fopen (const std::string &path, const std::string &mode)
void fclose () const
size_t fread (char *, size_t, size_t) const
size_t fwrite (const char *, size_t, size_t)
char * fgets (char *, int) const
void fprintf (const char *format,...)
off_t size () const
bool eof () const
void reset_read () const
void reset_write ()
const std::string & Path () const

Private Member Functions

 File (const File &)
Fileoperator= (const File &)

Private Attributes

std::string m_path
std::string m_mode
FILE * m_fil
bool m_b_close
long m_rptr
long m_wptr

Detailed Description

IFile implementation of a disk file.

Definition at line 46 of file File.h.


Constructor & Destructor Documentation

File::File (  ) 

Definition at line 43 of file File.cpp.

00044 :m_fil(NULL)
00045 ,m_b_close(true)
00046 ,m_rptr(0)
00047 ,m_wptr(0)
00048 {
00049 }

File::File ( FILE *  fil  ) 

Definition at line 52 of file File.cpp.

00053 :m_fil(fil)
00054 ,m_b_close(false)
00055 ,m_rptr(0)
00056 ,m_wptr(0)
00057 {
00058 }

File::File ( const std::string &  path,
const std::string &  mode 
)

convenience: calls fopen()

Definition at line 61 of file File.cpp.

References fopen().

00062 :m_fil(NULL)
00063 ,m_b_close(true)
00064 ,m_rptr(0)
00065 ,m_wptr(0)
00066 {
00067         fopen(path, mode);
00068 }

File::~File (  ) 

Definition at line 71 of file File.cpp.

References fclose(), and m_b_close.

00072 {
00073         if (m_b_close)
00074         {
00075                 fclose();
00076         }
00077 }

File::File ( const File  )  [inline, private]

Definition at line 73 of file File.h.

00073 {} // copy constructor


Member Function Documentation

bool File::fopen ( const std::string &  path,
const std::string &  mode 
) [virtual]

Implements IFile.

Definition at line 80 of file File.cpp.

References m_fil, m_mode, and m_path.

Referenced by File().

00081 {
00082         m_path = path;
00083         m_mode = mode;
00084 #if defined( _WIN32) && !defined(__CYGWIN__)
00085         if (fopen_s(&m_fil, path.c_str(), mode.c_str()))
00086                 m_fil = NULL;
00087 #else
00088         m_fil = ::fopen(path.c_str(), mode.c_str());
00089 #endif
00090 
00091         return m_fil ? true : false;
00092 }

void File::fclose (  )  const [virtual]

Implements IFile.

Definition at line 95 of file File.cpp.

References m_fil.

Referenced by ~File().

00096 {
00097         if (m_fil)
00098         {
00099 		::fclose(m_fil);
00100                 m_fil = NULL;
00101         }
00102 }

size_t File::fread ( char *  ptr,
size_t  size,
size_t  nmemb 
) const [virtual]

Implements IFile.

Definition at line 106 of file File.cpp.

References m_fil, and m_rptr.

Referenced by MemFile::MemFile().

00107 {
00108         size_t r = 0;
00109         if (m_fil)
00110         {
00111                 fseek(m_fil, m_rptr, SEEK_SET);
00112                 r = ::fread(ptr, size, nmemb, m_fil);
00113                 m_rptr = ftell(m_fil);
00114         }
00115         return r;
00116 }

size_t File::fwrite ( const char *  ptr,
size_t  size,
size_t  nmemb 
) [virtual]

Implements IFile.

Definition at line 119 of file File.cpp.

References m_fil, and m_wptr.

00120 {
00121         size_t r = 0;
00122         if (m_fil)
00123         {
00124                 fseek(m_fil, m_wptr, SEEK_SET);
00125                 r = ::fwrite(ptr, size, nmemb, m_fil);
00126                 m_wptr = ftell(m_fil);
00127         }
00128         return r;
00129 }

char * File::fgets ( char *  s,
int  size 
) const [virtual]

Implements IFile.

Definition at line 133 of file File.cpp.

References m_fil, and m_rptr.

00134 {
00135         char *r = NULL;
00136         if (m_fil)
00137         {
00138                 fseek(m_fil, m_rptr, SEEK_SET);
00139                 r = ::fgets(s, size, m_fil);
00140                 m_rptr = ftell(m_fil);
00141         }
00142         return r;
00143 }

void File::fprintf ( const char *  format,
  ... 
) [virtual]

Implements IFile.

Definition at line 146 of file File.cpp.

References m_fil, and m_wptr.

00147 {
00148         if (!m_fil)
00149                 return;
00150         va_list ap;
00151         va_start(ap, format);
00152         fseek(m_fil, m_wptr, SEEK_SET);
00153         vfprintf(m_fil, format, ap);
00154         m_wptr = ftell(m_fil);
00155         va_end(ap);
00156 }

off_t File::size (  )  const [virtual]

Implements IFile.

Definition at line 159 of file File.cpp.

References m_path.

00160 {
00161         struct stat st;
00162         if (stat(m_path.c_str(), &st) == -1)
00163         {
00164                 return 0;
00165         }
00166         return st.st_size;
00167 }

bool File::eof (  )  const [virtual]

Implements IFile.

Definition at line 170 of file File.cpp.

References m_fil.

00171 {
00172         if (m_fil)
00173         {
00174                 if (feof(m_fil))
00175                         return true;
00176         }
00177         return false;
00178 }

void File::reset_read (  )  const [virtual]

Implements IFile.

Definition at line 181 of file File.cpp.

References m_rptr.

00182 {
00183         m_rptr = 0;
00184 }

void File::reset_write (  )  [virtual]

Implements IFile.

Definition at line 187 of file File.cpp.

References m_wptr.

00188 {
00189         m_wptr = 0;
00190 }

const std::string & File::Path (  )  const [virtual]

Implements IFile.

Definition at line 193 of file File.cpp.

References m_path.

00194 {
00195         return m_path;
00196 }

File& File::operator= ( const File  )  [inline, private]

Definition at line 74 of file File.h.

00074 { return *this; } // assignment operator


Member Data Documentation

std::string File::m_path [private]

Definition at line 76 of file File.h.

Referenced by fopen(), Path(), and size().

std::string File::m_mode [private]

Definition at line 77 of file File.h.

Referenced by fopen().

FILE* File::m_fil [mutable, private]

Definition at line 78 of file File.h.

Referenced by eof(), fclose(), fgets(), fopen(), fprintf(), fread(), and fwrite().

bool File::m_b_close [private]

Definition at line 79 of file File.h.

Referenced by ~File().

long File::m_rptr [mutable, private]

Definition at line 80 of file File.h.

Referenced by fgets(), fread(), and reset_read().

long File::m_wptr [private]

Definition at line 81 of file File.h.

Referenced by fprintf(), fwrite(), and reset_write().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4