"Fossies" - the Fresh Open Source Software Archive

Member "fusesmb-0.8.7/stringlist.c" (4 May 2006, 5522 Bytes) of package /linux/privat/old/fusesmb-0.8.7.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stringlist.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * The MIT License
    3  *
    4  * Copyright (c) 2006 Vincent Wagelaar
    5  *
    6  * Permission is hereby granted, free of charge, to any person obtaining a
    7  * copy of this software and associated documentation files (the
    8  * "Software"), to deal in the Software without restriction, including
    9  * without limitation the rights to use, copy, modify, merge, publish,
   10  * distribute, sublicense, and/or sell copies of the Software, and to
   11  * permit persons to whom the Software is furnished to do so, subject to
   12  * the following conditions:
   13  *
   14  * The above copyright notice and this permission notice shall be included
   15  * in all copies or substantial portions of the Software.
   16  *
   17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
   21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
   22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
   23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   24  */
   25 
   26 #include "stringlist.h"
   27 #include <strings.h>
   28 
   29 #define NUM_ROWS_PER_MALLOC 128
   30 
   31 static int sl_strcmp(const void *p1, const void *p2)
   32 {
   33     return strcmp(*(char * const *)p1, *(char * const *)p2);
   34 }
   35 
   36 static int sl_strcasecmp(const void *p1, const void *p2)
   37 {
   38     return strcasecmp(*(char * const *)p1, *(char * const *)p2);
   39 }
   40 
   41 /*
   42  * initialize the stringlist
   43  */
   44 stringlist_t *sl_init(void)
   45 {
   46     stringlist_t *sl;
   47     sl = (stringlist_t *)malloc(sizeof(stringlist_t));
   48     if (sl == NULL)
   49         return NULL;
   50 
   51     sl->lines = (char **)malloc(NUM_ROWS_PER_MALLOC * sizeof(char *));
   52     if (sl->lines == NULL)
   53         return NULL;
   54     sl->maxlines = NUM_ROWS_PER_MALLOC;
   55     sl->numlines = 0;
   56     sl->sorted = 0;
   57     return sl;
   58 }
   59 /*
   60  * free the stringlist
   61  */
   62 void sl_free(stringlist_t *sl)
   63 {
   64     size_t i;
   65     if (sl == NULL)
   66         return;
   67     if (sl->lines)
   68     {
   69         for (i=0; i < sl_count(sl); i++)
   70         {
   71             free(sl->lines[i]);
   72         }
   73         free(sl->lines);
   74     }
   75     free(sl);
   76 }
   77 /*
   78  * add string to stringlist
   79  * do_malloc: allocate memory for the string
   80  */
   81 int sl_add(stringlist_t *sl, char *str, int do_malloc)
   82 {
   83     /* resize the array if needed */
   84     //printf("Inserting %s %i\n", str, sl->numlines);
   85     if (sl->numlines == sl->maxlines)
   86     {
   87         //printf("Realloc\n");
   88         char **new;
   89         new = (char **)realloc(sl->lines, (sl->maxlines + NUM_ROWS_PER_MALLOC)*sizeof(char *));
   90         if (new == NULL)
   91         {
   92             //printf("Realloc failed\n");
   93             return -1;
   94         }
   95         sl->maxlines += NUM_ROWS_PER_MALLOC;
   96         sl->lines = new;
   97     }
   98     if (do_malloc)
   99     {
  100         sl->lines[sl->numlines] = (char *)malloc( (strlen(str)+1) * sizeof(char));
  101         if (NULL == sl->lines[sl->numlines])
  102         {
  103             return -1;
  104         }
  105         strcpy(sl->lines[sl->numlines], str);
  106         sl->numlines++;
  107         sl->sorted = 0;
  108         return 0;
  109    }
  110    sl->lines[sl->numlines] = str;
  111    sl->numlines++;
  112    sl->sorted = 0;
  113    return 0;
  114 }
  115 
  116 /*
  117  * return the number of items in the stringlist
  118  */
  119 size_t sl_count(stringlist_t *sl)
  120 {
  121     return sl->numlines;
  122 }
  123 
  124 void sl_clear(stringlist_t *sl)
  125 {
  126     size_t i;
  127     for (i=0; i < sl_count(sl); i++)
  128     {
  129         free( sl->lines[i] );
  130     }
  131     sl->numlines = 0;
  132 }
  133 
  134 /*
  135  * return the item at the index: index
  136  */
  137 char *sl_item(stringlist_t *sl, size_t index)
  138 {
  139     if (sl_count(sl) == 0)
  140         return NULL;
  141     if (index >= sl_count(sl))
  142         return NULL;
  143     return sl->lines[index];
  144 }
  145 /*
  146  * search for a item in the stringlist
  147  */
  148 char *sl_find(stringlist_t *sl, const char *str)
  149 {
  150     /* use binary search if stringlist is sorted */
  151     if (sl->sorted == 1)
  152     {
  153         char **res;
  154         if (NULL != (res = bsearch (&str, sl->lines, sl_count(sl), sizeof(char *), sl_strcmp)))
  155             return *res;
  156         return NULL;
  157     }
  158 
  159     size_t i;
  160     for (i=0; i < sl_count(sl); i++)
  161     {
  162         if (strcmp(sl_item(sl, i), str) == 0)
  163         {
  164             return sl_item(sl, i);
  165         }
  166     }
  167     return NULL;
  168 }
  169 
  170 /*
  171  * case insensitive search
  172  */
  173 char *sl_casefind(stringlist_t *sl, const char *str)
  174 {
  175     /* use binary search if stringlist is case insensitively sorted */
  176     if (sl->sorted == 2)
  177     {
  178         char **res;
  179         if (NULL != (res = bsearch (&str, sl->lines, sl_count(sl), sizeof(char *), sl_strcasecmp)))
  180             return *res;
  181         return NULL;
  182     }
  183 
  184     size_t i;
  185     for (i=0; i < sl_count(sl); i++)
  186     {
  187         if (strcasecmp(sl_item(sl, i), str) == 0)
  188         {
  189             return sl_item(sl, i);
  190         }
  191     }
  192     return NULL;
  193 }
  194 
  195 /*
  196  * case sensitive sort of the stringlist
  197  */
  198 void sl_sort(stringlist_t *sl)
  199 {
  200     qsort(sl->lines, sl_count(sl), sizeof(char *), sl_strcmp);
  201     sl->sorted = 1;
  202 }
  203 
  204 /*
  205  * case insensitive sort of the stringlist
  206  */
  207 void sl_casesort(stringlist_t *sl)
  208 {
  209     qsort(sl->lines, sl_count(sl), sizeof(char *), sl_strcasecmp);
  210     sl->sorted = 2;
  211 }
  212 #if 0
  213 int sl_remove(stringlist_t *sl, size_t index)
  214 {
  215     if (sl_count(sl) == 0)
  216         return -1;
  217     if (index >= sl_count(sl))
  218         return -1;
  219 
  220     free(sl->lines[index]);
  221     sl->lines[index] = sl->lines[sl_count(sl)-1];
  222     sl->numlines--;
  223     sl->sorted = 0;
  224     return 0;
  225 }
  226 
  227 
  228 void sl_lock(stringlist_t *sl)
  229 {
  230     pthread_mutex_lock(sl->mutex);
  231 }
  232 
  233 void sl_unlock(stringlist_t *sl)
  234 {
  235     pthread_mutex_unlock(sl->mutex);
  236 }
  237 #endif