Example #1
0
int file_to_items(const char *file,char split, ITEM *items, int *num)
{
    FILE *fp;
    fp = fopen(file,"r");
    if(fp == NULL)
        return 1;
	char line[MAX_LENGTH];
	int i = 0;
	memset(line,0,MAX_LENGTH);
    while(fgets(line, MAX_LENGTH - 1, fp))
	{
		if(items == NULL) //获取有多少条目
		{
			++i;
			memset(line,0,MAX_LENGTH);
			continue;
		}
		if(*num == i) //获取前num条目
		{
			break;
		}
        if(get_item_from_line(line,split,&items[i]) == 0)
		{
			++i;
		}
		memset(line,0,MAX_LENGTH);
    }
    *num = i;
	
    fclose(fp);
	
    return 0;
}
Example #2
0
/*
 *读取value
 */
int read_conf_value(const char *key,char *value,const char *file)
{
    char line[MAX_LENGTH];
    FILE *fp = NULL;
	ITEM item;
	int iret = -1;
    fp = fopen(file,"r");
    if(fp == NULL)
        return iret;//文件打开错误
	
	iret = 1;
	memset(line,0,MAX_LENGTH);
    while (fgets(line, MAX_LENGTH - 1, fp))
	{        
	    memset(&item, 0, sizeof(ITEM));
        if(get_item_from_line(line,':',&item) != 0)
		{
			memset(line,0,MAX_LENGTH);
			continue;
		}
		//printf("****%s:%s\n",item.key,item.value);
        if(strcmp(item.key,key) == 0){
			strcpy(value,item.value);
			iret = 0;
            break;
        }
		memset(line,0,MAX_LENGTH);
    }
	
	fclose(fp);
    return iret;//成功
}
Example #3
0
int read_conf_value(const char *key, char *value, const char *file)
{
	FILE *fp = NULL;
	item_t item;
	char line[MAX_READ_LINE_NUM];

	if (key == NULL || value == NULL || file == NULL) {
		printf("params error!\n");
		return -1;
	}

	fp = fopen(file, "r");
	if (fp == NULL) {
		printf("file %s not exit!\n", file);
		return -1;
	}

	memset(&item, 0, sizeof(item_t));

	while (fgets(line, MAX_READ_LINE_NUM - 1, fp)) {
		if (!get_item_from_line(line, &item)) {
			if (item.key != NULL && item.value != NULL) {
				if (!strcmp(item.key, key)) {
					strcpy(value, item.value);
					break;
				}
			}
			free_item_space(&item);
		}
	}

	free_item_space(&item);
	fclose(fp);
	fp = NULL;

	return 0;
}
Example #4
0
int file_to_memory(const char *file, item_t * items, int items_num)
{
	FILE *fp = NULL;
	int i = 0, j = 0;
	char line[MAX_READ_LINE_NUM];

	fp = fopen(file, "r");
	if (fp == NULL) {
		printf("file %s not exit!\n", file);
		return -1;
	}

	memset(items, 0, (items_num * sizeof(item_t)));

	while (fgets(line, MAX_READ_LINE_NUM - 1, fp)) {
		if (i >= MAX_ITEMS_NUM) {
			printf("too much items,only support 100 items!\n");
			return MAX_ITEMS_NUM;
		}

		if (!get_item_from_line(line, &items[i])) {
			i++;
		}
	}

	for (j < 0; j < i + 1; j++) {
		if (items[j].comment == NULL && items[j].key == NULL
			&& items[j].value == NULL)
			break;
	}

	fclose(fp);
	fp = NULL;

	return j;
}