Dillo v3.1.1-95-g3af05307
Loading...
Searching...
No Matches
misc.cc
Go to the documentation of this file.
1/*
2 * Dillo Widget
3 *
4 * Copyright 2005-2007 Sebastian Geerken <sgeerken@dillo.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21
22#include "misc.hh"
23
24#include <ctype.h>
25#include <config.h>
26
27#define PRGNAME PACKAGE "/" VERSION
28
29namespace lout {
30
31namespace misc {
32
33const char *prgName = PRGNAME;
34
35// ------------------
36// StringBuffer
37// ------------------
38
39
41{
42 firstNode = lastNode = NULL;
43 numChars = 0;
44 str = NULL;
45 strValid = false;
46}
47
49{
50 clear ();
51 if (str)
52 delete[] str;
53}
54
63{
64 Node *node = new Node();
65 node->data = str;
66 node->next = NULL;
67
68 if (firstNode == NULL) {
69 firstNode = node;
70 lastNode = node;
71 } else {
72 lastNode->next = node;
73 lastNode = node;
74 }
75
76 numChars += strlen(str);
77 strValid = false;
78}
79
87{
88 if (strValid)
89 return str;
90
91 if (str)
92 delete[] str;
93 str = new char[numChars + 1];
94 char *p = str;
95
96 for (Node *node = firstNode; node; node = node->next) {
97 int l = strlen(node->data);
98 memcpy(p, node->data, l * sizeof(char));
99 p += l;
100 }
101
102 *p = 0;
103 strValid = true;
104 return str;
105}
106
111{
112 Node *node, *nextNode;
113 for (node = firstNode; node; node = nextNode) {
114 nextNode = node->next;
115 free(node->data);
116 delete node;
117 }
118 firstNode = lastNode = NULL;
119 numChars = 0;
120 strValid = false;
121}
122
123
124// ------------
125// BitSet
126// ------------
127
128BitSet::BitSet(int initBits)
129{
130 numBits = initBits;
131 numBytes = bytesForBits(initBits);
132 bits = (unsigned char*)malloc(numBytes * sizeof(unsigned char));
133 clear();
134}
135
137{
138 free(bits);
139}
140
142{
143 sb->append("[");
144 for (int i = 0; i < numBits; i++)
145 sb->append(get(i) ? "1" : "0");
146 sb->append("]");
147}
148
149bool BitSet::get(int i) const
150{
151 if (8 * i >= numBytes)
152 return false;
153 else
154 return bits[i / 8] & (1 << (i % 8));
155}
156
157void BitSet::set(int i, bool val)
158{
159 if (i > numBits)
160 numBits = i;
161
162 if (8 * i >= numBytes) {
163 int newNumBytes = numBytes;
164 while (8 * i >= newNumBytes)
165 newNumBytes *= 2;
166
167 void *vp;
168 assert((vp = realloc(bits, newNumBytes * sizeof(unsigned char))));
169 if (!vp) exit(-2); // when NDEBUG is defined
170 bits = (unsigned char*)vp;
171 memset(bits + numBytes, 0, newNumBytes - numBytes);
172 numBytes = newNumBytes;
173 }
174
175 if (val)
176 bits[i / 8] |= (1 << (i % 8));
177 else
178 bits[i / 8] &= ~(1 << (i % 8));
179}
180
182{
183 memset(bits, 0, numBytes);
184}
185
186} // namespace misc
187
188} // namespace lout
void intoStringBuffer(misc::StringBuffer *sb)
Definition misc.cc:141
BitSet(int initBits)
Definition misc.cc:128
void set(int i, bool val)
Definition misc.cc:157
bool get(int i) const
Definition misc.cc:149
int bytesForBits(int bits)
Definition misc.hh:614
unsigned char * bits
Definition misc.hh:611
A class for fast concatenation of a large number of strings.
Definition misc.hh:570
void clear()
Remove all strings appended to the string buffer.
Definition misc.cc:110
void append(const char *str)
Append a NUL-terminated string to the buffer, with copying.
Definition misc.hh:593
void appendNoCopy(char *str)
Append a NUL-terminated string to the buffer, without copying.
Definition misc.cc:62
const char * getChars()
Return a NUL-terminated strings containing all appended strings.
Definition misc.cc:86
#define PRGNAME
Definition misc.cc:27
const char * prgName
Definition misc.cc:33