Skip to content

Commit b81a04f

Browse files
committed
Renamer 1.2 (Aug / 05 / 2008)
* Columns are now sortable * Columns are capable of auto-sorting after a search and replace operation * Replaced literal search and replace code for increased stability and more logical behavior * Improved entity handling under Linux * Fixed a crash bug during file list population where entities followed certain characters in a file name * Improved performance when populating the file list
1 parent 8d6f362 commit b81a04f

File tree

1 file changed

+84
-60
lines changed

1 file changed

+84
-60
lines changed

main.c

Lines changed: 84 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,64 +18,59 @@ GRegex *rx_name = NULL;
1818
GRegex *rx_path = NULL;
1919
GRegex *rx_replace = NULL;
2020
GRegex *rx_split = NULL;
21+
GRegex *rx_e23 = NULL;
22+
#ifdef WIN_32
23+
GRegex *rx_e20 = NULL;
24+
GRegex *rx_e60 = NULL;
25+
GRegex *rx_e5E = NULL;
26+
GRegex *rx_e7B = NULL;
27+
GRegex *rx_e7D = NULL;
28+
GRegex *rx_e5B = NULL;
29+
GRegex *rx_e5D = NULL;
30+
GRegex *rx_e25 = NULL;
31+
#else
32+
GRegex *rx_e40 = NULL;
33+
GRegex *rx_e3F = NULL;
34+
GRegex *rx_ep = NULL;
35+
#endif
36+
gint sort_id;
37+
GtkSortType sort_order;
2138

22-
//int g_rename(const gchar *oldfilename, const gchar *newfilename);
2339
void destroy(void);
2440

2541
/* ************************************************************************ *
2642
* UTILITY FUNCTIONS
2743
* ************************************************************************ */
28-
gchar *replace_str(gchar *str, const gchar *orig, const gchar *rep)
44+
void enable_sorting(gboolean enable)
2945
{
30-
if (orig == NULL || rep == NULL || str == NULL) return NULL;
31-
if (strlen(orig) == 0 || strlen(rep) == 0 || strlen(str) == 0) return NULL;
32-
if (strstr(str, orig) == NULL) return str;
33-
34-
gchar *replaced = (char*)calloc(1, 1), *temp = NULL;
35-
gchar *p = str, *p3 = str, *p2 = NULL;
36-
37-
while ( (p = strstr(p, orig)) != NULL) {
38-
temp = realloc(replaced, strlen(replaced) + (p - p3) + strlen(rep));
39-
if (temp == NULL) {
40-
free(replaced);
41-
return NULL;
42-
}
43-
replaced = temp;
44-
strncat(replaced, p - (p - p3), p - p3);
45-
strcat(replaced, rep);
46-
p3 = p + strlen(orig);
47-
p += strlen(orig);
48-
p2 = p;
49-
}
50-
51-
if (p2 != NULL && strlen(p2) > 0) {
52-
temp = realloc(replaced, strlen(replaced) + strlen(p2) + 1);
53-
if (temp == NULL) {
54-
free(replaced);
55-
return NULL;
56-
}
57-
replaced = temp;
58-
strcat(replaced, p2);
46+
if (enable) {
47+
gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), sort_id, sort_order);
48+
} else {
49+
gtk_tree_sortable_get_sort_column_id(GTK_TREE_SORTABLE(store), &sort_id, &sort_order);
50+
gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
5951
}
60-
return replaced;
6152
}
6253

63-
#ifdef WIN_32
6454
gchar *process_entities(gchar *str)
6555
{
6656
gchar *buffer = str;
67-
buffer = replace_str(buffer, "%20", " ");
68-
buffer = replace_str(buffer, "%60", "`");
69-
buffer = replace_str(buffer, "%23", "#");
70-
buffer = replace_str(buffer, "%5E", "^");
71-
buffer = replace_str(buffer, "%7B", "{");
72-
buffer = replace_str(buffer, "%7D", "}");
73-
buffer = replace_str(buffer, "%5B", "[");
74-
buffer = replace_str(buffer, "%5D", "]");
75-
buffer = replace_str(buffer, "%25", "%");
57+
buffer = g_regex_replace_literal(rx_e23, buffer, -1, 0, "#", 0, NULL);
58+
#ifdef WIN_32
59+
buffer = g_regex_replace_literal(rx_e20, buffer, -1, 0, " ", 0, NULL);
60+
buffer = g_regex_replace_literal(rx_e60, buffer, -1, 0, "`", 0, NULL);
61+
buffer = g_regex_replace_literal(rx_e5E, buffer, -1, 0, "^", 0, NULL);
62+
buffer = g_regex_replace_literal(rx_e7B, buffer, -1, 0, "{", 0, NULL);
63+
buffer = g_regex_replace_literal(rx_e7D, buffer, -1, 0, "}", 0, NULL);
64+
buffer = g_regex_replace_literal(rx_e5B, buffer, -1, 0, "[", 0, NULL);
65+
buffer = g_regex_replace_literal(rx_e5D, buffer, -1, 0, "]", 0, NULL);
66+
buffer = g_regex_replace_literal(rx_e25, buffer, -1, 0, "%", 0, NULL);
67+
#else
68+
buffer = g_regex_replace_literal(rx_e40, buffer, -1, 0, "@", 0, NULL);
69+
buffer = g_regex_replace_literal(rx_e3F, buffer, -1, 0, "?", 0, NULL);
70+
buffer = g_regex_replace_literal(rx_ep, buffer, -1, 0, "%", 0, NULL);
71+
#endif
7672
return buffer;
7773
}
78-
#endif
7974

8075
/* ************************************************************************ *
8176
* POPULATING THE LIST (DRAG-N-DROP)
@@ -88,11 +83,12 @@ void view_onDragDataReceived(GtkWidget *wgt, GdkDragContext *context, int x, int
8883
gchar *input = NULL;
8984
int index = 0;
9085

86+
gtk_tree_view_set_model(GTK_TREE_VIEW(list), NULL);
87+
enable_sorting(FALSE);
9188
while (strings[index] != NULL && strcmp(strings[index], "") != 0)
9289
{
9390
input = strings[index];
9491
gtk_list_store_append(GTK_LIST_STORE(store), &iter);
95-
#ifdef WIN_32
9692
gtk_list_store_set(GTK_LIST_STORE(store), &iter, COL_NAME,
9793
process_entities( g_regex_replace(rx_name,
9894
input,
@@ -105,14 +101,10 @@ void view_onDragDataReceived(GtkWidget *wgt, GdkDragContext *context, int x, int
105101
strlen(input),
106102
0, "\\1", 0,
107103
NULL) ), -1);
108-
#else
109-
gtk_list_store_set(GTK_LIST_STORE(store), &iter, COL_NAME,
110-
g_regex_replace(rx_name, input, strlen(input), 0, "\\1", 0, NULL), -1);
111-
gtk_list_store_set(GTK_LIST_STORE(store), &iter, COL_PATH,
112-
g_regex_replace(rx_path, input, strlen(input), 0, "\\1", 0, NULL), -1);
113-
#endif
114104
++index;
115105
}
106+
enable_sorting(TRUE);
107+
gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
116108
}
117109

118110
/* ************************************************************************ *
@@ -175,13 +167,8 @@ gboolean rename_foreach (GtkTreeModel *model,
175167
gtk_tree_model_get (model, iter, COL_NAME, &_name, -1);
176168
gtk_tree_model_get (model, iter, COL_PATH, &_path, -1);
177169

178-
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox))) {
179-
_name2 = g_regex_replace(rx_replace, _name, strlen(_name), 0,
170+
_name2 = g_regex_replace(rx_replace, _name, strlen(_name), 0,
180171
gtk_entry_get_text(GTK_ENTRY(entry_replace)), 0, NULL);
181-
} else {
182-
_name2 = replace_str(_name, (gchar*) gtk_entry_get_text(GTK_ENTRY(entry_search)),
183-
(gchar*) gtk_entry_get_text(GTK_ENTRY(entry_replace)));
184-
}
185172

186173
if (g_rename(g_strconcat(_path, _name, NULL), g_strconcat(_path, _name2, NULL)) == 0)
187174
{ gtk_list_store_set (store, iter, COL_NAME, _name2, -1); }
@@ -191,11 +178,19 @@ gboolean rename_foreach (GtkTreeModel *model,
191178

192179
void search_and_replace(void)
193180
{
194-
rx_replace = g_regex_new(gtk_entry_get_text(GTK_ENTRY(entry_search)), G_REGEX_OPTIMIZE, 0, NULL);
181+
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox))) {
182+
rx_replace = g_regex_new( gtk_entry_get_text(GTK_ENTRY(entry_search)), G_REGEX_OPTIMIZE, 0, NULL );
183+
} else {
184+
rx_replace = g_regex_new( g_regex_escape_string(gtk_entry_get_text(GTK_ENTRY(entry_search)), -1), G_REGEX_OPTIMIZE, 0, NULL);
185+
}
195186

187+
gtk_tree_view_set_model(GTK_TREE_VIEW(list), NULL);
188+
enable_sorting(FALSE);
196189
gtk_tree_model_foreach(GTK_TREE_MODEL(store),
197-
(GtkTreeModelForeachFunc) rename_foreach,
198-
NULL);
190+
(GtkTreeModelForeachFunc) rename_foreach,
191+
NULL);
192+
enable_sorting(TRUE);
193+
gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
199194

200195
gtk_tree_view_columns_autosize(GTK_TREE_VIEW(list));
201196
}
@@ -228,6 +223,21 @@ void destroy(void)
228223
g_regex_unref(rx_split);
229224
if (rx_replace != NULL)
230225
g_regex_unref(rx_replace);
226+
g_regex_unref(rx_e23);
227+
#ifdef WIN_32
228+
g_regex_unref(rx_e20);
229+
g_regex_unref(rx_e60);
230+
g_regex_unref(rx_e5E);
231+
g_regex_unref(rx_e7B);
232+
g_regex_unref(rx_e7D);
233+
g_regex_unref(rx_e5B);
234+
g_regex_unref(rx_e5D);
235+
g_regex_unref(rx_e25);
236+
#else
237+
g_regex_unref(rx_e40);
238+
g_regex_unref(rx_e3F);
239+
g_regex_unref(rx_ep);
240+
#endif
231241
gtk_main_quit();
232242
}
233243

@@ -307,7 +317,7 @@ int main (int argc, char *argv[])
307317
win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
308318
gtk_window_set_default_size(GTK_WINDOW(win), 500, 380);
309319
gtk_container_set_border_width (GTK_CONTAINER (win), 8);
310-
gtk_window_set_title (GTK_WINDOW (win), "Multi-File Renamer - v1.1.4"); /* Jul / 27 / 2008 */
320+
gtk_window_set_title (GTK_WINDOW (win), "Multi-File Renamer - v1.2"); /* Aug / 05 / 2008 */
311321
gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_NONE);
312322
gtk_widget_realize (win);
313323
g_signal_connect (win, "destroy", destroy, NULL);
@@ -326,6 +336,8 @@ int main (int argc, char *argv[])
326336

327337
list = create_view_and_model();
328338
gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(list)), GTK_SELECTION_MULTIPLE);
339+
gtk_tree_view_column_set_sort_column_id( gtk_tree_view_get_column(GTK_TREE_VIEW(list), COL_NAME), 0 );
340+
gtk_tree_view_column_set_sort_column_id( gtk_tree_view_get_column(GTK_TREE_VIEW(list), COL_PATH), 1 );
329341
gtk_container_add(GTK_CONTAINER(scroll), list);
330342

331343
/* Second / Third rows of the Form */
@@ -367,11 +379,23 @@ int main (int argc, char *argv[])
367379
#ifdef WIN_32
368380
rx_name = g_regex_new(".*/([^/\r]+)\r?$", G_REGEX_OPTIMIZE, 0, NULL);
369381
rx_path = g_regex_new("file:///(.*?/)[^/]+$", G_REGEX_OPTIMIZE, 0, NULL);
382+
rx_e20 = g_regex_new("%20", G_REGEX_OPTIMIZE, 0, NULL);
383+
rx_e60 = g_regex_new("%60", G_REGEX_OPTIMIZE, 0, NULL);
384+
rx_e5E = g_regex_new("%5E", G_REGEX_OPTIMIZE, 0, NULL);
385+
rx_e7B = g_regex_new("%7B", G_REGEX_OPTIMIZE, 0, NULL);
386+
rx_e7D = g_regex_new("%7D", G_REGEX_OPTIMIZE, 0, NULL);
387+
rx_e5B = g_regex_new("%5B", G_REGEX_OPTIMIZE, 0, NULL);
388+
rx_e5D = g_regex_new("%5D", G_REGEX_OPTIMIZE, 0, NULL);
389+
rx_e25 = g_regex_new("%25", G_REGEX_OPTIMIZE, 0, NULL);
370390
#else
371391
rx_name = g_regex_new(".*/([^/]+)$", G_REGEX_OPTIMIZE, 0, NULL);
372392
rx_path = g_regex_new("file://(.*?/)[^/]+$", G_REGEX_OPTIMIZE, 0, NULL);
393+
rx_e40 = g_regex_new("%40", G_REGEX_OPTIMIZE, 0, NULL);
394+
rx_e3F = g_regex_new("%3F", G_REGEX_OPTIMIZE, 0, NULL);
395+
rx_ep = g_regex_new("%%", G_REGEX_OPTIMIZE, 0, NULL);
373396
#endif
374397
rx_split = g_regex_new("\n", G_REGEX_OPTIMIZE, 0, NULL);
398+
rx_e23 = g_regex_new("%23", G_REGEX_OPTIMIZE, 0, NULL);
375399

376400
/* Enter the main loop */
377401
gtk_widget_show_all (win);

0 commit comments

Comments
 (0)