porting/dpl_os/src/config_file.c
/*
 * Try to compress configuration file by keeping unique names only.
 */
Try to compress configuration file by keeping unique names only.
Try to compress configuration file by keeping unique names only .
void
conf_file_compress(struct conf_file *cf)
{
    int rc;
    FILE *rf;
    FILE *wf;
    char tmp_file[CONF_FILE_NAME_MAX];
    char buf1[CONF_MAX_NAME_LEN + CONF_MAX_VAL_LEN + 32];
    char buf2[CONF_MAX_NAME_LEN + CONF_MAX_VAL_LEN + 32];
    uint32_t loc1, loc2;
    char *name1, *val1;
    char *name2, *val2;
    int copy;
    int len, len2;
    int lines;
    rf = fopen(cf->cf_name, "r");
    if (!rf) {
        return;
    }
    conf_tmpfile(tmp_file, cf->cf_name, ".cmp");
    wf = fopen(tmp_file, "w");
    if (!wf) {
        fclose(rf);
        return;
    }
    loc1 = 0;
    lines = 0;
    while (1) {
        len = conf_getnext_line(rf, buf1, sizeof(buf1), &loc1);
        if (loc1 == 0 || len < 0) {
            break;
        }
        rc = conf_line_parse(buf1, &name1, &val1);
        if (rc) {
            continue;
        }
        if (!val1) {
            continue;
        }
        loc2 = loc1;
        copy = 1;
        while ((len2 = conf_getnext_line(rf, buf2, sizeof(buf2), &loc2)) > 0) {
            rc = conf_line_parse(buf2, &name2, &val2);
            if (rc) {
                continue;
            }
            if (!strcmp(name1, name2)) {
                copy = 0;
                break;
            }
        }
        if (!copy) {
            continue;
        }
        len = conf_line_make(buf2, sizeof(buf2), name1, val1);
        if (len < 0 || len + 2 > sizeof(buf2)) {
            continue;
        }
        buf2[len++] = '\n';
        fwrite(buf2, 1, len, wf);
        lines++;
    }
    fclose(wf);
    fclose(rf);
    unlink(cf->cf_name);
    rename(tmp_file, cf->cf_name);
    cf->cf_lines = lines;
}
void conf_file_compress ( struct conf_file * cf ) { int rc ; FILE * rf ; FILE * wf ; char tmp_file [ CONF_FILE_NAME_MAX ] ; char buf1 [ CONF_MAX_NAME_LEN + CONF_MAX_VAL_LEN + 32 ] ; char buf2 [ CONF_MAX_NAME_LEN + CONF_MAX_VAL_LEN + 32 ] ; uint32_t loc1 , loc2 ; char * name1 , * val1 ; char * name2 , * val2 ; int copy ; int len , len2 ; int lines ; rf = fopen ( cf -> cf_name , " " ) ; if ( ! rf ) { return ; } conf_tmpfile ( tmp_file , cf -> cf_name , " " ) ; wf = fopen ( tmp_file , " " ) ; if ( ! wf ) { fclose ( rf ) ; return ; } loc1 = 0 ; lines = 0 ; while ( 1 ) { len = conf_getnext_line ( rf , buf1 , sizeof ( buf1 ) , & loc1 ) ; if ( loc1 == 0 || len < 0 ) { break ; } rc = conf_line_parse ( buf1 , & name1 , & val1 ) ; if ( rc ) { continue ; } if ( ! val1 ) { continue ; } loc2 = loc1 ; copy = 1 ; while ( ( len2 = conf_getnext_line ( rf , buf2 , sizeof ( buf2 ) , & loc2 ) ) > 0 ) { rc = conf_line_parse ( buf2 , & name2 , & val2 ) ; if ( rc ) { continue ; } if ( ! strcmp ( name1 , name2 ) ) { copy = 0 ; break ; } } if ( ! copy ) { continue ; } len = conf_line_make ( buf2 , sizeof ( buf2 ) , name1 , val1 ) ; if ( len < 0 || len + 2 > sizeof ( buf2 ) ) { continue ; } buf2 [ len ++ ] = ' \n ' ; fwrite ( buf2 , 1 , len , wf ) ; lines ++ ; } fclose ( wf ) ; fclose ( rf ) ; unlink ( cf -> cf_name ) ; rename ( tmp_file , cf -> cf_name ) ; cf -> cf_lines = lines ; }
Try to compress configuration file by keeping unique names only.
Try to compress configuration file by keeping unique names only .
/*
         * Can't find one. Must copy.
         */ /*
     * XXX at conf_file_load(), look for .cmp if actual file does not
     * exist.
     */