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.
32 #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
)
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
)
68 ,m_b_read_caused_eof(false)
73 m_files
[path
] = m_base
;
75 m_current_read
= m_base
;
76 m_current_write
= m_base
;
82 while (m_base
&& m_temporary
)
91 bool MemFile::fopen(const std::string
& path
, const std::string
& mode
)
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
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
= size
- sz1
;
126 memcpy(ptr
, m_current_read
-> data
+ p
, sz1
);
128 if (m_current_read
-> next
)
130 m_current_read
= m_current_read
-> next
;
131 memcpy(ptr
+ sz1
, m_current_read
-> data
, sz2
);
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
);
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
);
169 char *MemFile::fgets(char *s
, int size
)
172 while (n
< size
- 1 && !eof())
175 size_t sz
= fread(&c
, 1, 1);
191 void MemFile::fprintf(const char *format
, ...)
195 va_start(ap
, format
);
197 vsprintf(tmp
, format
, ap
);
199 vsnprintf(tmp
, BLOCKSIZE
- 1, format
, ap
);
202 fwrite(tmp
, 1, strlen(tmp
));
206 off_t
MemFile::size()
208 return (off_t
)m_write_ptr
;
214 return m_b_read_caused_eof
; //(m_read_ptr < m_write_ptr) ? false : true;
218 #ifdef SOCKETS_NAMESPACE