![]() |
MemFile Class ReferenceImplements a memory file.
More...
|
Public Member Functions | |
MemFile () | |
Create temporary memory buffer, will be freed when object destructs. | |
MemFile (MemFile &) | |
Copy buffer from source, reset read ptr. | |
MemFile (File &f) | |
Read file and write to this, create non-temporary memory buffer from f.Path(). | |
~MemFile () | |
bool | fopen (const std::string &path, const std::string &mode) |
void | fclose () const |
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 |
void | reset_read () const |
void | reset_write () |
int | RefCount () const |
Reference count when copy constructor is used. | |
void | Increase () |
void | Decrease () |
const std::string & | Path () const |
Private Member Functions | |
MemFile & | operator= (const MemFile &) |
Private Attributes | |
MemFile & | m_src |
bool | m_src_valid |
block_t * | m_base |
block_t * | m_current_read |
block_t * | m_current_write |
int | m_current_write_nr |
size_t | m_read_ptr |
size_t | m_write_ptr |
bool | m_b_read_caused_eof |
int | m_ref_count |
bool | m_ref_decreased |
std::string | m_path |
Classes | |
struct | block_t |
File block structure. More... |
Definition at line 49 of file MemFile.h.
MemFile::MemFile | ( | ) |
Create temporary memory buffer, will be freed when object destructs.
Definition at line 51 of file MemFile.cpp.
00052 :m_src(m_src) 00053 ,m_src_valid(false) 00054 ,m_base(new block_t) 00055 ,m_current_read(m_base) 00056 ,m_current_write(m_base) 00057 ,m_current_write_nr(0) 00058 ,m_read_ptr(0) 00059 ,m_write_ptr(0) 00060 ,m_b_read_caused_eof(false) 00061 ,m_ref_count(0) 00062 ,m_ref_decreased(false) 00063 { 00064 }
MemFile::MemFile | ( | MemFile & | s | ) |
Copy buffer from source, reset read ptr.
Definition at line 67 of file MemFile.cpp.
References Increase(), and m_src.
00068 :m_src(s) 00069 ,m_src_valid(true) 00070 ,m_base(s.m_base) 00071 ,m_current_read(m_base) 00072 ,m_current_write(s.m_current_write) 00073 ,m_current_write_nr(s.m_current_write_nr) 00074 ,m_read_ptr(0) 00075 ,m_write_ptr(s.m_write_ptr) 00076 ,m_b_read_caused_eof(false) 00077 ,m_ref_count(0) 00078 ,m_ref_decreased(false) 00079 ,m_path(s.m_path) 00080 { 00081 m_src.Increase(); 00082 }
MemFile::MemFile | ( | File & | f | ) |
Read file and write to this, create non-temporary memory buffer from f.Path().
Definition at line 85 of file MemFile.cpp.
References File::fread(), fwrite(), m_base, m_current_read, and m_current_write.
00086 :m_src(m_src) 00087 ,m_src_valid(false) 00088 ,m_base(new block_t) 00089 ,m_current_read(NULL) 00090 ,m_current_write(NULL) 00091 ,m_current_write_nr(0) 00092 ,m_read_ptr(0) 00093 ,m_write_ptr(0) 00094 ,m_b_read_caused_eof(false) 00095 ,m_ref_count(0) 00096 ,m_ref_decreased(false) 00097 ,m_path(f.Path()) 00098 { 00099 m_current_read = m_base; 00100 m_current_write = m_base; 00101 char slask[32768]; 00102 size_t n; 00103 while ((n = f.fread(slask, 1, 32768)) > 0) 00104 { 00105 fwrite(slask, 1, n); 00106 } 00107 }
MemFile::~MemFile | ( | ) |
Definition at line 110 of file MemFile.cpp.
References Decrease(), Utility::l2string(), m_base, m_ref_count, m_ref_decreased, m_src, and m_src_valid.
00111 { 00112 if (m_ref_count) 00113 std::cerr << "MemFile destructor with a ref count of " + Utility::l2string(m_ref_count) << std::endl; 00114 while (m_base && !m_src_valid) 00115 { 00116 block_t *p = m_base; 00117 m_base = p -> next; 00118 delete p; 00119 } 00120 if (m_src_valid && !m_ref_decreased) 00121 { 00122 m_src.Decrease(); 00123 m_ref_decreased = true; 00124 } 00125 }
bool MemFile::fopen | ( | const std::string & | path, | |
const std::string & | mode | |||
) | [virtual] |
void MemFile::fclose | ( | ) | const [virtual] |
Implements IFile.
Definition at line 134 of file MemFile.cpp.
References Decrease(), m_ref_decreased, m_src, and m_src_valid.
00135 { 00136 if (m_src_valid && !m_ref_decreased) 00137 { 00138 m_src.Decrease(); 00139 m_ref_decreased = true; 00140 } 00141 }
size_t MemFile::fread | ( | char * | ptr, | |
size_t | size, | |||
size_t | nmemb | |||
) | const [virtual] |
Implements IFile.
Definition at line 144 of file MemFile.cpp.
References BLOCKSIZE, m_b_read_caused_eof, m_current_read, m_read_ptr, and m_write_ptr.
Referenced by fgets().
00145 { 00146 size_t p = m_read_ptr % BLOCKSIZE; 00147 size_t sz = size * nmemb; 00148 size_t available = m_write_ptr - m_read_ptr; 00149 if (sz > available) // read beyond eof 00150 { 00151 sz = available; 00152 m_b_read_caused_eof = true; 00153 } 00154 if (!sz) 00155 { 00156 return 0; 00157 } 00158 if (p + sz < BLOCKSIZE) 00159 { 00160 memcpy(ptr, m_current_read -> data + p, sz); 00161 m_read_ptr += sz; 00162 } 00163 else 00164 { 00165 size_t sz1 = BLOCKSIZE - p; 00166 size_t sz2 = sz - sz1; 00167 memcpy(ptr, m_current_read -> data + p, sz1); 00168 m_read_ptr += sz1; 00169 while (sz2 > BLOCKSIZE) 00170 { 00171 if (m_current_read -> next) 00172 { 00173 m_current_read = m_current_read -> next; 00174 memcpy(ptr + sz1, m_current_read -> data, BLOCKSIZE); 00175 m_read_ptr += BLOCKSIZE; 00176 sz1 += BLOCKSIZE; 00177 sz2 -= BLOCKSIZE; 00178 } 00179 else 00180 { 00181 return sz1; 00182 } 00183 } 00184 if (m_current_read -> next) 00185 { 00186 m_current_read = m_current_read -> next; 00187 memcpy(ptr + sz1, m_current_read -> data, sz2); 00188 m_read_ptr += sz2; 00189 } 00190 else 00191 { 00192 return sz1; 00193 } 00194 } 00195 return sz; 00196 }
size_t MemFile::fwrite | ( | const char * | ptr, | |
size_t | size, | |||
size_t | nmemb | |||
) | [virtual] |
Implements IFile.
Definition at line 199 of file MemFile.cpp.
References BLOCKSIZE, m_current_write, m_current_write_nr, and m_write_ptr.
Referenced by fprintf(), and MemFile().
00200 { 00201 size_t p = m_write_ptr % BLOCKSIZE; 00202 int nr = (int)m_write_ptr / BLOCKSIZE; 00203 size_t sz = size * nmemb; 00204 if (m_current_write_nr < nr) 00205 { 00206 block_t *next = new block_t; 00207 m_current_write -> next = next; 00208 m_current_write = next; 00209 m_current_write_nr++; 00210 } 00211 if (p + sz <= BLOCKSIZE) 00212 { 00213 memcpy(m_current_write -> data + p, ptr, sz); 00214 m_write_ptr += sz; 00215 } 00216 else 00217 { 00218 size_t sz1 = BLOCKSIZE - p; // size left 00219 size_t sz2 = sz - sz1; 00220 memcpy(m_current_write -> data + p, ptr, sz1); 00221 m_write_ptr += sz1; 00222 while (sz2 > BLOCKSIZE) 00223 { 00224 if (m_current_write -> next) 00225 { 00226 m_current_write = m_current_write -> next; 00227 m_current_write_nr++; 00228 } 00229 else 00230 { 00231 block_t *next = new block_t; 00232 m_current_write -> next = next; 00233 m_current_write = next; 00234 m_current_write_nr++; 00235 } 00236 memcpy(m_current_write -> data, ptr + sz1, BLOCKSIZE); 00237 m_write_ptr += BLOCKSIZE; 00238 sz1 += BLOCKSIZE; 00239 sz2 -= BLOCKSIZE; 00240 } 00241 if (m_current_write -> next) 00242 { 00243 m_current_write = m_current_write -> next; 00244 m_current_write_nr++; 00245 } 00246 else 00247 { 00248 block_t *next = new block_t; 00249 m_current_write -> next = next; 00250 m_current_write = next; 00251 m_current_write_nr++; 00252 } 00253 memcpy(m_current_write -> data, ptr + sz1, sz2); 00254 m_write_ptr += sz2; 00255 } 00256 return sz; 00257 }
char * MemFile::fgets | ( | char * | s, | |
int | size | |||
) | const [virtual] |
Implements IFile.
Definition at line 261 of file MemFile.cpp.
References eof(), and fread().
00262 { 00263 int n = 0; 00264 while (n < size - 1 && !eof()) 00265 { 00266 char c; 00267 size_t sz = fread(&c, 1, 1); 00268 if (sz) 00269 { 00270 if (c == 10) 00271 { 00272 s[n] = 0; 00273 return s; 00274 } 00275 s[n++] = c; 00276 } 00277 } 00278 s[n] = 0; 00279 return s; 00280 }
void MemFile::fprintf | ( | const char * | format, | |
... | ||||
) | [virtual] |
off_t MemFile::size | ( | ) | const [virtual] |
Implements IFile.
Definition at line 294 of file MemFile.cpp.
References m_write_ptr.
00295 { 00296 return (off_t)m_write_ptr; 00297 }
bool MemFile::eof | ( | ) | const [virtual] |
Implements IFile.
Definition at line 300 of file MemFile.cpp.
References m_b_read_caused_eof.
Referenced by fgets().
00301 { 00302 return m_b_read_caused_eof; //(m_read_ptr < m_write_ptr) ? false : true; 00303 }
void MemFile::reset_read | ( | ) | const [virtual] |
Implements IFile.
Definition at line 306 of file MemFile.cpp.
References m_base, m_current_read, and m_read_ptr.
00307 { 00308 m_read_ptr = 0; 00309 m_current_read = m_base; 00310 }
void MemFile::reset_write | ( | ) | [virtual] |
Implements IFile.
Definition at line 313 of file MemFile.cpp.
References m_base, m_current_write, m_current_write_nr, and m_write_ptr.
00314 { 00315 m_write_ptr = 0; 00316 m_current_write = m_base; 00317 m_current_write_nr = 0; 00318 }
int MemFile::RefCount | ( | ) | const |
Reference count when copy constructor is used.
Definition at line 321 of file MemFile.cpp.
References m_ref_count.
00322 { 00323 return m_ref_count; 00324 }
void MemFile::Increase | ( | ) |
Definition at line 327 of file MemFile.cpp.
References m_ref_count.
Referenced by MemFile().
00328 { 00329 ++m_ref_count; 00330 }
void MemFile::Decrease | ( | ) |
Definition at line 333 of file MemFile.cpp.
References m_ref_count.
Referenced by fclose(), and ~MemFile().
00334 { 00335 --m_ref_count; 00336 }
const std::string & MemFile::Path | ( | ) | const [virtual] |
Implements IFile.
Definition at line 339 of file MemFile.cpp.
References m_path.
00340 { 00341 return m_path; 00342 }
MemFile& MemFile::m_src [private] |
bool MemFile::m_src_valid [private] |
block_t* MemFile::m_base [private] |
Definition at line 95 of file MemFile.h.
Referenced by MemFile(), reset_read(), reset_write(), and ~MemFile().
block_t* MemFile::m_current_read [mutable, private] |
block_t* MemFile::m_current_write [private] |
int MemFile::m_current_write_nr [private] |
size_t MemFile::m_read_ptr [mutable, private] |
size_t MemFile::m_write_ptr [private] |
bool MemFile::m_b_read_caused_eof [mutable, private] |
int MemFile::m_ref_count [private] |
Definition at line 102 of file MemFile.h.
Referenced by Decrease(), Increase(), RefCount(), and ~MemFile().
bool MemFile::m_ref_decreased [mutable, private] |
std::string MemFile::m_path [private] |