Interface implementation to read the corresponding value in the configuration file by key key

Source: Internet
Author: User
Tags key string

/********************************************************************* * Author:samson * date:03/13/2015 * Test PL Atform: * 3.13.0-24-generic * GNU bash, 4.3.11 (1)-release * *********************************** ********************************/

In order to finish reading the value of a key key of the configuration file in the system, because the use of other people's library is always uncomfortable, and for the format has certain requirements, then you write an interface for use. The principle of implementation is very simple, by opening the configuration file, a line of reading, comparing whether there is a key string in the row and whether the next character of this key string is ' = ', if so, then get the value after the ' = ' number.

Note: This implementation only applies to configuration formats such as Key=value, not to the key= "value" format of many profiles.

The following is the implementation of this interface and the test code:

#define _gnu_source
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int Get_conf_value (char *file_path, Char *key_name, char *value)
{
FILE *FP = NULL;
Char *line = NULL, *SUBSTR = NULL;
size_t len = 0, Tlen = 0;
ssize_t Read = 0;

if (File_path = = NULL | | key_name = = NULL | | value = NULL)
{
printf ("Paramer is invaild!\n");
return-1;
}
fp = fopen (File_path, "R");
if (fp = = NULL)
{
printf ("Open config file is error!\n");
return-1;
}

while (read = Getline (&line, &len, FP))!! =-1)
{
substr = Strstr (line, key_name);
if (substr = = NULL)
{
Continue
}
Else
{
Tlen = strlen (key_name);
if (line[tlen] = = ' = ')
{
strncpy (value, &line[tlen+1], len-tlen+1);
printf ("Config file format is invaild Tlen are%d len is%d\n", Tlen, Len);
Tlen = strlen (value);
printf ("Get value is%s Tlen is%d\n", value, Tlen);
Replace enter key
* (value+tlen-1) = ' + ';
Break
}
Else
{
printf ("Config file format is invaild Tlen are%d len is%d\n", Tlen, Len);
Fclose (FP);
Return-2;
}
}
}
if (substr = = NULL)
{
printf ("Key:%s is not in config file!\n", key_name);
Fclose (FP);
return-1;
}

(line);
Fclose (FP);
return 0;
}

int main ()
{
Char getva[128] = {0};
Char pathname_key[] = "Path";
Char profilename[] = "/home/ufo/.mozilla/firefox/profiles.ini";
int ret = Get_conf_value (ProfileName, Pathname_key, Getva);
if (ret = = 0)
printf ("Get Pathname_key ' s value from profile:%s is%s\n", ProfileName, Getva);
return ret;
}

Where ProfileName is the Firefox configuration file, get the value of Key:path. The results of the operation are as follows:
[Email protected]:~/$./a.out
Config file format is invaild Tlen are 4 len is 120
Get Value is Cojs83dh.default
Tlen is 17
Get Pathname_key ' s value from Profile:/home/ufo/.mozilla/firefox/profiles.ini is Cojs83dh.default

The contents of the configuration file are as follows:
[Email protected]:~$ Cat/home/ufo/.mozilla/firefox/profiles.ini
[General]
Startwithlastprofile=1

[PROFILE0]
Name=default
Isrelative=1
Path=cojs83dh.default
Default=1

Interface implementation to read the corresponding value in the configuration file by key key

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.