summaryrefslogtreecommitdiff
path: root/libsidplay2/sidplay-libs-2.1.0/libsidutils/src/ini/keys.i
blob: 5507b95dd62b5c90aa58e151cc44fead66abbc7b (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/***************************************************************************
                          keys.i - Key Manipulation Functions

                             -------------------
    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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "keys.h"

#define INI_EQUALS_ALIGN 10


/********************************************************************************************************************
 * Function          : __ini_addKey
 * Parameters        : ini - pointer to ini file database.  key - key name
 * Returns           : Pointer to key.
 * Globals Used      :
 * Globals Modified  :
 * Description       : Adds a new key to the ini file database and updates the temporary workfile.
 *                   : A heading operation must be called before this function.
 ********************************************************************************************************************
 *  Rev   |   Date   |  By   | Comment
 * ----------------------------------------------------------------------------------------------------------------
 ********************************************************************************************************************/
struct key_tag *__ini_addKey (ini_t *ini, char *key)
{
    struct key_tag *_key;
    size_t length;
    long   pos;

    // Format heading for storing
    __ini_strtrim (key);
    if (!*key)
        return NULL;

    // Add new key to work file and read it in
    // using file add
    fseek (ini->ftmp, 0, SEEK_END);
    pos = ftell (ini->ftmp);
    fputs (key, ini->ftmp);
    length = (size_t) (ftell (ini->ftmp) - pos);
    _key   = __ini_faddKey (ini, ini->ftmp, pos, length);
    fseek (ini->ftmp, 0, SEEK_END);
    fputc ('=', ini->ftmp);
    return _key;
}


/********************************************************************************************************************
 * Function          : __ini_averageLengthKey
 * Parameters        : current_h - pointer to current header.
 * Returns           : Returns the average key length
 * Globals Used      :
 * Globals Modified  :
 * Description       : Finds average key length for aligning equals.
 ********************************************************************************************************************/
size_t __ini_averageLengthKey (struct section_tag *current_h)
{
#ifdef INI_EQUALS_ALIGN
    size_t equal_pos, equal_max, keylength;
    size_t average = 0, count = 0;
    struct key_tag *current_k;
   
    // Rev 1.1 Added - Line up equals characters for keys
    // Calculate Average
    current_k = current_h->first;
    while (current_k)
    {
        count++;
        average  += strlen (current_k->key);
        current_k = current_k->pNext;
    }

    if (!count)
        return 0;

    average  /= count;
    equal_pos = (equal_max = average);

#if INI_EQUALS_ALIGN > 0
    // Work out the longest key in that range
    current_k = current_h->first;
    while (current_k)
    {
        keylength = strlen (current_k->key);
        equal_max = average + INI_EQUALS_ALIGN;

        if ((equal_max > keylength) && (keylength > equal_pos))
            equal_pos = keylength;
        current_k = current_k->pNext;
    }
#endif // INI_EQUALS_ALIGN > 0
    return equal_pos;
#else
    return 0;
#endif // INI_EQUALS_ALIGN
}


/********************************************************************************************************************
 * Function          : __ini_faddKey
 * Parameters        : ini - pointer to ini file database,  file - ini file to read key from
 *                   : pos - key position in file, length - key length
 * Returns           : Pointer to key.
 * Globals Used      :
 * Globals Modified  :
 * Description       : Adds a new key to the ini file database from the input file.
 *                   : A heading operation must be called before this function.
 ********************************************************************************************************************
 *  Rev   |   Date   |  By   | Comment
 * ----------------------------------------------------------------------------------------------------------------
 ********************************************************************************************************************/
struct key_tag *__ini_faddKey (ini_t *ini, FILE *file, long pos, size_t length)
{
    struct key_tag *_key;
    char  *str;

    length++;
    str = (char *) malloc (sizeof(char) * length);
    assert (str);
    fseek  (file, pos, SEEK_SET);
    fgets  (str, (int) length, file);
    __ini_strtrim (str);

    _key = __ini_createKey (ini, str);
    if (!_key)
    {   free (str);
        return NULL;
    }

    _key->pos = pos + (long) length;
    return _key;
}


/********************************************************************************************************************
 * Function          : __ini_createKey
 * Parameters        : ini - pointer to ini file database.  key - key name
 * Returns           : Pointer to key.
 * Globals Used      :
 * Globals Modified  :
 * Description       : Adds an entry into the key linked list ready for formating by the addKey commands
 *                   : A heading operation must be called before this function.
 ********************************************************************************************************************
 *  Rev   |   Date   |  By   | Comment
 * ----------------------------------------------------------------------------------------------------------------
 ********************************************************************************************************************/
struct key_tag *__ini_createKey (ini_t *ini, char *key)
{
    struct section_tag *section;
    struct key_tag     *pNew;
    long   pos;

    if (!*key)
        return NULL;

    section = ini->selected;
    pNew    = __ini_locateKey (ini, key);
    if (pNew)
    {   // Reset details of existing key
        free (pNew->key);
        pNew->key = key;
        pos       = 0;
    }
    else
    {   // Create a new key and add at end;
        pNew = (struct key_tag *) malloc (sizeof (struct key_tag));
        if (!pNew)
            return NULL;
        memset (pNew, 0, sizeof (struct key_tag));
        pNew->key = key;

        if (!section->first)
            section->first = pNew;
        else
            section->last->pNext = pNew;

        pNew->pPrev       = section->last;
        section->last     = pNew;
        section->selected = pNew;

#ifdef INI_USE_HASH_TABLE
        {   // Rev 1.3 - Added
            struct   key_tag *pOld;
            unsigned long crc32;
            unsigned char accel;

            crc32     = __ini_createCrc32 (key, strlen (key));
            pNew->crc = crc32;
            // Rev 1.3 - Add accelerator list
            accel = (unsigned char) crc32 & 0x0FF;
            pNew->pPrev_Acc = NULL;
            pOld = section->keys[accel];
            section->keys[accel]      = pNew;
            if (pOld) pOld->pPrev_Acc = pNew;
            pNew->pNext_Acc           = pOld;
        }
#endif
    }

    section->selected = pNew;
    ini->changed      = true;
    return pNew;
}


/********************************************************************************************************************
 * Function          : __ini_deleteKey
 * Parameters        : ini - pointer to ini file database.  key - key name
 * Returns           :
 * Globals Used      :
 * Globals Modified  :
 * Description       : Removes a key from the database only.
 *                   : This change does not occur in the file until ini_close is called.
 *                   : A heading operation must be called before this function.
 ********************************************************************************************************************
 *  Rev   |   Date   |  By   | Comment
 * ----------------------------------------------------------------------------------------------------------------
 ********************************************************************************************************************/
void __ini_deleteKey (ini_t *ini)
{
    struct key_tag     *current_k;
    struct section_tag *section = ini->selected;

    current_k = section->selected;
    if (current_k)
    {
        // Tidy up all users of this key
        section->selected =  NULL;
        if (section->last == current_k)
            section->last =  current_k->pPrev;

        // Check to see if all keys were removed
        if (!current_k->pPrev)
            section->first = current_k->pNext;
        else
            current_k->pPrev->pNext = current_k->pNext;
        if (current_k->pNext)
            current_k->pNext->pPrev = current_k->pPrev;

#ifdef INI_USE_HASH_TABLE
        // Rev 1.3 - Take member out of accelerator list
        if (!current_k->pPrev_Acc)
            section->keys[(unsigned char) current_k->crc & 0x0FF] = current_k->pNext_Acc;
        else
            current_k->pPrev_Acc->pNext_Acc = current_k->pNext_Acc;
        if (current_k->pNext_Acc)
            current_k->pNext_Acc->pPrev_Acc = current_k->pPrev_Acc;
#endif // INI_USE_HASH_TABLE

        // Delete Key
        free (current_k->key);
        free (current_k);
        ini->changed = true;
    }
}


/********************************************************************************************************************
 * Function          : __ini_locateKey
 * Parameters        : ini - pointer to ini file database.  key - key name
 * Returns           : Pointer to key.
 * Globals Used      :
 * Globals Modified  :
 * Description       : Locates a key entry in the database.
 *                   : A heading operation must be called before this function.
 ********************************************************************************************************************
 *  Rev   |   Date   |  By   | Comment
 * ----------------------------------------------------------------------------------------------------------------
 ********************************************************************************************************************/
struct key_tag *__ini_locateKey (ini_t *ini, char *key)
{
    struct key_tag     *current_k;
    struct section_tag *section;
    section = ini->selected;

#ifdef INI_USE_HASH_TABLE
    {   // Rev 1.3 - Added
        unsigned long crc32;
        crc32 = __ini_createCrc32 (key, strlen (key));

        // Search for key
        for (current_k = section->keys[(unsigned char) crc32 & 0x0FF]; current_k; current_k = current_k->pNext_Acc)
        {
            if (current_k->crc == crc32)
        {
                if (!strcmp (current_k->key, key))
                break;
            }
        }
    }
#else
    {   // Search for key
        for (current_k = section->first; current_k; current_k = current_k->pNext)
        {
            if (!strcmp (current_k->key, key))
            break;
        }
    }
#endif // INI_USE_HASH_TABLE

    section->selected = current_k;
    return current_k;
}


/********************************************************************************************************************
 * Function          : (public) ini_deleteKey
 * Parameters        : ini - pointer to ini file database.  heading - heading name.  key - key name.
 * Returns           : -1 for Error and 0 on success
 * Globals Used      :
 * Globals Modified  :
 * Description       : Equivalent Microsoft write string API call where data set to NULL
 ********************************************************************************************************************/
int INI_LINKAGE ini_deleteKey (ini_fd_t fd)
{
    ini_t *ini = (ini_t *) fd;
    if (!ini->selected)
        return -1;
    // Can't delete a temporary key
    if (ini->selected->selected == &(ini->tmpKey))
        return -1;
    
    __ini_deleteKey (ini);
    return 0;
}


/********************************************************************************************************************
 * Function          : (public) ini_locateKey
 * Parameters        : fd - pointer to ini file descriptor
 * Returns           : -1 for Error and 0 on success
 * Globals Used      :
 * Globals Modified  :
 * Description       : 
 ********************************************************************************************************************/
int INI_LINKAGE ini_locateKey (ini_fd_t fd, char *key)
{
    ini_t *ini = (ini_t *) fd;
    struct key_tag *_key = NULL;

    if (!key)
        return -1;
    if (!ini->selected)
        return -1;

    // Can't search for a key in a temporary heading
    if (ini->selected != &(ini->tmpSection))
        _key = __ini_locateKey (ini, key);

#ifdef INI_ADD_LIST_SUPPORT
    // Rev 1.1 - Remove buffered list
    if (ini->list)
    {
        free (ini->list);
        ini->list = NULL;
    }
#endif // INI_ADD_LIST_SUPPORT

    if (_key)
        return 0;

    // Ok no key was found, but maybe the user is wanting to create a
    // new one so create it temporarily and see what actually happens later
    {
        char  *p;
        size_t length;
        // Remove all key
        _key = &(ini->tmpKey);
       if (_key->key)
            free (_key->key);

        // Add new key
        length = strlen (key) + 1;
        p = (char *) malloc (length);
        if (!p)
            return -1;
        memcpy (p, key, length);
        _key->key = p;
        ini->selected->selected = _key;
    }
    return -1;
}