summaryrefslogtreecommitdiff
path: root/libsidplay2/sidplay-libs-2.1.0/libsidutils/include/sidplay/utils/libini.h
blob: 798dfe8e717a8f6e39659c714c8694e011db6e9b (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
/***************************************************************************
                          libini.h  -  Header file of functions to
                                       manipulate an ini file.
                             -------------------
    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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef _libini_h_
#define _libini_h_

#ifdef __cplusplus
extern "C" {
#endif

/* Rev 1.3 Added scripting support using Swig 1.3a5 */
#ifdef SWIG
%module libini
#endif

#include <stdio.h>

#define INI_ADD_EXTRAS
#define INI_ADD_LIST_SUPPORT

typedef void* ini_fd_t;

//#ifdef _WINDOWS
//#   define INI_LINKAGE __stdcall
//#else
#   define INI_LINKAGE 
//#endif

/* DLL building support on win32 hosts */
#ifndef INI_EXTERN
#   ifdef DLL_EXPORT          /* defined by libtool (if required) */
#       define INI_EXTERN __declspec(dllexport)
#   endif
#   ifdef LIBINI_DLL_IMPORT   /* define if linking with this dll */
#       define INI_EXTERN extern __declspec(dllimport)
#   endif
#   ifndef INI_EXTERN         /* static linking or !_WIN32 */
#       define INI_EXTERN extern
#   endif
#endif

#ifndef INI_ADD_EXTRAS
#undef  INI_ADD_LIST_SUPPORT
#endif

#ifdef SWIG
%include typemaps.i
%apply int    *BOTH { int    *value };
%apply long   *BOTH { long   *value };
%apply double *BOTH { double *value };
%name (ini_readString)
    int ini_readFileToBuffer    (ini_fd_t fd, ini_buffer_t *buffer);

ini_buffer_t *ini_createBuffer        (unsigned long size);
void          ini_deleteBuffer        (ini_buffer_t *buffer);
char         *ini_getBuffer           (ini_buffer_t *buffer);
int           ini_setBuffer           (ini_buffer_t *buffer, char *str);

%{
#include "libini.h"

typedef struct 
{
    char  *buffer;
    size_t size;
} ini_buffer_t;

ini_buffer_t *ini_createBuffer (unsigned int size)
{
    ini_buffer_t *b;
    /* Allocate memory to structure */
    if (size == ( ((unsigned) -1 << 1) >> 1 ))
        return 0; /* Size is too big */
    b = malloc (sizeof (ini_buffer_t));
    if (!b)
        return 0;

    /* Allocate memory to buffer */
    b->buffer = malloc (sizeof (char) * (size + 1));
    if (!b->buffer)
    {
        free (b);
        return 0;
    }
    b->size = size;

    /* Returns address to tcl */
    return b;
}

void ini_deleteBuffer (ini_buffer_t *buffer)
{
    if (!buffer)
        return;
    free (buffer->buffer);
    free (buffer);
}

/*************************************************************
 * SWIG helper functions to create C compatible string buffers
 *************************************************************/
int ini_readFileToBuffer (ini_fd_t fd, ini_buffer_t *buffer)
{
    if (!buffer)
        return -1;
    return ini_readString (fd, buffer->buffer, buffer->size + 1);
}

char *ini_getBuffer (ini_buffer_t *buffer)
{
    if (!buffer)
        return "";
    return buffer->buffer;
}

int ini_setBuffer (ini_buffer_t *buffer, char *str)
{
    size_t len;
    if (!buffer)
        return -1;
    len = strlen (str);
    if (len > buffer->size)
        len = buffer->size;

    memcpy (buffer->buffer, str, len);
    buffer->buffer[len] = '\0';
    return len;
}

%}

#endif /* SWIG */


/* Rev 1.2 Added new fuction */
INI_EXTERN ini_fd_t INI_LINKAGE ini_open     (const char *name, const char *mode,
                                              const char *comment);
INI_EXTERN int      INI_LINKAGE ini_close    (ini_fd_t fd);
INI_EXTERN int      INI_LINKAGE ini_flush    (ini_fd_t fd);
INI_EXTERN int      INI_LINKAGE ini_delete   (ini_fd_t fd);

/* Rev 1.2 Added these functions to make life a bit easier, can still be implemented
 * through ini_writeString though. */
INI_EXTERN int INI_LINKAGE ini_locateKey     (ini_fd_t fd, char *key);
INI_EXTERN int INI_LINKAGE ini_locateHeading (ini_fd_t fd, char *heading);
INI_EXTERN int INI_LINKAGE ini_deleteKey     (ini_fd_t fd);
INI_EXTERN int INI_LINKAGE ini_deleteHeading (ini_fd_t fd);

/* Returns the number of bytes required to be able to read the key as a
 * string from the file. (1 should be added to this length to account
 * for a NULL character).  If delimiters are used, returns the length
 * of the next data element in the key to be read */
INI_EXTERN int INI_LINKAGE ini_dataLength (ini_fd_t fd);

/* Default Data Type Operations
 * Arrays implemented to help with reading, for writing you should format the
 * complete array as a string and perform an ini_writeString. */
#ifndef SWIG
INI_EXTERN int INI_LINKAGE ini_readString  (ini_fd_t fd, char *str, size_t size);
#endif /* SWIG */
INI_EXTERN int INI_LINKAGE ini_writeString (ini_fd_t fd, char *str);
INI_EXTERN int INI_LINKAGE ini_readInt     (ini_fd_t fd, int  *value);


#ifdef INI_ADD_EXTRAS
    /* Read Operations */
    INI_EXTERN int INI_LINKAGE ini_readLong    (ini_fd_t fd, long   *value);
    INI_EXTERN int INI_LINKAGE ini_readDouble  (ini_fd_t fd, double *value);

    /* Write Operations */
    INI_EXTERN int INI_LINKAGE ini_writeInt    (ini_fd_t fd, int    value);
    INI_EXTERN int INI_LINKAGE ini_writeLong   (ini_fd_t fd, long   value);
    INI_EXTERN int INI_LINKAGE ini_writeDouble (ini_fd_t fd, double value);

    /* Extra Functions */
    INI_EXTERN int INI_LINKAGE ini_append      (ini_fd_t fddst, ini_fd_t fdsrc);
#endif /* INI_ADD_EXTRAS */


#ifdef INI_ADD_LIST_SUPPORT
    /* Rev 1.1 Added - List Operations (Used for read operations only)
     * Be warned, once delimiters are set, every key that is read will be checked for the
     * presence of sub strings.  This will incure a speed hit and therefore once a line
     * has been read and list/array functionality is not required, set delimiters
     * back to NULL.
     */

    /* Returns the number of elements in an list being seperated by the provided delimiters */
    INI_EXTERN int INI_LINKAGE ini_listLength      (ini_fd_t fd);
    /* Change delimiters, default "" */
    INI_EXTERN int INI_LINKAGE ini_listDelims      (ini_fd_t fd, char *delims);
    /* Set index to access in a list.  When read the index will automatically increment */
    INI_EXTERN int INI_LINKAGE ini_listIndex       (ini_fd_t fd, unsigned long index);
#endif // INI_ADD_LIST_SUPPORT

#ifdef __cplusplus
}
#endif

#endif /* _libini_h_ */