you can do it by basis c file operations(fopen/fclose/fgets) and after that using string operations to break and cut the read string(strtok is the best example).
Following source code is not complete as per your requirement. But for sure it will provide you some basic idea. To execute this code put create file named temp.config and put your file contain inside that.
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<stdint.h>
#define MAXLEN 1024
#define CONFIG_FILE "temp.config"
/*
* remove trailing and leading whitespace
*/
static inline char *
trim (char * s)
{
/* Initialize start, end pointers */
char *s1 = s, *s2 = &s[strlen (s) - 1];
/* Trim and delimit right side */
while ( (isspace (*s2)) && (s2 >= s1) )
s2--;
*(s2+1) = '\0';
/* Trim left side */
while ( (isspace (*s1)) && (s1 < s2) )
s1++;
/* Copy finished string */
strcpy (s, s1);
return s;
}
inline bool
parse_config ( ){
#ifdef DEBUG
fprintf(stdout,"__parse_config__\n");
#endif
char *s, buff[MAXLEN];
char *temp1,*temp2;
FILE *fp = fopen (CONFIG_FILE, "r");
if (fp == NULL){
fprintf(stderr,"Not able to open file\n");
return false;
}
/* Read next line */
while ( ( (s = fgets (buff, sizeof buff, fp)) != NULL) ){
/* Skip blank lines and comments */
if (buff[0] == '\n' || buff[0] == '#')
continue;
/* Parse name/value pair from line */
char name[MAXLEN], value[MAXLEN];
s = strtok (buff, " ");
if (s==NULL)
continue;
else
strncpy (name, s, MAXLEN);
s = strtok (NULL, " ");
if (s==NULL)
continue;
else
strncpy (value, s, MAXLEN);
trim (value);
/* you can use a switch case*/
if (strcmp(name, "LOAD")==0){
fprintf(stdout,"%s\n",name);
temp1 = strtok(value,",");
fprintf(stdout,"%s\n",value);
//this is the logic.. rest you have to implement.
} if (strcmp(name, "LOADI")==0){
fprintf(stdout,"%s\n",name);
temp1 = strtok(value,",");
fprintf(stdout,"%s\n",value);
}
}
fclose(fp);
return true;
}
int main(){
parse_config();
return 0;
}
OUTPUT:
root@suman-OptiPlex-380:/home/suman/poc# ./a.out
LOAD
A1
LOADI
R1
Note: I have just printed the output, you can store it.