/* 
 * File:   utilities.h
 * Author: Andréa
 *
 * Created on 15 octobre 2010, 17:45
 */

#ifndef UTILITIES_H
#define	UTILITIES_H

#include <stdlib.h>

#include <libxml/parser.h>
#include <libxml/xpath.h>

#include "configuration.h"

/**
 * Data Structure which represents a xml property file use with test unit case
 */
typedef struct {
	char *fichier;
	xmlDocPtr doc;
	xmlNodePtr racine;
	xmlXPathContextPtr ctxt;
} xmlConfig_t;

/**
 * Use to handle nicely some basic error and return what we want
 */
#define handleError(test,errorReturn,header,message) \
        if(test)                                     \
        {                                            \
            logMessage(SEVERE,header,message);       \
            return errorReturn;                      \
        }

/**
 * (Dirty) way to avoid decimal limit overflow
 */
#define checkDecimalLimit(value,method)                      \
        if( value >= 1000000000000)                          \
        {                                                    \
        	method->_result_loaded              = FALSE;     \
        	return;                                          \
        }                                                    \

/**
 * Get the current session group's id
 */
long getSessionId();
/**
 * Set the current session group's id
 */
void setSessionId(long sessionId);

/**
 * Nicely way to use built-in random
 */
double homeRandom(double min, double max);

/**
 * Return a representation of a vector of the given dimension
 */
char* printVector(double vector[], size_t dimension);

/**
 * Return a representation of a matrix of given size
 */
char* printMatrix(double **matrix, size_t row, size_t column);

/**
 * Return a representation of given boolean
 */
char* printBoolean(boolean b);

/**
 * Helper to compute an approximation of a function's derivative
 */
double* approximateGradient(problem_function function, double x[],
		size_t dimension, double delta);

/**
 * Compute euclidian's norme of given vector
 */
double normeEuclidienne(const double x[], const size_t dimension);

/**
 * Compute the difference between x and solution of dimension's size
 */
double error(unsigned int dimension, double x[], double solution[]);

/**
 * Nice function to quickly parse an int to his representation
 */
char* integerToString(int val);

/**
 * Nice function to quickly parse a double to his representation
 */
char* doubleToString(double val);

/**
 * Check if a file exists or not
 */
boolean fileExists(char * filepath);

/**
 * Free a 'xmlConfig_t' structure
 */
void destroyConfig(xmlConfig_t *conf);
/**
 * Load a xml property file into a xmlConfig_t structure
 */
xmlConfig_t * loadConfiguration(const char *fichier);
/**
 * Get the 'directive' property value from a 'conf'
 */
char * getProperty(xmlConfig_t *conf, const char *directive);

/**
 * Load every data from 'parameters' to 'bundle'
 */
void
loadMethodParameters(MethodParameters *bundle, MethodParameters parameters);

/**
 * Set a 'propertyFlag' with 'propertyValue' in 'parameters' structure
 */
void setParameterProperty(MethodParameters *parameters, int propertyFlag,
		void * propertyValue);
/**
 * Get a 'propertyFlag' value in 'parameters' structure
 */
void* getParameterProperty(MethodParameters parameters, int propertyFlag);

#endif	/* UTILITIES_H */

