3 ** \author grymse@alhem.net
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.
31 #pragma warning(disable:4786)
40 #ifdef SOCKETS_NAMESPACE
41 namespace SOCKETS_NAMESPACE
{
45 std::map
<std::string
,MemFile::block_t
*> MemFile::m_files
;
51 ,m_current_read(m_base
)
52 ,m_current_write(m_base
)
53 ,m_current_write_nr(0)
56 ,m_b_read_caused_eof(false)
61 MemFile::MemFile(const std::string
& path
)
64 ,m_base(m_files
[path
])
66 ,m_current_write(NULL
)
67 ,m_current_write_nr(0)
70 ,m_b_read_caused_eof(false)
75 m_files
[path
] = m_base
;
77 m_current_read
= m_base
;
78 m_current_write
= m_base
;
84 while (m_base
&& m_temporary
)
93 bool MemFile::fopen(const std::string
& path
, const std::string
& mode
)
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
112 m_b_read_caused_eof
= true;
118 if (p
+ sz
< BLOCKSIZE
)
120 memcpy(ptr
, m_current_read
-> data
+ p
, sz
);
125 size_t sz1
= BLOCKSIZE
- p
;
126 size_t sz2
= sz
- sz1
;
127 memcpy(ptr
, m_current_read
-> data
+ p
, 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
;
144 if (m_current_read
-> next
)
146 m_current_read
= m_current_read
-> next
;
147 memcpy(ptr
+ sz1
, m_current_read
-> data
, sz2
);
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
);
178 size_t sz1
= BLOCKSIZE
- p
; // size left
179 size_t sz2
= sz
- sz1
;
180 memcpy(m_current_write
-> data
+ p
, 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
++;
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
;
201 if (m_current_write
-> next
)
203 m_current_write
= m_current_write
-> next
;
204 m_current_write_nr
++;
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
);
221 char *MemFile::fgets(char *s
, int size
) const
224 while (n
< size
- 1 && !eof())
227 size_t sz
= fread(&c
, 1, 1);
243 void MemFile::fprintf(const char *format
, ...)
247 va_start(ap
, format
);
249 vsprintf(tmp
, format
, ap
);
251 vsnprintf(tmp
, BLOCKSIZE
- 1, format
, 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
273 m_current_read
= m_base
;
277 void MemFile::reset_write()
280 m_current_write
= m_base
;
281 m_current_write_nr
= 0;
285 #ifdef SOCKETS_NAMESPACE