summaryrefslogtreecommitdiff
path: root/sid/sidplay-libs-2.1.0/libsidutils/src/ini/list.i
blob: 57f6011d4b69015158c650606ea7bc9417a1a50e (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/***************************************************************************
                          list.i - Adds list support to ini files

                             -------------------
    begin                : Fri Apr 21 2000
    copyright            : (C) 2000 by Simon White
    email                : s_a_white@email.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <stdlib.h>
#include <string.h>
#include "list.h"

#ifdef INI_ADD_LIST_SUPPORT

/********************************************************************************************************************
 * Function          : ini_listEval
 * Parameters        : ini - pointer to ini file database.
 * Returns           : -1 for Error or the numbers of list items found
 * Globals Used      :
 * Globals Modified  :
 * Description       : Uses the currently specified delimiters to calculate the number of eliments in a list
 ********************************************************************************************************************
 *  Rev   |   Date   |  By   | Comment
 * ----------------------------------------------------------------------------------------------------------------
 ********************************************************************************************************************/
int __ini_listEval (ini_t *ini)
{
    int  length, count, i, ret;
    int  ldelim;
    char ch;

    // Remove old list
    if (ini->list)
    {
        free (ini->list);
        ini->list = '\0';
    }

    // Re-evaluate with new settings
    length = ini->selected->selected->length;
    if (length < 0)
        return -1;
    if (!length)
    {
        ini->listIndex  = '\0';
        ini->listLength = 0;
        if (ini->selected->selected == &ini->tmpKey)
            return -1; // Can't read tmpKey
        return 0;
    }

    // See if there are any chars which can be used to split the string into sub ones
    if (!ini->listDelims)
        return -1;
    ldelim = (int) strlen (ini->listDelims);
        
    // Buffer string for faster access
    ini->list = (char *) malloc (length + 1);
    if (!ini->list)
        return -1;

    {   // Backup up delims to avoid causing problems with readString
        char *delims    = ini->listDelims;
        ini->listDelims = NULL;
        ret = ini_readString ((ini_fd_t) ini, ini->list, length + 1);
        ini->listDelims = delims;
        if (ret < 0)
            return -1;
    }
    
    // Process buffer string to find number of sub strings
    {
        char lastch = '\0';
        count = 1;
        while (length)
        {
            length--;
            ch = ini->list[length];
            for (i = 0; i < ldelim; i++)
            {
                if ((char) ch == ini->listDelims[i])
                {   // Prevent lots of NULL strings on multiple
                    // whitespace
                    if (lastch == '\0')
                    {
                        if (isspace (ch))
                        {
                            ch = '\0';
                            break;
                        }
                    }
 
                    // Seperate strings
                    ini->list[length] = (ch = '\0');
                    count++;
                    break;
                }
            }
            lastch = ch;
        }
    }

    ini->listLength   = count;
    ini->listIndexPtr = ini->list;
    ini->listIndex    = 0;
    return count;
}


/********************************************************************************************************************
 * Function          : __ini_listRead
 * Parameters        : ini - pointer to ini file database.
 * Returns           : NULL for error, string point otherwise
 * Globals Used      :
 * Globals Modified  :
 * Description       : Reads the indexed parameter from the list and auto
 *                   : increments
 ********************************************************************************************************************
 *  Rev   |   Date   |  By   | Comment
 * ----------------------------------------------------------------------------------------------------------------
 ********************************************************************************************************************/
char *__ini_listRead (ini_t *ini)
{
    char *p;

    // we must read an element from the list
    // Rev 1.2 Changed order of these two ifs as test was performed
    // sometimes before anything was read
    if (!ini->list)
    {
        if (__ini_listEval (ini) < 0)
            return 0;
    }

    // Check to see if we are trying to get a value beyond the end of the list
    if (ini->listIndex >= ini->listLength)
        return 0;
    p = ini->listIndexPtr;
    // Auto increment pointers to next index
    ini->listIndexPtr += (strlen (ini->listIndexPtr) + 1);
    ini->listIndex++;
    return p;
}


/********************************************************************************************************************
 * Function          : ini_listLength
 * Parameters        : ini - pointer to ini file database.  heading - heading name.  key - key name.
 * Returns           : -1 for Error or the numbers of list items found
 * Globals Used      :
 * Globals Modified  :
 * Description       : Uses the currently specified delimiters to calculate the number of eliments in a list
 ********************************************************************************************************************
 *  Rev   |   Date   |  By   | Comment
 * ----------------------------------------------------------------------------------------------------------------
 ********************************************************************************************************************/
int INI_LINKAGE ini_listLength (ini_fd_t fd)
{
    ini_t *ini = (ini_t *) fd;
    struct key_tag *_key;

    // Check to make sure a section/key has
    // been asked for by the user
    if (!ini->selected)
        return -1;
    _key = ini->selected->selected;
    if (!_key)
        return -1;

    // Check to see if we have moved to a new key
    if (!ini->list)
        return __ini_listEval (ini);

    return ini->listLength;
}


/********************************************************************************************************************
 * Function          : ini_listDelims
 * Parameters        : ini - pointer to ini file database.  delims - string of delimitor chars
 * Returns           : -1 for Error or 0 for success
 * Globals Used      :
 * Globals Modified  :
 * Description       : Sets the delimiters used for list accessing, (default delim is NULL)
 ********************************************************************************************************************
 *  Rev   |   Date   |  By   | Comment
 * ----------------------------------------------------------------------------------------------------------------
 ********************************************************************************************************************/
int INI_LINKAGE ini_listDelims (ini_fd_t fd, char *delims)
{
    ini_t *ini = (ini_t *) fd;
    if (ini->listDelims)
        free (ini->listDelims);
    ini->listDelims = NULL;

    // Make sure we have something to copy
    if (delims)
    {
        if (*delims)
        {   // Store delims for later use
            ini->listDelims = (char *) malloc (strlen (delims) + 1);
            if (!ini->listDelims)
                return -1;
            strcpy (ini->listDelims, delims);
        }
    }

    // List will need recalculating at some point
    if (ini->list)
    {
        free (ini->list);
        ini->list = NULL;
    }
    return 0;
}


/********************************************************************************************************************
 * Function          : ini_listIndex
 * Parameters        : ini - pointer to ini file database.  delims - string of delimitor chars
 * Returns           : 
 * Globals Used      :
 * Globals Modified  :
 * Description       : Sets the index that the next call to any of the read function will obtain (default 0)
 ********************************************************************************************************************
 *  Rev   |   Date   |  By   | Comment
 * ----------------------------------------------------------------------------------------------------------------
 ********************************************************************************************************************/
int INI_LINKAGE ini_listIndex (ini_fd_t fd, unsigned long index)
{
    ini_t *ini = (ini_t *) fd;
    unsigned int count;
    char    *p;

    // Check to make sure a section/key has
    // been asked for by the user
    if (!ini->selected)
        return -1;
    if (!ini->selected->selected)
        return -1;

    // Pull in new list
    if (!ini->list)
    {
        if (__ini_listEval (ini) < 0)
            return -1;
    }

    // Now scroll through to required index
    if (!ini->listLength)
        return -1;
    if (index == ini->listIndex)
        return 0;

    if (index > ini->listIndex)
    {   // Continue search from the from current position
        count = ini->listIndex;
        p     = ini->listIndexPtr;
    }
    else
    {   // Reset list and search from beginning
        count = 0;
        p     = ini->list;
    }
    
    while (count != index)
    {
        count++;
        if (count >= ini->listLength)
            return -1;
        // Jump to next sub string
        p += (strlen (p) + 1);
    }

    ini->listIndex    = count;
    ini->listIndexPtr = p;
    return 0;
}


/********************************************************************************************************************
 * Function          : __ini_listIndexLength
 * Parameters        : ini - pointer to ini file database
 * Returns           : 
 * Globals Used      :
 * Globals Modified  :
 * Description       : Returns the length the indexed sub string
 ********************************************************************************************************************
 *  Rev   |   Date   |  By   | Comment
 * ----------------------------------------------------------------------------------------------------------------
 ********************************************************************************************************************/
int __ini_listIndexLength (ini_t *ini)
{
    if (!ini->list)
    {   // No list yet.  So try to get one
        if (__ini_listEval (ini) < 0)
            return -1;
    }

    // Now return length
    return strlen (ini->listIndexPtr);
}

#endif // INI_ADD_LIST_SUPPORT