dillo  3.0.5
About: dillo is a small, fast, extensible Web browser particularly suitable for older or smaller computers and embedded systems (but only limited or no support for frames, CSS, JavaScript, Java).
  Fossies Dox: dillo-3.0.5.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

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 
29 namespace lout {
30 
31 namespace misc {
32 
33 const char *prgName = PRGNAME;
34 
35 void 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 
134 BitSet::BitSet(int initBits)
135 {
136  numBytes = bytesForBits(initBits);
137  bits = (unsigned char*)malloc(numBytes * sizeof(unsigned char));
138  clear();
139 }
140 
142 {
143  free(bits);
144 }
145 
147 {
148  sb->append("[");
149  for (int i = 0; i < numBytes; i++)
150  sb->append(get(i) ? "1" : "0");
151  sb->append("]");
152 }
153 
154 bool BitSet::get(int i) const
155 {
156  if (8 * i >= numBytes)
157  return false;
158  else
159  return bits[i / 8] & (1 << (i % 8));
160 }
161 
162 void BitSet::set(int i, bool val)
163 {
164  if (8 * i >= numBytes) {
165  int newNumBytes = numBytes;
166  while (8 * i >= newNumBytes)
167  newNumBytes *= 2;
168  bits =
169  (unsigned char*)realloc(bits, newNumBytes * sizeof(unsigned char));
170  memset(bits + numBytes, 0, newNumBytes - numBytes);
171  numBytes = newNumBytes;
172  }
173 
174  if (val)
175  bits[i / 8] |= (1 << (i % 8));
176  else
177  bits[i / 8] &= ~(1 << (i % 8));
178 }
179 
181 {
182  memset(bits, 0, numBytes);
183 }
184 
185 } // namespace misc
186 
187 } // namespace lout
lout::misc::StringBuffer::Node
Definition: misc.hh:496
lout::misc::StringBuffer::firstNode
Node * firstNode
Definition: misc.hh:502
lout::misc::BitSet::clear
void clear()
Definition: misc.cc:180
lout::misc::StringBuffer::strValid
bool strValid
Definition: misc.hh:505
lout::misc::StringBuffer::numChars
int numChars
Definition: misc.hh:503
lout::misc::BitSet::bits
unsigned char * bits
Definition: misc.hh:530
lout::misc::StringBuffer::~StringBuffer
~StringBuffer()
Definition: misc.cc:54
lout::misc::BitSet::~BitSet
~BitSet()
Definition: misc.cc:141
lout::misc::StringBuffer::Node::data
char * data
Definition: misc.hh:498
lout::misc::prgName
const char * prgName
Definition: misc.cc:33
lout::misc::StringBuffer
A class for fast concatenation of a large number of strings.
Definition: misc.hh:493
lout::misc::StringBuffer::StringBuffer
StringBuffer()
Definition: misc.cc:46
lout::misc::BitSet::get
bool get(int i) const
Definition: misc.cc:154
lout
Definition: container.cc:26
lout::misc::StringBuffer::append
void append(const char *str)
Append a NUL-terminated string to the buffer, with copying.
Definition: misc.hh:517
PRGNAME
#define PRGNAME
Definition: misc.cc:27
lout::misc::BitSet::set
void set(int i, bool val)
Definition: misc.cc:162
lout::misc::BitSet::intoStringBuffer
void intoStringBuffer(misc::StringBuffer *sb)
Definition: misc.cc:146
lout::misc::init
void init(int argc, char *argv[])
Definition: misc.cc:35
lout::misc::BitSet::numBytes
int numBytes
Definition: misc.hh:531
lout::misc::BitSet::bytesForBits
int bytesForBits(int bits)
Definition: misc.hh:533
lout::misc::StringBuffer::appendNoCopy
void appendNoCopy(char *str)
Append a NUL-terminated string to the buffer, without copying.
Definition: misc.cc:68
lout::misc::StringBuffer::Node::next
Node * next
Definition: misc.hh:499
lout::misc::StringBuffer::getChars
const char * getChars()
Return a NUL-terminated strings containing all appended strings.
Definition: misc.cc:92
misc.hh
lout::misc::StringBuffer::lastNode
Node * lastNode
Definition: misc.hh:502
lout::misc::StringBuffer::clear
void clear()
Remove all strings appended to the string buffer.
Definition: misc.cc:116
lout::misc::StringBuffer::str
char * str
Definition: misc.hh:504
lout::misc::BitSet::BitSet
BitSet(int initBits)
Definition: misc.cc:134