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)
39 #ifdef SOCKETS_NAMESPACE
40 namespace SOCKETS_NAMESPACE
{
44 std::map
<std::string
,MemFile::block_t
*> MemFile::m_files
;
50 ,m_current_read(m_base
)
51 ,m_current_write(m_base
)
52 ,m_current_write_nr(0)
55 ,m_b_read_caused_eof(false)
60 MemFile::MemFile(const std::string
& path
)
63 ,m_base(m_files
[path
])
65 ,m_current_write(NULL
)
66 ,m_current_write_nr(0)
69 ,m_b_read_caused_eof(false)
74 m_files
[path
] = m_base
;
76 m_current_read
= m_base
;
77 m_current_write
= m_base
;
83 while (m_base
&& m_temporary
)
92 bool MemFile::fopen(const std::string
& path
, const std::string
& mode
)
98 void MemFile::fclose()
103 size_t MemFile::fread(char *ptr
, size_t size
, size_t nmemb
) const
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
111 m_b_read_caused_eof
= true;
117 if (p
+ sz
< BLOCKSIZE
)
119 memcpy(ptr
, m_current_read
-> data
+ p
, sz
);
124 size_t sz1
= BLOCKSIZE
- p
;
125 size_t sz2
= sz
- sz1
;
126 memcpy(ptr
, m_current_read
-> data
+ p
, sz1
);
128 while (sz2
> BLOCKSIZE
)
130 if (m_current_read
-> next
)
132 m_current_read
= m_current_read
-> next
;
133 memcpy(ptr
+ sz1
, m_current_read
-> data
, BLOCKSIZE
);
134 m_read_ptr
+= BLOCKSIZE
;
143 if (m_current_read
-> next
)
145 m_current_read
= m_current_read
-> next
;
146 memcpy(ptr
+ sz1
, m_current_read
-> data
, sz2
);
158 size_t MemFile::fwrite(const char *ptr
, size_t size
, size_t nmemb
)
160 size_t p
= m_write_ptr
% BLOCKSIZE
;
161 int nr
= (int)m_write_ptr
/ BLOCKSIZE
;
162 size_t sz
= size
* nmemb
;
163 if (m_current_write_nr
< nr
)
165 block_t
*next
= new block_t
;
166 m_current_write
-> next
= next
;
167 m_current_write
= next
;
168 m_current_write_nr
++;
170 if (p
+ sz
<= BLOCKSIZE
)
172 memcpy(m_current_write
-> data
+ p
, ptr
, sz
);
177 size_t sz1
= BLOCKSIZE
- p
; // size left
178 size_t sz2
= sz
- sz1
;
179 memcpy(m_current_write
-> data
+ p
, ptr
, sz1
);
181 while (sz2
> BLOCKSIZE
)
183 if (m_current_write
-> next
)
185 m_current_write
= m_current_write
-> next
;
186 m_current_write_nr
++;
190 block_t
*next
= new block_t
;
191 m_current_write
-> next
= next
;
192 m_current_write
= next
;
193 m_current_write_nr
++;
195 memcpy(m_current_write
-> data
, ptr
+ sz1
, BLOCKSIZE
);
196 m_write_ptr
+= BLOCKSIZE
;
200 if (m_current_write
-> next
)
202 m_current_write
= m_current_write
-> next
;
203 m_current_write_nr
++;
207 block_t
*next
= new block_t
;
208 m_current_write
-> next
= next
;
209 m_current_write
= next
;
210 m_current_write_nr
++;
212 memcpy(m_current_write
-> data
, ptr
+ sz1
, sz2
);
220 char *MemFile::fgets(char *s
, int size
) const
223 while (n
< size
- 1 && !eof())
226 size_t sz
= fread(&c
, 1, 1);
242 void MemFile::fprintf(const char *format
, ...)
246 va_start(ap
, format
);
248 vsprintf(tmp
, format
, ap
);
250 vsnprintf(tmp
, BLOCKSIZE
- 1, format
, ap
);
253 fwrite(tmp
, 1, strlen(tmp
));
257 off_t
MemFile::size() const
259 return (off_t
)m_write_ptr
;
263 bool MemFile::eof() const
265 return m_b_read_caused_eof
; //(m_read_ptr < m_write_ptr) ? false : true;
269 void MemFile::reset_read() const
272 m_current_read
= m_base
;
276 void MemFile::reset_write()
279 m_current_write
= m_base
;
280 m_current_write_nr
= 0;
284 #ifdef SOCKETS_NAMESPACE