| 1 |
/* |
| 2 |
* AiksaurusGTK - A GTK interface to the AikSaurus library |
| 3 |
* Copyright (C) 2001 by Jared Davis |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU General Public License |
| 7 |
* as published by the Free Software Foundation; either version 2 |
| 8 |
* of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 18 |
* 02111-1307, USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "AiksaurusGTK_strlist.h" |
| 22 |
#include "AiksaurusGTK_utils.h" |
| 23 |
#include <cstring> |
| 24 |
|
| 25 |
|
| 26 |
////////////////////////////////////////////////////////////////////////// |
| 27 |
// // |
| 28 |
// Creation and Destruction // |
| 29 |
// // |
| 30 |
////////////////////////////////////////////////////////////////////////// |
| 31 |
|
| 32 |
AiksaurusGTK_strlist::AiksaurusGTK_strlist() |
| 33 |
{ |
| 34 |
d_size = 0; |
| 35 |
d_front_ptr = d_back_ptr = static_cast<GList*>(NULL); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
AiksaurusGTK_strlist::~AiksaurusGTK_strlist() |
| 41 |
{ |
| 42 |
clear(); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
////////////////////////////////////////////////////////////////////////// |
| 48 |
// // |
| 49 |
// Inspection Routines // |
| 50 |
// // |
| 51 |
////////////////////////////////////////////////////////////////////////// |
| 52 |
|
| 53 |
const GList* |
| 54 |
AiksaurusGTK_strlist::list() const |
| 55 |
{ |
| 56 |
return d_front_ptr; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
unsigned int |
| 61 |
AiksaurusGTK_strlist::size() const |
| 62 |
{ |
| 63 |
return d_size; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
const char* |
| 68 |
AiksaurusGTK_strlist::look_back() const |
| 69 |
{ |
| 70 |
return (d_back_ptr) |
| 71 |
? (static_cast<char*>(d_back_ptr->data)) |
| 72 |
: (static_cast<char*>(NULL)); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
const char* |
| 77 |
AiksaurusGTK_strlist::look_front() const |
| 78 |
{ |
| 79 |
return (d_front_ptr) |
| 80 |
? (static_cast<char*>(d_front_ptr->data)) |
| 81 |
: (static_cast<char*>(NULL)); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
////////////////////////////////////////////////////////////////////////// |
| 87 |
// // |
| 88 |
// Node Destruction and Allocation // |
| 89 |
// // |
| 90 |
////////////////////////////////////////////////////////////////////////// |
| 91 |
|
| 92 |
GList* |
| 93 |
AiksaurusGTK_strlist::create_node(const char* str) const |
| 94 |
{ |
| 95 |
char* x = AiksaurusGTK_strCopy(str); |
| 96 |
GList* ret = g_list_alloc(); |
| 97 |
ret->data = x; |
| 98 |
ret->prev = ret->next = static_cast<GList*>(NULL); |
| 99 |
return ret; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
void |
| 104 |
AiksaurusGTK_strlist::free_data(GList* node) const |
| 105 |
{ |
| 106 |
delete[] static_cast<char*>(node->data); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
void |
| 111 |
AiksaurusGTK_strlist::remove_node(GList* node) |
| 112 |
{ |
| 113 |
// get pointers to the nodes we need to join. |
| 114 |
GList* node_next = node->next; |
| 115 |
GList* node_prev = node->prev; |
| 116 |
|
| 117 |
// if this is in the middle, we need to reassign both pointers. |
| 118 |
if ((node != d_front_ptr) && (node != d_back_ptr)) |
| 119 |
{ |
| 120 |
node_next->prev = node_prev; |
| 121 |
node_prev->next = node_next; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
// else if front of list, we want to reassign front to be |
| 126 |
// the new front. |
| 127 |
if (node == d_front_ptr) |
| 128 |
{ |
| 129 |
d_front_ptr = node_next; |
| 130 |
|
| 131 |
if (node_next) |
| 132 |
node_next->prev = static_cast<GList*>(NULL); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
// similarly, if back of list, we want to reassign |
| 137 |
// back to be the new back. |
| 138 |
if (node == d_back_ptr) |
| 139 |
{ |
| 140 |
d_back_ptr = node_prev; |
| 141 |
|
| 142 |
if (node_prev) |
| 143 |
node_prev->next = static_cast<GList*>(NULL); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
// delete the node itself. |
| 148 |
free_data(node); |
| 149 |
node->prev = node->next = static_cast<GList*>(NULL); |
| 150 |
g_list_free(node); |
| 151 |
|
| 152 |
|
| 153 |
// mark that we have deleted the node by reducing our size by one. |
| 154 |
d_size--; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
void |
| 159 |
AiksaurusGTK_strlist::clear() |
| 160 |
{ |
| 161 |
for(GList* iterator = d_front_ptr; iterator != static_cast<GList*>(NULL); iterator = iterator->next) |
| 162 |
free_data(iterator); |
| 163 |
|
| 164 |
g_list_free(d_front_ptr); |
| 165 |
|
| 166 |
d_front_ptr = d_back_ptr = static_cast<GList*>(NULL); |
| 167 |
|
| 168 |
d_size = 0; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
////////////////////////////////////////////////////////////////////////// |
| 174 |
// // |
| 175 |
// Stack-Like Routines // |
| 176 |
// // |
| 177 |
////////////////////////////////////////////////////////////////////////// |
| 178 |
|
| 179 |
void |
| 180 |
AiksaurusGTK_strlist::push_front(const char* str) |
| 181 |
{ |
| 182 |
// create new node from string. |
| 183 |
GList* node = create_node(str); |
| 184 |
|
| 185 |
// if nodes exist, prepend node to begin of list. |
| 186 |
if (d_front_ptr) |
| 187 |
{ |
| 188 |
node->next = d_front_ptr; |
| 189 |
d_front_ptr->prev = node; |
| 190 |
d_front_ptr = node; |
| 191 |
} |
| 192 |
|
| 193 |
// else if no nodes, init list to single node. |
| 194 |
else |
| 195 |
{ |
| 196 |
d_front_ptr = d_back_ptr = node; |
| 197 |
} |
| 198 |
|
| 199 |
// we have now added a node, so increase size by 1. |
| 200 |
d_size++; |
| 201 |
} |
| 202 |
|
| 203 |
void |
| 204 |
AiksaurusGTK_strlist::push_back(const char* str) |
| 205 |
{ |
| 206 |
// create new node from string. |
| 207 |
GList* node = create_node(str); |
| 208 |
|
| 209 |
// if nodes exist, append node to end of list. |
| 210 |
if (d_back_ptr) |
| 211 |
{ |
| 212 |
node->prev = d_back_ptr; |
| 213 |
d_back_ptr->next = node; |
| 214 |
d_back_ptr = node; |
| 215 |
} |
| 216 |
|
| 217 |
// else if no nodes, init list to single node. |
| 218 |
else |
| 219 |
{ |
| 220 |
d_front_ptr = d_back_ptr = node; |
| 221 |
} |
| 222 |
|
| 223 |
// we have now added a node, so increase size by 1. |
| 224 |
d_size++; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
void |
| 231 |
AiksaurusGTK_strlist::pop_front() |
| 232 |
{ |
| 233 |
// first ensure that we have elements. if not, nothing to do. |
| 234 |
if (!d_front_ptr) |
| 235 |
return; |
| 236 |
|
| 237 |
remove_node(d_front_ptr); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
void |
| 242 |
AiksaurusGTK_strlist::pop_back() |
| 243 |
{ |
| 244 |
// first ensure that we have elements. if not, nothing to do. |
| 245 |
if (!d_back_ptr) |
| 246 |
return; |
| 247 |
|
| 248 |
remove_node(d_back_ptr); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
////////////////////////////////////////////////////////////////////////// |
| 260 |
// // |
| 261 |
// Deletion Routines // |
| 262 |
// // |
| 263 |
////////////////////////////////////////////////////////////////////////// |
| 264 |
|
| 265 |
GList* AiksaurusGTK_strlist::find_first(const char* str) |
| 266 |
{ |
| 267 |
for(GList* iterator = d_front_ptr; iterator != static_cast<GList*>(NULL); iterator = iterator->next) |
| 268 |
{ |
| 269 |
char* x = static_cast<char*>(iterator->data); |
| 270 |
|
| 271 |
if (AiksaurusGTK_strEquals(x, str)) |
| 272 |
{ |
| 273 |
return iterator; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
return static_cast<GList*>(NULL); |
| 278 |
} |
| 279 |
|
| 280 |
void AiksaurusGTK_strlist::remove_first(const char* str) |
| 281 |
{ |
| 282 |
GList* node = find_first(str); |
| 283 |
|
| 284 |
if (node) |
| 285 |
remove_node(node); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
////////////////////////////////////////////////////////////////////////// |
| 294 |
// // |
| 295 |
// Debugging Routines // |
| 296 |
// // |
| 297 |
////////////////////////////////////////////////////////////////////////// |
| 298 |
#ifndef NDEBUG |
| 299 |
|
| 300 |
#include <iostream> |
| 301 |
using namespace std; |
| 302 |
|
| 303 |
void AiksaurusGTK_strlist::debug() |
| 304 |
{ |
| 305 |
cout << "Strlist Contents: (" << size() << " elements)\n"; |
| 306 |
for(GList* i = d_front_ptr;i != static_cast<GList*>(NULL);i = i->next) |
| 307 |
{ |
| 308 |
cout << " " << i << ": " << static_cast<char*>(i->data) << "\n"; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
#endif // NDEBUG |