summaryrefslogtreecommitdiff
path: root/sid/sidplay-libs-2.1.0/libsidutils/src/ini/headings.i
blob: 11833a990d5a3974d48f70f56507fe61bad8d019 (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
/***************************************************************************
                          headings.i - Section Heading 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 "headings.h"


/********************************************************************************************************************
 * Function          : __ini_addHeading
 * Parameters        : ini - pointer to ini file database.  heading - heading name
 * Returns           : Pointer to new heading.
 * Globals Used      :
 * Globals Modified  :
 * Description       : Adds a new heading to the ini file database and updates the temporary workfile
 ********************************************************************************************************************
 *  Rev   |   Date   |  By   | Comment
 * ----------------------------------------------------------------------------------------------------------------
 ********************************************************************************************************************/
struct section_tag *__ini_addHeading (ini_t *ini, char *heading)
{
    struct section_tag *section;
    size_t length;
    long   pos;

    // Format heading for storing
    __ini_strtrim (heading);

    /* Create a backup of the file we are about to edit */
    if (ini->write == heading)
        return __ini_locateHeading (ini, heading);

    // Add new heading to work file and read it in
    // using file add
    fseek (ini->ftmp, 0, SEEK_END);
    fputs ("\n[", ini->ftmp);
    pos = ftell (ini->ftmp);
    fputs (heading, ini->ftmp);
    length  = (size_t) (ftell (ini->ftmp) - pos);
    section = __ini_faddHeading (ini, ini->ftmp, pos, length);
    fseek (ini->ftmp, 0, SEEK_END);
    fputs ("]\n", ini->ftmp);
    ini->write = section->heading;
    return section;
}


/********************************************************************************************************************
 * Function          : __ini_faddheading
 * Parameters        : ini - pointer to ini file database,  file - ini file to read heading from
 *                   : pos - heading position in file, length - heading length
 * Returns           : Pointer to new heading.
 * Globals Used      :
 * Globals Modified  :
 * Description       : Adds a new heading to the ini file database from the input file.
 ********************************************************************************************************************
 *  Rev   |   Date   |  By   | Comment
 * ----------------------------------------------------------------------------------------------------------------
 ********************************************************************************************************************/
struct section_tag *__ini_faddHeading (ini_t *ini, FILE *file, long pos, size_t length)
{
    struct section_tag *section;
    char  *str;

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

    section = __ini_createHeading (ini, str);
    // Make sure heading was created
    if (!(section || length))
    {
        free (str);
        return NULL;
    }

    return section;
}


/********************************************************************************************************************
 * Function          : __ini_createHeading
 * Parameters        : ini - pointer to ini file database.  heading - heading name
 * Returns           : Pointer to new heading.
 * Globals Used      :
 * Globals Modified  :
 * Description       : Adds an entry into the heading linked list ready for formating by the addHeading commands
 ********************************************************************************************************************
 *  Rev   |   Date   |  By   | Comment
 * ----------------------------------------------------------------------------------------------------------------
 ********************************************************************************************************************/
struct section_tag *__ini_createHeading (ini_t *ini, char *heading)
{
    struct section_tag *pNew;

    pNew  = __ini_locateHeading (ini, heading);
    // Check to see if heading already exists
    if (!pNew)
    {   // Create a new heading;
        pNew = (struct section_tag *) malloc (sizeof (struct section_tag));
        assert (pNew);
        memset (pNew, 0, sizeof (struct section_tag));
        pNew->heading = heading;

        if (*heading)
    {   // Normal case
            pNew->pPrev = ini->last;
            ini->last   = pNew;
            if (pNew->pPrev)
                pNew->pPrev->pNext = pNew;
            else
            ini->first = pNew;
        }
        else
    {   // This case must always be first
        pNew->pNext = ini->first;
            ini->first  = pNew;
            if (pNew->pNext)
                pNew->pNext->pPrev = pNew;
            else
            ini->last = pNew;
    }
        
            
#ifdef INI_USE_HASH_TABLE
        {   // Rev 1.3 - Added
            struct   section_tag *pOld;
            unsigned long crc32;
            unsigned char accel;

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

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


/********************************************************************************************************************
 * Function          : __ini_deleteHeading
 * Parameters        : ini - pointer to ini file database.  heading - heading name
 * Returns           :
 * Globals Used      :
 * Globals Modified  :
 * Description       : Removes a heading from the database only.
 *                   : This change does not occur in the file until ini_close is called.
 ********************************************************************************************************************
 *  Rev   |   Date   |  By   | Comment
 * ----------------------------------------------------------------------------------------------------------------
 ********************************************************************************************************************/
void __ini_deleteHeading (ini_t *ini)
{
    struct section_tag *current_h;

    // Delete Heading
    current_h = ini->selected;
    if (current_h)
    {   // Delete Keys
        while (current_h->first)
        {
            current_h->selected = current_h->first;
            __ini_deleteKey (ini);
        }
    
        // Tidy up all users of this heading
        ini->selected =  NULL;
        if (ini->last == current_h)
            ini->last =  current_h->pPrev;

        // Break heading out of list
        if (!current_h->pPrev)
            ini->first = current_h->pNext;
        else
            current_h->pPrev->pNext = current_h->pNext;
        if (current_h->pNext)
            current_h->pNext->pPrev = current_h->pPrev;

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

        // Delete Heading
        if (*current_h->heading)
            free (current_h->heading);
        free (current_h);
        ini->changed = true;
    }
}


/********************************************************************************************************************
 * Function          : __ini_locateHeading
 * Parameters        : ini - pointer to ini file database.  heading - heading name
 * Returns           : Pointer to heading.
 * Globals Used      :
 * Globals Modified  :
 * Description       : Locates a heading entry in the database.
 ********************************************************************************************************************
 *  Rev   |   Date   |  By   | Comment
 * ----------------------------------------------------------------------------------------------------------------
 ********************************************************************************************************************/
struct section_tag *__ini_locateHeading (ini_t *ini, char *heading)
{
    struct   section_tag *current_h;

#ifdef INI_USE_HASH_TABLE
    // Rev 1.3 - Revised to use new accelerator
    unsigned long crc32;
    crc32 = __ini_createCrc32 (heading, strlen (heading));

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

    ini->selected = current_h;
    return current_h;
}


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


/********************************************************************************************************************
 * Function          : (public) ini_locateHeading
 * Parameters        : fd - pointer to ini file descriptor
 * Returns           : -1 for Error and 0 on success
 * Globals Used      :
 * Globals Modified  :
 * Description       : Equivalent Microsoft write string API call where both data & key are set to NULL.
 ********************************************************************************************************************/
int INI_LINKAGE ini_locateHeading (ini_fd_t fd, char *heading)
{
    ini_t *ini = (ini_t *) fd;
    struct section_tag *section;

    if (!heading)
        return -1;

    section = __ini_locateHeading (ini, heading);

#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 (section)
        return 0;

    // Ok no section 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 old heading
        section = &(ini->tmpSection);
        if (section->heading)
            free (section->heading);

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