Qucs-core  0.0.19
strlist.cpp
Go to the documentation of this file.
00001 /*
00002  * strlist.cpp - string list class implementation
00003  *
00004  * Copyright (C) 2003, 2004, 2005, 2006 Stefan Jahn <stefan@lkcc.org>
00005  *
00006  * This is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2, or (at your option)
00009  * any later version.
00010  *
00011  * This software is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this package; see the file COPYING.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  * $Id$
00022  *
00023  */
00024 
00025 #if HAVE_CONFIG_H
00026 # include <config.h>
00027 #endif
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 
00033 #include "strlist.h"
00034 
00035 namespace qucs {
00036 
00037 // Constructor creates an instance of the strlist class.
00038 strlist::strlist () {
00039   root = NULL;
00040   txt = NULL;
00041 }
00042 
00043 /* This copy constructor creates a instance of the strlist class based
00044    on the given strlist. */
00045 strlist::strlist (const strlist & o) {
00046   struct strlist_t * s;
00047   root = NULL;
00048   txt = NULL;
00049   for (s = o.root; s != NULL; s = s->next) append (s->str);
00050 }
00051 
00052 // Destructor deletes an instance of the strlist class.
00053 strlist::~strlist () {
00054   struct strlist_t * next;
00055   while (root) {
00056     next = root->next;
00057     free (root->str);
00058     free (root);
00059     root = next;
00060   }
00061   free (txt);
00062 }
00063 
00064 // This function adds a string to the list.
00065 void strlist::add (const char * const str) {
00066   struct strlist_t * s;
00067   s = (struct strlist_t *) calloc (sizeof (struct strlist_t), 1);
00068   s->next = root;
00069   s->str = str ? strdup (str) : NULL;
00070   root = s;
00071 }
00072 
00073 // The function adds the given string list to the list.
00074 void strlist::add (const strlist * const lst) {
00075   if (lst)
00076     for (int i = lst->length () - 1; i >= 0; i--)
00077       add (lst->get (i));
00078 }
00079 
00080 // The function apends the given string list to the list.
00081 void strlist::append (const strlist * const lst) {
00082   if (lst) for (int i = 0; i < lst->length (); i++)
00083              append (lst->get (i));
00084 }
00085 
00086 // This function append a string to the list.
00087 void strlist::append (const char * const str) {
00088   struct strlist_t * s;
00089   s = (struct strlist_t *) calloc (sizeof (struct strlist_t), 1);
00090   s->next = NULL;
00091   s->str = str ? strdup (str) : NULL;
00092   if (root) {
00093     struct strlist_t * e;
00094     for (e = root; e->next != NULL; e = e->next) ;
00095     e->next = s;
00096   }
00097   else {
00098     root = s;
00099   }
00100 }
00101 
00102 // This function counts the string in the list.
00103 int strlist::length (void) const {
00104   int res = 0;
00105   for (struct strlist_t * s = root; s != NULL; s = s->next) res++;
00106   return res;
00107 }
00108 
00109 // This function finds the specified string in the list.
00110 int strlist::contains (const char * const str) const {
00111   int res = 0;
00112   for (struct strlist_t * s = root; s != NULL; s = s->next) {
00113     if (s->str != NULL && str != NULL && !strcmp (s->str, str))
00114       res++;
00115   }
00116   return res;
00117 }
00118 
00119 /* The returns the position of the first occurrence of the specified
00120    string in the list or -1 if it does not contains the string. */
00121 int strlist::index (char * str) {
00122   int res = 0;
00123   for (struct strlist_t * s = root; s != NULL; s = s->next, res++) {
00124     if (s->str != NULL && str != NULL && !strcmp (s->str, str))
00125       return res;
00126   }
00127   return -1;
00128 }
00129 
00130 /* This function returns the string positioned at the specified
00131    location in the string list. */
00132 char * strlist::get (int pos) const {
00133   struct strlist_t * s = root;
00134   for (int i = 0; i < pos && s != NULL; s = s->next, i++) ;
00135   return s ? s->str : NULL;
00136 }
00137 
00138 /* This function returns the string positioned at the end of the
00139    string list. */
00140 char * strlist::last (void) const {
00141   struct strlist_t * s;
00142   for (s = root; s != NULL && s->next != NULL; s = s->next) ;
00143   return s ? s->str : NULL;
00144 }
00145 
00146 /* This function returns the string positioned at the beginning of the
00147    string list. */
00148 char * strlist::first (void) const {
00149   struct strlist_t * s = root;
00150   return s ? s->str : NULL;
00151 }
00152 
00153 /* The function removes each occurrence of the given string list entry
00154    from the string list object. */
00155 void strlist::del (strlist * cand) {
00156   if (cand == NULL) return;
00157   struct strlist_t * next;
00158   strlist * res = new strlist ();
00159   while (root) {
00160     next = root->next;
00161     if (cand->contains (root->str) == 0) res->append (root->str);
00162     free (root->str);
00163     free (root);
00164     root = next;
00165   }
00166   *this = *res;
00167 }
00168 
00169 /* The function joins the given string lists to each other and returns
00170    the resulting list. */
00171 strlist * strlist::join (strlist * pre, strlist * post) {
00172   strlist * res = pre ? new strlist (*pre) : new strlist ();
00173   for (int i = 0; post != NULL && i < post->length (); i++)
00174     res->append (post->get (i));
00175   return res;
00176 }
00177 
00178 /* The function returns a space seperated string representation of the
00179    string list instance. */
00180 char * strlist::toString (const char * concat) {
00181   if (txt) { free (txt); txt = NULL; }
00182   int size = 0;
00183   for (struct strlist_t * s = root; s != NULL; s = s->next) {
00184     char * t = s->str ? s->str : (char *) "(null)";
00185     int len = strlen (t);
00186     size += len + strlen (concat) + 1;
00187     txt = (char *) (txt ? realloc (txt, size) : malloc (size));
00188     txt = (s == root) ? strcpy (txt, t) : strcat (txt, t);
00189     txt = strcat (txt, concat);
00190   }
00191   if (txt) txt[strlen (txt) - 1] = '\0';
00192   return txt ? txt : (char *) "";
00193 }
00194 
00195 // Constructor for string list iterator.
00196 strlistiterator::strlistiterator (strlist & s) {
00197   _strlist = &s;
00198   toLast ();
00199   toFirst ();
00200 }
00201 
00202 // Another constructor for string list iterator.
00203 strlistiterator::strlistiterator (strlist * s) {
00204   _strlist = s;
00205   toLast ();
00206   toFirst ();
00207 }
00208 
00209 // Default constructor for string list iterator.
00210 strlistiterator::strlistiterator () {
00211   _strlist = NULL;
00212   _first = _last = _current = NULL;
00213 }
00214 
00215 // Destructor for string list iterator.
00216 strlistiterator::~strlistiterator () {
00217 }
00218 
00219 // Returns number of items this iterator operates on.
00220 int strlistiterator::count (void) {
00221   return _strlist->length ();
00222 }
00223 
00224 // Sets the current to the first item in the iterator list.
00225 char * strlistiterator::toFirst (void) {
00226   _current = _first = _strlist->root;
00227   return _current ? _current->str : NULL;
00228 }
00229 
00230 // Sets the current to the last item in the iterator list.
00231 char * strlistiterator::toLast (void) {
00232   for (_last = _strlist->root; _last && _last->next; _last = _last->next) ;
00233   _current = _last;
00234   return _current ? _current->str : NULL;
00235 }
00236 
00237 // Makes the succeeding item current and returns the new current item.
00238 char * strlistiterator::operator++ (void) {
00239   _current = _current->next;
00240   return _current ? _current->str : NULL;
00241 }
00242 
00243 // Returns the current iterator item.
00244 char * strlistiterator::current (void) {
00245   return _current ? _current->str : NULL;
00246 }
00247 
00248 // Returns the first iterator item.
00249 char * strlistiterator::first (void) {
00250   return _first ? _first->str : NULL;
00251 }
00252 
00253 // Returns the last iterator item.
00254 char * strlistiterator::last (void) {
00255   return _last ? _last->str : NULL;
00256 }
00257 
00258 } // namespace qucs