Mercurial > dillo_port1.3
annotate lout/misc.cc @ 2104:3e7e5395f0bc
non-ASCII keybindings
Alexander Voigt has kindly done some testing, and it seems that this
makes bindings to most keys on a German keyboard possible -- except
those that need AltGr don't work yet.
author | corvid <corvid@lavabit.com> |
---|---|
date | Thu, 23 Jun 2011 19:24:11 +0000 |
parents | 74925c1746dd |
children |
rev | line source |
---|---|
347 | 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 | |
972
d7dbd3dcfa38
Updated the GPL copyright note in the source files
Detlef Riekenberg <wine.dev@web.de>
parents:
930
diff
changeset
|
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
347 | 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 // Comparable | |
42 // ---------------- | |
43 | |
44 Comparable::~Comparable() | |
45 { | |
46 } | |
47 | |
48 /** | |
49 * \brief This static method may be used as compare function for qsort(3), for | |
50 * an array of Object* (Object*[] or Object**). | |
51 */ | |
52 int Comparable::compareFun(const void *p1, const void *p2) | |
53 { | |
54 Comparable **c1 = (Comparable**)p1; | |
55 Comparable **c2 = (Comparable**)p2; | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
347
diff
changeset
|
56 if (c1 && c2) |
347 | 57 return ((*c1)->compareTo(*c2)); |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
347
diff
changeset
|
58 else if (c1) |
347 | 59 return 1; |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
347
diff
changeset
|
60 else if (c2) |
347 | 61 return -1; |
62 else | |
63 return 0; | |
64 } | |
65 | |
66 | |
67 // ------------------ | |
68 // StringBuffer | |
69 // ------------------ | |
70 | |
71 | |
72 StringBuffer::StringBuffer() | |
73 { | |
74 firstNode = lastNode = NULL; | |
75 numChars = 0; | |
76 str = NULL; | |
77 strValid = false; | |
78 } | |
79 | |
80 StringBuffer::~StringBuffer() | |
81 { | |
82 clear (); | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
347
diff
changeset
|
83 if (str) |
347 | 84 delete[] str; |
85 } | |
86 | |
87 /** | |
88 * \brief Append a NUL-terminated string to the buffer, without copying. | |
89 * | |
90 * No copy is made, so this method should only be used in cases, where | |
91 * the string would otherwise be freed again. (This method may then | |
92 * save some CPU cycles.) | |
93 */ | |
94 void StringBuffer::appendNoCopy(char *str) | |
95 { | |
96 Node *node = new Node(); | |
97 node->data = str; | |
98 node->next = NULL; | |
99 | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
347
diff
changeset
|
100 if (firstNode == NULL) { |
347 | 101 firstNode = node; |
102 lastNode = node; | |
103 } else { | |
104 lastNode->next = node; | |
105 lastNode = node; | |
106 } | |
107 | |
108 numChars += strlen(str); | |
109 strValid = false; | |
110 } | |
111 | |
112 /** | |
113 * \brief Return a NUL-terminated strings containing all appended strings. | |
114 * | |
115 * The caller does not have to free the string, this is done in | |
116 * misc::StringBuffer::~StringBuffer. | |
117 */ | |
118 const char *StringBuffer::getChars() | |
119 { | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
347
diff
changeset
|
120 if (strValid) |
347 | 121 return str; |
122 | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
347
diff
changeset
|
123 if (str) |
347 | 124 delete[] str; |
125 str = new char[numChars + 1]; | |
126 char *p = str; | |
127 | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
347
diff
changeset
|
128 for (Node *node = firstNode; node; node = node->next) { |
347 | 129 int l = strlen(node->data); |
130 memcpy(p, node->data, l * sizeof(char)); | |
131 p += l; | |
132 } | |
930
b277eed3119c
whitespace cleanup: 's/ +$//g'
Jorge Arellano Cid <jcid@dillo.org>
parents:
928
diff
changeset
|
133 |
347 | 134 *p = 0; |
135 strValid = true; | |
136 return str; | |
137 } | |
138 | |
139 /** | |
140 * \brief Remove all strings appended to the string buffer. | |
141 */ | |
142 void StringBuffer::clear () | |
143 { | |
144 Node *node, *nextNode; | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
347
diff
changeset
|
145 for (node = firstNode; node; node = nextNode) { |
347 | 146 nextNode = node->next; |
2011 | 147 free(node->data); |
347 | 148 delete node; |
149 } | |
150 firstNode = lastNode = NULL; | |
151 numChars = 0; | |
152 strValid = false; | |
153 } | |
154 | |
155 | |
156 // ------------ | |
157 // BitSet | |
158 // ------------ | |
159 | |
160 BitSet::BitSet(int initBits) | |
161 { | |
930
b277eed3119c
whitespace cleanup: 's/ +$//g'
Jorge Arellano Cid <jcid@dillo.org>
parents:
928
diff
changeset
|
162 numBytes = bytesForBits(initBits); |
347 | 163 bits = (unsigned char*)malloc(numBytes * sizeof(unsigned char)); |
164 clear(); | |
165 } | |
166 | |
167 BitSet::~BitSet() | |
168 { | |
169 free(bits); | |
170 } | |
171 | |
172 void BitSet::intoStringBuffer(misc::StringBuffer *sb) | |
173 { | |
174 sb->append("["); | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
347
diff
changeset
|
175 for (int i = 0; i < numBytes; i++) |
347 | 176 sb->append(get(i) ? "1" : "0"); |
177 sb->append("]"); | |
178 } | |
179 | |
180 bool BitSet::get(int i) | |
181 { | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
347
diff
changeset
|
182 if (8 * i >= numBytes) |
347 | 183 return false; |
184 else | |
185 return bits[i / 8] & (1 << (i % 8)); | |
186 } | |
187 | |
188 void BitSet::set(int i, bool val) | |
189 { | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
347
diff
changeset
|
190 if (8 * i >= numBytes) { |
347 | 191 int newNumBytes = numBytes; |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
347
diff
changeset
|
192 while (8 * i >= newNumBytes) |
347 | 193 newNumBytes *= 2; |
194 bits = | |
195 (unsigned char*)realloc(bits, newNumBytes * sizeof(unsigned char)); | |
196 memset(bits + numBytes, 0, newNumBytes - numBytes); | |
197 numBytes = newNumBytes; | |
198 } | |
930
b277eed3119c
whitespace cleanup: 's/ +$//g'
Jorge Arellano Cid <jcid@dillo.org>
parents:
928
diff
changeset
|
199 |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
347
diff
changeset
|
200 if (val) |
347 | 201 bits[i / 8] |= (1 << (i % 8)); |
202 else | |
203 bits[i / 8] &= ~(1 << (i % 8)); | |
204 } | |
205 | |
206 void BitSet::clear() | |
207 { | |
208 memset(bits, 0, numBytes); | |
209 } | |
210 | |
211 } // namespace misc | |
212 | |
213 } // namespace lout |