My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
strlist.cpp
Go to the documentation of this file.
1 /*
2  * strlist.cpp - string list class implementation
3  *
4  * Copyright (C) 2003, 2004, 2005, 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: strlist.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 <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "strlist.h"
34 
35 // Constructor creates an instance of the strlist class.
37  root = NULL;
38  txt = NULL;
39 }
40 
41 /* This copy constructor creates a instance of the strlist class based
42  on the given strlist. */
44  struct strlist_t * s;
45  root = NULL;
46  txt = NULL;
47  for (s = o.root; s != NULL; s = s->next) append (s->str);
48 }
49 
50 // Destructor deletes an instance of the strlist class.
52  struct strlist_t * next;
53  while (root) {
54  next = root->next;
55  if (root->str) free (root->str);
56  free (root);
57  root = next;
58  }
59  if (txt) free (txt);
60 }
61 
62 // This function adds a string to the list.
63 void strlist::add (char * str) {
64  struct strlist_t * s;
65  s = (struct strlist_t *) calloc (sizeof (struct strlist_t), 1);
66  s->next = root;
67  s->str = str ? strdup (str) : NULL;
68  root = s;
69 }
70 
71 // The function adds the given string list to the list.
72 void strlist::add (strlist * lst) {
73  if (lst) for (int i = lst->length () - 1; i >= 0; i--) add (lst->get (i));
74 }
75 
76 // The function apends the given string list to the list.
77 void strlist::append (strlist * lst) {
78  if (lst) for (int i = 0; i < lst->length (); i++) append (lst->get (i));
79 }
80 
81 // This function append a string to the list.
82 void strlist::append (char * str) {
83  struct strlist_t * s;
84  s = (struct strlist_t *) calloc (sizeof (struct strlist_t), 1);
85  s->next = NULL;
86  s->str = str ? strdup (str) : NULL;
87  if (root) {
88  struct strlist_t * e;
89  for (e = root; e->next != NULL; e = e->next) ;
90  e->next = s;
91  }
92  else {
93  root = s;
94  }
95 }
96 
97 // This function counts the string in the list.
98 int strlist::length (void) {
99  int res = 0;
100  for (struct strlist_t * s = root; s != NULL; s = s->next) res++;
101  return res;
102 }
103 
104 // This function finds the specified string in the list.
105 int strlist::contains (char * str) {
106  int res = 0;
107  for (struct strlist_t * s = root; s != NULL; s = s->next) {
108  if (s->str != NULL && str != NULL && !strcmp (s->str, str))
109  res++;
110  }
111  return res;
112 }
113 
114 /* The returns the position of the first occurrence of the specified
115  string in the list or -1 if it does not contains the string. */
116 int strlist::index (char * str) {
117  int res = 0;
118  for (struct strlist_t * s = root; s != NULL; s = s->next, res++) {
119  if (s->str != NULL && str != NULL && !strcmp (s->str, str))
120  return res;
121  }
122  return -1;
123 }
124 
125 /* This function returns the string positioned at the specified
126  location in the string list. */
127 char * strlist::get (int pos) {
128  struct strlist_t * s = root;
129  for (int i = 0; i < pos && s != NULL; s = s->next, i++) ;
130  return s ? s->str : NULL;
131 }
132 
133 /* This function returns the string positioned at the end of the
134  string list. */
135 char * strlist::last (void) {
136  struct strlist_t * s;
137  for (s = root; s != NULL && s->next != NULL; s = s->next) ;
138  return s ? s->str : NULL;
139 }
140 
141 /* This function returns the string positioned at the beginning of the
142  string list. */
143 char * strlist::first (void) {
144  struct strlist_t * s = root;
145  return s ? s->str : NULL;
146 }
147 
148 /* The function removes each occurrence of the given string list entry
149  from the string list object. */
150 void strlist::del (strlist * cand) {
151  if (cand == NULL) return;
152  struct strlist_t * next;
153  strlist * res = new strlist ();
154  while (root) {
155  next = root->next;
156  if (cand->contains (root->str) == 0) res->append (root->str);
157  if (root->str) free (root->str);
158  free (root);
159  root = next;
160  }
161  *this = *res;
162 }
163 
164 /* The function joins the given string lists to each other and returns
165  the resulting list. */
167  strlist * res = pre ? new strlist (*pre) : new strlist ();
168  for (int i = 0; post != NULL && i < post->length (); i++)
169  res->append (post->get (i));
170  return res;
171 }
172 
173 /* The function returns a space seperated string representation of the
174  string list instance. */
175 char * strlist::toString (const char * concat) {
176  if (txt) { free (txt); txt = NULL; }
177  int size = 0;
178  for (struct strlist_t * s = root; s != NULL; s = s->next) {
179  char * t = s->str ? s->str : (char *) "(null)";
180  int len = strlen (t);
181  size += len + strlen (concat) + 1;
182  txt = (char *) (txt ? realloc (txt, size) : malloc (size));
183  txt = (s == root) ? strcpy (txt, t) : strcat (txt, t);
184  txt = strcat (txt, concat);
185  }
186  if (txt) txt[strlen (txt) - 1] = '\0';
187  return txt ? txt : (char *) "";
188 }
189 
190 // Constructor for string list iterator.
192  _strlist = &s;
193  toLast ();
194  toFirst ();
195 }
196 
197 // Another constructor for string list iterator.
199  _strlist = s;
200  toLast ();
201  toFirst ();
202 }
203 
204 // Default constructor for string list iterator.
206  _strlist = NULL;
207  _first = _last = _current = NULL;
208 }
209 
210 // Destructor for string list iterator.
212 }
213 
214 // Returns number of items this iterator operates on.
216  return _strlist->length ();
217 }
218 
219 // Sets the current to the first item in the iterator list.
221  _current = _first = _strlist->root;
222  return _current ? _current->str : NULL;
223 }
224 
225 // Sets the current to the last item in the iterator list.
226 char * strlistiterator::toLast (void) {
227  for (_last = _strlist->root; _last && _last->next; _last = _last->next) ;
228  _current = _last;
229  return _current ? _current->str : NULL;
230 }
231 
232 // Makes the succeeding item current and returns the new current item.
234  _current = _current->next;
235  return _current ? _current->str : NULL;
236 }
237 
238 // Returns the current iterator item.
240  return _current ? _current->str : NULL;
241 }
242 
243 // Returns the first iterator item.
244 char * strlistiterator::first (void) {
245  return _first ? _first->str : NULL;
246 }
247 
248 // Returns the last iterator item.
249 char * strlistiterator::last (void) {
250  return _last ? _last->str : NULL;
251 }