Simplify GString usage
authorArkadiy Illarionov <qarkai@gmail.com>
Sun, 20 Aug 2023 16:00:40 +0000 (19:00 +0300)
committerColin Clark <colin.clark@cclark.uk>
Sun, 20 Aug 2023 17:01:11 +0000 (18:01 +0100)
- Replace g_string_append_printf with g_string_append
  if format string doesn't contain format specifiers.
- Use g_string_append_printf instead of g_string_append(string, g_strdup_printf).
- Merge g_string_new and following g_string_append.
- Remove unnecessary memory allocations and fix memleaks.

src/bar-gps.cc
src/bar-keywords.cc
src/collect-dlg.cc
src/collect.cc
src/dupe.cc
src/image-load-collection.cc
src/layout-util.cc
src/preferences.cc
src/rcfile.cc
src/remote.cc
src/utilops.cc

index 7269082..197cd41 100644 (file)
@@ -191,12 +191,12 @@ static void bar_pane_gps_close_save_cb(GenericDialog *, gpointer data)
                                        }
                                if (geocoded_count == 1 && count == 1)
                                        {
-                                       g_string_append_printf(message,
+                                       g_string_append(message,
                                                        _("\nThis image is already geocoded!"));
                                        }
                                else if (geocoded_count == 1 && count > 1)
                                        {
-                                       g_string_append_printf(message,
+                                       g_string_append(message,
                                                        _("\nOne image is already geocoded!"));
                                        }
                                else if (geocoded_count > 1 && count > 1)
@@ -205,9 +205,7 @@ static void bar_pane_gps_close_save_cb(GenericDialog *, gpointer data)
                                                        _("\n%i Images are already geocoded!"), geocoded_count);
                                        }
 
-                               location = g_strdup_printf("%lf %lf", pgd->dest_latitude,
-                                                                                                               pgd->dest_longitude);
-                               g_string_append_printf(message, _("\n\nPosition: %s \n"), location);
+                               g_string_append_printf(message, _("\n\nPosition: %lf %lf \n"), pgd->dest_latitude, pgd->dest_longitude);
 
                                gd = generic_dialog_new(_("Geocode images"),
                                                        "geocode_images", nullptr, TRUE,
@@ -220,7 +218,6 @@ static void bar_pane_gps_close_save_cb(GenericDialog *, gpointer data)
                                                                                                bar_pane_gps_close_save_cb, TRUE);
 
                                gtk_widget_show(gd->dialog);
-                               g_free(location);
                                g_string_free(message, TRUE);
                                }
                        }
index e769ddf..21652ab 100644 (file)
@@ -1046,19 +1046,14 @@ static void bar_pane_keywords_disconnect_marks_cb(GtkWidget *menu_widget, gpoint
        auto pkd = static_cast<PaneKeywordsData *>(data);
 
        GenericDialog *gd;
-       GString *message = g_string_new("");
-
-       message = g_string_append(message, _("This will disconnect all Marks Keywords connections"));
 
        gd = generic_dialog_new(_("Marks Keywords"),
                                "marks_keywords", menu_widget, TRUE, dummy_cancel_cb, pkd);
        generic_dialog_add_message(gd, GQ_ICON_DIALOG_WARNING,
-                               "Disconnect all Marks Keywords connections?", message->str, TRUE);
+                               "Disconnect all Marks Keywords connections?", _("This will disconnect all Marks Keywords connections"), TRUE);
        generic_dialog_add_button(gd, GQ_ICON_OK, "OK", bar_pane_keywords_disconnect_marks_ok_cb, TRUE);
 
        gtk_widget_show(gd->dialog);
-
-       g_string_free(message, TRUE);
 }
 
 static void bar_pane_keywords_delete_cb(GtkWidget *, gpointer data)
index e148c51..286ea2e 100644 (file)
@@ -175,7 +175,7 @@ static void collection_save_or_append_dialog(gint type, CollectionData *cd)
                while (work)
                        {
                        auto collection_name = static_cast<const gchar *>(work->data);
-                       out_string = g_string_append(out_string, g_strdup(collection_name));
+                       out_string = g_string_append(out_string, collection_name);
                        out_string = g_string_append(out_string, "\n");
 
                        work = work->next;
index 8d68bcd..a512995 100644 (file)
@@ -396,7 +396,7 @@ void collection_contents(const gchar *name, GString **contents)
                        {
                        ci = static_cast<CollectInfo *>(work->data);
                        fd = ci->fd;
-                       *contents = g_string_append(*contents, g_strdup(fd->path));
+                       *contents = g_string_append(*contents, fd->path);
                        *contents = g_string_append(*contents, "\n");
 
                        work = work->next;
index be0ff47..ff135f4 100644 (file)
@@ -5218,7 +5218,6 @@ static void export_duplicates_data_save_cb(FileDialog *fdlg, gpointer data)
        GFileOutputStream *gfstream;
        GFile *out_file;
        GString *output_string;
-       gchar *sep;
        gchar* rank;
        GList *work;
        GtkTreeSelection *selection;
@@ -5243,7 +5242,7 @@ static void export_duplicates_data_save_cb(FileDialog *fdlg, gpointer data)
                return;
                }
 
-       sep = g_strdup((edd->separator == EXPORT_CSV) ?  "," : "\t");
+       const gchar *sep = (edd->separator == EXPORT_CSV) ?  "," : "\t";
        output_string = g_string_new(g_strjoin(sep, _("Match"), _("Group"), _("Similarity"), _("Set"), _("Thumbnail"), _("Name"), _("Size"), _("Date"), _("Width"), _("Height"), _("Path\n"), NULL));
 
        selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(edd->dupewindow->listview));
@@ -5269,7 +5268,7 @@ static void export_duplicates_data_save_cb(FileDialog *fdlg, gpointer data)
                        match_count++;
                        }
                color_old = color_new;
-               output_string = g_string_append(output_string, g_strdup_printf("%d", match_count));
+               g_string_append_printf(output_string, "%d", match_count);
                output_string = g_string_append(output_string, sep);
 
                if ((dupe_match_find_parent(edd->dupewindow, di) == di))
@@ -5290,13 +5289,13 @@ static void export_duplicates_data_save_cb(FileDialog *fdlg, gpointer data)
                        }
                else
                        {
-                       output_string = g_string_append(output_string, g_strdup_printf("%s", rank_split[0]));
+                       output_string = g_string_append(output_string, rank_split[0]);
                        }
                output_string = g_string_append(output_string, sep);
                g_free(rank);
                g_strfreev(rank_split);
 
-               output_string = g_string_append(output_string, g_strdup_printf("%d", (di->second + 1)));
+               g_string_append_printf(output_string, "%d", di->second + 1);
                output_string = g_string_append(output_string, sep);
 
                thumb_cache = cache_find_location(CACHE_TYPE_THUMB, di->fd->path);
@@ -5316,13 +5315,13 @@ static void export_duplicates_data_save_cb(FileDialog *fdlg, gpointer data)
                output_string = g_string_append(output_string, sep);
                g_free(name);
 
-               output_string = g_string_append(output_string, g_strdup_printf("%" PRIu64, di->fd->size));
+               g_string_append_printf(output_string, "%" PRIu64, di->fd->size);
                output_string = g_string_append(output_string, sep);
                output_string = g_string_append(output_string, text_from_time(di->fd->date));
                output_string = g_string_append(output_string, sep);
-               output_string = g_string_append(output_string, g_strdup_printf("%d", (di->width ? di->width : 0)));
+               g_string_append_printf(output_string, "%d", di->width);
                output_string = g_string_append(output_string, sep);
-               output_string = g_string_append(output_string, g_strdup_printf("%d", (di->height ? di->height : 0)));
+               g_string_append_printf(output_string, "%d", di->height);
                output_string = g_string_append(output_string, sep);
                output_string = g_string_append(output_string, di->fd->path);
                output_string = g_string_append_c(output_string, '\n');
@@ -5332,7 +5331,6 @@ static void export_duplicates_data_save_cb(FileDialog *fdlg, gpointer data)
 
        g_output_stream_write(G_OUTPUT_STREAM(gfstream), output_string->str, output_string->len, nullptr, &error);
 
-       g_free(sep);
        g_string_free(output_string, TRUE);
        g_object_unref(gfstream);
        g_object_unref(out_file);
index 9e2ced5..5ab918a 100644 (file)
@@ -70,7 +70,7 @@ static gboolean image_loader_collection_load(gpointer loader, const guchar *, gs
                                        cache_found = cache_find_location(CACHE_TYPE_THUMB, split_line[1]);
                                        if (cache_found)
                                                {
-                                               file_names = g_string_append(file_names, g_strconcat("\"", cache_found,"\" ", NULL));
+                                               g_string_append_printf(file_names, "\"%s\" ", cache_found);
                                                line_count++;
                                                }
                                        g_free(cache_found);
index 5ad4994..a55bf0f 100644 (file)
@@ -601,8 +601,7 @@ static void layout_menu_write_rotate(GtkToggleAction *, gpointer data, gboolean
                        }
                else
                        {
-                       message = g_string_new("");
-                       message = g_string_append(message, _("Operation failed:\n"));
+                       message = g_string_new(_("Operation failed:\n"));
 
                        if (run_result == 1)
                                message = g_string_append(message, _("No file extension\n"));
index a8589a2..982b967 100644 (file)
@@ -4061,8 +4061,7 @@ void show_about_window(LayoutWindow *lw)
        gsize size;
        guint32 flags;
 
-       copyright = g_string_new(nullptr);
-       copyright = g_string_append(copyright, "This program comes with absolutely no warranty.\nGNU General Public License, version 2 or later.\nSee https://www.gnu.org/licenses/old-licenses/gpl-2.0.html\n\n");
+       copyright = g_string_new("This program comes with absolutely no warranty.\nGNU General Public License, version 2 or later.\nSee https://www.gnu.org/licenses/old-licenses/gpl-2.0.html\n\n");
 
        timezone_path = g_build_filename(get_rc_dir(), TIMEZONE_DATABASE_FILE, NULL);
        if (g_file_test(timezone_path, G_FILE_TEST_EXISTS))
index 9b834a7..6bc142d 100644 (file)
@@ -654,11 +654,10 @@ gboolean save_config_to_file(const gchar *utf8_path, ConfOptions *options, Layou
                return FALSE;
                }
 
-       outstr = g_string_new("");
-       g_string_append_printf(outstr, "<!--\n");
-       g_string_append_printf(outstr, "######################################################################\n");
+       outstr = g_string_new("<!--\n");
+       g_string_append(outstr, "######################################################################\n");
        g_string_append_printf(outstr, "# %30s config file        version %-10s #\n", GQ_APPNAME, VERSION);
-       g_string_append_printf(outstr, "######################################################################\n");
+       g_string_append(outstr, "######################################################################\n");
        WRITE_SEPARATOR();
 
        WRITE_STRING("# Note: This file is autogenerated. Options can be changed here,\n");
@@ -754,11 +753,10 @@ gboolean save_default_layout_options_to_file(const gchar *utf8_path, ConfOptions
                return FALSE;
                }
 
-       outstr = g_string_new("");
-       g_string_append_printf(outstr, "<!--\n");
-       g_string_append_printf(outstr, "######################################################################\n");
+       outstr = g_string_new("<!--\n");
+       g_string_append(outstr, "######################################################################\n");
        g_string_append_printf(outstr, "# %8s default layout file         version %-10s #\n", GQ_APPNAME, VERSION);
-       g_string_append_printf(outstr, "######################################################################\n");
+       g_string_append(outstr, "######################################################################\n");
        WRITE_SEPARATOR();
 
        WRITE_STRING("# Note: This file is autogenerated. Options can be changed here,\n");
index c274de0..98e9e09 100644 (file)
@@ -877,7 +877,7 @@ static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurs
        while (work)
                {
                fd = static_cast<FileData *>(work->data);
-               g_string_append_printf(out_string, "%s", fd->path);
+               g_string_append(out_string, fd->path);
                format_class = filter_file_get_class(fd->path);
 
                switch (format_class)
@@ -1148,7 +1148,7 @@ static void gr_collection_list(const gchar *, GIOChannel *channel, gpointer)
        while (work)
                {
                auto collection_name = static_cast<const gchar *>(work->data);
-               out_string = g_string_append(out_string, g_strdup(collection_name));
+               out_string = g_string_append(out_string, collection_name);
                out_string = g_string_append(out_string, "\n");
 
                work = work->next;
index 2a63130..bd1e414 100644 (file)
@@ -2021,6 +2021,7 @@ static gchar *file_util_details_get_message(UtilityData *ud, FileData *fd, const
                gchar *err_msg = file_data_get_error_string(error);
                g_string_append(message, err_msg);
                if (icon_name) *icon_name = (error & CHANGE_ERROR_MASK) ? GQ_ICON_DIALOG_ERROR : GQ_ICON_DIALOG_WARNING;
+               g_free(err_msg);
                }
        else
                {