stringlist2.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  $RCSfile$
00003  -------------------
00004  cvs         : $Id$
00005  begin       : Thu Apr 03 2003
00006  copyright   : (C) 2003 by Martin Preuss
00007  email       : martin@libchipcard.de
00008 
00009  ***************************************************************************
00010  *                                                                         *
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU Lesser General Public            *
00013  *   License as published by the Free Software Foundation; either          *
00014  *   version 2.1 of the License, or (at your option) any later version.    *
00015  *                                                                         *
00016  *   This library is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00019  *   Lesser General Public License for more details.                       *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU Lesser General Public      *
00022  *   License along with this library; if not, write to the Free Software   *
00023  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
00024  *   MA  02111-1307  USA                                                   *
00025  *                                                                         *
00026  ***************************************************************************/
00027 
00028 
00029 #ifdef HAVE_CONFIG_H
00030 # include <config.h>
00031 #endif
00032 
00033 #include <gwenhywfar/gwenhywfarapi.h>
00034 #include <gwenhywfar/misc.h>
00035 #include "stringlist2_p.h"
00036 #include "debug.h"
00037 #include <stdlib.h>
00038 #include <assert.h>
00039 #include <string.h>
00040 #ifdef HAVE_STRINGS_H
00041 # include <strings.h>
00042 #endif
00043 
00044 
00045 GWEN_STRINGLIST2 *GWEN_StringList2_new(){
00046   GWEN_STRINGLIST2 *sl2;
00047   GWEN_REFPTR_INFO *rpi;
00048 
00049   GWEN_NEW_OBJECT(GWEN_STRINGLIST2, sl2);
00050   rpi=GWEN_RefPtrInfo_new();
00051   GWEN_RefPtrInfo_SetFreeFn(rpi,
00052                             (GWEN_REFPTR_INFO_FREE_FN)free);
00053   sl2->listPtr=GWEN_List_new();
00054   GWEN_List_SetRefPtrInfo(sl2->listPtr, rpi);
00055   GWEN_RefPtrInfo_free(rpi);
00056 
00057   return sl2;
00058 }
00059 
00060 
00061 
00062 void GWEN_StringList2_free(GWEN_STRINGLIST2 *sl2){
00063   if (sl2) {
00064     GWEN_List_free(sl2->listPtr);
00065     GWEN_FREE_OBJECT(sl2);
00066   }
00067 }
00068 
00069 
00070 
00071 GWEN_STRINGLIST2 *GWEN_StringList2_dup(GWEN_STRINGLIST2 *sl2){
00072   GWEN_STRINGLIST2 *nsl2;
00073 
00074   GWEN_NEW_OBJECT(GWEN_STRINGLIST2, nsl2);
00075   nsl2->listPtr=GWEN_List_dup(sl2->listPtr);
00076   nsl2->senseCase=sl2->senseCase;
00077 
00078   return nsl2;
00079 }
00080 
00081 
00082 
00083 void GWEN_StringList2_SetSenseCase(GWEN_STRINGLIST2 *sl2, int i){
00084   assert(sl2);
00085   sl2->senseCase=i;
00086 }
00087 
00088 
00089 
00090 int GWEN_StringList2_AppendString(GWEN_STRINGLIST2 *sl2,
00091                                   const char *s,
00092                                   int take,
00093                                   GWEN_STRINGLIST2_INSERTMODE m) {
00094   GWEN_REFPTR *rp;
00095 
00096   assert(sl2);
00097   assert(s);
00098 
00099   if (m!=GWEN_StringList2_IntertMode_AlwaysAdd) {
00100     GWEN_STRINGLIST2_ITERATOR *it;
00101 
00102     it=GWEN_StringList2__GetString(sl2, s);
00103     if (it) {
00104       if (m==GWEN_StringList2_IntertMode_NoDouble) {
00105         if (take)
00106           free((void*)s);
00107         GWEN_StringList2Iterator_free(it);
00108         return 0;
00109       }
00110       if (m==GWEN_StringList2_IntertMode_Reuse) {
00111         GWEN_ListIterator_IncLinkCount((GWEN_LIST_ITERATOR*)it);
00112         if (take)
00113           free((void*)s);
00114         GWEN_StringList2Iterator_free(it);
00115         return 0;
00116       }
00117       GWEN_StringList2Iterator_free(it);
00118     }
00119   }
00120 
00121   if (take)
00122     rp=GWEN_RefPtr_new((void*)s, GWEN_List_GetRefPtrInfo(sl2->listPtr));
00123   else
00124     rp=GWEN_RefPtr_new(strdup(s), GWEN_List_GetRefPtrInfo(sl2->listPtr));
00125   GWEN_RefPtr_AddFlags(rp, GWEN_REFPTR_FLAGS_AUTODELETE);
00126   GWEN_List_PushBackRefPtr(sl2->listPtr, rp);
00127   return 1;
00128 }
00129 
00130 
00131 
00132 int GWEN_StringList2_InsertString(GWEN_STRINGLIST2 *sl2,
00133                                   const char *s,
00134                                   int take,
00135                                   GWEN_STRINGLIST2_INSERTMODE m) {
00136   GWEN_REFPTR *rp;
00137 
00138   assert(sl2);
00139   assert(s);
00140 
00141   if (m!=GWEN_StringList2_IntertMode_AlwaysAdd) {
00142     GWEN_STRINGLIST2_ITERATOR *it;
00143 
00144     it=GWEN_StringList2__GetString(sl2, s);
00145     if (it) {
00146       if (m==GWEN_StringList2_IntertMode_NoDouble) {
00147         if (take)
00148           free((void*)s);
00149         GWEN_StringList2Iterator_free(it);
00150         return 0;
00151       }
00152       if (m==GWEN_StringList2_IntertMode_Reuse) {
00153         GWEN_ListIterator_IncLinkCount((GWEN_LIST_ITERATOR*)it);
00154         if (take)
00155           free((void*)s);
00156         GWEN_StringList2Iterator_free(it);
00157         return 0;
00158       }
00159       GWEN_StringList2Iterator_free(it);
00160     }
00161   }
00162 
00163   if (take)
00164     rp=GWEN_RefPtr_new((void*)s, GWEN_List_GetRefPtrInfo(sl2->listPtr));
00165   else
00166     rp=GWEN_RefPtr_new(strdup(s), GWEN_List_GetRefPtrInfo(sl2->listPtr));
00167   GWEN_RefPtr_AddFlags(rp, GWEN_REFPTR_FLAGS_AUTODELETE);
00168   GWEN_List_PushFrontRefPtr(sl2->listPtr, rp);
00169   return 1;
00170 }
00171 
00172 
00173 
00174 int GWEN_StringList2_RemoveString(GWEN_STRINGLIST2 *sl2,
00175                                   const char *s){
00176   GWEN_STRINGLIST2_ITERATOR *it;
00177 
00178   it=GWEN_StringList2__GetString(sl2, s);
00179   if (it) {
00180     int lc;
00181 
00182     lc=GWEN_ListIterator_GetLinkCount(it);
00183     GWEN_List_Erase(sl2->listPtr, it);
00184     GWEN_StringList2Iterator_free(it);
00185     if (lc<2)
00186       return 1;
00187   }
00188 
00189   return 0;
00190 }
00191 
00192 
00193 
00194 int GWEN_StringList2_HasString(const GWEN_STRINGLIST2 *sl2,
00195                                const char *s){
00196   GWEN_STRINGLIST2_ITERATOR *it;
00197   int gotIt;
00198 
00199   it=GWEN_StringList2_First(sl2);
00200   gotIt=0;
00201   if (it) {
00202     const char *t;
00203 
00204     t=GWEN_StringList2Iterator_Data(it);
00205     if (sl2->senseCase) {
00206       while(t) {
00207         if (strcmp(s, t)) {
00208           gotIt=1;
00209           break;
00210         }
00211         t=GWEN_StringList2Iterator_Next(it);
00212       }
00213     }
00214     else {
00215       while(t) {
00216         if (strcasecmp(s, t)) {
00217           gotIt=1;
00218           break;
00219         }
00220         t=GWEN_StringList2Iterator_Next(it);
00221       }
00222     }
00223     GWEN_StringList2Iterator_free(it);
00224   }
00225 
00226   return gotIt;
00227 }
00228 
00229 
00230 
00231 GWEN_STRINGLIST2_ITERATOR*
00232 GWEN_StringList2__GetString(const GWEN_STRINGLIST2 *sl2,
00233                             const char *s){
00234   GWEN_STRINGLIST2_ITERATOR *it;
00235   GWEN_REFPTR *rp;
00236 
00237   it=GWEN_StringList2_First(sl2);
00238   if (it) {
00239     rp=GWEN_ListIterator_DataRefPtr((GWEN_LIST_ITERATOR*)it);
00240 
00241     if (sl2->senseCase) {
00242       while(rp) {
00243         const char *t;
00244 
00245         t=(const char*)GWEN_RefPtr_GetData(rp);
00246         assert(t);
00247         if (strcmp(s, t)==0)
00248           return it;
00249         rp=GWEN_ListIterator_NextRefPtr((GWEN_LIST_ITERATOR*)it);
00250       }
00251     }
00252     else {
00253       while(rp) {
00254         const char *t;
00255 
00256         t=(const char*)GWEN_RefPtr_GetData(rp);
00257         assert(t);
00258         if (strcasecmp(s, t)==0)
00259           return it;
00260         rp=GWEN_ListIterator_NextRefPtr((GWEN_LIST_ITERATOR*)it);
00261       }
00262     }
00263     GWEN_StringList2Iterator_free(it);
00264   }
00265 
00266   return 0;
00267 }
00268 
00269 
00270 
00271 
00272 
00273 
00274 
00275 
00276 
00277 
00278 
00279 
00280 
00281 
00282 
00283 GWEN_STRINGLIST2_ITERATOR *GWEN_StringList2_First(const GWEN_STRINGLIST2 *l) {
00284   assert(l);
00285   return (GWEN_STRINGLIST2_ITERATOR*) GWEN_List_First(l->listPtr);
00286 }
00287 
00288 
00289 
00290 GWEN_STRINGLIST2_ITERATOR *GWEN_StringList2_Last(const GWEN_STRINGLIST2 *l) {
00291   assert(l);
00292   return (GWEN_STRINGLIST2_ITERATOR*) GWEN_List_Last(l->listPtr);
00293 }
00294 
00295 
00296 
00297 void GWEN_StringList2Iterator_free(GWEN_STRINGLIST2_ITERATOR *li) {
00298   assert(li);
00299   GWEN_ListIterator_free((GWEN_LIST_ITERATOR*)li);
00300 }
00301 
00302 
00303 const char *GWEN_StringList2Iterator_Previous(GWEN_STRINGLIST2_ITERATOR *li) {
00304   assert(li);
00305   return (const char*) GWEN_ListIterator_Previous((GWEN_LIST_ITERATOR*)li);
00306 }
00307 
00308 
00309 const char *GWEN_StringList2Iterator_Next(GWEN_STRINGLIST2_ITERATOR *li) {
00310   assert(li);
00311   return (const char*) GWEN_ListIterator_Next((GWEN_LIST_ITERATOR*)li);
00312 }
00313 
00314 
00315 const char *GWEN_StringList2Iterator_Data(GWEN_STRINGLIST2_ITERATOR *li) {
00316   assert(li);
00317   return (const char*) GWEN_ListIterator_Data((GWEN_LIST_ITERATOR*)li);
00318 }
00319 
00320 
00321 
00322 GWEN_REFPTR*
00323 GWEN_StringList2Iterator_DataRefPtr(GWEN_STRINGLIST2_ITERATOR *li) {
00324   assert(li);
00325   return (GWEN_REFPTR*) GWEN_ListIterator_DataRefPtr((GWEN_LIST_ITERATOR*)li);
00326 }
00327 
00328 
00329 
00330 unsigned int
00331 GWEN_StringList2Iterator_GetLinkCount(const GWEN_STRINGLIST2_ITERATOR *li){
00332   assert(li);
00333   return GWEN_ListIterator_GetLinkCount((const GWEN_LIST_ITERATOR*)li);
00334 }
00335 
00336 
00337 
00338 void GWEN_StringList2_Dump(const GWEN_STRINGLIST2 *sl2){
00339   GWEN_STRINGLIST2_ITERATOR *it;
00340 
00341   it=GWEN_StringList2_First(sl2);
00342   if (it) {
00343     const char *t;
00344     int i;
00345 
00346     t=GWEN_StringList2Iterator_Data(it);
00347     i=0;
00348     while(t) {
00349       fprintf(stderr, "String %d: \"%s\" [%d]\n", i, t,
00350               GWEN_StringList2Iterator_GetLinkCount(it));
00351       t=GWEN_StringList2Iterator_Next(it);
00352     }
00353     GWEN_StringList2Iterator_free(it);
00354   }
00355   else {
00356     fprintf(stderr, "Empty string list.\n");
00357   }
00358 }
00359 
00360 
00361 
00362 
00363 
00364 
00365 
00366 
00367 

Generated on Thu Aug 20 13:54:39 2009 for gwenhywfar by  doxygen 1.5.9