⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 conf.c

📁 linux下的FFT 频谱分析
💻 C
字号:
#define _GNU_SOURCE#include <stdio.h>#include <string.h>#include <ctype.h>#include <malloc.h>#include <glib.h>#include <stdlib.h>#include <errno.h>#include "conf.h"#define error(x...) fprintf(stderr,x)#define debug(x...) fprintf(stderr,x)static GHashTable *vals = NULL;int conf_read(char *fname){    FILE *fd = fopen(fname, "r");    if ( ! fd )    {        error("%s: %s\n", fname, strerror(errno));        return 0;    }    char *buf = NULL;    char *section = NULL;    char *lineptr = NULL;    size_t len;    int l=0,buflen;    if ( ! vals ) vals = g_hash_table_new(g_str_hash, g_str_equal);    while ( getline(&lineptr, &len, fd) > 0 )    {	            l++;        char *p = lineptr;        while ( p && *p && isspace(*p) ) p++;        if ( !p || ! *p ) continue;        if ( *p == '%' ) continue;        char *skey = p;        while ( *p && !isspace(*p) ) p++;        char *key  = strndup(skey,p-skey);        if ( *key=='[' && *(key+(p-skey)-1)==']' )         {            if ( section ) free(section);            buflen = p-skey-1;            section = malloc(buflen);            snprintf(section,buflen,"%s",key+1);            free(key);            continue;        }        while ( *p && isspace(*p) ) p++;        if ( !*p )         {             error("could not parse line %d: %s\n", l,lineptr);            return 0;        }        if ( *p != '=' )        {            error("expected '=' on line %d: %s\n", l,lineptr);            return 0;        }        p++;        while ( *p && isspace(*p) ) p++;        if ( !*p )        {            error("premature end of line %d: %s\n", l,lineptr);            return 0;        }        char *sval;        if ( *p == '"' )        {            p++;            sval = p;            while ( *p )            {                if ( *p=='"' && *(p-1)!='\\' ) break;                p++;            }        }        else        {            sval = p;            while ( *p && !isspace(*p) && (*p!='%' || *(p-1)=='\\') ) p++;        }        char *val = strndup(sval,p-sval);        if ( section )        {            buflen = strlen(key) + strlen(section) + 2;            buf = malloc(buflen);            snprintf(buf,buflen,"%s/%s",section,key);            free(key);            key = buf;        }        g_hash_table_insert(vals, key, val);        /*debug("conf: %-10s = \"%s\"\n", key,val);*/    }    fclose(fd);    return 1;}void conf_add_string(char *key, char *val){      if (!vals) vals = g_hash_table_new(g_str_hash, g_str_equal);      g_hash_table_insert(vals, key, val);}char *conf_get_string(char *key){      if (!vals) return NULL;      return g_hash_table_lookup(vals, key);}int conf_get_int(char *key, int *i){      if (!vals) return 0;      char *v = g_hash_table_lookup(vals, key);      if ( ! v ) return 0;            *i = atoi(g_hash_table_lookup(vals, key));      return 1;}int conf_get_float(char *key, float *f){      if (!vals) return 0;      char *v = g_hash_table_lookup(vals, key);      if ( ! v ) return 0;            *f = atof(g_hash_table_lookup(vals, key));      return 1;}int conf_get_double(char *key, double *f){      if (!vals) return 0;      char *v = g_hash_table_lookup(vals, key);      if ( ! v ) return 0;            *f = atof(g_hash_table_lookup(vals, key));      return 1;}GSList *conf_get_gslist(char *key){    GSList *list=NULL;    char *v, *tmp;    if (!vals) return NULL;    v = g_hash_table_lookup(vals, key);    if ( ! v ) return NULL;    while ( v && *v )    {        /* Spaces are ignored in the original variant:             while ( *v && (isspace(*v) || *v==',') ) v++;         */        while ( *v && *v==',' ) v++;        if ( ! *v ) return list;        char *t = v;        /* Spaces are ignored:             while ( *t && !isspace(*t) && *t!=',') t++;        */        while ( *t && *t!=',') t++;        tmp = malloc(t-v+1);        snprintf(tmp,t-v+1,"%s",v);        list = g_slist_append(list,tmp);        v = t;    }    return list;}char *sconf_get_string(char *section, char *key){    char *str, *ret;    int len;       len = 12;    if (section)         len += strlen(section);    if (key)         len += strlen(key);    str = malloc(len);    snprintf(str,len,"%s/%s", section?section:"default",key);    ret = conf_get_string(str);        if (!ret)    {        snprintf(str,len,"default/%s", key);        ret = conf_get_string(str);    }    free(str);    return ret;}int sconf_get_int(char *section, char *key, int *val){    char *str;    int len, ret;        len = 12;    if (section)         len += strlen(section);    if (key)         len += strlen(key);    str = malloc(len);    snprintf(str,len,"%s/%s", section?section:"default",key);    ret = conf_get_int(str,val);        if (!ret)    {        snprintf(str,len,"default/%s",key);        ret = conf_get_int(str,val);    }    free(str);    return ret;}int sconf_get_float(char *section, char *key, float *val){    char *str;    int len, ret;        len = 12;    if (section)         len += strlen(section);    if (key)         len += strlen(key);    str = malloc(len);    snprintf(str,len,"%s/%s", section?section:"default",key);    ret = conf_get_float(str,val);    if ( !ret )    {        snprintf(str,len,"default/%s",key);        ret = conf_get_float(str,val);    }    free(str);    return ret;}GSList *sconf_get_gslist(char *section, char *key){    GSList *ret;    char *str;    int len;        len = 12;    if (section)         len += strlen(section);    if (key)         len += strlen(key);    str = malloc(len);    snprintf(str,len,"%s/%s", section?section:"default",key);    ret = conf_get_gslist(str);        if (!ret)    {        snprintf(str,len,"default/%s",key);        ret = conf_get_gslist(str);    }    free(str);    return ret;}char *sconf_get_string_or_die(char *section, char *key){    char *ret = sconf_get_string(section,key);        if (!ret)        exit_nicely("Value not defined in the config file: %s/%s\n", section?section:"default", key);    return ret;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -