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