Processing Configuration documents in Linux is often a headache, because it does not have any APIs or class objects for INIFILE processing on the Win32 platform, we can only analyze and process the configuration in the form of a batch stream. The following program section provides a typical method for processing configuration documents...
/*
Read and process key-value documents
*/
Char name [20];
Byte age;
Char sex [8];
Int propertyconfigurator (char * configfilename)
{
File * FP;
Int I;
Char line [256];
Char * pstr = (char *) NULL;
If (FP = fopen (configfilename, "R") = (File *) null) // configfilename is the configuration file name to be processed
{
Printf ("can not open configfile/N"); // is the comment line in the configuration document.
Return 0;
}
Else
{
While (fgets (line, 256, FP )! = (Char *) null)
{
Detetewhitespace (line); // Delete Spaces
If (line [0] = '#'); // '#' is the comment line in the configuration document.
Continue;
Strupr (line); // convert character to lowercase.
If (pstr = strstr (line, "name") // process key-Value
{
Pstr = strstr (line, '= ');
Detetewhitespace (++ pstr );
Strcpy (name, pstr );
}
If (pstr = strstr (line, "Age") // process key-Value
{
Pstr = strstr (line, '= ');
Detetewhitespace (++ pstr );
Age = atoi (pstr );
}
Else if (pstr = strstr (line, "sex") // process key-Value
{
Pstr = strstr (line, '= ');
Detetewhitespace (++ pstr );
Strcpy (sex, pstr );
}
}
}
Fclose (FP );
Return 1;
}
/*
Two functions not provided by standard C
ITOA converts a numeric string
Strupr converts the characters in the string to uppercase.
*/
Char * ITOA (long value, char * string)
{// Enough for a 128 bit integer
If (string)
Sprintf (string, "% d", value );
Return string;
}
Char * strupr (char * string)
{
If (string! = NULL)
{
Char * CP; // traverses string for C locale conversion */
For (Cp = string; * CP; ++ CP)
{
If ('A' <= * CP & * CP <= 'Z ')
* CP + = 'a'-'A ';
}
Return (string );
} // C locale
Return NULL;
}
/*
Additional functions
Delete blank lines and spaces in the configuration document
*/
Char * detetewhitespace (char * Str) // Delete Space
{
Char strtemp [256];
Char strtemp2 [256];
Unsigned int I, J;
Memset (strtemp, 0,256 );
Strcpy (strtemp2, STR );
J = 0;
If (STR! = NULL)
{
For (I = 0; I <strlen (STR); I ++)
{
If (strtemp2 [I]! = '')
{
Strtemp [J ++] = strtemp2 [I];
}
}
}
Strcpy (STR, strtemp );
Return (STR );
}