Dillo v3.1.1-14-g8f67d6e0
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
35void init (int argc, char *argv[])
36{
37 prgName = strdup (argv[0]);
38}
39
40
41// ------------------
42// StringBuffer
43// ------------------
44
45
47{
48 firstNode = lastNode = NULL;
49 numChars = 0;
50 str = NULL;
51 strValid = false;
52}
53
55{
56 clear ();
57 if (str)
58 delete[] str;
59}
60
69{
70 Node *node = new Node();
71 node->data = str;
72 node->next = NULL;
73
74 if (firstNode == NULL) {
75 firstNode = node;
76 lastNode = node;
77 } else {
78 lastNode->next = node;
79 lastNode = node;
80 }
81
82 numChars += strlen(str);
83 strValid = false;
84}
85
93{
94 if (strValid)
95 return str;
96
97 if (str)
98 delete[] str;
99 str = new char[numChars + 1];
100 char *p = str;
101
102 for (Node *node = firstNode; node; node = node->next) {
103 int l = strlen(node->data);
104 memcpy(p, node->data, l * sizeof(char));
105 p += l;
106 }
107
108 *p = 0;
109 strValid = true;
110 return str;
111}
112
117{
118 Node *node, *nextNode;
119 for (node = firstNode; node; node = nextNode) {
120 nextNode = node->next;
121 free(node->data);
122 delete node;
123 }
124 firstNode = lastNode = NULL;
125 numChars = 0;
126 strValid = false;
127}
128
129
130// ------------
131// BitSet
132// ------------
133
134BitSet::BitSet(int initBits)
135{
136 numBits = initBits;
137 numBytes = bytesForBits(initBits);
138 bits = (unsigned char*)malloc(numBytes * sizeof(unsigned char));
139 clear();
140}
141
143{
144 free(bits);
145}
146
148{
149 sb->append("[");
150 for (int i = 0; i < numBits; i++)
151 sb->append(get(i) ? "1" : "0");
152 sb->append("]");
153}
154
155bool BitSet::get(int i) const
156{
157 if (8 * i >= numBytes)
158 return false;
159 else
160 return bits[i / 8] & (1 << (i % 8));
161}
162
163void BitSet::set(int i, bool val)
164{
165 if (i > numBits)
166 numBits = i;
167
168 if (8 * i >= numBytes) {
169 int newNumBytes = numBytes;
170 while (8 * i >= newNumBytes)
171 newNumBytes *= 2;
172
173 void *vp;
174 assert((vp = realloc(bits, newNumBytes * sizeof(unsigned char))));
175 if (!vp) exit(-2); // when NDEBUG is defined
176 bits = (unsigned char*)vp;
177 memset(bits + numBytes, 0, newNumBytes - numBytes);
178 numBytes = newNumBytes;
179 }
180
181 if (val)
182 bits[i / 8] |= (1 << (i % 8));
183 else
184 bits[i / 8] &= ~(1 << (i % 8));
185}
186
188{
189 memset(bits, 0, numBytes);
190}
191
192} // namespace misc
193
194} // namespace lout
void intoStringBuffer(misc::StringBuffer *sb)
Definition misc.cc:147
BitSet(int initBits)
Definition misc.cc:134
void set(int i, bool val)
Definition misc.cc:163
bool get(int i) const
Definition misc.cc:155
int bytesForBits(int bits)
Definition misc.hh:610
unsigned char * bits
Definition misc.hh:607
A class for fast concatenation of a large number of strings.
Definition misc.hh:566
void clear()
Remove all strings appended to the string buffer.
Definition misc.cc:116
void append(const char *str)
Append a NUL-terminated string to the buffer, with copying.
Definition misc.hh:589
void appendNoCopy(char *str)
Append a NUL-terminated string to the buffer, without copying.
Definition misc.cc:68
const char * getChars()
Return a NUL-terminated strings containing all appended strings.
Definition misc.cc:92
#define PRGNAME
Definition misc.cc:27
const char * prgName
Definition misc.cc:33
void init(int argc, char *argv[])
Definition misc.cc:35