Logo
~Sockets~
~Examples~
~Contact~


MemFile Class Reference
[File handling]

Implements a memory file. More...

#include <MemFile.h>

Inheritance diagram for MemFile:

Inheritance graph
[legend]
Collaboration diagram for MemFile:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 MemFile ()
 MemFile (const std::string &path)
 ~MemFile ()
bool fopen (const std::string &path, const std::string &mode)
void fclose ()
size_t fread (char *ptr, size_t size, size_t nmemb) const
size_t fwrite (const char *ptr, size_t size, size_t nmemb)
char * fgets (char *s, int size) const
void fprintf (const char *format,...)
off_t size () const
bool eof () const

Private Member Functions

 MemFile (const MemFile &)
MemFileoperator= (const MemFile &)

Private Attributes

std::string m_path
bool m_temporary
block_tm_base
block_tm_current_read
block_tm_current_write
size_t m_read_ptr
size_t m_write_ptr
bool m_b_read_caused_eof

Static Private Attributes

static std::map< std::string,
block_t * > 
m_files

Classes

struct  block_t
 File block structure. More...

Detailed Description

Implements a memory file.

Definition at line 46 of file MemFile.h.


Constructor & Destructor Documentation

MemFile::MemFile (  ) 

Definition at line 46 of file MemFile.cpp.

00047 :m_temporary(true)
00048 ,m_base(new block_t)
00049 ,m_current_read(m_base)
00050 ,m_current_write(m_base)
00051 ,m_read_ptr(0)
00052 ,m_write_ptr(0)
00053 ,m_b_read_caused_eof(false)
00054 {
00055 }

MemFile::MemFile ( const std::string &  path  ) 

Definition at line 58 of file MemFile.cpp.

References m_base, m_current_read, m_current_write, and m_files.

00059 :m_path(path)
00060 ,m_temporary(false)
00061 ,m_base(m_files[path])
00062 ,m_current_read(NULL)
00063 ,m_current_write(NULL)
00064 ,m_read_ptr(0)
00065 ,m_write_ptr(0)
00066 ,m_b_read_caused_eof(false)
00067 {
00068         if (!m_base)
00069         {
00070                 m_base = new block_t;
00071                 m_files[path] = m_base;
00072         }
00073         m_current_read = m_base;
00074         m_current_write = m_base;
00075 }

MemFile::~MemFile (  ) 

Definition at line 78 of file MemFile.cpp.

References m_base, and m_temporary.

00079 {
00080         while (m_base && m_temporary)
00081         {
00082                 block_t *p = m_base;
00083                 m_base = p -> next;
00084                 delete p;
00085         }
00086 }

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

Definition at line 74 of file MemFile.h.

00074 {} // copy constructor


Member Function Documentation

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

Implements IFile.

Definition at line 89 of file MemFile.cpp.

00090 {
00091         return true;
00092 }

void MemFile::fclose (  )  [virtual]

Implements IFile.

Definition at line 95 of file MemFile.cpp.

00096 {
00097 }

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

Todo:
: fix for reads much larger than BLOCKSIZE

Implements IFile.

Definition at line 101 of file MemFile.cpp.

References BLOCKSIZE, m_b_read_caused_eof, m_current_read, m_read_ptr, and m_write_ptr.

Referenced by fgets().

00102 {
00103         size_t p = m_read_ptr % BLOCKSIZE;
00104         size_t sz = size * nmemb;
00105         size_t available = m_write_ptr - m_read_ptr;
00106         if (sz > available) // read beyond eof
00107         {
00108                 sz = available;
00109                 m_b_read_caused_eof = true;
00110         }
00111         if (!sz)
00112         {
00113                 return 0;
00114         }
00115         if (p + sz < BLOCKSIZE)
00116         {
00117                 memcpy(ptr, m_current_read -> data + p, sz);
00118                 m_read_ptr += sz;
00119         }
00120         else
00121         {
00122                 size_t sz1 = BLOCKSIZE - p;
00123                 size_t sz2 = size - sz1;
00124                 memcpy(ptr, m_current_read -> data + p, sz1);
00125                 m_read_ptr += sz1;
00126                 if (m_current_read -> next)
00127                 {
00128                         m_current_read = m_current_read -> next;
00129                         memcpy(ptr + sz1, m_current_read -> data, sz2);
00130                         m_read_ptr += sz2;
00131                 }
00132                 else
00133                 {
00134                         return sz1;
00135                 }
00136         }
00137         return sz;
00138 }

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

Todo:
: fix for writes that are much larger than BLOCKSIZE

Implements IFile.

Definition at line 142 of file MemFile.cpp.

References BLOCKSIZE, m_current_write, and m_write_ptr.

Referenced by fprintf().

00143 {
00144         size_t p = m_write_ptr % BLOCKSIZE;
00145         size_t sz = size * nmemb;
00146         if (p + sz <= BLOCKSIZE)
00147         {
00148                 memcpy(m_current_write -> data + p, ptr, sz);
00149                 m_write_ptr += sz;
00150         }
00151         else
00152         {
00153                 size_t sz1 = BLOCKSIZE - p; // size left
00154                 size_t sz2 = sz - sz1;
00155                 memcpy(m_current_write -> data + p, ptr, sz1);
00156                 block_t *next = new block_t;
00157                 m_current_write -> next = next;
00158                 m_current_write = next;
00159                 memcpy(m_current_write -> data, ptr + sz1, sz2);
00160                 m_write_ptr += sz;
00161         }
00162         return sz;
00163 }

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

Implements IFile.

Definition at line 167 of file MemFile.cpp.

References eof(), and fread().

00168 {
00169         int n = 0;
00170         while (n < size - 1 && !eof())
00171         {
00172                 char c;
00173                 size_t sz = fread(&c, 1, 1);
00174                 if (sz)
00175                 {
00176                         if (c == 10)
00177                         {
00178                                 s[n] = 0;
00179                                 return s;
00180                         }
00181                         s[n++] = c;
00182                 }
00183         }
00184         s[n] = 0;
00185         return s;
00186 }

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

Implements IFile.

Definition at line 189 of file MemFile.cpp.

References BLOCKSIZE, and fwrite().

00190 {
00191         va_list ap;
00192         char tmp[BLOCKSIZE];
00193         va_start(ap, format);
00194 #ifdef _WIN32
00195         vsprintf(tmp, format, ap);
00196 #else
00197         vsnprintf(tmp, BLOCKSIZE - 1, format, ap);
00198 #endif
00199         va_end(ap);
00200         fwrite(tmp, 1, strlen(tmp));
00201 }

off_t MemFile::size (  )  const [virtual]

Implements IFile.

Definition at line 204 of file MemFile.cpp.

References m_write_ptr.

00205 {
00206         return (off_t)m_write_ptr;
00207 }

bool MemFile::eof (  )  const [virtual]

Implements IFile.

Definition at line 210 of file MemFile.cpp.

References m_b_read_caused_eof.

Referenced by fgets().

00211 {
00212         return m_b_read_caused_eof; //(m_read_ptr < m_write_ptr) ? false : true;
00213 }

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

Definition at line 75 of file MemFile.h.

00075 { return *this; } // assignment operator


Member Data Documentation

std::map< std::string, MemFile::block_t * > MemFile::m_files [static, private]

Definition at line 77 of file MemFile.h.

Referenced by MemFile().

std::string MemFile::m_path [private]

Definition at line 78 of file MemFile.h.

bool MemFile::m_temporary [private]

Definition at line 79 of file MemFile.h.

Referenced by ~MemFile().

Definition at line 80 of file MemFile.h.

Referenced by MemFile(), and ~MemFile().

block_t* MemFile::m_current_read [mutable, private]

Definition at line 81 of file MemFile.h.

Referenced by fread(), and MemFile().

Definition at line 82 of file MemFile.h.

Referenced by fwrite(), and MemFile().

size_t MemFile::m_read_ptr [mutable, private]

Definition at line 83 of file MemFile.h.

Referenced by fread().

size_t MemFile::m_write_ptr [private]

Definition at line 84 of file MemFile.h.

Referenced by fread(), fwrite(), and size().

bool MemFile::m_b_read_caused_eof [mutable, private]

Definition at line 85 of file MemFile.h.

Referenced by eof(), and fread().


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