C语言中配置文件解析案例

int parse_config()
{
    FILE *fp;
    int ret = 0, count = 0, err = 0;
    int id,flag;
    char *type,*name;
    char buffer[MAX_LINE_LEN]={0};

    fp = fopen(PCBA_SUPPORT_CONFIG, "r");
    if(fp == NULL) {
        LOGE("mmitest open %s failed! %d IN", PCBA_SUPPORT_CONFIG, __LINE__);
        return -1;
    }

    /* parse line by line */
    ret = 0;
    while(fgets(buffer, MAX_LINE_LEN, fp) != NULL) {
        if('#'==buffer[0])
            continue;
        if((buffer[0]>='0') && (buffer[0]<='9')){
            ret = parse_case_entries(buffer,&id,&flag);
            if(ret != 0) {
                LOGD("mmitest parse %s,buffer=%s return %d.  reload",PCBA_SUPPORT_CONFIG, buffer,ret);
                fclose(fp);
                return -1;
            }
            support_result[count].id = id;
            support_result[count++].support= flag;
            err = 1;
        }else if(!strncmp("sensor", buffer, 6)){
            ret = parse_sensor_entries(buffer);
            if(ret != 0) {
                LOGD("mmitest parse %s,buffer=%s return %d.  reload",PCBA_SUPPORT_CONFIG, buffer,ret);
                fclose(fp);
                return -1;
            }
            err = 1;
        }else if(!strncmp("key", buffer, 3)){
            ret = parse_key_entries(buffer);
            if(ret != 0) {
                LOGD("mmitest parse %s,buffer=%s return %d.  reload",PCBA_SUPPORT_CONFIG, buffer,ret);
                fclose(fp);
                return -1;
            }
            err = 1;
        }
        if(0 == err)
            LOGD("can't check line = %s", buffer);
        err = 0;
    }

    fclose(fp);
    if(count < TOTAL_NUM) {
        LOGD("mmitest parse slog.conf failed");
    }

    return ret;
}


#define PCBA_SUPPORT_CONFIG   "/system/etc/factorytest/PCBA.conf"

你可能感兴趣的:(c)