Skip to content

Commit

Permalink
Merge pull request #461 from joakim-hove/stringlist-append
Browse files Browse the repository at this point in the history
Stringlist append
  • Loading branch information
joakim-hove authored Aug 31, 2018
2 parents 1a9d81a + e3cdaa7 commit 180e713
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 156 deletions.
2 changes: 1 addition & 1 deletion applications/ecl/kw_extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int main(int argc, char ** argv) {


for (int iarg=3; iarg < argc; iarg++)
stringlist_append_ref(kw_set, argv[iarg]);
stringlist_append_copy(kw_set, argv[iarg]);

if (!ecl_util_fmt_file(src_file, &fmt_src))
util_exit("Hmm - could not determine formatted/unformatted status for:%s \n",src_file);
Expand Down
6 changes: 0 additions & 6 deletions lib/include/ert/util/stringlist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ typedef bool ( file_pred_ftype) (const char *, const void *);
void stringlist_clear(stringlist_type * );

void stringlist_append_copy(stringlist_type * , const char *);
void stringlist_append_ref(stringlist_type * , const char *);
void stringlist_append_owned_ref(stringlist_type * , const char *);

const char * stringlist_safe_iget( const stringlist_type * stringlist , int index);
bool stringlist_unique(const stringlist_type * stringlist );
Expand Down Expand Up @@ -86,14 +84,10 @@ typedef bool ( file_pred_ftype) (const char *, const void *);
stringlist_type * stringlist_alloc_argv_copy(const char ** , int );
stringlist_type * stringlist_alloc_argv_ref (const char ** , int );
stringlist_type * stringlist_alloc_argv_owned_ref(const char ** argv , int argc);
stringlist_type * stringlist_alloc_shallow_copy(const stringlist_type *);
stringlist_type * stringlist_alloc_shallow_copy_with_offset(const stringlist_type * stringlist, int offset);
stringlist_type * stringlist_alloc_shallow_copy_with_limits(const stringlist_type * stringlist, int offset , int num_strings);
stringlist_type * stringlist_alloc_from_split( const char * input_string , const char * sep );
stringlist_type * stringlist_fread_alloc(FILE * );

void stringlist_append_stringlist_copy(stringlist_type * , const stringlist_type * );
void stringlist_append_stringlist_ref(stringlist_type * , const stringlist_type * );
void stringlist_insert_stringlist_copy(stringlist_type * , const stringlist_type *, int);

bool stringlist_equal(const stringlist_type * , const stringlist_type *);
Expand Down
6 changes: 4 additions & 2 deletions lib/util/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,10 @@ stringlist_type * hash_alloc_stringlist(const hash_type * hash) {
stringlist_type * stringlist = stringlist_alloc_new();
char ** keylist = hash_alloc_keylist__(hash);
int i;
for (i = 0; i < hash_get_size( hash ); i++)
stringlist_append_owned_ref( stringlist , keylist[i] );
for (i = 0; i < hash_get_size( hash ); i++) {
stringlist_append_copy( stringlist , keylist[i] );
free(keylist[i]);
}

free( keylist );
return stringlist;
Expand Down
9 changes: 5 additions & 4 deletions lib/util/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,9 @@ stringlist_type * basic_parser_tokenize_buffer(
if( is_in_quoters( buffer[position], parser ) ) {
int length = length_of_quotation( &buffer[position] );
char * token = alloc_quoted_token( &buffer[position], length, strip_quote_marks );
stringlist_append_owned_ref( tokens, token );
stringlist_append_copy( tokens, token );
position += length;
free(token);
continue;
}

Expand Down Expand Up @@ -452,9 +453,9 @@ stringlist_type * basic_parser_tokenize_buffer(

if (token_length > 0) { /* We do not insert empty tokens. */
token[token_length] = '\0';
stringlist_append_owned_ref( tokens, token );
} else
free( token ); /* The whole thing is discarded. */
stringlist_append_copy( tokens, token );
}
free( token );

position += length;
continue;
Expand Down
5 changes: 3 additions & 2 deletions lib/util/path_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ bool path_stack_push( path_stack_type * path_stack , const char * path ) {

void path_stack_push_cwd( path_stack_type * path_stack ) {
char * cwd = util_alloc_cwd();
stringlist_append_owned_ref( path_stack->storage , cwd);
stringlist_append_ref( path_stack->stack , cwd );
stringlist_append_copy( path_stack->storage , cwd);
stringlist_append_copy( path_stack->stack , cwd );
free(cwd);
}

const char * path_stack_pop( path_stack_type * path_stack ) {
Expand Down
97 changes: 25 additions & 72 deletions lib/util/stringlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,10 @@ void stringlist_fprintf_fmt(const stringlist_type * stringlist, const stringlist
This function appends a copy of s into the stringlist.
*/
void stringlist_append_copy(stringlist_type * stringlist , const char * s) {
vector_append_buffer(stringlist->strings , s , strlen(s) + 1);
}

void stringlist_append_ref(stringlist_type * stringlist , const char * s) {
vector_append_ref(stringlist->strings , s);
}

void stringlist_append_owned_ref(stringlist_type * stringlist , const char * s) {
vector_append_owned_ref(stringlist->strings , s , free);
if (s)
vector_append_buffer(stringlist->strings , s , strlen(s) + 1);
else
vector_append_ref(stringlist->strings, NULL );
}

/*****************************************************************/
Expand Down Expand Up @@ -167,54 +162,7 @@ stringlist_type * stringlist_alloc_argv_copy(const char ** argv , int argc) {
}


stringlist_type * stringlist_alloc_argv_ref(const char ** argv , int argc) {
int iarg;
stringlist_type * stringlist = stringlist_alloc_empty( true );
for (iarg = 0; iarg < argc; iarg++)
stringlist_append_ref( stringlist , argv[iarg]);

return stringlist;
}


stringlist_type * stringlist_alloc_argv_owned_ref(const char ** argv , int argc) {
int iarg;
stringlist_type * stringlist = stringlist_alloc_empty( true );
for (iarg = 0; iarg < argc; iarg++)
stringlist_append_owned_ref( stringlist , argv[iarg]);

return stringlist;
}



/**
Allocates a new stringlist instance where all the new string are
references to the string found in the existing stringlist
instance.
*/
stringlist_type * stringlist_alloc_shallow_copy(const stringlist_type * src) {
stringlist_type * copy = stringlist_alloc_empty( false );
copy->strings = vector_alloc_copy( src->strings , false);
return copy;
}


/**
Allocates a new stringlist where the strings are references to the
num_strings found in stringlist from start.
*/
stringlist_type * stringlist_alloc_shallow_copy_with_limits(const stringlist_type * stringlist, int offset, int num_strings) {
stringlist_type * copy = stringlist_alloc_empty( true );
int i;
for( i=0; i<num_strings; i++)
{
const char * str = stringlist_iget(stringlist, i + offset);
vector_append_ref(copy->strings, str);
}

return copy;
}


/*
Expand Down Expand Up @@ -251,12 +199,6 @@ void stringlist_append_stringlist_copy(stringlist_type * stringlist , const stri
}


void stringlist_append_stringlist_ref(stringlist_type * stringlist , const stringlist_type * src) {
int i;
for (i = 0; i < stringlist_get_size( src ); i++)
stringlist_append_ref(stringlist , stringlist_iget(src , i));
}


/**
Insert a copy of a stringlist in some position.
Expand All @@ -277,10 +219,10 @@ void stringlist_insert_stringlist_copy(stringlist_type * stringlist, const strin
int i;

for( i=0; i<pos; i++)
stringlist_append_ref(start, stringlist_iget(stringlist, i));
stringlist_append_copy(start, stringlist_iget(stringlist, i));

for( i=pos; i<size_old; i++)
stringlist_append_ref(end , stringlist_iget(stringlist, i));
stringlist_append_copy(end , stringlist_iget(stringlist, i));

stringlist_append_stringlist_copy(newList, start);
stringlist_append_stringlist_copy(newList, src );
Expand Down Expand Up @@ -643,8 +585,11 @@ void stringlist_fread(stringlist_type * s, FILE * stream) {
int size = util_fread_int(stream);
int i;
stringlist_clear(s);
for (i=0; i < size; i++)
stringlist_append_owned_ref( s , util_fread_alloc_string( stream ));
for (i=0; i < size; i++) {
char * tmp = util_fread_alloc_string(stream);
stringlist_append_copy( s , tmp);
free(tmp);
}
}


Expand Down Expand Up @@ -750,7 +695,8 @@ int stringlist_select_matching_files(stringlist_type * names , const char * path
if (file_handle != INVALID_HANDLE_VALUE) {
do {
char * full_path = util_alloc_filename( path , file_data.cFileName , NULL);
stringlist_append_owned_ref( names , full_path );
stringlist_append_copy( names , full_path );
free( full_path );
} while (FindNextFile( file_handle , &file_data) != 0);
}
FindClose( file_handle );
Expand Down Expand Up @@ -787,7 +733,11 @@ int stringlist_select_files(stringlist_type * names, const char * path, file_pre
if (predicate && !predicate(entry->d_name, pred_arg))
continue;

stringlist_append_owned_ref(names, util_alloc_filename(path, entry->d_name, NULL));
{
char * fname = util_alloc_filename(path, entry->d_name, NULL);
stringlist_append_copy(names, fname);
free(fname);
}
}

closedir(dir);
Expand All @@ -809,8 +759,11 @@ int stringlist_select_files(stringlist_type * names, const char * path, file_pre

if (predicate && !predicate(file_data.cFileName, pred_arg))
continue;

stringlist_append_owned_ref(names, util_alloc_filename(path, file_data.cFileName, NULL));
{
char * tmp_fname = util_alloc_filename(path, file_data.cFileName, NULL);
stringlist_append_copy(names, tmp_fname);
free(tmp_fname);
}
} while (FindNextFile( file_handle , &file_data) != 0);
FindClose( file_handle );
}
Expand All @@ -836,7 +789,7 @@ int stringlist_append_matching_elements(stringlist_type * target , const stringl
return match_count;
}

int stringlist_select_matching_elements(stringlist_type * target , const stringlist_type * src , const char * pattern) {
int stringlist_select_matching_elements(stringlist_type * target , const stringlist_type * src , const char * pattern) {
stringlist_clear( target );
return stringlist_append_matching_elements( target , src , pattern );
}
Expand All @@ -849,7 +802,7 @@ static int void_strcmp(const void* s1, const void *s2) {
bool stringlist_unique(const stringlist_type * stringlist )
{
bool unique = true;
stringlist_type * cpy = stringlist_alloc_shallow_copy(stringlist);
stringlist_type * cpy = stringlist_alloc_deep_copy(stringlist);

stringlist_sort(cpy, void_strcmp);
for (int i = 0; i < stringlist_get_size(cpy) - 1; i++) {
Expand Down
3 changes: 2 additions & 1 deletion lib/util/tests/ert_util_spawn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ void test_spawn_redirect_threaded() {
util_make_path( path );
char * script = util_alloc_filename( path , "script" , NULL);
make_script(script, stdout_msg, stderr_msg);
stringlist_append_owned_ref(script_fullpaths, script);
stringlist_append_copy(script_fullpaths, script);
free(script);
free(path);
}

Expand Down
Loading

0 comments on commit 180e713

Please sign in to comment.