switched from PracticalSocket to libasio
[anytun.git] / src / Sockets / MemFile.cpp
blob94b7f72ba5e555d2c8a51dbf15973ee1fe282a51
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 _MSC_VER
31 #pragma warning(disable:4786)
32 #endif
33 #include <cstring>
34 #include <stdio.h>
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_current_write_nr(0)
54 ,m_read_ptr(0)
55 ,m_write_ptr(0)
56 ,m_b_read_caused_eof(false)
61 MemFile::MemFile(const std::string& path)
62 :m_path(path)
63 ,m_temporary(false)
64 ,m_base(m_files[path])
65 ,m_current_read(NULL)
66 ,m_current_write(NULL)
67 ,m_current_write_nr(0)
68 ,m_read_ptr(0)
69 ,m_write_ptr(0)
70 ,m_b_read_caused_eof(false)
72 if (!m_base)
74 m_base = new block_t;
75 m_files[path] = m_base;
77 m_current_read = m_base;
78 m_current_write = m_base;
82 MemFile::~MemFile()
84 while (m_base && m_temporary)
86 block_t *p = m_base;
87 m_base = p -> next;
88 delete p;
93 bool MemFile::fopen(const std::string& path, const std::string& mode)
95 return true;
99 void MemFile::fclose()
104 size_t MemFile::fread(char *ptr, size_t size, size_t nmemb) const
106 size_t p = m_read_ptr % BLOCKSIZE;
107 size_t sz = size * nmemb;
108 size_t available = m_write_ptr - m_read_ptr;
109 if (sz > available) // read beyond eof
111 sz = available;
112 m_b_read_caused_eof = true;
114 if (!sz)
116 return 0;
118 if (p + sz < BLOCKSIZE)
120 memcpy(ptr, m_current_read -> data + p, sz);
121 m_read_ptr += sz;
123 else
125 size_t sz1 = BLOCKSIZE - p;
126 size_t sz2 = sz - sz1;
127 memcpy(ptr, m_current_read -> data + p, sz1);
128 m_read_ptr += sz1;
129 while (sz2 > BLOCKSIZE)
131 if (m_current_read -> next)
133 m_current_read = m_current_read -> next;
134 memcpy(ptr + sz1, m_current_read -> data, BLOCKSIZE);
135 m_read_ptr += BLOCKSIZE;
136 sz1 += BLOCKSIZE;
137 sz2 -= BLOCKSIZE;
139 else
141 return sz1;
144 if (m_current_read -> next)
146 m_current_read = m_current_read -> next;
147 memcpy(ptr + sz1, m_current_read -> data, sz2);
148 m_read_ptr += sz2;
150 else
152 return sz1;
155 return sz;
159 size_t MemFile::fwrite(const char *ptr, size_t size, size_t nmemb)
161 size_t p = m_write_ptr % BLOCKSIZE;
162 int nr = (int)m_write_ptr / BLOCKSIZE;
163 size_t sz = size * nmemb;
164 if (m_current_write_nr < nr)
166 block_t *next = new block_t;
167 m_current_write -> next = next;
168 m_current_write = next;
169 m_current_write_nr++;
171 if (p + sz <= BLOCKSIZE)
173 memcpy(m_current_write -> data + p, ptr, sz);
174 m_write_ptr += sz;
176 else
178 size_t sz1 = BLOCKSIZE - p; // size left
179 size_t sz2 = sz - sz1;
180 memcpy(m_current_write -> data + p, ptr, sz1);
181 m_write_ptr += sz1;
182 while (sz2 > BLOCKSIZE)
184 if (m_current_write -> next)
186 m_current_write = m_current_write -> next;
187 m_current_write_nr++;
189 else
191 block_t *next = new block_t;
192 m_current_write -> next = next;
193 m_current_write = next;
194 m_current_write_nr++;
196 memcpy(m_current_write -> data, ptr + sz1, BLOCKSIZE);
197 m_write_ptr += BLOCKSIZE;
198 sz1 += BLOCKSIZE;
199 sz2 -= BLOCKSIZE;
201 if (m_current_write -> next)
203 m_current_write = m_current_write -> next;
204 m_current_write_nr++;
206 else
208 block_t *next = new block_t;
209 m_current_write -> next = next;
210 m_current_write = next;
211 m_current_write_nr++;
213 memcpy(m_current_write -> data, ptr + sz1, sz2);
214 m_write_ptr += sz2;
216 return sz;
221 char *MemFile::fgets(char *s, int size) const
223 int n = 0;
224 while (n < size - 1 && !eof())
226 char c;
227 size_t sz = fread(&c, 1, 1);
228 if (sz)
230 if (c == 10)
232 s[n] = 0;
233 return s;
235 s[n++] = c;
238 s[n] = 0;
239 return s;
243 void MemFile::fprintf(const char *format, ...)
245 va_list ap;
246 char tmp[BLOCKSIZE];
247 va_start(ap, format);
248 #ifdef _WIN32
249 vsprintf(tmp, format, ap);
250 #else
251 vsnprintf(tmp, BLOCKSIZE - 1, format, ap);
252 #endif
253 va_end(ap);
254 fwrite(tmp, 1, strlen(tmp));
258 off_t MemFile::size() const
260 return (off_t)m_write_ptr;
264 bool MemFile::eof() const
266 return m_b_read_caused_eof; //(m_read_ptr < m_write_ptr) ? false : true;
270 void MemFile::reset_read() const
272 m_read_ptr = 0;
273 m_current_read = m_base;
277 void MemFile::reset_write()
279 m_write_ptr = 0;
280 m_current_write = m_base;
281 m_current_write_nr = 0;
285 #ifdef SOCKETS_NAMESPACE
287 #endif