예제 #1
0
파일: iniparser.c 프로젝트: eledot/libnge2
/*--------------------------------------------------------------------------*/
dictionary * iniparser_load(const char * ininame)
{
    FILE * in ;

    char line    [ASCIILINESZ+1] ;
    char section [ASCIILINESZ+1] ;
    char key     [ASCIILINESZ+1] ;
    char tmp     [ASCIILINESZ+1] ;
    char val     [ASCIILINESZ+1] ;

    int  last=0 ;
    int  len ;
    int  lineno=0 ;
    int  errs=0;

    dictionary * dict ;

    if ((in=fopen(ininame, "r"))==NULL) {
        fprintf(stderr, "iniparser: cannot open %s\n", ininame);
        return NULL ;
    }

    dict = dictionary_new(0) ;
    if (!dict) {
        fclose(in);
        return NULL ;
    }

    memset(line,    0, ASCIILINESZ);
    memset(section, 0, ASCIILINESZ);
    memset(key,     0, ASCIILINESZ);
    memset(val,     0, ASCIILINESZ);
    last=0 ;

    while (fgets(line+last, ASCIILINESZ-last, in)!=NULL) {
        lineno++ ;
        len = (int)strlen(line)-1;
        /* Safety check against buffer overflows */
        if (line[len]!='\n') {
            fprintf(stderr,
                    "iniparser: input line too long in %s (%d)\n",
                    ininame,
                    lineno);
            dictionary_del(dict);
            fclose(in);
            return NULL ;
        }
        /* Get rid of \n and spaces at end of line */
        while ((len>=0) &&
                ((line[len]=='\n') || (isspace(line[len])))) {
            line[len]=0 ;
            len-- ;
        }
        /* Detect multi-line */
        if (line[len]=='\\') {
            /* Multi-line value */
            last=len ;
            continue ;
        } else {
            last=0 ;
        }
        switch (iniparser_line(line, section, key, val)) {
            case LINE_EMPTY:
            case LINE_COMMENT:
            break ;

            case LINE_SECTION:
            errs = dictionary_set(dict, section, NULL);
            break ;

            case LINE_VALUE:
            sprintf(tmp, "%s:%s", section, key);
            errs = dictionary_set(dict, tmp, val) ;
            break ;

            case LINE_ERROR:
            fprintf(stderr, "iniparser: syntax error in %s (%d):\n",
                    ininame,
                    lineno);
            fprintf(stderr, "-> %s\n", line);
            errs++ ;
            break;

            default:
            break ;
        }
        memset(line, 0, ASCIILINESZ);
        last=0;
        if (errs<0) {
            fprintf(stderr, "iniparser: memory allocation failure\n");
            break ;
        }
    }
    if (errs) {
        dictionary_del(dict);
        dict = NULL ;
    }
    fclose(in);
    return dict ;
}
예제 #2
0
bool CConfigure::load(){
	const char* ininame = _file_name.c_str();
	FILE * in ;

	char line    [ASCIILINESZ+1] ;
	char section [ASCIILINESZ+1] ;
	char key     [ASCIILINESZ+1] ;
	//char tmp     [ASCIILINESZ+1] ;
	char val     [ASCIILINESZ+1] ;

	int  last=0 ;
	int  len ;
	int  lineno=0 ;
	//int  errs=0;

	if ((in=fopen(ininame, "r"))==NULL){
		TRACE(1, "CConfigure::load 打开配置文件失败。名字: "<<ininame);
		return false ;
	}

	memset(line,    0, ASCIILINESZ);
	memset(section, 0, ASCIILINESZ);
	memset(key,     0, ASCIILINESZ);
	memset(val,     0, ASCIILINESZ);
	last=0 ;

	while (fgets(line+last, ASCIILINESZ-last, in)!=NULL) {
		lineno++ ;
		len = (int)strlen(line)-1;
		/* Safety check against buffer overflows */
		if (line[len]!='\n') {
			TRACE(1, "CConfigure::load 加载配置文件失败。行数: "<<lineno);
			fclose(in);
			return false ;
		}
		/* Get rid of \n and spaces at end of line */
		while ((len>=0) &&
			((line[len]=='\n') || (isspace(line[len])))) {
				line[len]=0 ;
				len-- ;
		}
		/* Detect multi-line */
		if (line[len]=='\\') {
			/* Multi-line value */
			last=len ;
			continue ;
		} else {
			last=0 ;
		}
		switch (iniparser_line(line, section, key, val)) 
		{
		case LINE_EMPTY:
		case LINE_COMMENT:
			{
				break ;
			}			
		case LINE_SECTION:
			break ;
		case LINE_VALUE:
			{
				if(!parse_value(key, val)){
					TRACE(1, "CConfigure::load 分析配置文件内容失败。行数:"<<lineno);
				}
				break;
			}
		case LINE_ERROR:
			{
				TRACE(1, "CConfigure::load 分析配置文件失败。行数: "<<lineno);
				break;
			}
		default:
			{
				break ;
			}	
		}
		memset(line, 0, ASCIILINESZ);
		last=0;
	}
	fclose(in);
	return true;
}
예제 #3
0
string CConfigure::GetStrParam(const string& astrKey,const string& astrSection,const string& astrCfgFile){
	const char* ininame = astrCfgFile.c_str();
	FILE * in ;

	char line    [ASCIILINESZ+1] ;
	char section [ASCIILINESZ+1] ;
	char key     [ASCIILINESZ+1] ;
	char val     [ASCIILINESZ+1] ;

	int  last=0 ;
	int  len ;
	int  lineno=0 ;
	int  errs=0;

	if ((in=fopen(ininame, "r"))==NULL){
		TRACE(1, "CConfigure::load 打开配置文件失败。名字: "<<ininame);
		return false ;
	}

	memset(line,    0, ASCIILINESZ);
	memset(section, 0, ASCIILINESZ);
	memset(key,     0, ASCIILINESZ);
	memset(val,     0, ASCIILINESZ);
	last=0 ;
	bool lbFound = false;
	string lstrValue = "";
	while (!lbFound && fgets(line+last, ASCIILINESZ-last, in)!=NULL){
		lineno++ ;
		len = (int)strlen(line)-1;
		/* Safety check against buffer overflows */
		if (line[len]!='\n') {
			TRACE(1, "CConfigure::load 加载配置文件失败。行数: "<<lineno);
			fclose(in);
			return false ;
		}
		/* Get rid of \n and spaces at end of line */
		while ((len>=0) &&
			((line[len]=='\n') || (isspace(line[len])))) {
			line[len]=0 ;
			len-- ;
		}
		/* Detect multi-line */
		if (line[len]=='\\') {
			/* Multi-line value */
			last=len ;
			continue ;
		} else {
			last=0 ;
		}
		switch (iniparser_line(line, section, key, val)) 
		{
		case LINE_EMPTY:
		case LINE_COMMENT:
			{
				break ;
			}			
		case LINE_SECTION:
			break ;
		case LINE_VALUE:
			{
				//parse_value(key, val);
				if(astrKey == key && astrSection == section)
				{
					lbFound = true;
					lstrValue = val;
				}
				break;
			}
		case LINE_ERROR:
			{
				TRACE(1, "CConfigure::load 分析配置文件失败。行数: "<<lineno);
				break;
			}
		default:
			{
				break ;
			}	
		}
		memset(line, 0, ASCIILINESZ);
		last=0;
	}
	fclose(in);
	return lstrValue;
}
예제 #4
0
void Test_iniparser_line(CuTest *tc)
{
    char section [ASCIILINESZ+1] ;
    char key     [ASCIILINESZ+1] ;
    char val     [ASCIILINESZ+1] ;

    /* Test empty line */
    CuAssertIntEquals(tc, LINE_EMPTY, iniparser_line("", section, key, val));
    CuAssertIntEquals(tc, LINE_EMPTY, iniparser_line("    ", section, key, val));
    CuAssertIntEquals(tc, LINE_EMPTY, iniparser_line("\t", section, key, val));

    /* Test valid syntax */
    CuAssertIntEquals(tc, LINE_SECTION, iniparser_line("[s]", section, key, val));
    CuAssertStrEquals(tc, "s", section);

    CuAssertIntEquals(tc, LINE_SECTION, iniparser_line("[ section ]", section, key, val));
    CuAssertStrEquals(tc, "section", section);

    CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("k=1", section, key, val));
    CuAssertStrEquals(tc, "k", key);
    CuAssertStrEquals(tc, "1", val);

    CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("key = 0x42", section, key, val));
    CuAssertStrEquals(tc, "key", key);
    CuAssertStrEquals(tc, "0x42", val);

    CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("key= value with spaces", section, key, val));
    CuAssertStrEquals(tc, "key", key);
    CuAssertStrEquals(tc, "value with spaces", val);

    CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("k =_!<>''", section, key, val));
    CuAssertStrEquals(tc, "k", key);
    CuAssertStrEquals(tc, "_!<>''", val);

    CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("empty_value =", section, key, val));
    CuAssertStrEquals(tc, "empty_value", key);
    CuAssertStrEquals(tc, "", val);

    CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("key =\tval # comment", section, key, val));
    CuAssertStrEquals(tc, "key", key);
    CuAssertStrEquals(tc, "val", val);

    CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("key \n\n = \n val", section, key, val));
    CuAssertStrEquals(tc, "key", key);
    CuAssertStrEquals(tc, "val", val);

    CuAssertIntEquals(tc, LINE_COMMENT, iniparser_line(";comment", section, key, val));
    CuAssertIntEquals(tc, LINE_COMMENT, iniparser_line(" # comment", section, key, val));

    /* Test syntax error */
    CuAssertIntEquals(tc, LINE_ERROR, iniparser_line("empty_value", section, key, val));
    CuAssertIntEquals(tc, LINE_ERROR, iniparser_line("not finished\\", section, key, val));
    CuAssertIntEquals(tc, LINE_ERROR, iniparser_line("0x42 / 0b101010", section, key, val));
}
예제 #5
0
/*--------------------------------------------------------------------------*/
dictionary * iniparser_load(const char * ininame)
{
    FILE *in, *include = NULL, *inifile;

    char line    [ASCIILINESZ+1] ;
    char section [ASCIILINESZ+1] ;
    char key     [ASCIILINESZ+1] ;
    char tmp     [ASCIILINESZ+1] ;
    char val     [ASCIILINESZ+1] ;

    int  last=0 ;
    int  len ;
    int  lineno=0 ;
    int  errs=0;

    dictionary * dict ;

    if ((inifile=fopen(ininame, "r"))==NULL) {
        LOG(log_error, logtype_default, "iniparser: cannot open \"%s\"", ininame);
        return NULL ;
    }

    dict = dictionary_new(0) ;
    if (!dict) {
        fclose(inifile);
        return NULL ;
    }

    memset(line,    0, ASCIILINESZ);
    memset(section, 0, ASCIILINESZ);
    memset(key,     0, ASCIILINESZ);
    memset(val,     0, ASCIILINESZ);
    last=0 ;

    in = inifile;
    while (1) {
        if (fgets(line+last, ASCIILINESZ-last, in) == NULL) {
            if (include) {
                fclose(include);
                include = NULL;
                in = inifile;
                continue;
            }
            break;
        }
        lineno++ ;
        len = (int)strlen(line)-1;
        if (len==0)
            continue;
        /* Safety check against buffer overflows */
        if (line[len]!='\n') {
            LOG(log_error, logtype_default, "iniparser: input line too long in \"%s\" (lineno: %d)",
                ininame, lineno);
            dictionary_del(dict);
            fclose(in);
            return NULL ;
        }
        /* Get rid of \n and spaces at end of line */
        while ((len>=0) &&
                ((line[len]=='\n') || (isspace(line[len])))) {
            line[len]=0 ;
            len-- ;
        }
        /* Detect multi-line */
        if (line[len]=='\\') {
            /* Multi-line value */
            last=len ;
            continue ;
        } else {
            last=0 ;
        }
        switch (iniparser_line(line, section, key, val)) {
        case LINE_EMPTY:
        case LINE_COMMENT:
            break ;
        case LINE_SECTION:
            if (strchr(section, ':') != NULL)
                LOG(log_error, logtype_default, "iniparser: syntax error \"%s\" section name must not contain \":\".", section);
            errs = dictionary_set(dict, section, NULL, NULL);
            break ;
        case LINE_VALUE:
            if (strcmp(key, "include") == 0) {
                if ((include = fopen(val, "r")) == NULL) {
                    LOG(log_error, logtype_default, "iniparser: cannot open \"%s\"", val);
                    continue;
                }
                in = include;
                continue;
            }
            errs = dictionary_set(dict, section, key, val) ;
            break ;
        case LINE_ERROR:
            LOG(log_error, logtype_default, "iniparser: syntax error in %s (lineno: %d): %s",
                ininame, lineno, line);
            errs++ ;
            break;
        default:
            break ;
        }
        memset(line, 0, ASCIILINESZ);
        last=0;
        if (errs<0) {
            LOG(log_error, logtype_default, "iniparser: memory allocation failure");
            break ;
        }
    }
    if (errs) {
        dictionary_del(dict);
        dict = NULL ;
    }
    fclose(in);
    return dict ;
}
예제 #6
0
/*--------------------------------------------------------------------------*/
dictionary * iniparser_load(const char * ininame)
{
    FILE * in = NULL ;

    char line    [ASCIILINESZ+1] ;
    char *section = xstrdup("");
    char *current_section = NULL;
    char *key = NULL;
    char *val = NULL;
    char* full_line = NULL;
    char* prev_line = NULL;

    int  len ;
    int  lineno=0 ;
    int  errs=0;
    int  seckey_size=0;

    dictionary * dict = NULL ;

    if ((in=fopen(ininame, "r"))==NULL) {
        fprintf(stderr, "iniparser: cannot open %s\n", ininame);
        goto out;
    }

    dict = dictionary_new(0) ;
    if (!dict) {
        goto out;
    }

    memset(line,    0, ASCIILINESZ);

    while (fgets(line, ASCIILINESZ, in)!=NULL) {
        int prev_line_len = 0;
        int multi_line = 0;
        int total_size = 0;

        if (key) {
            free(key);
            key = NULL;
        }
        if (val) {
            free(val);
            val = NULL;
        }

        lineno++ ;
        len = (int)strlen(line)-1;
        if (len==0)
            continue;
        /* Safety check against buffer overflows */
        if (line[len]!='\n' && !feof(in)) {
            fprintf(stderr,
                    "iniparser: input line too long in %s (%d)\n",
                    ininame,
                    lineno);
            errs++;
            goto out;
        }
        /* Get rid of \n and spaces at end of line */
        while ((len>=0) &&
                ((line[len]=='\n') || (isspace(line[len])))) {
            line[len]=0 ;
            len-- ;
        }

        if (len < 0) { /* Line was entirely \n and/or spaces */
            len = 0;
        }

        /* Detect multi-line */
        if (line[len]=='\\') {
            multi_line = 1;
        }
        if (multi_line) {
            /* Multi-line value */
            /* length without trailing '\' */
            /* remove multi-line indicator before appending*/
            line[len] = 0;
            len--;
        }

        /*
         * If processing a multi-line then append it the previous portion,
         * at this point 'full_line' has the previously read portion of a
         * multi-line line (or NULL)
         */
        prev_line = full_line;
        prev_line_len=0;
        if (prev_line) {
            prev_line_len = (int)strlen(prev_line);
        }

        /* len is not strlen(line) but strlen(line) -1 */
        total_size = (len+1) + prev_line_len + 1;

        full_line = malloc(total_size);
        if (!full_line) {
            fprintf(stderr,
                    "iniparser: out of mem\n");
            errs++;
            goto out;
        }

        memset(full_line, 0, total_size);

        if (prev_line) {
            strcpy(full_line, prev_line);
        }

        strcpy(full_line + prev_line_len, line);
        free(prev_line);
        prev_line = NULL;

        if (multi_line) {
            continue ;
        }

        switch (iniparser_line(total_size, full_line, &current_section, &key, &val)) {
            case LINE_EMPTY:
            case LINE_COMMENT:
            break ;

            case LINE_SECTION:
            if (section) {
                free(section);
                section=NULL;
            }
            errs = dictionary_set(dict, current_section, NULL);
            section = current_section;
            break ;

            case LINE_VALUE:
            {
                char *seckey;
                /* section + ':' + key + eos */
                seckey_size = (int)strlen(section) + (int)strlen(key) +2;
                seckey = malloc(seckey_size);
                if (!seckey) {
                    errs++;
                    fprintf(stderr,
                           "iniparser: out of mem\n");
                    goto out;
                }
                snprintf(seckey, seckey_size, "%s:%s", section, key);
                errs = dictionary_set(dict, seckey, val) ;
                free(seckey);
                seckey = NULL;
            }
            break ;

            case LINE_ERROR:
            fprintf(stderr, "iniparser: syntax error in %s (%d):\n",
                    ininame,
                    lineno);
            fprintf(stderr, "-> %s\n", full_line);
            errs++ ;
            break;

            default:
            break ;
        }
        memset(line, 0, ASCIILINESZ);
        if (full_line) {
            free(full_line);
            full_line = NULL;
        }
        if (errs<0) {
            fprintf(stderr, "iniparser: memory allocation failure\n");
            break ;
        }
    }
out:
    if (errs) {
        dictionary_del(dict);
        dict = NULL ;
    }
    if (val) {
        free(val);
        val = NULL;
    }
    if (key) {
        free(key);
        key = NULL;
    }
    if (section) {
        free(section);
        section = NULL;
    }
    if (full_line) {
        free(full_line);
        full_line = NULL;
    }
    if (prev_line) {
        free(prev_line);
        prev_line = NULL;
    }
    if (in) {
        fclose(in);
    }
    return dict ;
}