1 /*
2  *
3  *  C++ Portable Types Library (PTypes)
4  *  Version 2.1.1  Released 27-Jun-2007
5  *
6  *  Copyright (C) 2001-2007 Hovik Melikyan
7  *
8  *  http://www.melikyan.com/ptypes/
9  *
10  */
11 
12 #include "pstreams.h"
13 
14 
15 PTYPES_BEGIN
16 
17 
outmemory(int ilimit)18 outmemory::outmemory(int ilimit)
19     : outstm(false, 0), mem(), limit(ilimit)
20 {
21 }
22 
23 
~outmemory()24 outmemory::~outmemory()
25 {
26     close();
27 }
28 
29 
classid()30 int outmemory::classid()
31 {
32     return CLASS2_OUTMEMORY;
33 }
34 
35 
doopen()36 void outmemory::doopen()
37 {
38 }
39 
40 
doclose()41 void outmemory::doclose()
42 {
43     clear(mem);
44 }
45 
46 
doseek(large newpos,ioseekmode mode)47 large outmemory::doseek(large newpos, ioseekmode mode)
48 {
49     large pos;
50 
51     switch (mode)
52     {
53     case IO_BEGIN:
54         pos = newpos;
55         break;
56     case IO_CURRENT:
57         pos = abspos + newpos;
58         break;
59     default: // case IO_END:
60         pos = length(mem) + newpos;
61         break;
62     }
63 
64     if (limit >= 0 && pos > limit)
65         pos = limit;
66 
67     return pos;
68 }
69 
70 
dorawwrite(const char * buf,int count)71 int outmemory::dorawwrite(const char* buf, int count)
72 {
73     if (count <= 0)
74         return 0;
75     if (limit >= 0 && abspos + count > limit)
76     {
77         count = limit - (int)abspos;
78         if (count <= 0)
79             return 0;
80     }
81 
82     // the string reallocator takes care of efficiency
83     if ((int)abspos + count > length(mem))
84         setlength(mem, (int)abspos + count);
85     memcpy(pchar(pconst(mem)) + (int)abspos, buf, count);
86     return count;
87 }
88 
89 
get_streamname()90 string outmemory::get_streamname()
91 {
92     return "mem";
93 }
94 
95 
get_strdata()96 string outmemory::get_strdata()
97 {
98     if (!active)
99         errstminactive();
100     return mem;
101 }
102 
103 
104 PTYPES_END
105