1 //
2 //	aegis - project change supervisor
3 //	Copyright (C) 2004-2008 Peter Miller
4 //
5 //	This program is free software; you can redistribute it and/or modify
6 //	it under the terms of the GNU General Public License as published by
7 //	the Free Software Foundation; either version 3 of the License, or
8 //	(at your option) any later version.
9 //
10 //	This program is distributed in the hope that it will be useful,
11 //	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //	GNU General Public License for more details.
14 //
15 //	You should have received a copy of the GNU General Public License
16 //	along with this program. If not, see
17 //	<http://www.gnu.org/licenses/>.
18 //
19 
20 #include <common/ac/string.h>
21 
22 #include <common/mem.h>
23 #include <common/wstring.h>
24 #include <libaegis/collect.h>
25 
26 
~collect()27 collect::~collect()
28 {
29     delete [] buf;
30 }
31 
32 
collect()33 collect::collect() :
34     pos(0),
35     size(0),
36     buf(0)
37 {
38 }
39 
40 
collect(const collect & arg)41 collect::collect(const collect &arg) :
42     pos(0),
43     size(0),
44     buf(0)
45 {
46     append(arg.buf, arg.size);
47 }
48 
49 
50 collect &
operator =(const collect & arg)51 collect::operator=(const collect &arg)
52 {
53     if (this != &arg)
54     {
55 	pos = 0;
56 	append(arg.buf, arg.size);
57     }
58     return *this;
59 }
60 
61 
62 void
append(wchar_t c)63 collect::append(wchar_t c)
64 {
65     if (pos >= size)
66     {
67 	size_t new_size = size * 2 + 8;
68 	wchar_t *new_buf = new wchar_t[new_size];
69 	if (pos)
70 	    memcpy(new_buf, buf, pos * sizeof(wchar_t));
71 	delete [] buf;
72 	size = new_size;
73 	buf = new_buf;
74     }
75     buf[pos++] = c;
76 }
77 
78 
79 void
append(const wchar_t * s,size_t n)80 collect::append(const wchar_t *s, size_t n)
81 {
82     while (n > 0)
83     {
84 	append(*s++);
85 	--n;
86     }
87 }
88 
89 
90 void
push_back(const wstring & s)91 collect::push_back(const wstring &s)
92 {
93     append(s.c_str(), s.size());
94 }
95 
96 
97 wstring
end()98 collect::end()
99 {
100     wstring result(buf, pos);
101     pos = 0;
102     return result;
103 }
104