// Utils to load files
//
// author: Marcelo de Castro Cardoso

#include <string.h>
#include <stdio.h>

#include "ig_pmiutils.h"

/* Load variable in config file
 * 
 * Input param:
 * fileName - file name
 * configName - variable name
 *
 * Output param:
 * value - pointer to create value of the file
 *
 * Return param:
 * error code
 */
int PMIU_loadConfig(char * fileName, char * configName, char ** value)
{
	FILE * file = fopen(fileName, "r");
	char line[100];
	char * token;
	int size;
	int err = UTILS_FAIL;
	
	if (file != NULL)
	{
		while(fgets(line, 100, file) != NULL)
		{
			token = strtok(line, " \t\n");
			if (token != NULL && strcmp(token, configName) == 0)
			{
				token = strtok(NULL, " \t\n");
				if (token != NULL)
				{
					size = strlen(token);
					*value = (char*) malloc(sizeof(char) * (size + 1));
					strcpy(*value, token);
					err = UTILS_SUCCESS;
				}
			}
		}
		
		fclose(file);
	}
	
	return err;
}
