Пример #1
0
/**
* @brief writeFile a profile string to a ini file
* @param section [in] name of the section,can't be NULL and empty string
* @param key [in] name of the key pairs to value, can't be NULL and empty string
* @param value [in] profile string value
* @param file [in] path of ini file
* @return 1 : success\n 0 : failure
*/
int write_profile_string(const char *section, const char *key,
        const char *value, const char *file) {
    char buf[MAX_FILE_SIZE] = {0};
    char w_buf[MAX_FILE_SIZE] = {0};
    int sec_s, sec_e, key_s, key_e, value_s, value_e;
    int value_len = (int) strlen(value);
    int file_size;
    FILE *out;

    //check parameters
    assert(section != NULL && strlen(section));
    assert(key != NULL && strlen(key));
    assert(value != NULL);
    assert(file != NULL && strlen(key));

    if (!load_ini_file(file, buf, &file_size)) {
        sec_s = -1;
    } else {
        parse_file(section, key, buf, &sec_s, &sec_e, &key_s, &key_e, &value_s,
                &value_e);
    }

    if (-1 == sec_s) {
        if (0 == file_size) {
            sprintf(w_buf + file_size, "[%s]\n%s=%s\n", section, key, value);
        } else {
            //not find the section, then add the new section at end of the file
            memcpy(w_buf, buf, file_size);
            sprintf(w_buf + file_size, "\n[%s]\n%s=%s\n", section, key, value);
        }
    } else if (-1 == key_s) {
        //not find the key, then add the new key=value at end of the section
        memcpy(w_buf, buf, sec_e);
        sprintf(w_buf + sec_e, "%s=%s\n", key, value);
        sprintf(w_buf + sec_e + strlen(key) + strlen(value) + 2, buf + sec_e,
                file_size - sec_e);
    } else {
        //update value with new value
        memcpy(w_buf, buf, value_s);
        memcpy(w_buf + value_s, value, value_len);
        memcpy(w_buf + value_s + value_len, buf + value_e, file_size - value_e);
    }

    out = fopen(file, "w");
    if (NULL == out) {
        return 0;
    }

    if (-1 == fputs(w_buf, out)) {
        fclose(out);
        return 0;
    }

    fclose(out);
    return 1;
}
Пример #2
0
/**
*@brief read string in initialization file\n
* retrieves a string from the specified section in an initialization file
*@param section [in] name of the section containing the key name
*@param key [in] name of the key pairs to value 
*@param value [in] pointer to the buffer that receives the retrieved string
*@param size [in] size of result's buffer 
*@param default_value [in] default value of result
*@param file [in] path of the initialization file
*@return 1 : read success; \n 0 : read fail
*/
int read_profile_string( const char *section, const char *key,char *value, 
		 int size, const char *default_value, const char *file)
{
	char *buf;
	int file_size;
	int sec_s,sec_e,key_s,key_e, value_s, value_e;

	//check parameters
	assert(section != NULL && strlen(section));
	assert(key != NULL && strlen(key));
	assert(value != NULL);
	assert(size > 0);
	assert(file !=NULL &&strlen(key));

	buf = (char *)sys_malloc(MAX_FILE_SIZE);
	memset(buf,0,MAX_FILE_SIZE);

	if(!load_ini_file(file,buf,&file_size))
	{
		if(default_value!=NULL)
		{
			strncpy(value,default_value, size);
		}
		sys_free(buf);
		return 0;
	}

	if(!parse_file(section,key,buf,&sec_s,&sec_e,&key_s,&key_e,&value_s,&value_e))
	{
		if(default_value!=NULL)
		{
			strncpy(value,default_value, size);
		}
		sys_free(buf);
		return 0; //not find the key
	}
	else
	{
		int cpcount = value_e -value_s;

		if( size-1 < cpcount)
		{
			cpcount =  size-1;
		}
	
		memset(value, 0, size);
		memcpy(value,buf+value_s, cpcount );
		value[cpcount] = '\0';

		sys_free(buf);

		return 1;
	}
}
Пример #3
0
void preset_user_interface(UniversalInterface& ui, stdx::fun_ref<void(UniversalInterface&)> target
                         , char const* defaultFile, char const* activeFilePath, ui::UniversalInterface::InteractionParam<char const*> activeFileChange
                         , char const* groupName, UniqueElementIdentifier id)
{
	if (!id.value) id = UEI(preset_user_interface);

	if (auto presetGroup = ui::Group(ui, id))
	{
		uintptr_t controlIdentifier = 0;

		ui.addText(controlIdentifier++, (groupName) ? groupName : "Preset", "", nullptr);

		auto&& loadPreset = [&](char const* file)
		{
			try
			{
				activeFilePath = defaultFile = nullptr; // warning: everything may change
				if (activeFileChange) activeFileChange->updateValue(file);
				load_ini_file(file, target);
			}
			catch(...) { stdx::prompt(appx::exception_string().c_str(), "Error applying preset"); }
		};
		if (activeFilePath)
		{
			auto hiddenIdentifier = controlIdentifier++;
			if (activeFilePath[0]) ui.addHidden(hiddenIdentifier, "active", activeFilePath, loadPreset);
		}
		ui.addButton(controlIdentifier++, (activeFilePath && activeFilePath[0] && defaultFile && defaultFile[0]) ? defaultFile : "load", [&]()
		{
			auto mf = stdx::prompt_file_compat(defaultFile, "Ini files=*.ini|All files=*.*", stdx::dialog::open, true);
			for (auto&&f : mf) loadPreset(f.c_str());
		});
		

		ui.addButton(controlIdentifier++, "save", [&]()
		{
			auto mf = stdx::prompt_file_compat(defaultFile, "Ini files=*.ini|All files=*.*", stdx::dialog::save);
			for (auto&&f : mf)
			{
				try
				{
					activeFilePath = defaultFile = nullptr; // warning: everything may change
					if (activeFileChange) activeFileChange->updateValue(f.c_str());
					save_ini_file(f.c_str(), target);
				}
				catch(...) { stdx::prompt(appx::exception_string().c_str(), "Error saving preset"); }
			}
		});
	}
}
Пример #4
0
static void load_config(bool reload)
{
	bool ok = load_ini_file(cf.config_file, conf_sects, NULL);
	if (!ok) {
		if (reload) {
			log_warning("failed to read config");
		} else {
			fatal("failed to read config");
		}
	}

	/* fixme */
	cf_syslog_ident = cf.syslog ? "pgqd" : NULL;
	reset_logging();
}
Пример #5
0
/*********************************************************************************
  *函数名称:int param_load(void)
  *功能描述:将ini文件参数加载进来,如果为空则创建文件,如果没有某一个参数,则添加该参数
  *输	入: none
  *输	出: none
  *返 回 值:  0:成功读取,大于0:读取时发现需要更新,-1:表示失败
  *---------------------------------------------------------------------------------
  * @修改人		修改时间   	修改内容
  * 白养民		2015-06-08	创建
*********************************************************************************/
int param_load(void)
{
	int		i,i_sect;
	const char *file =DF_PARAM_INI_FILE;
	char read_val[BUF_SIZE]={0};
	char buf[MAX_FILE_SIZE]={0};
	int age;
	int file_size;
	int ret_val = 0;
	long temp_val;
	
	memset(buf,0,sizeof(buf));
	load_ini_file(file,buf,&file_size);
	
	for( i_sect = 0; i_sect < sizeof( tbl_ini_lookup ) / sizeof( struct _st_tbl_ini_lookup ); i_sect++ )
	{
		for( i = 0; i < tbl_ini_lookup[i_sect].param_size / sizeof( st_tbl_id_lookup ); i++ )
		{
			//printf("%s\n",tbl_id_lookup[i].id);
			memset(read_val,0,sizeof(read_val));
			ret_val += read_profile_write_buf(tbl_ini_lookup[i_sect].section, tbl_ini_lookup[i_sect].param[i].id,read_val, 
				BUF_SIZE,tbl_ini_lookup[i_sect].param[i].default_val,buf);
			if(tbl_ini_lookup[i_sect].param[i].type <= TYPE_DWORD)
			{
				temp_val = atoi(read_val);
				memcpy(tbl_ini_lookup[i_sect].param[i].val,(uint8_t *)&(temp_val),tbl_ini_lookup[i_sect].param[i].type);
			}
			else if(tbl_ini_lookup[i_sect].param[i].type != TYPE_CAN)
			{
				memcpy(tbl_ini_lookup[i_sect].param[i].val,(uint8_t *)(read_val),tbl_ini_lookup[i_sect].param[i].type);
			}
			else
			{
				Ascii_To_Hex(tbl_ini_lookup[i_sect].param[i].val,read_val,8);
			}
		}
	}
	if(ret_val)
	{
		save_ini_file(file,buf);
		printf("****************存储数据更新!***********************\n");
	}
	return ret_val;
}
Пример #6
0
/*********************************************************************************
  *函数名称:int param_save_ex(char * section)
  *功能描述:将所有当前参数保存
  *输	入: section	:ini文件参数对应的的section
  *输	出: none
  *返 回 值:  0:成功,非0:表示失败, -1 表示打开文件失败
  *---------------------------------------------------------------------------------
  * @修改人		修改时间   	修改内容
  * 白养民		2015-06-08	创建
*********************************************************************************/
int param_save_ex(char * section)
{
	int		i,i_sect;
	const char *file =DF_PARAM_INI_FILE;
	char write_val[BUF_SIZE]={0};
	char buf[MAX_FILE_SIZE]={0};
	int age;
	int file_size;
	int ret_val = 0;
	long temp_val;
	u32		ul_data;
	
	if(load_ini_file(file,buf,&file_size))
		return -1;

	for( i_sect = 0; i_sect < sizeof( tbl_ini_lookup ) / sizeof( struct _st_tbl_ini_lookup ); i_sect++ )
	{
		if((strlen(section) == 0)||(strncmp(section,tbl_ini_lookup[i_sect].section,strlen(tbl_ini_lookup[i_sect].section)) == 0))
		{
			for( i = 0; i < tbl_ini_lookup[i_sect].param_size / sizeof( st_tbl_id_lookup ); i++ )
			{
				memset(write_val,0,sizeof(write_val));
				switch(tbl_ini_lookup[i_sect].param[i].type)
				{
					case TYPE_BYTE:
					{
						ul_data=*(uint8_t *)tbl_ini_lookup[i_sect].param[i].val;
					}
					case TYPE_WORD:
					{
						ul_data=*(uint16_t *)tbl_ini_lookup[i_sect].param[i].val;
					}
					case TYPE_DWORD:
					{
						ul_data=*(uint32_t *)tbl_ini_lookup[i_sect].param[i].val;
						sprintf(write_val,"%d",ul_data);
						break;
					}
					case TYPE_CAN:
					{
						Hex_To_Ascii(write_val, tbl_ini_lookup[i_sect].param[i].val, 8);
						break;
					}
					/*
					case TYPE_STR:
					{
						memset(buffer,0,sizeof(buffer));
						memcpy(buffer,tbl_id_lookup[i].val,32);
						rt_kprintf("\"%s\";",buffer);
						break;
					}
					*/
					default :
					{
						memcpy(write_val,tbl_ini_lookup[i_sect].param[i].val,tbl_ini_lookup[i_sect].param[i].type);
						break;
					}
				}
				write_profile_string_buf(tbl_ini_lookup[i_sect].section,tbl_ini_lookup[i_sect].param[i].id,write_val,buf);
			}
		}
	}
	return save_ini_file(file,buf);
}
Пример #7
0
/**
 * @brief write a profile string to a ini file
 * @param section [in] name of the section,can't be NULL and empty string
 * @param key [in] name of the key pairs to value, can't be NULL and empty string
 * @param value [in] profile string value
 * @param file [in] path of ini file
 * @return 1 : success\n 0 : failure
 */
int write_profile_string(const char *section, const char *key,
					const char *value, const char *file)
{
	char *buf;
	char *w_buf;
	int sec_s,sec_e,key_s,key_e, value_s, value_e;
	int value_len = (int)strlen(value);
	int file_size;
	unsigned int new_file_size;

	//check parameters
	assert(section != NULL && strlen(section));
	assert(key != NULL && strlen(key));
	assert(value != NULL);
	assert(file !=NULL &&strlen(key));

	buf = (char *)sys_malloc(MAX_FILE_SIZE);
	w_buf = (char *)sys_malloc(MAX_FILE_SIZE);
	memset(buf,0,MAX_FILE_SIZE);
	memset(w_buf,0,MAX_FILE_SIZE);

	if(!load_ini_file(file,buf,&file_size))
	{
		sec_s = -1;
	}
	else
	{
		parse_file(section,key,buf,&sec_s,&sec_e,&key_s,&key_e,&value_s,&value_e);
	}

	if( -1 == sec_s)
	{
		if(0==file_size)
		{
			sprintf(w_buf,"[%s]\r\n%s=%s\r\n",section,key,value);
			new_file_size = strlen(section) + strlen(key) + strlen(value) + 5;
		}
		else
		{
			//not find the section, then add the new section at end of the file
			memcpy(w_buf,buf,file_size);
			sprintf(w_buf+file_size,"\r\n[%s]\r\n%s=%s\r\n",section,key,value);
			new_file_size = file_size + strlen(section) + strlen(key) + value_len + 7;
		}
	}
	else if(-1 == key_s)
	{
		//not find the key, then add the new key=value at end of the section
		memcpy(w_buf, buf, sec_e);
		sprintf(w_buf + sec_e, "%s=%s\r\n", key, value);
		memcpy(w_buf + sec_e + strlen(key) + value_len + 3,buf + sec_e, file_size - sec_e);
		new_file_size = file_size + strlen(key) + value_len + 3;
	}
	else
	{
		//update value with new value
		memcpy(w_buf, buf, value_s);
		memcpy(w_buf + value_s, value, value_len);
		memcpy(w_buf+value_s+value_len, buf+value_e, file_size - value_e);
		new_file_size = file_size - value_e + value_len + value_s;
	}

	FIL file_obj;
	unsigned int br;

	if (f_open(&file_obj, file, FA_WRITE)) {
		sys_free(buf);
		sys_free(w_buf);
		return 0;
	}

	if (f_write(&file_obj, w_buf, new_file_size, &br)) {
		sys_free(buf);
		sys_free(w_buf);
		return 0;
	}

	if (f_truncate(&file_obj)) {		/* Truncate unused area */
		sys_free(buf);
		sys_free(w_buf);
		return 0;
	}

	f_close(&file_obj);

	sys_free(buf);
	sys_free(w_buf);
	return 1;
}