My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
valuelist.cpp
Go to the documentation of this file.
1 /*
2  * valuelist.cpp - value list template class implementation
3  *
4  * Copyright (C) 2006 Stefan Jahn <stefan@lkcc.org>
5  *
6  * This 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 2, or (at your option)
9  * any later version.
10  *
11  * This software 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 package; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  * $Id: valuelist.cpp 1825 2011-03-11 20:42:14Z ela $
22  *
23  */
24 
25 #if HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #include <assert.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include "valuelist.h"
35 
36 // Constructor creates an instance of the valuelist class.
37 template <class type_t>
39  size = 0;
40  last = root = NULL;
41 }
42 
43 /* This copy constructor creates a instance of the valuelist class based
44  on the given valuelist. */
45 template <class type_t>
47  valentry<type_t> * ptr;
48  size = 0;
49  last = root = NULL;
50  for (ptr = p.root; ptr != NULL; ptr = ptr->next)
51  append (ptr->key, new type_t (*(ptr->value)));
52 }
53 
54 // Destructor deletes a valuelist object.
55 template <class type_t>
57  clear ();
58 }
59 
60 // Resets the value list.
61 template <class type_t>
63  valentry<type_t> * next;
64  while (root) {
65  next = root->next;
66  delete root;
67  root = next;
68  }
69  size = 0;
70  last = NULL;
71 }
72 
73 // Puts a new entry at the beginning of the value list.
74 template <class type_t>
75 void valuelist<type_t>::add (const char * n, type_t * ptr) {
76  valentry<type_t> * entry = new valentry<type_t> ();
77  if (root) root->prev = entry;
78  entry->key = strdup (n);
79  entry->value = ptr;
80  entry->next = root;
81  entry->prev = NULL;
82  root = entry;
83  if (!last) last = root;
84  size++;
85 }
86 
87 // Appends a new entry at the end of the value list.
88 template <class type_t>
89 void valuelist<type_t>::append (char * n, type_t * ptr) {
90  valentry<type_t> * entry = new valentry<type_t> ();
91  entry->key = strdup (n);
92  entry->value = ptr;
93  entry->next = NULL;
94  if (root) {
95  valentry<type_t> * p;
96  for (p = root; p->next != NULL; p = p->next) ;
97  p->next = entry;
98  entry->prev = p;
99  }
100  else {
101  root = entry;
102  entry->prev = NULL;
103  }
104  last = entry;
105  size++;
106 }
107 
108 // Appends another value list at the end of the value list.
109 template <class type_t>
111  for (valentry<type_t> * ptr = p->root; ptr != NULL; ptr = ptr->next)
112  append (ptr->key, new type_t (*(ptr->value)));
113 }
114 
115 // Returns the size of the value list.
116 template <class type_t>
118  return size;
119 }
120 
121 // Removes any occurrence of the given key from the value list.
122 template <class type_t>
123 void valuelist<type_t>::del (char * n) {
124  valentry<type_t> * next = NULL;
125  for (valentry<type_t> * p = root; p != NULL; p = next) {
126  next = p->next;
127  if (!strcmp (p->key, n)) {
128  if (p == root) {
129  root = p->next;
130  if (root) root->prev = NULL;
131  }
132  else {
133  p->prev->next = p->next;
134  if (p->next) p->next->prev = p->prev;
135  }
136  if (p == last) last = p->prev;
137  delete p;
138  size--;
139  }
140  }
141 }
142 
143 // Returns the number of occurrences of the given key in the list.
144 template <class type_t>
146  int count = 0;
147  for (valentry<type_t> * p = root; p != NULL; p = p->next) {
148  if (!strcmp (p->key, n)) count++;
149  }
150  return count;
151 }
152 
153 // Returns the value associated with the given key.
154 template <class type_t>
155 type_t * valuelist<type_t>::get (const char * n) {
156  for (valentry<type_t> * p = root; p != NULL; p = p->next) {
157  if (!strcmp (p->key, n)) return p->value;
158  }
159  return NULL;
160 }
161 
162 // Constructor for value list iterator.
163 template <class type_t>
165  _valuelist = &v;
166  toLast ();
167  toFirst ();
168 }
169 
170 // Destructor for value list iterator.
171 template <class type_t>
173 }
174 
175 // Returns number of items this iterator operates on.
176 template <class type_t>
178  return _valuelist->size;
179 }
180 
181 // Sets the current to the first item in the iterator list.
182 template <class type_t>
184  _current = _first = _valuelist->root;
185  return _current ? _current->key : NULL;
186 }
187 
188 // Sets the current to the last item in the iterator list.
189 template <class type_t>
191  _current = _last = _valuelist->last;
192  return _current ? _current->key : NULL;
193 }
194 
195 // Makes the succeeding item current and returns the new current item.
196 template <class type_t>
198  _current = _current->next;
199  return _current ? _current->key : NULL;
200 }
201 
202 // Makes the preceding item current and returns the new current item.
203 template <class type_t>
205  _current = _current->prev;
206  return _current ? _current->key : NULL;
207 }
208 
209 // Returns the current iterator item.
210 template <class type_t>
212  return _current ? _current->key : NULL;
213 }
214 
215 // Returns the current iterator items key.
216 template <class type_t>
218  return current ();
219 }
220 
221 // Returns the current iterator items value.
222 template <class type_t>
224  return _current ? _current->value : NULL;
225 }
226 
227 // Returns the first iterator item.
228 template <class type_t>
230  return _first ? _first->key : NULL;
231 }
232 
233 // Returns the last iterator item.
234 template <class type_t>
236  return _last ? _last->key : NULL;
237 }