Chunk is purely a wrapper class, any 'state variables', like 'characters in room...
[UnsignedByte.git] / src / Sockets / MemFile.cpp
blob19d5ff8de5d9b25123a6d6ed4df8a925af8c0ad6
1 /** \file MemFile.cpp
2 ** \date 2005-04-25
3 ** \author grymse@alhem.net
4 **/
5 /*
6 Copyright (C) 2004-2007 Anders Hedstrom
8 This library is made available under the terms of the GNU GPL.
10 If you would like to use this library in a closed-source application,
11 a separate license agreement is available. For information about
12 the closed-source license agreement for the C++ sockets library,
13 please visit http://www.alhem.net/Sockets/license.html and/or
14 email license@alhem.net.
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License
18 as published by the Free Software Foundation; either version 2
19 of the License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #ifdef _WIN32
31 #ifndef __MINGW32__
32 #pragma warning(disable:4786)
33 #endif
34 #endif
35 #include <stdarg.h>
37 #include "MemFile.h"
40 #ifdef SOCKETS_NAMESPACE
41 namespace SOCKETS_NAMESPACE {
42 #endif
45 std::map<std::string,MemFile::block_t *> MemFile::m_files;
48 MemFile::MemFile()
49 :m_temporary(true)
50 ,m_base(new block_t)
51 ,m_current_read(m_base)
52 ,m_current_write(m_base)
53 ,m_read_ptr(0)
54 ,m_write_ptr(0)
55 ,m_b_read_caused_eof(false)
60 MemFile::MemFile(const std::string& path)
61 :m_path(path)
62 ,m_temporary(false)
63 ,m_base(m_files[path])
64 ,m_current_read(NULL)
65 ,m_current_write(NULL)
66 ,m_read_ptr(0)
67 ,m_write_ptr(0)
68 ,m_b_read_caused_eof(false)
70 if (!m_base)
72 m_base = new block_t;
73 m_files[path] = m_base;
75 m_current_read = m_base;
76 m_current_write = m_base;
80 MemFile::~MemFile()
82 while (m_base && m_temporary)
84 block_t *p = m_base;
85 m_base = p -> next;
86 delete p;
91 bool MemFile::fopen(const std::string& path, const std::string& mode)
93 return true;
97 void MemFile::fclose()
102 /// \todo: fix for reads much larger than BLOCKSIZE
103 size_t MemFile::fread(char *ptr, size_t size, size_t nmemb)
105 size_t p = m_read_ptr % BLOCKSIZE;
106 size_t sz = size * nmemb;
107 size_t available = m_write_ptr - m_read_ptr;
108 if (sz > available) // read beyond eof
110 sz = available;
111 m_b_read_caused_eof = true;
113 if (!sz)
115 return 0;
117 if (p + sz < BLOCKSIZE)
119 memcpy(ptr, m_current_read -> data + p, sz);
120 m_read_ptr += sz;
122 else
124 size_t sz1 = BLOCKSIZE - p;
125 size_t sz2 = size - sz1;
126 memcpy(ptr, m_current_read -> data + p, sz1);
127 m_read_ptr += sz1;
128 if (m_current_read -> next)
130 m_current_read = m_current_read -> next;
131 memcpy(ptr + sz1, m_current_read -> data, sz2);
132 m_read_ptr += sz2;
134 else
136 return sz1;
139 return sz;
143 /// \todo: fix for writes that are much larger than BLOCKSIZE
144 size_t MemFile::fwrite(const char *ptr, size_t size, size_t nmemb)
146 size_t p = m_write_ptr % BLOCKSIZE;
147 size_t sz = size * nmemb;
148 if (p + sz < BLOCKSIZE)
150 memcpy(m_current_write -> data + p, ptr, sz);
151 m_write_ptr += sz;
153 else
155 size_t sz1 = BLOCKSIZE - p;
156 size_t sz2 = size - sz1;
157 memcpy(m_current_write -> data + p, ptr, sz1);
158 block_t *next = new block_t;
159 m_current_write -> next = next;
160 m_current_write = next;
161 memcpy(m_current_write -> data, ptr + sz1, sz2);
162 m_write_ptr += sz;
164 return sz;
169 char *MemFile::fgets(char *s, int size)
171 int n = 0;
172 while (n < size - 1 && !eof())
174 char c;
175 size_t sz = fread(&c, 1, 1);
176 if (sz)
178 if (c == 10)
180 s[n] = 0;
181 return s;
183 s[n++] = c;
186 s[n] = 0;
187 return s;
191 void MemFile::fprintf(const char *format, ...)
193 va_list ap;
194 char tmp[BLOCKSIZE];
195 va_start(ap, format);
196 #ifdef _WIN32
197 vsprintf(tmp, format, ap);
198 #else
199 vsnprintf(tmp, BLOCKSIZE - 1, format, ap);
200 #endif
201 va_end(ap);
202 fwrite(tmp, 1, strlen(tmp));
206 off_t MemFile::size()
208 return (off_t)m_write_ptr;
212 bool MemFile::eof()
214 return m_b_read_caused_eof; //(m_read_ptr < m_write_ptr) ? false : true;
218 #ifdef SOCKETS_NAMESPACE
220 #endif