配置文件的读取

来源:互联网 发布:mac页面放大缩小快捷键 编辑:程序博客网 时间:2024/05/16 14:24

/*******ReadConfig.c***********/

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

#include "ReadConfig.h"

#define MAX_WIDTH 256
int h_item_num;

int name_multi;
int value_multi;
long file_size(FILE *fp)
{
 long begin,end,current;

 if(fp==NULL)
  return -1;
 current=ftell(fp);
 fseek(fp,0,SEEK_SET);
 begin=ftell(fp);
 fseek(fp,0,SEEK_END);
 end=ftell(fp);
 fseek(fp,current,SEEK_SET);
 return end-begin;
 
}
int open(const char * m_file)
{
 int i;
 char *str;
 FILE *fp;

 long  len;

 if((fp=fopen(m_file,"r"))==NULL){
 // fprintf(stderr,"cann't open config file,filename is:%s/n",m_file);
  return 0;

 }
 len=file_size(fp)+1;
 str=(char *)malloc(len*sizeof(char));
 if(str==NULL){
 // fprintf(stderr,"cann't alloc mem in open function/n");
  return 0;
 }
 memset(str,0,len);
 i=fread(str,1,len-1,fp);
 fclose(fp);
 str[i]=0;
 h_item_num=split_config_file(str);
 free(str);
 return 1;
}
int create(const char *str)
{
// config_name[0][0]=0;
 h_item_num=split_config_file(str);
 return 1;
}
int skip_next_line(const char *str)
{
 int size=strlen(str);
 int i;
 for(i=0;i<size-1;i++){
  if(str[i]=='/n')
   return i+1;
 }
 return 0;
}
int get_word(const char *str,char *word,int *multi)

 int i,p,size=strlen(str);
 int len;

 //printf("str=%s./n",str);
 *multi=0;
 for(i=0;i<size;i++){
  if( (str[i]=='#') || (str[i]=='/n') || (str[i]=='/r')  )
   return 0;
  if(str[i]=='{'){
   *multi=1;
   continue;
  }
  if((str[i]!=32)&&(str[i]!='/t'))
   break; 

 } 
 p=i;
 if(!*multi){
  for(i=p+1;i<size;i++){
   if((str[i]==32)||(str[i]=='/n')||(str[i]=='/t')||(str[i]=='/r'))
    break;
  }
 }else{
  for(i=p+1;i<size;i++)
   if(str[i]=='}')
    break;
 }
 len=((MAX_WIDTH>(i-p))?i-p:MAX_WIDTH );

 if(len<=0)
  return 0;
 //printf("len=%d ",len);
 memcpy(word,str+p,len);
 word[len]='/0';
// printf("word=%s.i=%d,p=%d/n",word,i,p);
 return i+1;
}
int match_name(const char *str,char *name,char *value)
{
 int point;
// printf("str=%s/n",str);
 if((point=get_word(str,name,&name_multi))<=0)
  return point;
 point=get_word(str+point,value,&value_multi);
 
// printf("value=%s/n",value);
 // return 1;
 return 1;

}

/*
 typedef struct
 {
  std::string name;//[MAX_WIDTH+1];
  std::string value;//[MAX_WIDTH+1];
  bool name_multi;
  bool value_multi;
 } ITEM_T;
 typedef std::vector<ITEM_T> item_vector;
*/

int split_config_file(const char *str)
{
 int i=0,point=0;
 int tmp;
 char name[MAX_WIDTH+1];
 char value[MAX_WIDTH+1];

// ITEM_T t_item;

// printf("str=%s/n",str);

 while(1){
  name[0]=0;
  value[0]=0;
  if(match_name(str+point,name,value)>0){
//   printf("match_name > 0/n");

   printf("name [%s]/n",name);
   printf("value[%s]/n",value);

   printf("-----------------------------/n");
/*
   t_item.name_multi=name_multi;
   t_item.value_multi=value_multi;
   t_item.name=name;
   t_item.value=value;
   item.push_back(t_item);
*/
  }  
  if((tmp=skip_next_line(str+point))<=0)
   break;
  point+=tmp;

 // printf("i=%d,point=%d/n",i,point);
 }
 return i;
}


int main()
{
 
 open("Config.txt");

 while(1);
 return 0;
}

 

 

/*******ReadConfig.h**************/

 #ifndef _READ_CONFIG_H
#define _READ_CONFIG_H

 

long file_size(FILE *fp);
int open(const char * m_file);
int create(const char *str);
int skip_next_line(const char *str);
int get_word(const char *str,char *word,int * multil);
int match_name(const char *str,char *name,char *value);
int split_config_file(const char *str);


#endif

 

 

//任意配置文件的内容,例:

/****************Config.txt********************/

 #the kingate config file

#service
#if you don't want open one service ,please set it off


http                    on
ftp                     on
pop3                    on
smtp                    on
telnet                  on
socks                   on
mms                     on
rtsp                    on
manage                  on

bind_addr               192.168.0.253

#run_user               nobody


max                     500
#max thread limit.

max_per_ip              0
#thread number limit for every ip address.if the value is 0 ,mean no limit.

min_free_thread         3
#the min_free_thread first add in v1.5.


#[http]
http_port               8082
#http listen port

http_accelerate         off
#be care for don't set http_accelerate to on when you use kingate proxy http protocol
#you must set it on when you use kingate to accelerate the http server
#when you use kingate to accelerate the http server Please add
#http_redirect          { * *  http_host:http_port  none }

x_forwarded_for         off
#add the client ip(who use kingate http proxy) in the http request head


http_time_out           300
#time out in http service.(sec)

#use http redirect ,for second proxy.

#http_redirect          { dst[/dst_mask][:dst_port]     file    redirect_host:redirect_port     flag    }
#http_redirect          { * *  210.15.29.16:80 none }
#http_redirect          { 211. *  210.15.29.16:8082 proxy }
#[ftp]
ftp_port                2121
ftp_time_out            300

#[pop3]
pop3_port               1100

#[smtp]
smtp_port               2525
smtp_time_out           300


#[telnet]
telnet_port             2323
telnet_time_out         300

#[socks]
socks_port              1082
socks_time_out          300
socks5_user             off

#[mms]
mms_port                1755
mms_time_out            300

#[rtsp]
rtsp_port               5540
rtsp_time_out           300

#[manage]
manage_port             2222
manage_time_out         300

#log section
log_model               user
log_level               2                       ^M
log_rotate              { 0 0 * * * }
log_close_msg           off
#the log_close_msg first add in v1.5 when it on the kingate will log the connect close msg to log_file.
#and when it set off kingate won't log the connect close msg to log_file

#?o′?2?·?
#cache section

mem_min_cache           10m
mem_max_cache           12m
disk_min_cache          20m
disk_max_cache          30m
use_disk_cache          off
refresh                 never
refresh_time            0


#???ú???¨?ò
#redirect               port_host:port          #???ú???¨?ò1|?ü£??ùóDport???úμ????ó·¢?í?áhost:port
#redirect               9999_211.141.90.201:23
#redirect               3333_127.0.0.1:23

#[user]
user_time_out           0
#set the user login time out
                                                 
                                                   

 

原创粉丝点击