File: stringlist.c

package info (click to toggle)
fusesmb 0.8.7-1.3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 592 kB
  • ctags: 254
  • sloc: sh: 3,185; ansic: 2,912; makefile: 67
file content (237 lines) | stat: -rw-r--r-- 5,522 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * The MIT License
 *
 * Copyright (c) 2006 Vincent Wagelaar
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "stringlist.h"
#include <strings.h>

#define NUM_ROWS_PER_MALLOC 128

static int sl_strcmp(const void *p1, const void *p2)
{
    return strcmp(*(char * const *)p1, *(char * const *)p2);
}

static int sl_strcasecmp(const void *p1, const void *p2)
{
    return strcasecmp(*(char * const *)p1, *(char * const *)p2);
}

/*
 * initialize the stringlist
 */
stringlist_t *sl_init(void)
{
    stringlist_t *sl;
    sl = (stringlist_t *)malloc(sizeof(stringlist_t));
    if (sl == NULL)
        return NULL;

    sl->lines = (char **)malloc(NUM_ROWS_PER_MALLOC * sizeof(char *));
    if (sl->lines == NULL)
        return NULL;
    sl->maxlines = NUM_ROWS_PER_MALLOC;
    sl->numlines = 0;
    sl->sorted = 0;
    return sl;
}
/*
 * free the stringlist
 */
void sl_free(stringlist_t *sl)
{
    size_t i;
    if (sl == NULL)
        return;
    if (sl->lines)
    {
        for (i=0; i < sl_count(sl); i++)
        {
            free(sl->lines[i]);
        }
        free(sl->lines);
    }
    free(sl);
}
/*
 * add string to stringlist
 * do_malloc: allocate memory for the string
 */
int sl_add(stringlist_t *sl, char *str, int do_malloc)
{
    /* resize the array if needed */
    //printf("Inserting %s %i\n", str, sl->numlines);
    if (sl->numlines == sl->maxlines)
    {
        //printf("Realloc\n");
        char **new;
        new = (char **)realloc(sl->lines, (sl->maxlines + NUM_ROWS_PER_MALLOC)*sizeof(char *));
        if (new == NULL)
        {
            //printf("Realloc failed\n");
            return -1;
        }
        sl->maxlines += NUM_ROWS_PER_MALLOC;
        sl->lines = new;
    }
    if (do_malloc)
    {
        sl->lines[sl->numlines] = (char *)malloc( (strlen(str)+1) * sizeof(char));
        if (NULL == sl->lines[sl->numlines])
        {
            return -1;
        }
        strcpy(sl->lines[sl->numlines], str);
        sl->numlines++;
        sl->sorted = 0;
        return 0;
   }
   sl->lines[sl->numlines] = str;
   sl->numlines++;
   sl->sorted = 0;
   return 0;
}

/*
 * return the number of items in the stringlist
 */
size_t sl_count(stringlist_t *sl)
{
    return sl->numlines;
}

void sl_clear(stringlist_t *sl)
{
    size_t i;
    for (i=0; i < sl_count(sl); i++)
    {
        free( sl->lines[i] );
    }
    sl->numlines = 0;
}

/*
 * return the item at the index: index
 */
char *sl_item(stringlist_t *sl, size_t index)
{
    if (sl_count(sl) == 0)
        return NULL;
    if (index >= sl_count(sl))
        return NULL;
    return sl->lines[index];
}
/*
 * search for a item in the stringlist
 */
char *sl_find(stringlist_t *sl, const char *str)
{
    /* use binary search if stringlist is sorted */
    if (sl->sorted == 1)
    {
        char **res;
        if (NULL != (res = bsearch (&str, sl->lines, sl_count(sl), sizeof(char *), sl_strcmp)))
            return *res;
        return NULL;
    }

    size_t i;
    for (i=0; i < sl_count(sl); i++)
    {
        if (strcmp(sl_item(sl, i), str) == 0)
        {
            return sl_item(sl, i);
        }
    }
    return NULL;
}

/*
 * case insensitive search
 */
char *sl_casefind(stringlist_t *sl, const char *str)
{
    /* use binary search if stringlist is case insensitively sorted */
    if (sl->sorted == 2)
    {
        char **res;
        if (NULL != (res = bsearch (&str, sl->lines, sl_count(sl), sizeof(char *), sl_strcasecmp)))
            return *res;
        return NULL;
    }

    size_t i;
    for (i=0; i < sl_count(sl); i++)
    {
        if (strcasecmp(sl_item(sl, i), str) == 0)
        {
            return sl_item(sl, i);
        }
    }
    return NULL;
}

/*
 * case sensitive sort of the stringlist
 */
void sl_sort(stringlist_t *sl)
{
    qsort(sl->lines, sl_count(sl), sizeof(char *), sl_strcmp);
    sl->sorted = 1;
}

/*
 * case insensitive sort of the stringlist
 */
void sl_casesort(stringlist_t *sl)
{
    qsort(sl->lines, sl_count(sl), sizeof(char *), sl_strcasecmp);
    sl->sorted = 2;
}
#if 0
int sl_remove(stringlist_t *sl, size_t index)
{
    if (sl_count(sl) == 0)
        return -1;
    if (index >= sl_count(sl))
        return -1;

    free(sl->lines[index]);
    sl->lines[index] = sl->lines[sl_count(sl)-1];
    sl->numlines--;
    sl->sorted = 0;
    return 0;
}


void sl_lock(stringlist_t *sl)
{
    pthread_mutex_lock(sl->mutex);
}

void sl_unlock(stringlist_t *sl)
{
    pthread_mutex_unlock(sl->mutex);
}
#endif