home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-387-Vol-3of3.iso
/
i
/
inifn135.zip
/
INIFUNC.DOC
< prev
next >
Wrap
Text File
|
1993-03-05
|
13KB
|
436 lines
INI file handler package
(c) Copyright 1992, 1993 DAF, Inc.
All rights reserved.
What is an INI file ?
=====================
INI files are used to store configuration information for application
programs. They are ASCII text files that can be edited with text editors
in plain ASCII mode (i.e. no formatting information enclosed). INI files
are interpreted on a line-by-line basis. Empty lines are ignored.
INI files can contain information that has Boolean, numerical or text
character, both individual values as well as one-dimensional arrays of
values.
Information is stored in assignments of the form 'Keyword=Information'
referred to herein as an Item. 'Keyword' represents an alpha-numeric
identifier referred to herein as a Key.
INI files are separated into sections. Each section may contain any number
of items, including none at all. Information is retrieved by referencing
an item within a section. Thus, items in different sections may have the
same key. An INI file section is identified by a line starting with the
section name enclosed in square brackets in the leftmost column. The
section's end is defined as either the next line starting with a square
bracket or the end of the file.
Lines are interpreted until the first semicolon (';'), asterisk ('*') or
crosshatch ('#') is reached. These characters are used to separate comments
from INI information.
What does this package offer ?
==============================
This package offers functions to retrieve and manipulate information
stored in INI files. These functions are written in C and use the
'standard I/O' package provided with virtually all C compilers today.
The size of the INI files is not limited in any way, and runtime heap
and stack memory requirements are minimal.
Information is retrieved from INI files by creating an INI handle and
associating data items with it. After reading information from the INI
file these items can be retrieved and changed, and the INI file can be
updated to reflect changes.
Although this object-oriented approach takes up more space than the
underlying set of functions based on 'C' structures and unions, it
provides for type and boundary checking and makes applications less
dependent on this package's internal workings.
Data types
==========
Data types understood by this set of functions are Boolean, numerical and
string types. The following is a list of the types and the kind of data
area the functions work on.
BOOL: Boolean assignments in an INI file take on the form
'Key=true' or 'Key=false'. 'True' and 'false'
alternatively can be represented by '1' or 'y' and
'0' or 'n', respectively. BOOL pointers point to
signed C integer values. For most PC-based C compilers
this means a 16bit integer.
SHORT: Short and unsigned short assignments have the form
USHORT: 'Key=Number', with Number being a decimal or
hexadecimal number within the valid range for C 'short'
or 'unsigned short' values. SHORT and USHORT
data pointers address 'short' and 'unsigned short'
values, respectively.
LONG: Long and unsigned long assignments have the form
ULONG: 'Key=Number', with Number being a decimal or
hexadecimal number within the valid range for C 'long'
or 'unsigned long' values. LONG and ULONG
data pointers address 'long' and 'unsigned long'
values, respectively.
STRING: String assignments have the form 'Key=String'. If
'String' contains whitespace characters, commas or
characters indicating INI file comments, it needs
to be enclosed by quotes or double quotes. STRING
data pointers address arrays of C 'char' values and
arrays of pointers to arrays of 'char' values.
INI file handle data type
=========================
The data type used for handles referring to INI files is defined as a
pointer to a void:
typedef void *INIHNDL;
This void pointer actually addresses a control structure that is managed
internally.
Status codes
============
Functions that return an 'int' status value will return zero if successful
or a combination of the following values in case of errors:
IF_OK 0x0000 /* No error */
IF_BADRANGE 0x0001 /* Value out of range */
IF_BUSY 0x0002 /* Item exists already */
IF_NOMEM 0x0004 /* Out of memory */
IF_BADTYPE 0x0008 /* Bad type */
IF_BADHNDL 0x0010 /* Bad handle */
IF_NOTFILLED 0x0020 /* Item was not filled by scan */
IF_NOSECT 0x0040 /* Section not found in INI file */
IF_BADKEY 0x0080 /* Unrecognized key */
IF_SYNTAX 0x0100 /* Bad syntax in INI file */
IF_TOOMANY 0x0200 /* Too many elements for item */
IF_OPEN 0x0400 /* File not found */
IF_IOERR 0x0800 /* I/O error */
IF_LOST 0x1000 /* Update: lost original version of INI file*/
IF_ERROR 0x1fff /* All the error values */
INI handle constructor/destructor
=================================
INIHNDL CreateIniHandle(char *section);
Purpose: Creates an INI handle.
Arguments: Pointer to string with the section name.
Returns: Handle if successful, NULL otherwise (out of memory).
int DeleteIniHandle(INIHNDL inihndl);
Purpose: Disposes an INI handle and all associated data.
Arguments: INI handle
Returns: Status
Functions to access and modify INI file information
===================================================
int ReadIniInfo(INIHNDL inihndl, char *inifile);
Purpose: Read information from INI file to handle
Arguments: INI handle, Name of file
Returns: Status
int WriteIniInfo(INIHNDL inihndl, char *inifile);
Purpose: Write information from INI handle to a file
Arguments: INI handle, Name of file
Returns: Status
Functions to build and delete INI items
=======================================
int DeleteIniItem(INIHNDL inihndl, char *key);
Purpose: Delete item from handle
Arguments: INI handle, key of item to be deleted
Returns: Status
int BuildBoolIniItem(INIHNDL inihndl, char *key, int defval);
Purpose: Build Boolean item
Arguments: INI handle, key of item to be created, default value
Returns: Status
int BuildShortIniItem(INIHNDL inihndl, char *key, short defval, short minval, short maxval);
int BuildUshortIniItem(INIHNDL inihndl, char *key, ushort defval, ushort minval, ushort maxval);
int BuildLongIniItem(INIHNDL inihndl, char *key, long defval, long minval, long maxval);
int BuildUlongIniItem(INIHNDL inihndl, char *key, ulong defval, ulong minval, ulong maxval);
Purpose: Build numerical item
Arguments: INI handle, key of item to be created, default value,
minimum and maximum values
Returns: Status
int BuildStringIniItem(INIHNDL inihndl, char *key, int maxlen, char *defstr);
Purpose: Build string item
Arguments: INI handle, key of item to be created, maximum length
(including trailing 0), default string
Returns: Status
int BuildBoolsIniItem(INIHNDL inihndl, char *key, int num, int defval);
Purpose: Build array of Boolean values item
Arguments: INI handle, key of item to be created, number of elements
in array, default value
Returns: Status
int BuildShortsIniItem(INIHNDL inihndl, char *key, int num, short defval, short minval, short maxval);
int BuildUshortsIniItem(INIHNDL inihndl, char *key, int num, ushort defval, ushort minval, ushort maxval);
int BuildLongsIniItem(INIHNDL inihndl, char *key, int num, long defval, long minval, long maxval);
int BuildUlongsIniItem(INIHNDL inihndl, char *key, int num, ulong defval, ulong minval, ulong maxval);
Purpose: Build array of numerical values item
Arguments: INI handle, key of item to be created, number of elements
in array, default value, minimum and maximum values
Returns: Status
int BuildStringsIniItem(INIHNDL inihndl, char *key, int num, int maxlen, char *defstr);
Purpose: Build array of strings item
Arguments: INI handle, key of item to be created, number of elements
in array, maximum length (including trailing 0) per element,
default string
Returns: Status
Functions to retrieve data from INI items
=========================================
int GetBoolIniItem(INIHNDL inihndl, char *key, int *valptr);
Purpose: Get data from Boolean item
Arguments: INI handle, key of item, pointer to Boolean variable
Returns: Status
int GetShortIniItem(INIHNDL inihndl, char *key, short *valptr);
int GetUshortIniItem(INIHNDL inihndl, char *key, ushort *valptr);
int GetLongIniItem(INIHNDL inihndl, char *key, long *valptr);
int GetUlongIniItem(INIHNDL inihndl, char *key, ulong *valptr);
Purpose: Get data from numerical item
Arguments: INI handle, key of item, pointer to numerical variable
Returns: Status
int GetStringIniItem(INIHNDL inihndl, char *key, char *strptr);
Purpose: Get data from string item
Arguments: INI handle, key of item, pointer to string buffer
Returns: Status
int GetBoolsIniItem(INIHNDL inihndl, char *key, int *valptr, int num);
Purpose: Get data from array of Boolean values item
Arguments: INI handle, key of item, pointer to array of Boolean
variables, number of elements to get
Returns: Status
int GetShortsIniItem(INIHNDL inihndl, char *key, short *valptr, int num);
int GetUshortsIniItem(INIHNDL inihndl, char *key, ushort *valptr, int num);
int GetLongsIniItem(INIHNDL inihndl, char *key, long *valptr, int num);
int GetUlongsIniItem(INIHNDL inihndl, char *key, ulong *valptr, int num);
Purpose: Get data from array of numerical values item
Arguments: INI handle, key of item, pointer to array of numerical
variables, number of elements to get
Returns: Status
int GetStringsIniItem(INIHNDL inihndl, char *key, char **strptr, int num);
Purpose: Get data from array of strings item
Arguments: INI handle, key of item, pointer to array of pointers
to string buffers, number of elements to get
Returns: Status
Functions to modify data of INI items
=====================================
int SetBoolIniItem(INIHNDL inihndl, char *key, int value);
Purpose: Set data of Boolean item
Arguments: INI handle, key of item, Boolean value
Returns: Status
int SetShortIniItem(INIHNDL inihndl, char *key, short value);
int SetUshortIniItem(INIHNDL inihndl, char *key, ushort value);
int SetLongIniItem(INIHNDL inihndl, char *key, long value);
int SetUlongIniItem(INIHNDL inihndl, char *key, ulong value);
Purpose: Set data of numerical item
Arguments: INI handle, key of item, numerical value
Returns: Status
int SetStringIniItem(INIHNDL inihndl, char *key, char *strptr);
Purpose: Set data of string item
Arguments: INI handle, key of item, string to copy from
Returns: Status
int SetBoolsIniItem(INIHNDL inihndl, char *key, int *valptr, int num);
Purpose: Set data of array of Boolean values item
Arguments: INI handle, key of item, pointer to array of Boolean
values, number of elements to set
Returns: Status
int SetShortsIniItem(INIHNDL inihndl, char *key, short *valptr, int num);
int SetUshortsIniItem(INIHNDL inihndl, char *key, ushort *valptr, int num);
int SetLongsIniItem(INIHNDL inihndl, char *key, long *valptr, int num);
int SetUlongsIniItem(INIHNDL inihndl, char *key, ulong *valptr, int num);
Purpose: Set data of array of numerical values item
Arguments: INI handle, key of item, pointer to array of numerical
values, number of elements to set
Returns: Status
int SetStringsIniItem(INIHNDL inihndl, char *key, char **strptr, int num);
Purpose: Set data of array of strings item
Arguments: INI handle, key of item, pointer to array of pointers
to strings to copy, number of elements to set
Returns: Status
Using the functions
===================
In order to access or modify INI file information, the following steps
need to be taken:
1) Create INI handle
INIHNDL ih;
if( (ih = CreateIniHandle("MySection")) == NULL ) {
// Error: out of memory
}
2) Build item descriptions
int res;
res = BuildLongIniItem(ih, "SomeLongValue", 0, 0, 99);
res |= BuildShortIniItem(ih, "SomeShortValue", 123, 100, 999);
...
if(res & IF_ERROR) {
// Error
DeleteIniHandle(ih);
// handle error case...
}
3) Read INI file
int res;
res = ReadIniInfo(ih, "MyApp.Ini");
if(res & IF_ERROR) {
// Error
...
}
4) Get the data
short s;
long l;
res = GetLongIniItem(ih, "SomeLongValue", &l);
res |= GetShortIniItem(ih, "SomeShortValue", &s);
...
if(res & IF_ERROR) {
// Error
...
}
5) Modify and update information
res = SetShortIniItem(ih, "SomeShortValue", 333);
res |= WriteIniInfo(ih, "MyApp.Ini");
if(res & IF_ERROR) {
// Error
...
}