zoukankan      html  css  js  c++  java
  • C实现键值对(Map功能)

    kv.h代码如下:
    
    #define UT_BASE_NODE_T(TYPE) 
        struct {          
            TYPE *start;  
            TYPE *end;    
        }
    typedef struct _kv
    {
        char *k;
        char *v;
        unsigned int klen;
        unsigned int vlen;
        unsigned int id;
        struct _kv *next;
    }kv;
    typedef struct _map
    {
        char *base;
        unsigned int id;
        unsigned int   kv_count; //number of maps
        UT_BASE_NODE_T(struct _kv) bt;
        struct _map *next;
    }map;
    typedef struct _table
    {
        unsigned int map_count;
        UT_BASE_NODE_T(struct _map) info;
    }table;
    /*
     * read map info from file 
     */
    extern table *Map_init(const char *path);
    /*
     * acroding a index of key,get a basename, key,value
     */
    extern char **Get_bkv(table *tb,int map_id,int key_index);
    /*
     *
     */
    extern void Map_free(table  *tb);
    /*
     *
     */
    extern void *Map_prt(table *tb);
    kv.c代码如下:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "kv.h"
    #define MAX  1024
    #define CFLAG  10
    #define L  91
    #define R  93
    char *trim(char *msg)
    {
        
        int msg_len=strlen(msg);
        char *trunc=strchr(msg,CFLAG);
        int trunc_len=strlen(trunc);
        *(msg+msg_len-trunc_len)='';
        //printf("%s,len=%d
    ",msg,strlen(msg));
        return msg;
    }
    inline static int lr(const char *msg)
    {
        int len=strlen(msg);
        return (*(msg+0)==L)&&(*(msg+len-1)==R);
    }
    char *base(char *name)
    {
        char *tmp=(name+1);
        *(tmp+strlen(tmp)-1)='';
        return tmp;
    }
    table *Map_init(const char *path)
    {
        FILE *fp=fopen(path,"r+");
        char buf[MAX];
        map *res=NULL;
        table *tb=NULL;
        int map_count=0;
        int kvid=0,mapid=0;
        kv *kv_head=NULL;
        if(!fp)
        {
            fprintf(stderr,"fopen %s is null
    ",path);
            return tb;
        }
        tb=(table *)malloc(sizeof(table));
        if(!tb)
            return tb;
        tb->map_count=0;
        while((fgets(buf,MAX,fp))!=NULL)
        {
            char *s=trim(buf);
            int len=strlen(s);
            int flag=lr(s);
            if(flag==1)
            {
               char *name=base(s);
               int name_len=strlen(name);
               printf("%s,flag=%d
    ",name,flag);
               if(res==NULL)
               {
                   res=(map *)malloc(sizeof(map));
                   tb->map_count=0;
                   tb->info.start=res;
               }
               else
               {
                   map *cur=(map *)malloc(sizeof(map));
                   res->next=cur;
                   res=cur;
               }
               mapid++;
               tb->info.end=res;
               tb->map_count++;
               res->id=mapid;
               res->base=(char *)malloc(name_len+1);
               res->kv_count=0;
               memset(res->base,'',name_len+1);
               memcpy(res->base,name,name_len);
               kvid=0;
               kv_head=NULL;
            }
            else
            {
                char *k=strtok(s,"=");
                char *v=strtok(NULL,"=");
                int k_len=strlen(k);
                int v_len=strlen(v);
                if(kv_head==NULL)
                {
                    kv_head=(kv *)malloc(sizeof(kv));
                    res->bt.start=kv_head;
                }
                else
                {
                    kv *cur=(kv *)malloc(sizeof(kv));
                    kv_head->next=cur;
                    kv_head=cur;
                }
                kv_head->klen=k_len;
                kv_head->vlen=v_len;
               // printf("k = %s[%d], v= %s[%d]
    ",k,kv_head->klen,v,kv_head->vlen);
                res->kv_count++;
                kvid++;
                res->bt.end=kv_head;
                kv_head->k=(char *)malloc(k_len+1);
                kv_head->v=(char *)malloc(v_len+1);
                memset(kv_head->k,'',k_len);
                memset(kv_head->v,'',v_len);
                memcpy(kv_head->k,k,k_len);
                memcpy(kv_head->v,v,v_len);
                kv_head->id=kvid;
            }
        }
        if(fp!=NULL)
        {
            fclose(fp);
        }
        //printf(" map header=%p
    ",head);
        return tb;
    }
    void *Map_prt(table *tb)
    {
        if(tb!=NULL)
        {
            map *mp=tb->info.start;
            printf("tb->map_count =%d,map start=%p,map end=%p
    ",tb->map_count,tb->info.start,tb->info.end);
            printf("#################################################
    ");
            printf(" map->kv.start=%p,map->kv.end=%p
    ",mp->bt.start,mp->bt.end);
            while(mp!=NULL)
            {
                printf("map addr =%p,next=%p,map base =%s, map id=%d
    ",mp,mp->next,mp->base,mp->id);
                kv *t=mp->bt.start;
                while(t!=NULL)
                {
                    printf("    kv addr=%p,next=%p
    ",t,t->next);
                    printf("        kv_id=%d,k =%s(k len=%d),v =%s(v len=%d)
    ",t->id,t->k,t->klen,t->v,t->vlen);
                    t=t->next;
                }
                mp=mp->next;
            }
        }
    }
    char **Get_bkv(table *tb,int map_id,int key_index)
    {
        char *s[3]={NULL};
        char **bkv=NULL;
        if(!tb)
        {
            fprintf(stderr," table map is null
    ");
            return bkv;
        }
        if(!(map_id&&key_index))
        {
            fprintf(stderr," map id and key index must > 0
    ");
            return bkv;
        }
        if(map_id>tb->map_count)
        {
            fprintf(stderr," map id over number of map count
    ");
            return bkv;
        }
        map *mp=tb->info.start;
        kv *t=NULL;
        while(mp!=NULL)
        {
            if(key_index>mp->kv_count)
            {
                fprintf(stderr," key index over number of keys
    ");
                break;
            }
            if(mp->id==map_id)
            {
                t=mp->bt.start;
                while(t!=NULL)
                {
                    if(t->id==key_index)
                    {
                        s[0]=mp->base;
                        s[1]=t->k;
                        s[2]=t->v;
                        bkv=(char **)&s;
                        break;
                    }
                    t=t->next;
                }
            }
            mp=mp->next;
        }
        //bkv=(char **)&s;
        return bkv;
    }
    void Map_free(table  *tb)
    {
        if(!tb){fprintf(stderr," table is null
    ");exit(0);}
        map *head=tb->info.start;
        while(head!=NULL)
        {
            map *maptmp=head->next;
            kv *kvtmp=head->bt.start;
            while(kvtmp!=NULL)
            {
                kv *tmp=kvtmp->next;
                printf(" free kv =%p
    ",kvtmp);
                free(kvtmp->k);
                free(kvtmp->v);
                kvtmp->k=NULL;
                kvtmp->v=NULL;
                free(kvtmp);
                kvtmp=tmp;
            }
            kvtmp=NULL;
            printf("free map =%p
    ",head);
            free(head);
            head=maptmp;
        }
        head=NULL;
        free(tb);
        tb=NULL;
    }
    int main(void)
    {
        char *path="./1.txt";
        table *tb=Map_init(path);
        Map_prt(tb);
        int i,j;
        for(i=1;i<=tb->map_count;i++)
        {
            for(j=1;j<=40;j++)
            {
                char **s=Get_bkv(tb,i,j);
                if(s!=NULL)
                {
                    printf("base = %s,key =%s,value=%s
    ",s[0],s[1],s[2]);
                }
            }
            printf("        ######################          
    ");
        }
        Map_free(tb);
        return 0;
    }
    运行结果:
    
    [mysql@centos2 mysql_tool]$ ./kv
    master,flag=1
    slave1,flag=1
    tb->map_count =2,map start=0xaa2270,map end=0xaa24f0
    #################################################
     map->kv.start=0xaa22c0,map->kv.end=0xaa2480
    map addr =0xaa2270,next=0xaa24f0,map base =master, map id=1
        kv addr=0xaa22c0,next=0xaa2330
            kv_id=1,k =master_user(k len=11),v =root(v len=4)
        kv addr=0xaa2330,next=0xaa23a0
            kv_id=2,k =master_password(k len=15),v =root(v len=4)
        kv addr=0xaa23a0,next=0xaa2410
            kv_id=3,k =master_port(k len=11),v =3306(v len=4)
        kv addr=0xaa2410,next=0xaa2480
            kv_id=4,k =master_socket(k len=13),v =/tmp/mysql.sock(v len=15)
        kv addr=0xaa2480,next=(nil)
            kv_id=5,k =master_host(k len=11),v =localhost(v len=9)
    map addr =0xaa24f0,next=(nil),map base =slave1, map id=2
        kv addr=0xaa2540,next=0xaa25b0
            kv_id=1,k =slave1_user(k len=11),v =root(v len=4)
        kv addr=0xaa25b0,next=0xaa2620
            kv_id=2,k =slave1_password(k len=15),v =root(v len=4)
        kv addr=0xaa2620,next=0xaa2690
            kv_id=3,k =slave1_port(k len=11),v =3306(v len=4)
        kv addr=0xaa2690,next=0xaa2700
            kv_id=4,k =slave1_socket(k len=13),v =/tmp/mysql.sock(v len=15)
        kv addr=0xaa2700,next=(nil)
            kv_id=5,k =slave1_host(k len=11),v =localhost(v len=9)
    base = master,key =master_user,value=root
    base = master,key =master_password,value=root
    base = master,key =master_port,value=3306
    base = master,key =master_socket,value=/tmp/mysql.sock
    base = master,key =master_host,value=localhost
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
            ######################          
    base = slave1,key =slave1_user,value=root
    base = slave1,key =slave1_password,value=root
    base = slave1,key =slave1_port,value=3306
    base = slave1,key =slave1_socket,value=/tmp/mysql.sock
    base = slave1,key =slave1_host,value=localhost
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
     key index over number of keys
            ######################          
     free kv =0xaa22c0
     free kv =0xaa2330
     free kv =0xaa23a0
     free kv =0xaa2410
     free kv =0xaa2480
    free map =0xaa2270
     free kv =0xaa2540
     free kv =0xaa25b0
     free kv =0xaa2620
     free kv =0xaa2690
     free kv =0xaa2700
    free map =0xaa24f0
  • 相关阅读:
    JavaScript中运算符的优先级
    JS中在当前日期上追加一天或者获取上一个月和下一个月
    Window命令行工具操作文件
    多线程Worker初尝试
    基于gulp的前端自动化开发构建新
    cURL和file_get_contents实现模拟post请求
    Thinkphp5使用validate实现验证功能
    微信小程序wx.pageScrollTo的替代方案
    js设计模式之代理模式以及订阅发布模式
    js设计模式之单例模式
  • 原文地址:https://www.cnblogs.com/innobase/p/4717470.html
Copyright © 2011-2022 走看看