source: sources/src/ioMemory.cc @ 1464:3f6711617be1

Revision 1464:3f6711617be1, 8.8 KB checked in by niam, 22 months ago (diff)

{issue #58[resolved]} support for non-c++ exceptions

Line 
1/***************************************************************************
2 *            ioMemory.cc
3 *
4 *  Wed Oct 8 2005
5 *  Copyright  2005  Dmytro Milinevskyy
6 *  milinevskyy@gmail.com
7 ****************************************************************************/
8
9/*
10 *  This program is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU Lesser General Public License version 2.1 as published by
12 *  the Free Software Foundation;
13 *
14 *  This program is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU Library General Public License for more details.
18 *
19 *  You should have received a copy of the GNU Lesser General Public License
20 *  along with this program; if not, write to the Free Software
21 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24/**
25 * vim indentation settings
26 * set tabstop=4
27 * set shiftwidth=4
28 */
29
30#include <libdodo/directives.h>
31
32#include <string.h>
33
34#include <libdodo/ioMemory.h>
35#include <libdodo/types.h>
36#include <libdodo/ioChannel.h>
37#include <libdodo/ioBlockChannel.h>
38#include <libdodo/ioMemoryEx.h>
39#include <libdodo/pcSyncStack.h>
40
41using namespace dodo::io;
42
43memory::memory(char          *a_data,
44               unsigned long size,
45               short         flags,
46               short         protection) : block::channel(protection),
47                                           size(size),
48                                           flags(flags),
49                                           nullEnd(false)
50{
51#ifndef IO_WO_XEXEC
52    collectedData.setExecObject(xexec::OBJECT_IOMEMORY);
53#endif
54
55    if (flags & FLAGS_NORMAL) {
56        data = new char[size];
57        memcpy(data, a_data, size);
58    } else {
59        if (flags & FLAGS_EXTERN)
60            data = a_data;
61        else
62            dodo_throw exception::basic(exception::MODULE_IOMEMORY, MEMORYEX_MEMORY, exception::ERRNO_LIBDODO, MEMORYEX_WRONGFLAGS, IOMEMORYEX_WRONGFLAGS_STR, __LINE__, __FILE__);
63    }
64}
65
66//-------------------------------------------------------------------
67
68memory::memory(short protection) : block::channel(protection),
69                                   data(NULL),
70                                   size(0),
71                                   flags(FLAGS_NORMAL),
72                                   nullEnd(false)
73{
74#ifndef IO_WO_XEXEC
75    collectedData.setExecObject(xexec::OBJECT_IOMEMORY);
76#endif
77}
78
79//-------------------------------------------------------------------
80
81memory::memory(const memory &fd) : block::channel(fd.protection),
82                                   size(fd.size),
83                                   flags(fd.flags),
84                                   nullEnd(fd.nullEnd)
85{
86#ifndef IO_WO_XEXEC
87    collectedData.setExecObject(xexec::OBJECT_IOFILEREGULAR);
88#endif
89
90    if (flags & FLAGS_NORMAL) {
91        if (nullEnd) {
92            data = new char[size + 1];
93            memcpy(data, fd.data, size + 1);
94        } else {
95            data = new char[size + 1];
96            memcpy(data, fd.data, size + 1);
97        }
98    } else if (flags & FLAGS_EXTERN) {
99        data = fd.data;
100    }
101
102    append = fd.append;
103    pos = fd.pos;
104    bs = fd.bs;
105}
106
107//-------------------------------------------------------------------
108
109memory::memory(const dodo::string &buffer,
110               short            protection) : block::channel(protection),
111                                              size(buffer.size()),
112                                              flags(FLAGS_NORMAL),
113                                              nullEnd(false)
114{
115#ifndef IO_WO_XEXEC
116    collectedData.setExecObject(xexec::OBJECT_IOFILEREGULAR);
117#endif
118
119    data = new char[size];
120    memcpy(data, buffer.data(), size);
121}
122
123//-------------------------------------------------------------------
124
125memory::~memory()
126{
127    if (!(flags & FLAGS_EXTERN))
128        delete [] data;
129}
130
131//-------------------------------------------------------------------
132
133int
134memory::inDescriptor() const
135{
136    dodo_throw exception::basic(exception::MODULE_IOMEMORY, MEMORYEX_INDESCRIPTOR, exception::ERRNO_LIBDODO, MEMORYEX_CANTBEUSEDWITHIOEVENT, IOMEMORYEX_CANTBEUSEDWITHIOEVENT_STR, __LINE__, __FILE__);
137}
138
139//-------------------------------------------------------------------
140
141int
142memory::outDescriptor() const
143{
144    dodo_throw exception::basic(exception::MODULE_IOMEMORY, MEMORYEX_OUTDESCRIPTOR, exception::ERRNO_LIBDODO, MEMORYEX_CANTBEUSEDWITHIOEVENT, IOMEMORYEX_CANTBEUSEDWITHIOEVENT_STR, __LINE__, __FILE__);
145}
146
147//-------------------------------------------------------------------
148
149void
150memory::flush() const
151{
152}
153
154//-------------------------------------------------------------------
155
156void
157memory::clear()
158{
159    if (!(flags & FLAGS_EXTERN)) {
160        delete [] data;
161        data = NULL;
162
163        size = 0;
164    }
165}
166
167//-------------------------------------------------------------------
168
169memory::operator const char
170*()
171{
172    return data;
173}
174
175//-------------------------------------------------------------------
176
177void
178memory::clone(const memory &fd)
179{
180    pc::sync::stack pg(keeper);
181
182    pos = fd.pos;
183    append = fd.append;
184    bs = fd.bs;
185    flags = fd.flags;
186    size = fd.size;
187
188    if (flags == FLAGS_NORMAL) {
189        data = new char[size];
190        memcpy(data, fd.data, size);
191    } else if (flags == FLAGS_EXTERN)
192        data = fd.data;
193}
194
195//-------------------------------------------------------------------
196
197unsigned long
198memory::_read(char * const a_data) const
199{
200    if (pos + bs > size)
201        dodo_throw exception::basic(exception::MODULE_IOMEMORY, MEMORYEX__READ, exception::ERRNO_LIBDODO, MEMORYEX_OUTOFBOUNDS, IOMEMORYEX_OUTOFBOUNDS_STR, __LINE__, __FILE__);
202
203    memcpy(a_data, data + pos, bs);
204
205    return bs;
206}
207
208//-------------------------------------------------------------------
209
210unsigned long
211memory::_write(const char *const a_data) const
212{
213    if (append) {
214        if (flags & FLAGS_FIXED_LENGTH) {
215            dodo_throw exception::basic(exception::MODULE_IOMEMORY, MEMORYEX__WRITE, exception::ERRNO_LIBDODO, MEMORYEX_APPENDTOFIXED, IOMEMORYEX_APPENDTOFIXED_STR, __LINE__, __FILE__);
216        } else {
217            char *newData = new char[size + bs];
218            memcpy(newData, data, size);
219            memcpy(newData + size, a_data, bs);
220            size += bs;
221            delete [] data;
222            data = newData;
223        }
224    } else {
225        unsigned long shift = pos + bs;
226        if (shift > size) {
227            if (flags & FLAGS_FIXED_LENGTH) {
228                dodo_throw exception::basic(exception::MODULE_IOMEMORY, MEMORYEX__WRITE, exception::ERRNO_LIBDODO, MEMORYEX_EXTENDFIXED, IOMEMORYEX_EXTENDFIXED_STR, __LINE__, __FILE__);
229            } else {
230                shift -= size;
231                char *newData = new char[size + shift];
232                memcpy(newData, data, size);
233                memset(newData + size, 0x0, shift);
234                size += shift;
235                delete [] data;
236                data = newData;
237            }
238        }
239
240        memcpy(data + pos, a_data, bs);
241    }
242
243    return bs;
244}
245
246//-------------------------------------------------------------------
247
248void
249memory::erase()
250{
251    pc::sync::stack pg(keeper);
252
253    unsigned long shift = pos + bs;
254    if (shift > size) {
255        if (flags & FLAGS_FIXED_LENGTH) {
256            dodo_throw exception::basic(exception::MODULE_IOMEMORY, MEMORYEX_ERASE, exception::ERRNO_LIBDODO, MEMORYEX_EXTENDFIXED, IOMEMORYEX_EXTENDFIXED_STR, __LINE__, __FILE__);
257        } else {
258            shift -= size;
259            char *newData = new char[size + shift];
260            memcpy(newData, data, size);
261            memset(newData + size, 0x0, shift);
262            size += shift;
263            delete [] data;
264            data = newData;
265        }
266    }
267
268    memset(data + pos, 0x0, bs);
269}
270
271//-------------------------------------------------------------------
272
273unsigned long
274memory::_readString(char * const a_data) const
275{
276    unsigned long readSize = bs + 1;
277
278    unsigned long read = 0;
279
280    for (unsigned long i = pos; i < size && read < readSize; ++i) {
281        a_data[read] = data[i];
282
283        ++read;
284
285        if (data[i] == '\n' || data[i] == '\0')
286            break;
287    }
288
289    return read;
290}
291
292//-------------------------------------------------------------------
293
294unsigned long
295memory::_writeString(const char *const a_data) const
296{
297    if (flags & FLAGS_FIXED_LENGTH)
298        dodo_throw exception::basic(exception::MODULE_IOMEMORY, MEMORYEX__WRITESTRING, exception::ERRNO_LIBDODO, MEMORYEX_EXTENDFIXED, IOMEMORYEX_EXTENDFIXED_STR, __LINE__, __FILE__);
299
300    unsigned long _bs = strnlen(a_data, bs);
301
302    char *newData = new char[size + _bs + 1];
303    memcpy(newData, data, size);
304    memcpy(newData + size, a_data, _bs);
305    size += _bs;
306    newData[size] = '\0';
307    delete [] data;
308    data = newData;
309    nullEnd = true;
310
311    return _bs;
312}
313
314//-------------------------------------------------------------------
315
Note: See TracBrowser for help on using the repository browser.