Ads 468x60px

##EasyReadMore##

28 12月, 2016

[C] 讀取 config.ini

將以下程式存成 readConfig.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STR 256

int get_number_of_line( const char *spath )
{
  int n_line = 0;
  char str[MAX_STR];
  FILE *f0 = fopen( spath, "rt" );
    if(f0==NULL)
    { 
     printf("[Error], get_number_of_line(), file %s not found\n", spath );
     return 0;
     //exit(-1); 
    }
    while( fgets( str, MAX_STR, f0 ) )
     n_line++;
  fclose(f0);

  return n_line;
}

void copy_lines( const char *spath, char **lines, int n_line )
{
  int i;
  char str[MAX_STR];
  if(0==n_line)
   return;
  FILE *f0 = fopen( spath, "rt" );
  if(f0==NULL)
  { 
   printf("[Error], copy_lines(), file %s not found\n", spath ); 
   exit(-1); 
  }
  for( i=0; i<n_line; i++ )
  {
     fgets( str, MAX_STR, f0 );
     lines[i] = (char*) malloc( MAX_STR *sizeof(char) );
     strcpy( lines[i], str );
  }
  fclose(f0);
}

void write_lines(const char *spath, char **lines, int n_line )
{
  int i;
  FILE *f0 = fopen( spath, "wt" );
    
  if(f0==NULL){ printf("[Error], write_lines(), file %s not found\n", spath ); exit(-1); }
  for( i=0; i<n_line; i++ )
     fprintf( f0, "%s", lines[i] );
  fclose(f0);
}

void free_lines( char **lines, int n_line )
{
  int i;
  for( i=0; i<n_line; i++ )
    free(lines[i]);
  free(lines);
}

void read_lines( const char *spath, char ***lines, int* n_line )
{
  *n_line = get_number_of_line( spath );
  *lines= (char**) malloc( *n_line * sizeof(char*) );
  copy_lines( spath, *lines, *n_line );
}

char* nexttoken( char *str, char *line )
{
  static char *tstr=NULL;
  if(line!=NULL) tstr=line;
  if(tstr==NULL){ printf( "[Error], nexttoken() tstr is not initialized\n"); exit(-1); }

  sscanf( tstr, "%[^ \r\n\t=]:", str );
  tstr += strlen(str)+1;

  return str;
}

char *readconfig(const char *file,const char *title,char *value)
{
  //char resolvfile[] = "resolv.ini";
  //char nameserver[][MAX_STR] = { "192.168.1.1", "192.168.1.2" };
  //char domain_name_servers[][MAX_STR] = { "192.168.1.3", "192.168.1.4" };
 //if(NULL==file || NULL==title)
 char **lines=0; int n_line;
 
 // read lines from file and allocate memory
 read_lines( file, &lines, &n_line );
 
 //int ns_idx=0, dns_idx=0;
 char str[MAX_STR];
 
 int i;
 for( i=0; i<n_line; i++ )
 {
   if( lines[i][0] == '#' || lines[i][0] == '\r' || lines[i][0] == '\n' )
    continue;
   nexttoken( str, lines[i] );
 
   if( strcmp( str, title ) ==0 )
   {
    nexttoken( str, NULL );
    strcpy(value,str);
    free_lines( lines, n_line );
    return value;
    //sprintf( lines[i], "nameserver %s\n", nameserver[ns_idx] );
 
    //printf( "nameserver : %s -> %s\n", str, nameserver[ns_idx] );
    //ns_idx++;
   }
 
   /*if( strcmp( str, "option" ) ==0 )
   {
    nexttoken( str, NULL );
    if( strcmp( str, "domain-name-servers" ) ==0 )
    {
     nexttoken( str, NULL );
     printf( "domain-name-servers : %s -> %s\n", str, domain_name_servers[dns_idx] );
     sprintf( lines[i], "option domain-name-servers %s;\n", domain_name_servers[dns_idx] );
     dns_idx++;
    }
   }*/
 }
 // write output to tmp.txt, you can overwrite source file directly.
 //write_lines( "tmp.txt", lines, n_line );
 
 // release memory
 return NULL;
 free_lines( lines, n_line );

}
void LittleMouse(char *title)
{
  char resolvfile[] = "resolv.ini";
  char nameserver[][MAX_STR] = { "192.168.1.1", "192.168.1.2" };
  char domain_name_servers[][MAX_STR] = { "192.168.1.3", "192.168.1.4" };

  char **lines=0; int n_line;

  // read lines from file and allocate memory
  read_lines( resolvfile, &lines, &n_line );

  int ns_idx=0, dns_idx=0;
  char str[MAX_STR];

  int i;
  for( i=0; i<n_line; i++ )
  {
   if( lines[i][0] == '#' || lines[i][0] == '\r' || lines[i][0] == '\n' )
    continue;
   nexttoken( str, lines[i] );

   if( strcmp( str, "nameserver" ) ==0 )
   {
    nexttoken( str, NULL );
    sprintf( lines[i], "nameserver %s\n", nameserver[ns_idx] );

    printf( "nameserver : %s -> %s\n", str, nameserver[ns_idx] );
    ns_idx++;
   }

   if( strcmp( str, "option" ) ==0 )
   {
    nexttoken( str, NULL );
    if( strcmp( str, "domain-name-servers" ) ==0 )
    {
     nexttoken( str, NULL );
     printf( "domain-name-servers : %s -> %s\n", str, domain_name_servers[dns_idx] );
     sprintf( lines[i], "option domain-name-servers %s;\n", domain_name_servers[dns_idx] );
     dns_idx++;
    }
   }
  }
  // write output to tmp.txt, you can overwrite source file directly.
  write_lines( "tmp.txt", lines, n_line );

  // release memory
  free_lines( lines, n_line );

}

使用方式如下:
if (NULL == readconfig("config.ini", "server_ip", g_server_ip)) {
printf("%s IP is empty!\n",__FILENAME__);
return 0;
}

0 意見:

張貼留言

 
Blogger Templates