Commit 6ceb1768 authored by Henrik Andersson's avatar Henrik Andersson

Fixed a memory leak.

Added checks for nullpointers
Added some codecomments..
parent a75ada63
......@@ -38,13 +38,16 @@ gchar *_string_substitute(gchar *string,const gchar *search,const gchar *replace
guint _string_occurence(const gchar *haystack,const gchar *needle)
{
guint o=0;
const gchar *p=haystack;
if( (p=g_strstr_len(p,strlen(p),needle)) != NULL)
if( haystack && needle )
{
do
const gchar *p=haystack;
if( (p=g_strstr_len(p,strlen(p),needle)) != NULL)
{
o++;
} while((p=g_strstr_len((p+1),strlen(p+1),needle)) != NULL);
do
{
o++;
} while((p=g_strstr_len((p+1),strlen(p+1),needle)) != NULL);
}
}
return o;
}
......@@ -199,9 +202,13 @@ gboolean dt_variables_expand(dt_variables_params_t *params, gchar *string, gbool
if( iterate )
params->data->sequence++;
// Let's free previous expanded result if any...
if( params->data->result )
g_free( params->data->result );
// Lets expand string
gchar *result=NULL;
params->data->result=params->data->source=string;
params->data->result = params->data->source = string;
if( (token=_string_get_first_variable(params->data->source,variable)) != NULL)
{
do {
......@@ -218,5 +225,7 @@ gboolean dt_variables_expand(dt_variables_params_t *params, gchar *string, gbool
}
} while( (token=_string_get_next_variable(token,variable)) !=NULL );
}
g_free(variable);
g_free(value);
return TRUE;
}
\ No newline at end of file
......@@ -25,26 +25,27 @@ typedef struct dt_variables_params_t
/** only validates string */
gboolean validate_only;
/** Used for expanding variables that uses filename $(FILE_DIRECTORY) $(FILE_NAME) and $(FILE_EXTENSION). */
/** used for expanding variables that uses filename $(FILE_DIRECTORY) $(FILE_NAME) and $(FILE_EXTENSION). */
const gchar *filename;
/** Used for expanding variable $(JOBCODE) */
/** used for expanding variable $(JOBCODE) */
const gchar *jobcode;
/** Used for expanding variables such as $(IMAGE_WIDTH) $(IMAGE_HEIGT). */
/** used for expanding variables such as $(IMAGE_WIDTH) $(IMAGE_HEIGT). */
struct dt_image_t *img;
/** Internal variables data */
/** internal variables data */
struct dt_variables_data_t *data;
} dt_variables_params_t;
/** allocate and init params */
/** allocate and initializes a dt_variables_params_t. */
void dt_variables_params_init(dt_variables_params_t **params);
/** destroys an initialized dt_variables_params_t, pointer is garbage after this call. */
void dt_variables_params_destroy(dt_variables_params_t *params);
/** expands variables in string */
/** expands variables in string, this free's previous expanding result */
gboolean dt_variables_expand(dt_variables_params_t *params, gchar *string, gboolean iterate);
/** get the expanded string result*/
/** get the expanded string result, use a copy of this string in your code like g_strdup(). */
const gchar *dt_variables_get_result(dt_variables_params_t *params);
#endif
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment