Esempio n. 1
0
void Settings::set(const StringView& _name, const StringView& _value)
{
	ini_t* ini = INI_T(m_ini);

	FilePath uri(_name);
	const StringView  path(strTrim(uri.getPath(), "/") );
	const StringView& fileName(uri.getFileName() );

	int32_t section = INI_GLOBAL_SECTION;

	if (!path.isEmpty() )
	{
		section = ini_find_section(ini, path.getPtr(), path.getLength() );
		if (INI_NOT_FOUND == section)
		{
			section = ini_section_add(ini, path.getPtr(), path.getLength() );
		}
	}

	int32_t property = ini_find_property(ini, section, fileName.getPtr(), fileName.getLength() );
	if (INI_NOT_FOUND == property)
	{
		ini_property_add(
			  ini
			, section
			, fileName.getPtr()
			, fileName.getLength()
			, _value.getPtr()
			, _value.getLength()
			);
	}
	else
	{
		ini_property_value_set(
			  ini
			, section
			, property
			, _value.getPtr()
			, _value.getLength()
			);
	}
}
Esempio n. 2
0
File: ini.c Progetto: oblalex/horus
void ini_load(INI_CONTAINER* _this)
{
	FILE *in_stream;
	char buffer[255];
	char comments[1024];
	char current_section[255];	
	char key[255];
	char value[255];
	char *pdest;
	int  index;

	strcpy(comments, "");
	strcpy(current_section, "");
	_this->error_msg = NULL;

	if ((in_stream = fopen(_this->filepath, "r" )) != NULL)
	{
		while (fgets(buffer, sizeof(buffer), in_stream) != NULL)
		{
			trim_new_line(buffer);
			switch (buffer[0])
			{
				case '[' :
					pdest = strrchr(buffer, ']');
					if (pdest == NULL)
					{
						fclose(in_stream);
						_this->error_msg = INI_PARSING_ERR;
						return;
					}
					index = pdest - buffer;
					memcpy(current_section, buffer + 1, index - 1);
					current_section[index - 1] = '\0';
					ini_section_add(_this, current_section, comments);	
					if (ini_has_err(_this) == TRUE) {
						fclose(in_stream);
						return;
					}
					strcpy(comments, "");
					break;
				case '#' :
				case ';' :
					if(strlen(comments) > 0)
						strcat(comments, "\n");
					strcat(comments, buffer);
					break;
				default :
                    if (strlen(buffer) == 0) continue;
					pdest = strrchr(buffer, '=');
					if (pdest == NULL) 
					{
						fclose(in_stream);
                        _this->error_msg = INI_PARSING_ERR;
						return;
					}
					index = pdest - buffer;
					memcpy(key, buffer, index);
					key[index] = '\0';
					memcpy(value, buffer + index + 1, strlen(buffer)-index);
					
					if (strcmp(current_section, "") == 0)
					{
						fclose(in_stream);
                        _this->error_msg = INI_PARSING_ERR;
						return;
					}
					else
					{
						ini_append(_this, current_section, key, value, comments);
						if (ini_has_err(_this) == TRUE) {
							fclose(in_stream);
							return;
						}
						strcpy(comments, "");
					}
					break;
			}
		}
		fclose(in_stream);
	}
	else
	{
		_this->error_msg = INI_OPENING_ERR;
	}
}
Esempio n. 3
0
File: ini.c Progetto: oblalex/horus
void ini_append(INI_CONTAINER* _this, const char *name, const char *key, const char *value, const char *comment)
{
	INI_SECTION* section;
	INI_RECORD* record;
	
	_this->error_msg = NULL;
	
	section = ini_section_get(_this, name);

	if (section != NULL)
	{
		record = ini_record_get(section, key);
		if (record == NULL)
		{
			record = (INI_RECORD*)malloc(sizeof(INI_RECORD));
			if (record == NULL)
			{
				_this->error_msg = INI_MALLOC_ERR_RECORD;
				return;
			}
			record->next = NULL;	
			
			if((comment[0] != '#' || comment[0] != ';') && (strlen(comment) > 0))
				sprintf(record->comments, "#%s", comment);
			else
				strcpy(record->comments, comment);
			
			strcpy(record->key, key);
			strcpy(record->value, value);			
			section->records_count++;

			if (section->record_first == NULL)
			{
				section->record_first = record;
				section->record_last  = record;
			}
			else
			{
				section->record_last->next = record;
				section->record_last = record;
			}			
		}
		else
		{
			if ((comment[0] != '#' || comment[0] != ';') && (strlen(comment) > 0))
				sprintf(record->comments, "#%s", comment);
			else
				strcpy(record->comments, comment);
			
			strcpy(record->key, key);
			strcpy(record->value, value);
		}
		
	}
	else
	{
		ini_section_add(_this, name, "");
		if (ini_has_err(_this) == FALSE)
			ini_append(_this, name, key, value, comment);
	}
}