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