Fix sanity checks in utils_str_replace().

Fix several memleaks.
Avoid using broken functions g_strdown() and g_strup().
Only create header and source files if filename is not empty.	   


git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@1625 ea778897-0a13-0410-b9d1-a72fbfd435f5
This commit is contained in:
Enrico Tröger 2007-06-15 17:16:06 +00:00
parent d8377881fd
commit b496302508
3 changed files with 52 additions and 20 deletions

View File

@ -1,6 +1,6 @@
2007-06-15 Frank Lanitz <frank@frank.uvena.de> 2007-06-15 Frank Lanitz <frank@frank.uvena.de>
* src/about.c: Update of About-dialog. * src/about.c: Update of about dialog.
* THANKS: Added Spanish translator Nacho Cabanes. * THANKS: Added Spanish translator Nacho Cabanes.
@ -12,6 +12,11 @@
was closed by a delete event. was closed by a delete event.
Fix creation of class files with an empty name and some code Fix creation of class files with an empty name and some code
reformatting. reformatting.
* src/utils.c: Fix sanity checks in utils_str_replace().
* src/tools.c:
Fix several memleaks.
Avoid using broken functions g_strdown() and g_strup().
Only create header and source files if filename is not empty.
2007-06-14 Frank Lanitz <frank@frank.uvena.de> 2007-06-14 Frank Lanitz <frank@frank.uvena.de>

View File

@ -1175,37 +1175,41 @@ static void on_base_name_entry_changed(GtkWidget *entry, CreateClassDialog *cc_d
{ {
gchar *base_name_splitted; gchar *base_name_splitted;
gchar *base_header; gchar *base_header;
gchar *base_gtype = NULL; gchar *tmp;
g_return_if_fail(entry != NULL); g_return_if_fail(entry != NULL);
g_return_if_fail(GTK_IS_ENTRY(entry)); g_return_if_fail(GTK_IS_ENTRY(entry));
g_return_if_fail(cc_dlg != NULL); g_return_if_fail(cc_dlg != NULL);
base_name_splitted = str_case_split(gtk_entry_get_text(GTK_ENTRY(entry)), '_'); base_name_splitted = str_case_split(gtk_entry_get_text(GTK_ENTRY(entry)), '_');
base_header = g_strdup_printf("%s%s.h", tmp = g_strdup_printf("%s%s.h",
g_ascii_strncasecmp(gtk_entry_get_text(GTK_ENTRY(entry)), "gtk", 3) ? "": "gtk/", g_ascii_strncasecmp(gtk_entry_get_text(GTK_ENTRY(entry)), "gtk", 3) ? "": "gtk/",
gtk_entry_get_text(GTK_ENTRY(entry))); gtk_entry_get_text(GTK_ENTRY(entry)));
g_strdown(base_header); base_header = g_ascii_strdown(tmp, -1);
g_free(tmp);
gtk_entry_set_text(GTK_ENTRY(cc_dlg->base_header_entry), base_header); gtk_entry_set_text(GTK_ENTRY(cc_dlg->base_header_entry), base_header);
if (cc_dlg->class_type == GEANY_CLASS_TYPE_GTK) if (cc_dlg->class_type == GEANY_CLASS_TYPE_GTK)
{ {
gchar *base_gtype;
if (! g_ascii_strncasecmp(gtk_entry_get_text(GTK_ENTRY(entry)), "gtk", 3)) if (! g_ascii_strncasecmp(gtk_entry_get_text(GTK_ENTRY(entry)), "gtk", 3))
{ {
base_gtype = g_strdup_printf("%.3s_TYPE%s", tmp = g_strdup_printf("%.3s_TYPE%s",
base_name_splitted, base_name_splitted,
base_name_splitted + 3); base_name_splitted + 3);
} }
else else
base_gtype = g_strconcat(base_name_splitted, "_TYPE", NULL); tmp = g_strconcat(base_name_splitted, "_TYPE", NULL);
g_strup(base_gtype); base_gtype = g_ascii_strup(tmp, -1);
gtk_entry_set_text(GTK_ENTRY(cc_dlg->base_gtype_entry), base_gtype); gtk_entry_set_text(GTK_ENTRY(cc_dlg->base_gtype_entry), base_gtype);
g_free(base_gtype);
g_free(tmp);
} }
g_free(base_name_splitted); g_free(base_name_splitted);
g_free(base_header); g_free(base_header);
g_free(base_gtype);
} }
@ -1214,6 +1218,7 @@ static void on_create_class(CreateClassDialog *cc_dlg)
ClassInfo *class_info; ClassInfo *class_info;
gint idx; gint idx;
gchar *text; gchar *text;
gchar *tmp;
g_return_if_fail(cc_dlg != NULL); g_return_if_fail(cc_dlg != NULL);
@ -1223,8 +1228,8 @@ static void on_create_class(CreateClassDialog *cc_dlg)
class_info = g_new0(ClassInfo, 1); class_info = g_new0(ClassInfo, 1);
class_info->type = cc_dlg->class_type; class_info->type = cc_dlg->class_type;
class_info->class_name = g_strdup(gtk_entry_get_text(GTK_ENTRY(cc_dlg->class_name_entry))); class_info->class_name = g_strdup(gtk_entry_get_text(GTK_ENTRY(cc_dlg->class_name_entry)));
class_info->class_name_up = str_case_split(class_info->class_name, '_'); tmp = str_case_split(class_info->class_name, '_');
g_strup(class_info->class_name_up); class_info->class_name_up = g_ascii_strup(tmp, -1);
class_info->class_name_low = g_ascii_strdown(class_info->class_name_up, -1); class_info->class_name_low = g_ascii_strdown(class_info->class_name_up, -1);
if (*gtk_entry_get_text(GTK_ENTRY(cc_dlg->base_name_entry)) != '\0') if (*gtk_entry_get_text(GTK_ENTRY(cc_dlg->base_name_entry)) != '\0')
{ {
@ -1317,16 +1322,30 @@ static void on_create_class(CreateClassDialog *cc_dlg)
} }
} }
text = templates_get_template_class_source(class_info); // only create the files if the filename is not empty
idx = document_new_file(class_info->source, NULL); if (*class_info->source != '\0')
sci_set_text(doc_list[idx].sci, text); {
g_free(text); text = templates_get_template_class_source(class_info);
idx = document_new_file(class_info->source, NULL);
sci_set_text(doc_list[idx].sci, text);
g_free(text);
}
text = templates_get_template_class_header(class_info); if (*class_info->header != '\0')
idx = document_new_file(class_info->header, NULL); {
sci_set_text(doc_list[idx].sci, text); text = templates_get_template_class_header(class_info);
g_free(text); idx = document_new_file(class_info->header, NULL);
sci_set_text(doc_list[idx].sci, text);
g_free(text);
}
gtk_object_destroy(GTK_OBJECT(cc_dlg->dialog)); gtk_object_destroy(GTK_OBJECT(cc_dlg->dialog));
utils_free_pointers(tmp, class_info->class_name, class_info->class_name_up,
class_info->base_name, class_info->class_name_low, class_info->base_include,
class_info->header, class_info->header_guard, class_info->source, class_info->base_decl,
class_info->constructor_decl, class_info->constructor_impl,
class_info->gtk_destructor_registration, class_info->destructor_decl,
class_info->destructor_impl, class_info->base_gtype, class_info, NULL);
} }

View File

@ -881,12 +881,20 @@ gchar *utils_str_replace(gchar *haystack, const gchar *needle, const gchar *repl
gchar *result; gchar *result;
GString *str; GString *str;
if (haystack == NULL) return NULL; if (haystack == NULL)
return NULL;
if (needle == NULL || replacement == NULL)
return haystack;
if (utils_str_equal(needle, replacement))
return haystack;
start = strstr(haystack, needle); start = strstr(haystack, needle);
lt_pos = utils_strpos(haystack, needle); lt_pos = utils_strpos(haystack, needle);
if (start == NULL || lt_pos == -1) return haystack; if (start == NULL || lt_pos == -1)
return haystack;
// substitute by copying // substitute by copying
str = g_string_sized_new(strlen(haystack)); str = g_string_sized_new(strlen(haystack));