Example #1
0
File: ini.c Project: oblalex/horus
void ini_value_with_comment_set(INI_CONTAINER* _this, const char* section_name, const char* key, const char* value, const char* comment)
{
	ini_append(_this, section_name, key, value, comment);
}
Example #2
0
File: ini.c Project: 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);
	}
}
Example #3
0
File: ini.c Project: 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;
	}
}
Example #4
0
int ini_append_int(INI *pini,const char *key,int value)
{
	char buffer[VINT_LEN];
	sprintf(buffer,"%d\0",value);
	return ini_append(pini,key,buffer);
}