Standard C processes key-value documents similar to ini configuration files

Source: Internet
Author: User

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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.