Linux C实现小项目配置文件读取

/*************************************************************************
        > File Name: Cfg.h
        > Author: zhoulin 
        > Created Time: Wed 16 Mar 2016 06:17:27 PM CST
 ************************************************************************/
项目需要,有时候需要读取配置文件,大多以kv的方式。该小程序仅仅是读取字符,控制字符和空白行,空白字符是不读取的。
#ifndef _Cfg_H
#define WORD "="
typedef struct _Item
{
    void *K;
    void *V;
    struct _Item *Next;
}Item;
typedef struct _Node
{
    int Size;
    char *Base;
    struct _Node *Next;
    struct _Item *iHead;
}Node;
typedef struct _Cfg 
{
    int Size;
    struct _Node *nHead;
}Cfg;
Cfg *CfgNew(const char *path);
Node *GetNodeByBase(char *base,Cfg *cfg);
char  *GetValByKey(char *base,char *key,Cfg *Cfg);
int CfgFree(Cfg *cfg);
#define _Cfg_H
#endif
/*************************************************************************
        > File Name: Cfg.c
        > Author: zhoulin 
        > Created Time: Wed 16 Mar 2016 06:31:46 PM CST
 ************************************************************************/

#include "cfg.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define bufSize  1024
char *trimSp(char *buf)
{
    char tbuf[bufSize] = {'\0'};
    if(buf == NULL || strlen(buf) ==0)
    {
        return NULL;
    }
    int len = strlen(buf);
    int i = 0, j =0;
    while(i < len)
    {
        if(iscntrl(buf[i]) ==0 && isspace(buf[i]) ==0)
        {
            tbuf[j] = buf[i];
            j++;
        }
        i++;
    }
    memset(buf,'\0',len);
    strncpy(buf,tbuf,strlen(tbuf));
    return buf;
}
int getPos(char *buf)
{
    if(buf == NULL || strlen(buf) == 0)
    {
        return -1;
    }
    int len = strlen(buf);
    int i = 0;
    while(i < len)
    {
        if(buf[i] == '='){
            break;
        }
        i++;
    }
    return i++;
}
Cfg *CfgNew(const char *path)
{
    Cfg *cfg = NULL;
    if(path == NULL || strlen(path) == 0)
    {
        return NULL;
    }
    FILE *fp = fopen(path,"r");
    char buf[bufSize] = {'\0'};
    if(fp == NULL)
    {
        return NULL;
    }
    cfg = (Cfg *)malloc(sizeof(*cfg));
    while(fgets(buf,bufSize,fp) != NULL)
    {
        char *tbuf = trimSp(buf);
        int pos = getPos(tbuf);
        int len = strlen(tbuf);
        if(len > 0 && len == pos  && tbuf[0] == '[' && tbuf[len-1] == ']')
        {
            Node *tmp = (Node *)malloc(sizeof(*tmp));
            tmp->Base = (char *)malloc(len-1);
            memset(tmp->Base,'\0',len-1);
            strncpy(tmp->Base,tbuf+1,len-2);
            if(cfg->nHead == NULL)
            {
                cfg->nHead  = tmp;
            }else{
                tmp->Next =cfg->nHead;
                cfg->nHead = tmp;
            }
            cfg->Size++;
        }else{
            if(len > 0 && pos != len)
            {
                Node *h = cfg->nHead;
                char *val = tbuf + pos+1;
                tbuf[pos] = '\0';
                char *key = tbuf;
                int k_len = strlen(key);
                int v_len = strlen(val);
                Item *t = h->iHead;
                if(t == NULL) {
                    t = (Item *)malloc(sizeof(*t));
                    h->iHead = t;
                }else {
                    Item *cur = (Item *)malloc(sizeof(*cur));
                    cur->Next = h->iHead;
                    h->iHead = cur;
                }
                h->iHead->K = (char *)malloc(k_len+1);
                h->iHead->V = (char *)malloc(v_len+1);
                memset(h->iHead->K,'\0',k_len+1);
                memset(h->iHead->V,'\0',v_len+1);
                strncpy(h->iHead->K,key,k_len);
                strncpy(h->iHead->V,val,v_len);
                h->Size++;
            }
        }
    memset(buf,'\0',bufSize);
    }
    if(fp != NULL){
        fclose(fp);
    }
    if(cfg->Size == 0){
        free(cfg);
        cfg = NULL;
    }
    return cfg;
}
static void prtCfg(Cfg *m)
{
    printf("cfg =%p,cfg->Size=%d\n",m,m->Size);
    Node *cur = m->nHead;
    while(cur != NULL)
    {
        printf("   -----cur node=%p,next =%p,base= %s[len=%d],size =%d\n",cur,cur->Next,\
               cur->Base,strlen(cur->Base),cur->Size);
        Item *icur = cur->iHead;
        while(icur != NULL)
        {
            printf("        *******cur item=%p,next =%p,key[len=%d] = %s,val[len=%d] = %s\n",icur,icur->Next,\
                   strlen(icur->K),icur->K,strlen(icur->V),icur->V);
            icur = icur ->Next;
        }
        cur = cur->Next;
    }
}
Node *GetNodeByBase(char *base,Cfg *cfg)
{
    Node *nv = NULL;
    if(cfg == NULL) {
        return nv;
    }
    Node *cur = cfg->nHead;
    while(cur != NULL)
    {
        if(strncmp(cur->Base,base,strlen(cur->Base)) == 0)
        {
            nv = cur;
        }
        cur = cur->Next;
    }
    return nv;
}
char  *GetValByKey(char *base,char *key,Cfg *cfg)
{
    char  *val = NULL;
    if(cfg == NULL || cfg->Size <= 0){
        return NULL;
    }
    Node *cur = cfg->nHead;
    while(cur != NULL)
    {
        if(strncmp(cur->Base,base,strlen(cur->Base)) == 0)
        {
            Item *icur = cur->iHead;
            while(icur != NULL)
            {
                if(strncmp(icur->K,key,strlen(icur->K)) == 0){
                    val = icur->V;
                    break;
                }
                icur = icur->Next;
            }
        }
        cur = cur->Next;
    }
    return val;
}
int  CfgFree(Cfg *cfg)
{
    if(cfg == NULL || cfg->Size ==0)
    {
        return -1;
    }
    Node *cur = cfg->nHead;
    while(cur != NULL)
    {
        Item *icur = cur->iHead;
        while(icur != NULL)
        {
            printf("        ******** free icur=%p,next=%p********\n",icur,icur->Next);
            free(icur->K);
            free(icur->V);
            free(icur);
            icur->K = icur->V = NULL;
            icur = icur ->Next;
        }
        if(icur != NULL){
            icur = NULL;
        }
        printf("******** free node=%p,next=%p********\n",cur,cur->Next);
        free(cur->Base);
        free(cur);
        cur->Base =  NULL;
        cur = cur->Next;
    }
    if(cur != NULL){
        cur = NULL;
    }
    return 0;
}
int main(void)
{
    Cfg *m = CfgNew("./test");
    prtCfg(m);
    char *v = GetValByKey("mysql","port",m);
    Node *t = GetNodeByBase("tr",m);
    if(t != NULL)
    {
        printf("%p,base =%s,size =%d\n",t,t->Base,t->Size);
        Item *x = t->iHead;
        while(x != NULL)
        {
            printf("cur = %p,next =%p,k=%s,v=%s\n",x,x->Next,x->K,x->V);
            x = x->Next;
        }
    }
    printf("v =%s\n",v);
    char *v1 = GetValByKey("tr","bn",m);
    printf("v1 =%s\n",v1);
    printf("free =%d\n",CfgFree(m));
    return 0;
}

运行结果:

测试的test文件:

[ ok ]
v1 =v2
k3 = v3
#k
        #df
dddd
  #[dd]
KK   #  D
KK = == V
        [yu]
        dgf = fd
[mysql]
user = root

port = 4450

[tr]
    #comment
  t1  = vo
  m2 == max
bn====m8

  

 

cfg =0x23da250,cfg->Size=4
   -----cur node=0x23da5a0,next =0x23da490,base= tr[len=2],size =3
        *******cur item=0x23da6b0,next =0x23da650,key[len=2] = bn,val[len=5] = ===m8
        *******cur item=0x23da650,next =0x23da5f0,key[len=2] = m2,val[len=4] = =max
        *******cur item=0x23da5f0,next =(nil),key[len=2] = t1,val[len=2] = vo
   -----cur node=0x23da490,next =0x23da3e0,base= mysql[len=5],size =2
        *******cur item=0x23da540,next =0x23da4e0,key[len=4] = port,val[len=4] = 4450
        *******cur item=0x23da4e0,next =(nil),key[len=4] = user,val[len=4] = root
   -----cur node=0x23da3e0,next =0x23da270,base= yu[len=2],size =1
        *******cur item=0x23da430,next =(nil),key[len=3] = dgf,val[len=2] = fd
   -----cur node=0x23da270,next =(nil),base= ok[len=2],size =3
        *******cur item=0x23da380,next =0x23da320,key[len=2] = KK,val[len=3] = ==V
        *******cur item=0x23da320,next =0x23da2c0,key[len=2] = k3,val[len=2] = v3
        *******cur item=0x23da2c0,next =(nil),key[len=2] = v1,val[len=2] = v2
0x23da5a0,base =tr,size =3
cur = 0x23da6b0,next =0x23da650,k=bn,v====m8
cur = 0x23da650,next =0x23da5f0,k=m2,v==max
cur = 0x23da5f0,next =(nil),k=t1,v=vo
v =4450
v1 ====m8
        ******** free icur=0x23da6b0,next=0x23da650********
        ******** free icur=0x23da650,next=0x23da5f0********
        ******** free icur=0x23da5f0,next=(nil)********
******** free node=0x23da5a0,next=0x23da490********
        ******** free icur=0x23da540,next=0x23da4e0********
        ******** free icur=0x23da4e0,next=(nil)********
******** free node=0x23da490,next=0x23da3e0********
        ******** free icur=0x23da430,next=(nil)********
******** free node=0x23da3e0,next=0x23da270********
        ******** free icur=0x23da380,next=0x23da320********
        ******** free icur=0x23da320,next=0x23da2c0********
        ******** free icur=0x23da2c0,next=(nil)********
********

  

posted @ 2016-03-18 09:59  一个万能盒子叫数据库  阅读(2761)  评论(0编辑  收藏  举报