Branch: refs/heads/master Author: Lukas Schwarz lukas@localhost.localdomain Committer: Lukas Schwarz lukas@localhost.localdomain Date: Wed, 12 Apr 2017 20:32:43 UTC Commit: 62314d1117a6c6655f046650ee76e76f82e7f3a1 https://github.com/geany/geany-plugins/commit/62314d1117a6c6655f046650ee76e7...
Log Message: ----------- Add Keep Unique Lines feature resolves #516
Modified Paths: -------------- lineoperations/README lineoperations/src/linefunctions.c lineoperations/src/linefunctions.h lineoperations/src/lineoperations.c
Modified: lineoperations/README 27 lines changed, 27 insertions(+), 0 deletions(-) =================================================================== @@ -16,6 +16,7 @@ Features * Remove Duplicate Lines, sorted * Remove Duplicate Lines, ordered * Remove Unique Lines +* Keep Unique Lines * Remove Empty Lines * Remove Whitespace Lines * Sort Lines Ascending @@ -113,6 +114,32 @@ Removes all lines that appear only once. Line 1 Line 2
+ +Keep Unique Lines +------------------- + +Keep all lines that appear only once. + + Example: Suppose a file has the following lines. (#comments added for + clarity) + + :: + + Line 4 + Line 1 #removed + Line 2 #removed + Line 3 + Line 1 #removed + Line 2 #removed + + The **Keep Unique Lines** will change the file into this: + + :: + + Line 4 + Line 3 + + Remove Empty Lines ------------------
Modified: lineoperations/src/linefunctions.c 44 lines changed, 44 insertions(+), 0 deletions(-) =================================================================== @@ -159,6 +159,50 @@ rmunqln(gchar **lines, gint num_lines, gchar *new_file) }
+/* Keep Unique Lines */ +gint +kpunqln(gchar **lines, gint num_lines, gchar *new_file) +{ + gchar *nf_end = new_file; /* points to last char of new_file */ + gint i = 0; /* iterator */ + gint j = 0; /* iterator */ + gboolean *to_remove = NULL; /* to 'mark' which lines to remove */ + gint changed = 0; /* number of lines removed */ + + + /* allocate and set *to_remove to all FALSE + * to_remove[i] represents whether lines[i] should be removed */ + to_remove = g_malloc(sizeof(gboolean) * num_lines); + for(i = 0; i < num_lines; i++) + to_remove[i] = FALSE; + + /* find all non unique lines and set them to TRUE (to be removed) */ + for(i = 0; i < num_lines; i++) + /* make sure that the line is not already determined to be non unique */ + if(!to_remove[i]) + for(j = (i+1); j < num_lines; j++) + if(!to_remove[j] && strcmp(lines[i], lines[j]) == 0) + { + to_remove[i] = TRUE; + to_remove[j] = TRUE; + } + + /* copy **lines into 'new_file' if it is not FALSE(not duplicate) */ + for(i = 0; i < num_lines; i++) + if(!to_remove[i]) + { + changed++; /* number of lines kept */ + nf_end = g_stpcpy(nf_end, lines[i]); + } + + /* free used memory */ + g_free(to_remove); + + /* return the number of lines deleted */ + return -(num_lines - changed); +} + + /* Remove Empty Lines */ gint rmemtyln(ScintillaObject *sci, gint line_num, gint end_line_num)
Modified: lineoperations/src/linefunctions.h 5 lines changed, 5 insertions(+), 0 deletions(-) =================================================================== @@ -45,6 +45,11 @@ gint rmunqln(gchar **lines, gint num_lines, gchar *new_file);
+/* Keep Unique Lines */ +gint +kpunqln(gchar **lines, gint num_lines, gchar *new_file); + + /* Remove Empty Lines */ gint rmemtyln(ScintillaObject *sci, gint line_num, gint end_line_num);
Modified: lineoperations/src/lineoperations.c 2 lines changed, 2 insertions(+), 0 deletions(-) =================================================================== @@ -257,6 +257,8 @@ lo_init(GeanyPlugin *plugin, gpointer gdata) G_CALLBACK(action_indir_manip_item), (gpointer) rmdupln }, { N_("Remove _Unique Lines"), G_CALLBACK(action_indir_manip_item), (gpointer) rmunqln }, + { N_("Keep _Unique Lines"), + G_CALLBACK(action_indir_manip_item), (gpointer) kpunqln }, { NULL }, { N_("Remove _Empty Lines"), G_CALLBACK(action_sci_manip_item), (gpointer) rmemtyln },
-------------- This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).
plugins-commits@lists.geany.org