From: Francois Fleuret Date: Wed, 1 Jul 2009 06:48:00 +0000 (+0200) Subject: Cosmetics. X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?p=selector.git;a=commitdiff_plain;h=9254379f447377e1da6244b989dbd2383cc0f35b Cosmetics. --- diff --git a/REVISION_NUMBER b/REVISION_NUMBER index a2ecc45..bb79365 100644 --- a/REVISION_NUMBER +++ b/REVISION_NUMBER @@ -1 +1 @@ -154 +155 diff --git a/selector.c b/selector.c index 2880b5f..c8ccb5c 100644 --- a/selector.c +++ b/selector.c @@ -146,9 +146,9 @@ int *new_hash_table(int hash_table_size) { string was not already in the table, returns -1. Otherwise, returns the previous index it had. */ -int test_and_add(char *new_string, int new_index, - char **strings, - int *hash_table, int hash_table_size) { +int add_and_get_previous_index(const char *new_string, int new_index, + char **strings, + int *hash_table, int hash_table_size) { unsigned int code = 0; int k; @@ -572,6 +572,49 @@ void update_screen(int *current_focus_line, int *displayed_focus_line, /*********************************************************************/ +void store_line(const char *t, + int nb_lines_max, int *nb_lines, char **lines, + int hash_table_size, int *hash_table) { + + /* Remove the zsh history prefix */ + + if(zsh_history && *t == ':') { + while(*t && *t != ';') t++; + if(*t == ';') t++; + } + + /* Remove the bash history prefix */ + + if(bash_history) { + while(*t == ' ') t++; + while(*t >= '0' && *t <= '9') t++; + while(*t == ' ') t++; + } + + /* Check for duplicates with the hash table and insert the line in + the list if necessary */ + + int dup; + + if(hash_table) { + dup = add_and_get_previous_index(t, *nb_lines, lines, hash_table, hash_table_size); + } else { + dup = -1; + } + + if(dup < 0) { + lines[*nb_lines] = (char *) malloc((strlen(t) + 1) * sizeof(char)); + strcpy(lines[*nb_lines], t); + } else { + /* The string was already in there, so we do not allocate a new + string but use the pointer to the first occurence of it */ + lines[*nb_lines] = lines[dup]; + lines[dup] = 0; + } + + (*nb_lines)++; +} + void read_file(const char *input_filename, int nb_lines_max, int *nb_lines, char **lines, int hash_table_size, int *hash_table) { @@ -612,45 +655,9 @@ void read_file(const char *input_filename, raw_line[eol] = '\0'; - char *t = raw_line + start; - - /* Remove the zsh history prefix */ - - if(zsh_history && *t == ':') { - while(*t && *t != ';') t++; - if(*t == ';') t++; - } - - /* Remove the bash history prefix */ - - if(bash_history) { - while(*t == ' ') t++; - while(*t >= '0' && *t <= '9') t++; - while(*t == ' ') t++; - } - - /* Check for duplicates with the hash table and insert the line in - the list if necessary */ - - int dup; - - if(hash_table) { - dup = test_and_add(t, *nb_lines, lines, hash_table, hash_table_size); - } else { - dup = -1; - } - - if(dup < 0) { - lines[*nb_lines] = (char *) malloc((strlen(t) + 1) * sizeof(char)); - strcpy(lines[*nb_lines], t); - } else { - /* The string was already in there, so we do not allocate a new - string but use the pointer to the first occurence of it */ - lines[*nb_lines] = lines[dup]; - lines[dup] = 0; - } - - (*nb_lines)++; + store_line(raw_line + start, + nb_lines_max, nb_lines, lines, + hash_table_size, hash_table); start = eol + 1; }