1 | /********************************************************************************** |
2 | * ini.h |
3 | * |
4 | * Include file for libini.a. Coding? Include this and link against libini.a. |
5 | * |
6 | * |
7 | ************************************************** |
8 | * This code was created by Peter Harvey @ CodeByDesign. |
9 | * Released under LGPL 28.JAN.99 |
10 | * |
11 | * Contributions from... |
12 | * ----------------------------------------------- |
13 | * Peter Harvey - pharvey@codebydesign.com |
14 | **************************************************/ |
15 | |
16 | #ifndef INCLUDED_INI_H |
17 | #define INCLUDED_INI_H |
18 | |
19 | /*********[ CONSTANTS AND TYPES ]**************************************************/ |
20 | #include <stdlib.h> |
21 | #include <stdio.h> |
22 | #include <ctype.h> |
23 | #include <string.h> |
24 | |
25 | #ifndef TRUE |
26 | #define TRUE 1 |
27 | #endif |
28 | |
29 | #ifndef FALSE |
30 | #define FALSE 0 |
31 | #endif |
32 | |
33 | #define STDINFILE ((char*)-1) |
34 | |
35 | #define INI_NO_DATA 2 |
36 | #define INI_SUCCESS 1 |
37 | #define INI_ERROR 0 |
38 | |
39 | #define INI_MAX_LINE 1000 |
40 | #define INI_MAX_OBJECT_NAME INI_MAX_LINE |
41 | #define INI_MAX_PROPERTY_NAME INI_MAX_LINE |
42 | #define INI_MAX_PROPERTY_VALUE INI_MAX_LINE |
43 | |
44 | #if HAVE_LIMITS_H |
45 | #include <limits.h> |
46 | #endif |
47 | |
48 | #ifdef PATH_MAX |
49 | #define ODBC_FILENAME_MAX PATH_MAX |
50 | #elif MAXPATHLEN |
51 | #define ODBC_FILENAME_MAX MAXPATHLEN |
52 | #else |
53 | #define ODBC_FILENAME_MAX FILENAME_MAX |
54 | #endif |
55 | |
56 | /******************************************** |
57 | * tINIPROPERTY |
58 | * |
59 | * Each property line has Name=Value pair. |
60 | * They are stored in this structure and linked together to provide a list |
61 | * of all properties for a given Object. |
62 | ********************************************/ |
63 | |
64 | typedef struct tINIPROPERTY |
65 | { |
66 | struct tINIPROPERTY *pNext; |
67 | struct tINIPROPERTY *pPrev; |
68 | |
69 | char szName[INI_MAX_PROPERTY_NAME+1]; |
70 | char szValue[INI_MAX_PROPERTY_VALUE+1]; |
71 | |
72 | } INIPROPERTY, *HINIPROPERTY; |
73 | |
74 | /******************************************** |
75 | * tINIOBJECT |
76 | * |
77 | * Each object line has just an object name. This structure |
78 | * stores the object name and its subordinate information. |
79 | * The lines that follow are considered to be properties |
80 | * and are stored in a list of tINIPROPERTY. |
81 | ********************************************/ |
82 | |
83 | typedef struct tINIOBJECT |
84 | { |
85 | struct tINIOBJECT *pNext; |
86 | struct tINIOBJECT *pPrev; |
87 | |
88 | char szName[INI_MAX_OBJECT_NAME+1]; |
89 | |
90 | HINIPROPERTY hFirstProperty; |
91 | HINIPROPERTY hLastProperty; |
92 | int nProperties; |
93 | |
94 | } INIOBJECT, *HINIOBJECT; |
95 | |
96 | /******************************************** |
97 | * tINI |
98 | * |
99 | * Each INI file contains a list of objects. This |
100 | * structure stores each object in a list of tINIOBJECT. |
101 | ********************************************/ |
102 | |
103 | typedef struct tINI |
104 | { |
105 | #ifdef __OS2__ |
106 | int iniFileType; /* ini file type 0 = text, 1 = binary (OS/2 only) */ |
107 | #endif |
108 | char szFileName[ODBC_FILENAME_MAX+1]; /* FULL INI FILE NAME */ |
109 | char [ 5 ]; /* COMMENT CHAR MUST BE IN FIRST COLUMN */ |
110 | char cLeftBracket; /* BRACKETS DELIMIT THE OBJECT NAME, THE LEFT BRACKET MUST BE IN COLUMN ONE */ |
111 | char cRightBracket; |
112 | char cEqual; /* SEPERATES THE PROPERTY NAME FROM ITS VALUE */ |
113 | int bChanged; /* IF true, THEN THE WHOLE FILE IS OVERWRITTEN UPON iniCommit */ |
114 | int bReadOnly; /* TRUE IF AT LEAST ONE CALL HAS BEEN MADE TO iniAppend() */ |
115 | |
116 | HINIOBJECT hFirstObject; |
117 | HINIOBJECT hLastObject; |
118 | HINIOBJECT hCurObject; |
119 | int nObjects; |
120 | |
121 | HINIPROPERTY hCurProperty; |
122 | |
123 | } INI, *HINI; |
124 | |
125 | /******************************************** |
126 | * tINIBOOKMARK |
127 | * |
128 | * Used to store the current Object and Property pointers so |
129 | * that the caller can quickly return to some point in the |
130 | * INI data. |
131 | ********************************************/ |
132 | |
133 | typedef struct tINIBOOKMARK |
134 | { |
135 | HINI hIni; |
136 | HINIOBJECT hCurObject; |
137 | HINIPROPERTY hCurProperty; |
138 | |
139 | } INIBOOKMARK, *HINIBOOKMARK; |
140 | |
141 | #if defined(__cplusplus) |
142 | extern "C" { |
143 | #endif |
144 | |
145 | /*********[ PRIMARY INTERFACE ]*****************************************************/ |
146 | |
147 | /****************************** |
148 | * iniOpen |
149 | * |
150 | * 1. make sure file exists |
151 | * 2. allocate memory for HINI |
152 | * 3. initialize HINI |
153 | * 4. load entire file into structured memory |
154 | * 5. set TRUE if you want to create file if non-existing |
155 | ******************************/ |
156 | #ifdef __OS2__ |
157 | int iniOpen( HINI *hIni, char *pszFileName, char *cComment, char cLeftBracket, char cRightBracket, char cEqual, int bCreate, int bFileType ); |
158 | #else |
159 | int iniOpen( HINI *hIni, char *pszFileName, char *, char cLeftBracket, char cRightBracket, char cEqual, int bCreate ); |
160 | #endif |
161 | /****************************** |
162 | * iniAppend |
163 | * |
164 | * 1. append Sections in pszFileName that do not already exist in hIni |
165 | * 2. Makes hIni ReadOnly! |
166 | ******************************/ |
167 | int iniAppend( HINI hIni, char *pszFileName ); |
168 | |
169 | /****************************** |
170 | * iniDelete |
171 | * |
172 | * 1. simple removes all objects (and their properties) from the list using iniObjectDelete() in a loop |
173 | ******************************/ |
174 | int iniDelete( HINI hIni ); |
175 | |
176 | /****************************** |
177 | * iniClose |
178 | * |
179 | * 1. free memory previously allocated for HINI |
180 | * 2. DO NOT SAVE ANY CHANGES (see iniCommit) |
181 | ******************************/ |
182 | int iniClose( HINI hIni ); |
183 | |
184 | /****************************** |
185 | * iniCommit |
186 | * |
187 | * 1. replaces file contents with memory contents (overwrites the file) |
188 | ******************************/ |
189 | int iniCommit( HINI hIni ); |
190 | |
191 | /****************************** |
192 | * iniObjectFirst |
193 | * |
194 | ******************************/ |
195 | int iniObjectFirst( HINI hIni ); |
196 | |
197 | /****************************** |
198 | * iniObjectLast |
199 | * |
200 | ******************************/ |
201 | int iniObjectLast( HINI hIni ); |
202 | |
203 | /****************************** |
204 | * iniObjectNext |
205 | * |
206 | * 1. iniObjects() if no current object name else |
207 | * 2. find and store next object name |
208 | ******************************/ |
209 | int iniObjectNext( HINI hIni ); |
210 | |
211 | /****************************** |
212 | * iniObjectSeek |
213 | * |
214 | * 1. find and store object name |
215 | ******************************/ |
216 | int iniObjectSeek( HINI hIni, char *pszObject ); |
217 | |
218 | /****************************** |
219 | * iniObjectSeekSure |
220 | * |
221 | * 1. find and store object name |
222 | * 2. ensure that it exists |
223 | ******************************/ |
224 | int iniObjectSeekSure( HINI hIni, char *pszObject ); |
225 | |
226 | /****************************** |
227 | * iniObjectEOL |
228 | * |
229 | ******************************/ |
230 | int iniObjectEOL( HINI hIni ); |
231 | |
232 | /****************************** |
233 | * iniObject |
234 | * |
235 | * 1. returns the current object name |
236 | ******************************/ |
237 | int iniObject( HINI hIni, char *pszObject ); |
238 | |
239 | /****************************** |
240 | * iniObjectDelete |
241 | * |
242 | * 1. deletes current Object |
243 | ******************************/ |
244 | int iniObjectDelete( HINI hIni ); |
245 | |
246 | /****************************** |
247 | * iniObjectUpdate |
248 | * |
249 | * 1. update current Object |
250 | ******************************/ |
251 | int iniObjectUpdate( HINI hIni, char *pszObject ); |
252 | |
253 | /****************************** |
254 | * iniPropertyObject |
255 | * |
256 | * 1. inserts a new Object |
257 | * 2. becomes current |
258 | ******************************/ |
259 | int iniObjectInsert( HINI hIni, char *pszObject ); |
260 | |
261 | /****************************** |
262 | * iniPropertyFirst |
263 | * |
264 | ******************************/ |
265 | int iniPropertyFirst( HINI hIni ); |
266 | |
267 | /****************************** |
268 | * iniPropertyLast |
269 | * |
270 | ******************************/ |
271 | int iniPropertyLast( HINI hIni ); |
272 | |
273 | /****************************** |
274 | * iniPropertyNext |
275 | * |
276 | * 1. iniProperties() if no current property name else |
277 | * 2. find and store next property name |
278 | ******************************/ |
279 | int iniPropertyNext( HINI hIni ); |
280 | |
281 | /****************************** |
282 | * iniPropertySeek |
283 | * |
284 | * 1. set current Object & Property positions where matching parameters |
285 | * 2. any parms which are empty strings (ie pszObject[0]) are ignored |
286 | * 3. it is kinda pointless to pass empty strings for all parms... you will get 1st Property in 1st Object |
287 | ******************************/ |
288 | int iniPropertySeek( HINI hIni, char *pszObject, char *pszProperty, char *pszValue ); |
289 | |
290 | /****************************** |
291 | * iniPropertySeekSure |
292 | * |
293 | * 1. same as iniPropertySeek but |
294 | * 2. will ensure that both Object and Property exist |
295 | ******************************/ |
296 | int iniPropertySeekSure( HINI hIni, char *pszObject, char *pszProperty, char *pszValue ); |
297 | |
298 | /****************************** |
299 | * iniPropertyEOL |
300 | * |
301 | ******************************/ |
302 | int iniPropertyEOL( HINI hIni ); |
303 | |
304 | /****************************** |
305 | * iniProperty |
306 | * |
307 | * 1. returns the current property name |
308 | ******************************/ |
309 | int iniProperty( HINI hIni, char *pszProperty ); |
310 | |
311 | /****************************** |
312 | * iniPropertyDelete |
313 | * |
314 | * 1. deletes current Property |
315 | ******************************/ |
316 | int iniPropertyDelete( HINI hIni ); |
317 | |
318 | /****************************** |
319 | * iniPropertyUpdate |
320 | * |
321 | * 1. update current Property |
322 | ******************************/ |
323 | int iniPropertyUpdate( HINI hIni, char *pszProperty, char *pszValue ); |
324 | |
325 | /****************************** |
326 | * iniPropertyInsert |
327 | * |
328 | * 1. inserts a new Property for current Object |
329 | * 2. becomes current |
330 | ******************************/ |
331 | int iniPropertyInsert( HINI hIni, char *pszProperty, char *pszValue ); |
332 | |
333 | /****************************** |
334 | * iniValue |
335 | * |
336 | * 1. returns the value for the current object/property |
337 | ******************************/ |
338 | int iniValue( HINI hIni, char *pszValue ); |
339 | |
340 | /****************************** |
341 | * iniGetBookmark |
342 | * |
343 | * 1. Store the current data positions (Object and Property) |
344 | * into hIniBookmark. |
345 | * 2. Does not allocate memory for hIniBookmark so pass a |
346 | * pointer to a valid bookmark. |
347 | ******************************/ |
348 | int iniGetBookmark( HINI hIni, HINIBOOKMARK hIniBookmark ); |
349 | |
350 | /****************************** |
351 | * iniGotoBookmark |
352 | * |
353 | * 1. Sets the current Object and Property positions to |
354 | * those stored in IniBookmark. |
355 | * 2. Does not account for the bookmark becoming |
356 | * invalid ie from the Object or Property being deleted. |
357 | ******************************/ |
358 | int iniGotoBookmark( INIBOOKMARK IniBookmark ); |
359 | |
360 | /****************************** |
361 | * iniCursor |
362 | * |
363 | * 1. Returns a copy of the hIni with a new |
364 | * set of position cursors (current Object and Property). |
365 | * 2. Not safe to use when in the possibility of |
366 | * deleting data in another cursor on same data. |
367 | * 3. Use when reading data only. |
368 | * 4. Does not allocate memory so hIniCursor should be valid. |
369 | * 5. All calls, other than those for movement, are |
370 | * global and will affect any other view of the data. |
371 | ******************************/ |
372 | int iniCursor( HINI hIni, HINI hIniCursor ); |
373 | |
374 | /*************************************************************************************/ |
375 | /*********[ SUPPORT FUNCS ]***********************************************************/ |
376 | /*************************************************************************************/ |
377 | |
378 | /****************************** |
379 | * iniElement |
380 | * |
381 | ******************************/ |
382 | int iniElement( char *pszData, char cSeperator, char cTerminator, int nElement, char *pszElement, int nMaxElement ); |
383 | int iniElementMax( char *pData, char cSeperator, int nDataLen, int nElement, char *pszElement, int nMaxElement ); |
384 | int iniElementToEnd( char *pszData, char cSeperator, char cTerminator, int nElement, char *pszElement, int nMaxElement ); |
385 | int iniElementEOL( char *pszData, char cSeperator, char cTerminator, int nElement, char *pszElement, int nMaxElement ); |
386 | |
387 | /****************************** |
388 | * iniElementCount |
389 | * |
390 | ******************************/ |
391 | int iniElementCount( char *pszData, char cSeperator, char cTerminator ); |
392 | |
393 | /****************************** |
394 | * iniPropertyValue |
395 | * |
396 | * 1. returns the property value for pszProperty in pszValue |
397 | * 2. pszString example; |
398 | * "PropertyName1=Value1;PropertyName2=Value2;..." |
399 | * 3. cEqual is usually '=' |
400 | * 4. cPropertySep is usually ';' |
401 | * |
402 | * This function can be called without calling any other functions in this lib. |
403 | ******************************/ |
404 | int iniPropertyValue( char *pszString, char *pszProperty, char *pszValue, char cEqual, char cPropertySep ); |
405 | |
406 | /****************************** |
407 | * iniAllTrim |
408 | * |
409 | * 1. trims blanks, tabs and newlines from start and end of pszString |
410 | * |
411 | * This function can be called without calling any other functions in this lib. |
412 | ******************************/ |
413 | int iniAllTrim( char *pszString ); |
414 | |
415 | /****************************** |
416 | * iniToUpper |
417 | * |
418 | * 1. Converts all chars in pszString to upper case. |
419 | * |
420 | * This function can be called without calling any other functions in this lib. |
421 | ******************************/ |
422 | int iniToUpper( char *pszString ); |
423 | |
424 | |
425 | /****************************** |
426 | * _iniObjectRead |
427 | * |
428 | ******************************/ |
429 | int _iniObjectRead( HINI hIni, char *szLine, char *pszObjectName ); |
430 | |
431 | /****************************** |
432 | * _iniPropertyRead |
433 | * |
434 | ******************************/ |
435 | int _iniPropertyRead( HINI hIni, char *szLine, char *pszPropertyName, char *pszPropertyValue ); |
436 | |
437 | /****************************** |
438 | * _iniDump |
439 | * |
440 | ******************************/ |
441 | int _iniDump( HINI hIni, FILE *hStream ); |
442 | |
443 | /****************************** |
444 | * _iniScanUntilObject |
445 | * |
446 | ******************************/ |
447 | int _iniScanUntilObject( HINI hIni, FILE *hFile, char *pszLine ); |
448 | int _iniScanUntilNextObject( HINI hIni, FILE *hFile, char *pszLine ); |
449 | |
450 | /* |
451 | * Some changes to avoid a 255 file handle limit, thanks MQJoe. |
452 | * Make it conditional as it does have some performance impact esp with LARGE ini files (like what I have :-) |
453 | */ |
454 | |
455 | #if defined( HAVE_VSNPRINTF ) && defined( USE_LL_FIO ) |
456 | |
457 | FILE *uo_fopen( const char *filename, const char *mode ); |
458 | int uo_fclose( FILE *stream ); |
459 | char *uo_fgets( char *szbuffer, int n, FILE *stream ); |
460 | int uo_fprintf( FILE *stream, const char *fmt, ...); |
461 | int uo_vfprintf( FILE *stream, const char *fmt, va_list ap); |
462 | |
463 | #else |
464 | |
465 | #define uo_fopen fopen |
466 | #define uo_fclose fclose |
467 | #define uo_fgets fgets |
468 | #define uo_fprintf fprintf |
469 | #define uo_vfprintf vfprintf |
470 | |
471 | #endif |
472 | |
473 | #if defined(__cplusplus) |
474 | } |
475 | #endif |
476 | |
477 | #endif |
478 | |