From 1d7d1361c661fdc9ad15cd49c8b36bf13aabc83f Mon Sep 17 00:00:00 2001 From: Eivind Jahren Date: Thu, 29 Feb 2024 13:02:13 +0100 Subject: [PATCH] Remove unused functions in ert/util/util --- lib/CMakeLists.txt | 5 - lib/include/ert/util/util.hpp | 107 +- lib/util/tests/ert_util_before_after.cpp | 22 - lib/util/tests/ert_util_chdir.cpp | 3 - lib/util/tests/ert_util_clamp.cpp | 34 - lib/util/tests/ert_util_datetime.cpp | 14 - lib/util/tests/ert_util_filename.cpp | 18 - lib/util/tests/ert_util_mkdir_p.cpp | 30 - lib/util/tests/ert_util_sscan_test.cpp | 135 -- lib/util/tests/ert_util_strstr_int_format.cpp | 46 - lib/util/util.cpp | 1543 +---------------- 11 files changed, 71 insertions(+), 1886 deletions(-) delete mode 100644 lib/util/tests/ert_util_before_after.cpp delete mode 100644 lib/util/tests/ert_util_clamp.cpp delete mode 100644 lib/util/tests/ert_util_mkdir_p.cpp delete mode 100644 lib/util/tests/ert_util_strstr_int_format.cpp diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 110ea97de..f6273cc95 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -210,25 +210,20 @@ foreach( ert_util_alloc_file_components ert_util_split_path ert_util_approx_equal - ert_util_before_after ert_util_binary_split - ert_util_clamp ert_util_chdir ert_util_filename ert_util_parent_path ert_util_realpath ert_util_relpath_test ert_util_rng - ert_util_sscan_test ert_util_strcat_test ert_util_stringlist_test ert_util_string_util - ert_util_strstr_int_format ert_util_type_vector_functions ert_util_vector_test ert_util_datetime ert_util_normal_path - ert_util_mkdir_p test_area) add_executable(${name} util/tests/${name}.cpp) diff --git a/lib/include/ert/util/util.hpp b/lib/include/ert/util/util.hpp index 04b6905ce..fc199a846 100644 --- a/lib/include/ert/util/util.hpp +++ b/lib/include/ert/util/util.hpp @@ -90,34 +90,18 @@ typedef bool(walk_dir_callback_ftype)( char *util_get_timezone(void); time_t util_make_datetime_utc(int, int, int, int, int, int); -bool util_make_datetime_utc_validated(int sec, int min, int hour, int mday, - int month, int year, time_t *t); time_t util_make_date_utc(int, int, int); time_t util_make_pure_date_utc(time_t t); void util_inplace_forward_seconds_utc(time_t *t, double seconds); void util_inplace_forward_days_utc(time_t *t, double days); -time_t util_file_mtime(const char *file); double util_difftime(time_t, time_t, int *, int *, int *, int *); double util_difftime_days(time_t, time_t); double util_difftime_seconds(time_t start_time, time_t end_time); -bool util_after(time_t t, time_t limit); -bool util_before(time_t t, time_t limit); -bool util_file_newer(const char *file, time_t t0); -bool util_file_older(const char *file, time_t t0); -char *util_alloc_date_string_utc(time_t t); -char *util_alloc_date_stamp_utc(void); - -double util_pow10(double x); -bool util_char_in(char c, int, const char *); char *util_alloc_sprintf_va(const char *fmt, va_list ap); char *util_alloc_sprintf(const char *, ...); -char *util_realloc_sprintf(char *, const char *, ...); -bool util_fscanf_date_utc(FILE *, time_t *); -bool util_sscanf_date_utc(const char *, time_t *); bool util_sscanf_isodate(const char *, time_t *); -bool util_sscanf_percent(const char *string, double *value); bool util_is_executable(const char *); bool util_entry_exists(const char *entry); bool util_file_exists(const char *); @@ -125,26 +109,15 @@ bool util_is_abs_path(const char *); char *util_alloc_abs_path(const char *path); char *util_alloc_rel_path(const char *__root_path, const char *path); bool util_fmt_bit8(const char *); -bool util_fmt_bit8_stream(FILE *); -char *util_strstr_int_format(const char *string); -int util_int_format_count(const char *string); -bool util_mkdir_p(const char *path); +bool util_mkdir_p(const char *); void util_make_path(const char *); -char *util_newest_file(const char *, const char *); double util_file_difftime(const char *, const char *); -bool util_file_update_required(const char *, const char *); size_t util_file_size(const char *); size_t util_fd_size(int fd); void util_clear_directory(const char *path, bool strict_uid, bool unlink_root); -void util_unlink_existing(const char *filename); void util_strupr(char *); bool util_string_equal(const char *s1, const char *s2); char *util_alloc_strupr_copy(const char *); -void util_string_tr(char *, char, char); -bool util_copy_stream(FILE *, FILE *, size_t, void *, bool abort_on_error); -void util_move_file(const char *src_file, const char *target_file); -void util_move_file4(const char *src_name, const char *target_name, - const char *src_path, const char *target_path); bool util_copy_file(const char *, const char *); bool util_copy_file__(const char *src_file, const char *target_file, size_t buffer_size, void *buffer, bool abort_on_error); @@ -153,15 +126,11 @@ bool util_is_cwd(const char *path); char *util_alloc_normal_path(const char *input_path); char *util_alloc_realpath(const char *); char *util_alloc_realpath__(const char *input_path); -bool util_string_match(const char *string, const char *pattern); bool util_string_has_wildcard(const char *s); bool util_file_readable(const char *file); bool util_entry_readable(const char *entry); -bool util_entry_writable(const char *entry); bool util_ftruncate(FILE *stream, long size); -void util_usleep(unsigned long micro_seconds); - int util_roundf(float x); int util_round(double x); @@ -192,82 +161,41 @@ bool util_chmod_if_owner(const char *filename, mode_t new_mode); int util_forward_line(FILE *, bool *); void util_rewind_line(FILE *); -int util_count_content_file_lines(FILE *); FILE *util_mkdir_fopen(const char *filename, const char *mode); -int util_fmove(FILE *stream, long offset, long shift); FILE *util_fopen(const char *, const char *); FILE *util_fopen__(const char *filename, const char *mode); bool util_fopen_test(const char *, const char *); -char *util_split_alloc_dirname(const char *input_path); char *util_split_alloc_filename(const char *input_path); void util_alloc_file_components(const char *, char **, char **, char **); -//char * util_realloc_full_path(char * , const char *, const char *); char *util_alloc_tmp_file(const char *, const char *, bool); char *util_fscanf_alloc_line(FILE *, bool *); -char *util_fscanf_alloc_token(FILE *); -void util_fskip_token(FILE *); -void util_fskip_space(FILE *, bool *); -void util_fskip_chars(FILE *, const char *, bool *); -void util_fskip_cchars(FILE *, const char *, bool *); -bool util_fscanf_int(FILE *, int *); -bool util_fscanf_bool(FILE *stream, bool *value); bool util_sscanf_bool(const char *, bool *); -bool util_sscanf_octal_int(const char *buffer, int *value); int util_strcmp_int(const char *s1, const char *s2); int util_strcmp_float(const char *s1, const char *s2); bool util_sscanf_int(const char *, int *); -const char *util_parse_int(const char *, int *, bool *); -const char *util_skip_sep(const char *, const char *, bool *); bool util_sscanf_double(const char *, double *); -//char * util_alloc_full_path(const char *, const char *); char *util_alloc_filename(const char *, const char *, const char *); -char *util_realloc_filename(char *, const char *, const char *, const char *); char *util_alloc_strip_copy(const char *); -char *util_alloc_string_sum(const char **, int); char *util_strcat_realloc(char *, const char *); char *util_alloc_string_copy(const char *); -char **util_stringlist_append_copy(char **, int, const char *); -char **util_stringlist_append_ref(char **, int, const char *); -char **util_alloc_stringlist_copy(char const *const *, int); -void util_split_string(const char *, const char *, int *, char ***); -void util_path_split(const char *, int *, char ***); +void util_path_split(const char *line, int *_tokens, char ***_token_list); char *util_alloc_parent_path(const char *path); void util_binary_split_string(const char *, const char *, bool, char **, char **); -void util_binary_split_string_from_max_length(const char *, const char *, int, - char **, char **); -char *util_alloc_joined_string(const char **, int, const char *); -char *util_alloc_multiline_string(const char **, int); -char *util_string_replace_alloc(const char *, const char *, const char *); -char *util_string_replacen_alloc(const char *, int, const char **, - const char **); int util_string_replace_inplace(char **, const char *, const char *); -char *util_string_strip_chars_alloc(const char *, const char *); char *util_realloc_string_copy(char *, const char *); -char *util_realloc_substring_copy(char *, const char *, int N); -char *util_realloc_dequoted_string(char *); -char *util_alloc_dequoted_copy(const char *s); void util_free_stringlist(char **, int); -void util_free_NULL_terminated_stringlist(char **string_list); char *util_alloc_substring_copy(const char *, int offset, int N); bool util_is_directory(const char *); bool util_is_file(const char *); - void util_set_datetime_values_utc(time_t, int *, int *, int *, int *, int *, int *); void util_set_date_values_utc(time_t, int *, int *, int *); - bool util_is_first_day_in_month_utc(time_t t); - -unsigned int util_dev_urandom_seed(); unsigned int util_clock_seed(void); void util_fread_dev_random(int, char *); void util_fread_dev_urandom(int, char *); -bool util_string_isspace(const char *s); - -char *util_alloc_dump_filename(void); void util_abort_test_set_intercept_function(const char *); -bool util_addr2line_lookup(const void *, char **, char **, int *); void util_exit(const char *fmt, ...); void util_install_signals(void); void util_update_signals(void); @@ -279,65 +207,35 @@ void *util_realloc_copy(void *org_ptr, const void *src, size_t byte_size); void *util_alloc_copy(const void *, size_t); void util_double_to_float(float *, const double *, int); void util_float_to_double(double *, const float *, int); - -int util_get_month_nr(const char *); - char *util_fread_alloc_file_content(const char *, int *); void util_fwrite_string(const char *, FILE *); -char *util_fread_realloc_string(char *, FILE *); char *util_fread_alloc_string(FILE *); -void util_fskip_string(FILE *stream); void util_endian_flip_vector(void *data, int element_size, int elements); -void util_clamp_double(double *value, double limit1, double limit2); -double util_double_vector_mean(int, const double *); -double util_double_vector_stddev(int, const double *); void util_double_vector_max_min(int, const double *, double *, double *); void util_update_double_max_min(double, double *, double *); void util_update_float_max_min(float, float *, float *); void util_update_int_max_min(int, int *, int *); -float util_float_max(float, float); -long int util_long_max(long int a, long int b); int util_int_max(int, int); double util_double_max(double, double); -float util_float_min(float, float); int util_int_min(int, int); size_t util_size_t_min(size_t a, size_t b); size_t util_size_t_max(size_t a, size_t b); -time_t util_time_t_min(time_t a, time_t b); -time_t util_time_t_max(time_t a, time_t b); double util_double_min(double, double); void util_fskip_lines(FILE *, int); bool util_same_file(const char *, const char *); void util_fread(void *, size_t, size_t, FILE *, const char *); void util_fwrite(const void *, size_t, size_t, FILE *, const char *); -time_t util_fread_time_t(FILE *stream); int util_fread_int(FILE *); -long util_fread_long(FILE *); -bool util_fread_bool(FILE *); -double util_fread_double(FILE *stream); void util_fwrite_offset(offset_type, FILE *); void util_fwrite_size_t(size_t, FILE *); void util_fwrite_int(int, FILE *); void util_fwrite_long(long, FILE *); -void util_fwrite_bool(bool, FILE *); -void util_fwrite_time_t(time_t, FILE *); void util_fwrite_double(double, FILE *); void util_fwrite_int_vector(const int *, int, FILE *, const char *); -void util_fwrite_double_vector(const double *, int, FILE *, const char *); -void util_fread_char_vector(char *, int, FILE *, const char *); -int util_type_get_id(const void *data); - -bool util_sscanf_bytesize(const char *, size_t *); int util_get_current_linenr(FILE *stream); - -void util_fskip_int(FILE *stream); -void util_fskip_long(FILE *stream); -void util_fskip_bool(FILE *stream); bool util_fseek_string(FILE *stream, const char *string, bool skip_string, bool case_sensitive); -char *util_fscanf_alloc_upto(FILE *stream, const char *stop_string, - bool include_stop_string); bool util_files_equal(const char *file1, const char *file2); double util_kahan_sum(const double *data, size_t N); bool util_double_approx_equal(double d1, double d2); @@ -350,7 +248,6 @@ void util_time_utc(time_t *t, struct tm *ts); bool util_is_link(const char *); // Will always return false on windows int util_chdir(const char *path); -bool util_chdir_file(const char *filename); #ifdef ERT_HAVE_UNISTD #include diff --git a/lib/util/tests/ert_util_before_after.cpp b/lib/util/tests/ert_util_before_after.cpp deleted file mode 100644 index 1bb85009d..000000000 --- a/lib/util/tests/ert_util_before_after.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include -#include - -#include -#include - -int main(int argc, char **argv) { - time_t t1 = util_make_date_utc(1, 1, 2000); - time_t t2 = util_make_date_utc(1, 1, 2001); - - test_assert_true(util_before(t1, t2)); - test_assert_true(util_after(t2, t1)); - - test_assert_false(util_before(t2, t1)); - test_assert_false(util_after(t1, t2)); - - test_assert_false(util_before(t1, t1)); - test_assert_false(util_after(t1, t1)); - - exit(0); -} diff --git a/lib/util/tests/ert_util_chdir.cpp b/lib/util/tests/ert_util_chdir.cpp index 98f9e6001..4e5b4be9b 100644 --- a/lib/util/tests/ert_util_chdir.cpp +++ b/lib/util/tests/ert_util_chdir.cpp @@ -9,13 +9,10 @@ void test_chdir() { rd::util::TestArea ta("chdir"); const char *cwd = ta.test_cwd().c_str(); - test_assert_false(util_chdir_file("/file/does/not/exist")); - test_assert_false(util_chdir_file(cwd)); { FILE *stream = util_mkdir_fopen("path/FILE", "w"); fclose(stream); } - test_assert_true(util_chdir_file("path/FILE")); { char *new_cwd = util_alloc_cwd(); char *fname = util_alloc_filename(cwd, "path", NULL); diff --git a/lib/util/tests/ert_util_clamp.cpp b/lib/util/tests/ert_util_clamp.cpp deleted file mode 100644 index 238ec1fe5..000000000 --- a/lib/util/tests/ert_util_clamp.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include - -#include -#include -#include - -int main(int argc, char **argv) { - double value; - - value = 0; - util_clamp_double(&value, -1, 1); - test_assert_double_equal(value, 0); - - value = 0; - util_clamp_double(&value, 1, 2); - test_assert_double_equal(value, 1); - - value = 0; - util_clamp_double(&value, 2, 1); - test_assert_double_equal(value, 1); - - value = 0; - util_clamp_double(&value, -2, 0); - test_assert_double_equal(value, 0); - - value = 0; - util_clamp_double(&value, -2, -1); - test_assert_double_equal(value, -1); - - value = 0; - util_clamp_double(&value, -1, -2); - test_assert_double_equal(value, -1); -} diff --git a/lib/util/tests/ert_util_datetime.cpp b/lib/util/tests/ert_util_datetime.cpp index b8cd613e6..23db646fb 100644 --- a/lib/util/tests/ert_util_datetime.cpp +++ b/lib/util/tests/ert_util_datetime.cpp @@ -8,23 +8,9 @@ int main(int argc, char **argv) { - /* Invalid seconds */ - test_assert_false( - util_make_datetime_utc_validated(75, 0, 0, 1, 1, 2010, NULL)); - - /* Invalid month */ - test_assert_false( - util_make_datetime_utc_validated(15, 0, 0, 1, 16, 2010, NULL)); - - /* Invalid mday */ - test_assert_false( - util_make_datetime_utc_validated(15, 0, 0, -1, 1, 2010, NULL)); - { time_t t; int sec, min, hour, mday, month, year; - test_assert_true( - util_make_datetime_utc_validated(15, 3, 6, 5, 7, 2010, &t)); util_set_datetime_values_utc(t, &sec, &min, &hour, &mday, &month, &year); diff --git a/lib/util/tests/ert_util_filename.cpp b/lib/util/tests/ert_util_filename.cpp index 3f31c638b..554c7189f 100644 --- a/lib/util/tests/ert_util_filename.cpp +++ b/lib/util/tests/ert_util_filename.cpp @@ -7,23 +7,6 @@ #include #include -void test_dirname() { - const char *src_file1 = "/some/very/long/path/file.txt"; - const char *src_file2 = "relative/path/file.txt"; - const char *src_file3 = "file.txt"; - - char *path1 = util_split_alloc_dirname(src_file1); - char *path2 = util_split_alloc_dirname(src_file2); - char *path3 = util_split_alloc_dirname(src_file3); - - test_assert_string_equal("/some/very/long/path", path1); - test_assert_string_equal("relative/path", path2); - test_assert_NULL(path3); - - free(path1); - free(path2); -} - void test_filename() { const char *src_file1 = "/some/very/long/path/file1.txt"; const char *src_file2 = "relative/path/file2"; @@ -52,7 +35,6 @@ void test_alloc_filename_empty_strings() { int main(int argc, char **argv) { - test_dirname(); test_filename(); test_alloc_filename_empty_strings(); exit(0); diff --git a/lib/util/tests/ert_util_mkdir_p.cpp b/lib/util/tests/ert_util_mkdir_p.cpp deleted file mode 100644 index 91c0892dc..000000000 --- a/lib/util/tests/ert_util_mkdir_p.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include - -#include -#include -#include - -int main(int argc, char **argv) { - rd::util::TestArea ta("mkdir"); - - // Regular use - test_assert_true(util_mkdir_p("some/path/with/many/levels")); - - // Absolute path where the root exists. - { - char *abs_path = util_alloc_abs_path("a/path/with/abs/prefix"); - test_assert_true(util_mkdir_p(abs_path)); - - // Already exists: - test_assert_true(util_mkdir_p(abs_path)); - free(abs_path); - } - - // Permission denied - test_assert_true(util_mkdir_p("read_only")); - chmod("read_only", 0555); - test_assert_false(util_mkdir_p("read_only/no/not/this")); - - exit(0); -} diff --git a/lib/util/tests/ert_util_sscan_test.cpp b/lib/util/tests/ert_util_sscan_test.cpp index 580fac68c..b139e294d 100644 --- a/lib/util/tests/ert_util_sscan_test.cpp +++ b/lib/util/tests/ert_util_sscan_test.cpp @@ -66,35 +66,6 @@ void test_sscanf_bool() { test_assert_false(util_sscanf_bool(NULL, NULL)); } -void test_sscanf_bytesize() { - size_t value = 0u; - test_assert_true(util_sscanf_bytesize("1KB", &value)); - test_assert_size_t_equal(value, 1024); - - test_assert_true(util_sscanf_bytesize("12 mB", &value)); - test_assert_size_t_equal(value, 12 * 1024 * 1024); - - test_assert_true(util_sscanf_bytesize("-47", &value)); - test_assert_size_t_equal( - value, - (size_t)(-47)); // documentation says overflows are not checked for - - value = 0u; - test_assert_false( - util_sscanf_bytesize("3.7 MB", &value)); // no decimals allowed - test_assert_size_t_equal(value, 0u); - test_assert_false( - util_sscanf_bytesize("14 TB", &value)); // TB not supported yet - test_assert_size_t_equal(value, 0u); - - // Test NULL buffer - test_assert_false(util_sscanf_bytesize(NULL, &value)); - test_assert_size_t_equal( - value, 0); // documentation says the value is set to 0 on parsing error - - test_assert_false(util_sscanf_bytesize(NULL, NULL)); -} - void test_sscanf_double() { double value = 1.0; test_assert_true(util_sscanf_double("0.0", &value)); @@ -192,86 +163,6 @@ void test_sscanf_int() { test_assert_false(util_sscanf_int(NULL, NULL)); } -void test_sscanf_octal_int() { - int value = 1; - - test_assert_true(util_sscanf_octal_int("0", &value)); - test_assert_int_equal(value, 0); - - test_assert_true(util_sscanf_octal_int("241", &value)); - test_assert_int_equal(value, 2 * 64 + 4 * 8 + 1); - - test_assert_true(util_sscanf_octal_int("-0", &value)); - test_assert_int_equal(value, 0); - - test_assert_true(util_sscanf_octal_int("-1742", &value)); - test_assert_int_equal(value, -(512 + 7 * 64 + 4 * 8 + 2)); - - value = 1; - test_assert_false(util_sscanf_octal_int("--5++", &value)); - test_assert_int_equal(value, 1); - - test_assert_false(util_sscanf_octal_int("89", &value)); - test_assert_int_equal(value, 1); - - test_assert_false(util_sscanf_octal_int("7.5", &value)); - test_assert_int_equal(value, 1); - - // NULL buffer - value = 3; - test_assert_false(util_sscanf_octal_int(NULL, &value)); - test_assert_int_equal(value, 3); - - test_assert_false(util_sscanf_octal_int(NULL, NULL)); -} - -void test_sscanf_percent() { - { - const char *MIN_REALIZATIONS = "10%"; - double value = 0.0; - test_assert_true(util_sscanf_percent(MIN_REALIZATIONS, &value)); - test_assert_double_equal(10.0, value); - } - - { - const char *MIN_REALIZATIONS_no_percent = "10"; - double value = 0.0; - test_assert_false( - util_sscanf_percent(MIN_REALIZATIONS_no_percent, &value)); - test_assert_double_equal(0.0, value); - } - - { - const char *MIN_REALIZATIONS_float = "10.2%"; - double value = 0.0; - test_assert_true(util_sscanf_percent(MIN_REALIZATIONS_float, &value)); - test_assert_double_equal(10.2, value); - } - - { - const char *MIN_REALIZATIONS_float_no_percentage = "10.2"; - double value = 0.0; - test_assert_false( - util_sscanf_percent(MIN_REALIZATIONS_float_no_percentage, &value)); - test_assert_double_equal(0.0, value); - } - - { - const char *MIN_REALIZATIONS = "9 %"; - double value = 0.0; - test_assert_false(util_sscanf_percent(MIN_REALIZATIONS, &value)); - test_assert_double_equal(0.0, value); - } - - { - double value = 12.5; - test_assert_false(util_sscanf_percent(NULL, &value)); - test_assert_double_equal(12.5, value); - } - - { test_assert_false(util_sscanf_percent(NULL, NULL)); } -} - void check_iso_date(time_t expected, const char *date_string, bool expected_return) { time_t t; @@ -312,36 +203,10 @@ void test_sscanf_isodate() { test_assert_false(util_sscanf_isodate(NULL, NULL)); } -void test_sscanf_date_utc() { - time_t value = 0; - test_assert_true(util_sscanf_date_utc("16.07^1997", &value)); - test_assert_time_t_equal(value, util_make_date_utc(16, 7, 1997)); - - value = util_make_date_utc(10, 11, 2011); - test_assert_false(util_sscanf_date_utc(NULL, &value)); - test_assert_time_t_equal(value, -1); - - /* - ISO dates will be parsed, but with unexpected results, 1997-07-06 - will be parsed as "day 1997" in July, year 6, which wraps to Dec 11 - in year 18: - */ - test_assert_true(util_sscanf_date_utc("1997-07-06", &value)); - test_assert_time_t_equal(value, util_make_date_utc(1997, 7, 6)); - test_assert_time_t_equal(value, util_make_date_utc(18, 12, 11)); - - test_assert_false(util_sscanf_date_utc(NULL, NULL)); -} - int main(int argc, char **argv) { test_sscanf_bool(); - test_sscanf_bytesize(); - test_sscanf_date_utc(); test_sscanf_double(); test_sscanf_int(); test_sscanf_isodate(); - test_sscanf_octal_int(); - test_sscanf_percent(); - exit(0); } diff --git a/lib/util/tests/ert_util_strstr_int_format.cpp b/lib/util/tests/ert_util_strstr_int_format.cpp deleted file mode 100644 index bd7f5b7a9..000000000 --- a/lib/util/tests/ert_util_strstr_int_format.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include - -#include -#include -#include - -void test_util_strstr() { - test_assert_NULL(util_strstr_int_format("StringWithoutIntFormatSpecifier")); - { - const char *source = "String with %d ---"; - char *next = util_strstr_int_format(source); - test_assert_ptr_equal(next, &source[13]); - } - - { - const char *source = "String with %04d ---"; - char *next = util_strstr_int_format(source); - test_assert_ptr_equal(next, &source[15]); - } - - { - const char *source = "String with %ld ---"; - test_assert_NULL(util_strstr_int_format(source)); - } - - { - const char *source = "String with %3d ---"; - test_assert_NULL(util_strstr_int_format(source)); - } -} - -void test_util_count_int_format() { - test_assert_int_equal(0, util_int_format_count("Abcdddd")); - test_assert_int_equal(0, util_int_format_count("%4d")); - test_assert_int_equal(0, util_int_format_count("%ld")); - test_assert_int_equal(1, util_int_format_count("%04d")); - test_assert_int_equal(2, util_int_format_count("%04dXX%034d")); - test_assert_int_equal(4, util_int_format_count("%04dXX%034d%d%d")); -} - -int main(int argc, char **argv) { - test_util_strstr(); - test_util_count_int_format(); - exit(0); -} diff --git a/lib/util/util.cpp b/lib/util/util.cpp index e14053b8a..c4653655f 100644 --- a/lib/util/util.cpp +++ b/lib/util/util.cpp @@ -231,12 +231,6 @@ void util_fread_dev_urandom(int buffer_size_, char *buffer) { fclose(stream); } -unsigned int util_dev_urandom_seed() { - unsigned int seed; - util_fread_dev_urandom(sizeof seed, (char *)&seed); - return seed; -} - unsigned int util_clock_seed() { int sec, min, hour; int mday, year, month; @@ -326,6 +320,16 @@ bool util_double_approx_equal(double d1, double d2) { return util_double_approx_equal__(d1, d2, epsilon, 0.0); } +char *util_alloc_string_copy(const char *src) { + if (src != NULL) { + int byte_size = (strlen(src) + 1) * sizeof *src; + char *copy = (char *)util_calloc(byte_size, sizeof *copy); + memcpy(copy, src, byte_size); + return copy; + } else + return NULL; +} + char *util_alloc_substring_copy(const char *src, int offset, int N_) { size_t N = N_; char *copy; @@ -338,51 +342,6 @@ char *util_alloc_substring_copy(const char *src, int offset, int N_) { return copy; } -char *util_alloc_dequoted_copy(const char *s) { - char first_char = s[0]; - char last_char = s[strlen(s) - 1]; - char *next; - int offset, len; - - if ((first_char == '\'') || (first_char == '\"')) - offset = 1; - else - offset = 0; - - if ((last_char == '\'') || (last_char == '\"')) - len = strlen(s) - offset - 1; - else - len = strlen(s) - offset; - - next = util_alloc_substring_copy(s, offset, len); - return next; -} - -/** - The input string is freed, and a new storage - without quotes is returned. -*/ -char *util_realloc_dequoted_string(char *s) { - char first_char = s[0]; - char last_char = s[strlen(s) - 1]; - char *next; - int offset, len; - - if ((first_char == '\'') || (first_char == '\"')) - offset = 1; - else - offset = 0; - - if ((last_char == '\'') || (last_char == '\"')) - len = strlen(s) - offset - 1; - else - len = strlen(s) - offset; - - next = util_alloc_substring_copy(s, offset, len); - free(s); - return next; -} - void util_strupr(char *s) { size_t i; for (i = 0; i < strlen(s); i++) @@ -398,7 +357,7 @@ char *util_alloc_strupr_copy(const char *s) { /** Replaces all occurences of c1 in s with c2. */ -void util_string_tr(char *s, char c1, char c2) { +static void util_string_tr(char *s, char c1, char c2) { size_t i; for (i = 0; i < strlen(s); i++) if (s[i] == c1) @@ -479,36 +438,6 @@ bool util_fseek_string(FILE *stream, const char *__string, bool skip_string, return string_found; } -/** - This function will allocate a character buffer, and read file - content all the way up to 'stop_string'. If the stop_string is not - found, the function will return NULL, and the file pointer will be - unchanged. - - If include_stop_string is true the returned string will end with - stop_string, and the file pointer will be positioned right AFTER - stop_string, otherwise the file_pointer will be positioned right - before stop_string. -*/ - -char *util_fscanf_alloc_upto(FILE *stream, const char *stop_string, - bool include_stop_string) { - long int start_pos = util_ftell(stream); - if (util_fseek_string(stream, stop_string, include_stop_string, - true)) { /* Default case sensitive. */ - long int end_pos = util_ftell(stream); - int len = end_pos - start_pos; - char *buffer = (char *)util_calloc((len + 1), sizeof *buffer); - - util_fseek(stream, start_pos, SEEK_SET); - util_fread(buffer, 1, len, stream, __func__); - buffer[len] = '\0'; - - return buffer; - } else - return NULL; /* stop_string not found */ -} - static char *util_fscanf_alloc_line__(FILE *stream, bool *at_eof, char *line) { int init_pos = util_ftell(stream); char *new_line; @@ -575,25 +504,6 @@ char *util_fscanf_alloc_line(FILE *stream, bool *at_eof) { return util_fscanf_alloc_line__(stream, at_eof, NULL); } -/** - WIndows does not have the usleep() function, on the other hand - Sleep() function in windows has millisecond resolution, instead of - seconds as in linux. -*/ - -void util_usleep(unsigned long micro_seconds) { -#ifdef HAVE__USLEEP - usleep(micro_seconds); -#else -#ifdef ERT_WINDOWS - { - int milli_seconds = micro_seconds / 1000; - Sleep(milli_seconds); - } -#endif -#endif -} - static char *util_getcwd(char *buffer, int size) { #ifdef HAVE_POSIX_GETCWD return getcwd(buffer, size); @@ -935,36 +845,6 @@ void util_fskip_lines(FILE *stream, int lines) { } while (cont); } -bool util_char_in(char c, int set_size, const char *set) { - int i; - bool in = false; - for (i = 0; i < set_size; i++) - if (set[i] == c) - in = true; - - return in; -} - -/** - Returns true if ALL the characters in 's' return true from - isspace(). -*/ -bool util_string_isspace(const char *s) { - size_t index = 0; - while (index < strlen(s)) { - if (!isspace(s[index])) - return false; - index++; - } - return true; -} - -void util_fskip_token(FILE *stream) { - char *tmp = util_fscanf_alloc_token(stream); - if (tmp != NULL) - free(tmp); -} - static void util_fskip_chars__(FILE *stream, const char *skip_set, bool complimentary_set, bool *at_eof) { bool cont = true; @@ -993,145 +873,6 @@ void util_fskip_chars(FILE *stream, const char *skip_set, bool *at_eof) { util_fskip_chars__(stream, skip_set, false, at_eof); } -void util_fskip_cchars(FILE *stream, const char *skip_set, bool *at_eof) { - util_fskip_chars__(stream, skip_set, true, at_eof); -} - -static void util_fskip_space__(FILE *stream, bool complimentary_set, - bool *at_eof) { - bool cont = true; - do { - int c = fgetc(stream); - if (c == EOF) { - *at_eof = true; - cont = false; - } else { - if (!isspace(c)) { - /* c is not in space set. */ - if (!complimentary_set) - cont = false; - } else { - /* c is in skip_set */ - if (complimentary_set) - cont = false; - } - } - } while (cont); - if (!*at_eof) - util_fseek(stream, -1, SEEK_CUR); -} - -void util_fskip_space(FILE *stream, bool *at_eof) { - util_fskip_space__(stream, false, at_eof); -} - -/** - This functions reads a token[1] from stream, allocates storage for - the it, and returns the newly allocated storage. Observe that the - function does *NOT* read past end of line. If no token can be the - function will return NULL. - - Example: - -------- - - File: - ________________ - / - | This is a file - | Line2 - | Line3 - \________________ - - - bool at_eof = 0; - char * token; - while (!at_eof) { - token = util_fscanf_alloc_token(stream); - if (token != NULL) { - printf("Have read token:%s \n",token); - free(token); - } else { - printf("Have reached EOL/EOF \n"); - util_forward_line(stream , &at_eof); - } - } - - This will produce the output: - Have read token: This - Have read token: is - Have read token: a - Have read token: file - Have reached EOL/EOF - Have read token: Line2 - Have reached EOL/EOF - Have read token: Line3 - Have reached EOL/EOF - - - [1]: A token is defined as a sequence of characters separated by - white-space or tab. -*/ - -char *util_fscanf_alloc_token(FILE *stream) { - const char *space_set = " \t"; - bool cont; - char *token = NULL; - char c; - - cont = true; - - /* Skipping initial whitespace */ - do { - int pos = util_ftell(stream); - c = fgetc(stream); - if (EOL_CHAR(c)) { - /* Going back to position at newline */ - util_fseek(stream, pos, SEEK_SET); - cont = false; - } else if (c == EOF) - cont = false; - else if (!util_char_in(c, 2, space_set)) { - util_fseek(stream, pos, SEEK_SET); - cont = false; - } - } while (cont); - if (EOL_CHAR(c)) - return NULL; - if (c == EOF) - return NULL; - - /* At this point we are guranteed to return something != NULL */ - cont = true; - { - int length = 0; - long int token_start = util_ftell(stream); - - do { - c = fgetc(stream); - if (c == EOF) - cont = false; - else if (EOL_CHAR(c)) - cont = false; - else if (util_char_in(c, 2, space_set)) - cont = false; - else - length++; - } while (cont); - if (EOL_CHAR(c)) - util_fseek(stream, -1, SEEK_CUR); - - token = (char *)util_calloc(length + 1, sizeof *token); - util_fseek(stream, token_start, SEEK_SET); - { - int i; - for (i = 0; i < length; i++) - token[i] = fgetc(stream); - token[length] = '\0'; - } - } - return token; -} - /** This function parses a string literal (hopfully) containing a represantation of a double. The return value is true|false depending @@ -1173,40 +914,11 @@ bool util_sscanf_double(const char *buffer, double *value) { return value_OK; } -/** - Base 8 -*/ - -bool util_sscanf_octal_int(const char *buffer, int *value) { - if (!buffer) - return false; - - bool value_OK = false; - char *error_ptr; - - int tmp_value = strtol(buffer, &error_ptr, 8); - - /* - Skip trailing white-space - */ - - while (error_ptr[0] != '\0' && isspace(error_ptr[0])) - error_ptr++; - - if (error_ptr[0] == '\0') { - value_OK = true; - if (value != NULL) - *value = tmp_value; - } - return value_OK; -} - /** Takes a char buffer as input, and parses it as an integer. Returns true if the parsing succeeded, and false otherwise. If parsing succeeded, the integer value is returned by reference. */ - bool util_sscanf_int(const char *buffer, int *value) { if (!buffer) return false; @@ -1235,7 +947,6 @@ bool util_sscanf_int(const char *buffer, int *value) { If any (or both) s1 or s2 is equal to NULL - the function will return false. */ - bool util_string_equal(const char *s1, const char *s2) { if (s1 == NULL || s2 == NULL) return false; @@ -1247,150 +958,9 @@ bool util_string_equal(const char *s1, const char *s2) { return false; } -/** - This function parses the string 's' for an integer. The return - value is a pointer to the first character which is not an integer - literal, or NULL if the whole string is read. - - The actual integer value is returned by reference. In addition a - bool 'OK' is returned by reference, observe that that the bool OK - is checked on function entry, and must point to true then. - - The somewhat contrived interface is to facilitate repeated calls on - the same string to get out all the integers, typically to be used - together with util_skip_sep(). - - Example - ------- - - s = "1, 10, 78, 67"; - | | | NULL - 1 2 3 3 - - The vertical bars indicate the return values. - -*/ - -const char *util_parse_int(const char *s, int *value, bool *OK) { - if (*OK) { - char *error_ptr; - *value = strtol(s, &error_ptr, 10); - if (error_ptr == s) - *OK = false; - return error_ptr; - } else - return NULL; -} - -/** - This function will skip the characters in s which are in the string - 'sep_set' and return a pointer to the first character NOT in - 'sep_set'; this is basically strspn() functionality. But it will - update a reference bool OK if no characters are skipped - - i.e. there should be some characters to skip. Typically used - together with util_parse_int(): - - - Example - ------- - - const char * s = "1, 6 , 79 , 89 , 782"; - const char * current_ptr = s; - bool OK = true; - while (OK) { - int value; - current_ptr = util_parse_int(current_ptr , &value , &OK); - if (OK) - printf("Found:%d \n",value); - current_ptr = util_skip_sep(current_ptr , " ," , &OK); - } - - -*/ - -const char *util_skip_sep(const char *s, const char *sep_set, bool *OK) { - if (*OK) { - int sep_length = strspn(s, sep_set); - if (sep_length == 0) - *OK = false; - return &s[sep_length]; - } else - return NULL; -} - -/** - This function will parse string containing an integer, and an - optional suffix. The valid suffixes are KB,MB and GB (any case is - allowed); if no suffix is appended the buffer is assumed to contain - a memory size already specified in bytes. - - - Observe that __this_function__ allows an arbitrary number of spaces - between between the integer literal and the suffix string; however - this might be tricky when parsing. It is probably best to disallow - these spaces? - - "1GB", "1 GB", "1 gB" - - are all legitimate. The universal factor used is 1024: - - KB => *= 1024 - MB => *= 1024 * 1024; - GB => *= 1024 * 1024 * 1024; - - Observe that if the functions fails to parse/interpret the string - it will return false, and set the reference value to 0. However it - will not fail with an abort. Overflows are *NOT* checked for. -*/ - -bool util_sscanf_bytesize(const char *buffer, size_t *size) { - if (!buffer) { - if (size) - *size = 0; - return false; - } - - size_t value; - char *suffix_ptr; - size_t KB_factor = 1024; - size_t MB_factor = 1024 * 1024; - size_t GB_factor = 1024 * 1024 * 1024; - size_t factor = 1; - bool parse_OK = true; - - value = strtol(buffer, &suffix_ptr, 10); - if (suffix_ptr[0] != '\0') { - while (isspace(suffix_ptr[0])) - suffix_ptr++; - { - char *upper = util_alloc_strupr_copy(suffix_ptr); - if (strcmp(upper, "KB") == 0) - factor = KB_factor; - else if (strcmp(upper, "MB") == 0) - factor = MB_factor; - else if (strcmp(upper, "GB") == 0) - factor = GB_factor; - else - parse_OK = false; - /* else - failed to parse - returning false. */ - free(upper); - } - } - - if (size != NULL) { - if (parse_OK) - *size = value * factor; - else - *size = 0; - } - - return parse_OK; -} - /** Checks if c is in the character class: [-0-9], and optionally "." */ - static int isnumeric(int c, bool float_mode) { if (isdigit(c)) return true; @@ -1645,79 +1215,6 @@ bool util_sscanf_bool(const char *buffer, bool *_value) { return parse_OK; } -bool util_fscanf_bool(FILE *stream, bool *value) { - char buffer[256]; - if (fscanf(stream, "%s", buffer) == 1) - return util_sscanf_bool(buffer, value); - - return false; -} - -/** - Takes a stream as input. Reads one string token from the stream, - and tries to interpret the token as an integer with the function - util_sscanf_int(). Returns true if the parsing succeeded, and false - otherwise. If parsing succeded, the integer value is returned by - reference. - - If the parsing fails the stream is repositioned at the location it - had on entry to the function. -*/ - -bool util_fscanf_int(FILE *stream, int *value) { - long int start_pos = util_ftell(stream); - char *token = util_fscanf_alloc_token(stream); - - bool value_OK = false; - if (token != NULL) { - value_OK = util_sscanf_int(token, value); - if (!value_OK) - util_fseek(stream, start_pos, SEEK_SET); - free(token); - } - return value_OK; -} - -int util_count_content_file_lines(FILE *stream) { - int lines = 0; - int empty_lines = 0; - int col = 0; - char c; - - do { - c = fgetc(stream); - if (EOL_CHAR(c)) { - if (col == 0) - empty_lines++; - else { - lines += empty_lines + 1; - empty_lines = 0; - } - col = 0; - c = fgetc(stream); - if (!feof(stream)) { - if (!EOL_CHAR(c)) { - util_fseek(stream, -1, SEEK_CUR); - } - } else if (c == EOF) { - lines++; - } - - } else if (c == EOF) { - lines++; - } else { - if (c != ' ') - col++; - } - } while (!feof(stream)); - if (col == 0) - /* - Not counting empty last line. - */ - lines--; - return lines; -} - /** buffer_size is _only_ for a return (by reference) of the size of the allocation. Can pass in NULL if that size is not interesting. @@ -1833,40 +1330,6 @@ bool util_copy_file(const char *src_file, const char *target_file) { } } -void util_move_file(const char *src_file, const char *target_file) { - if (util_file_exists(src_file)) { - if (util_copy_file(src_file, target_file)) - remove(src_file); - } -} - -void util_move_file4(const char *src_name, const char *target_name, - const char *src_path, const char *target_path) { - char *src; - char *target; - - if (src_path != NULL) - src = util_alloc_filename(src_path, src_name, NULL); - else - src = util_alloc_string_copy(src_name); - - if (target_name == NULL) - target_name = src_name; - - if (target_path == NULL) - target_path = src_path; - - if (target_path != NULL) - target = util_alloc_filename(target_path, target_name, NULL); - else - target = util_alloc_string_copy(target_name); - - util_move_file(src, target); - - free(target); - free(src); -} - /** Only the file _content_ is considered - file metadata is ignored. */ @@ -1906,98 +1369,13 @@ bool util_files_equal(const char *file1, const char *file2) { return equal; } -static void util_fclear_region(FILE *stream, long offset, long region_size) { - util_fseek(stream, offset, SEEK_SET); - { - int i; - for (i = 0; i < region_size; i++) - fputc(0, stream); - } -} +/* + Windows *might* have both the symbols _access() and access(), but we prefer + the _access() symbol as that seems to be preferred by Windows. We therefor do + the #HAVE_WINDOWS__ACCESS check first. +*/ -static void util_fmove_block(FILE *stream, long offset, long shift, - char *buffer, int buffer_size) { - util_fseek(stream, offset, SEEK_SET); - { - int bytes_read = fread(buffer, sizeof *buffer, buffer_size, stream); - util_fseek(stream, offset + shift, SEEK_SET); - fwrite(buffer, sizeof *buffer, bytes_read, stream); - } -} - -/** - This function will modify a file in place by moving the part from - @offset and to the end of the file @shift bytes. If @shift is - positive a 'hole' will be created in the file, if @shift is - negative a part of the file will be overwritten: - - Original : "ABCDEFGHIJKILMNOPQ" - fmove( 10 , 5 ) : "ABCDEFGHIJ KILMNOPQ" - fmove( 10 , -5) : "ABCDEKILMNOPQ" - - If offset is positioned at the end of the file a section initialized - to zero will be appended to the file. If @offset is beyond the end - of the file the function will return EINVAL. - - For this function to work the file must be opened in read-write mode - (i.e. r+). -*/ - -int util_fmove(FILE *stream, long offset, long shift) { - long file_size; - // Determine size of file. - { - long init_pos = util_ftell(stream); - util_fseek(stream, 0, SEEK_END); - file_size = util_ftell(stream); - util_fseek(stream, init_pos, SEEK_SET); - } - - // Validate offset and shift input values. - if ((offset > file_size) || (offset < 0)) - return EINVAL; - - if (offset + shift < 0) - return EINVAL; - - if (shift != 0) { - int buffer_size = 1024 * 1024 * 4; /* 4MB buffer size. */ - char *buffer = (char *)util_calloc(buffer_size, sizeof *buffer); - - /* Shift > 0: We are opening up a hole in the file. */ - if (shift > 0) { - long pos = offset; - while ((pos + buffer_size) < file_size) - pos += buffer_size; - - while (pos >= offset) { - util_fmove_block(stream, pos, shift, buffer, buffer_size); - pos -= buffer_size; - } - util_fclear_region(stream, offset, shift); - } else { - /* Shift < 0: We are overwriting a part of the file internally. */ - long pos = offset; - while (pos <= file_size) { - util_fmove_block(stream, pos, shift, buffer, buffer_size); - pos += buffer_size; - } - - // Make sure the file actually shrinks. - util_ftruncate(stream, file_size + shift); - } - free(buffer); - } - return 0; -} - -/* - Windows *might* have both the symbols _access() and access(), but we prefer - the _access() symbol as that seems to be preferred by Windows. We therefor do - the #HAVE_WINDOWS__ACCESS check first. -*/ - -#ifdef HAVE_WINDOWS__ACCESS +#ifdef HAVE_WINDOWS__ACCESS bool util_access(const char *entry, int mode) { return (_access(entry, mode) == 0); @@ -2107,14 +1485,6 @@ bool util_file_readable(const char *file) { return false; } -bool util_entry_writable(const char *entry) { - stat_type buffer; - if (util_stat(entry, &buffer) == 0) - return buffer.st_mode & S_IWUSR; - else - return false; /* If stat failed - typically not existing entry - we return false. */ -} - #else // Windows: executable status based purely on extension .... @@ -2137,15 +1507,6 @@ bool util_entry_readable(const char *entry) { return false; /* If stat failed - typically not existing entry - we return false. */ } -/* If it exists on windows it is readable ... */ -bool util_entry_writable(const char *entry) { - stat_type buffer; - if (util_stat(entry, &buffer) == 0) - return true; - else - return false; /* If stat failed - typically not existing entry - we return false. */ -} - #endif static size_t util_get_path_length(const char *file) { @@ -2369,11 +1730,6 @@ bool util_same_file(const char *file1, const char *file2) { #endif } -void util_unlink_existing(const char *filename) { - if (util_file_exists(filename)) - remove(filename); -} - bool util_fmt_bit8_stream(FILE *stream) { const int min_read = 256; /* Critically small */ const double bit8set_limit = 0.00001; @@ -2452,89 +1808,6 @@ double util_file_difftime(const char *file1, const char *file2) { return difftime(t1, t2); } -time_t util_file_mtime(const char *file) { - time_t mtime = -1; - int fd = open(file, O_RDONLY); - if (fd != -1) { - stat_type f_stat; - util_fstat(fd, &f_stat); - mtime = f_stat.st_mtime; - close(fd); - } - return mtime; -} - -/** - Will check if the st_mtime (i.e. last modification) of the file is - after the time given by @t0. -*/ - -bool util_file_newer(const char *file, time_t t0) { - time_t mtime = util_file_mtime(file); - if (difftime(mtime, t0) > 0) - return true; - else - return false; -} - -/** - Will check if the st_mtime (i.e. last modification) of the file is - before the time given by @t0. -*/ - -bool util_file_older(const char *file, time_t t0) { - time_t mtime = util_file_mtime(file); - if (difftime(mtime, t0) < 0) - return true; - else - return false; -} - -bool util_before(time_t t, time_t limit) { - if (difftime(limit, t) > 0) - return true; - else - return false; -} - -bool util_after(time_t t, time_t limit) { - if (difftime(limit, t) < 0) - return true; - else - return false; -} - -/** - This function will return a pointer to the newest of the two - files. If one of the files does not exist - the other is - returned. If none of the files exist - NULL is returned. -*/ - -char *util_newest_file(const char *file1, const char *file2) { - if (util_file_exists(file1)) { - if (util_file_exists(file2)) { - /* Actual comparison of two existing files. */ - if (util_file_difftime(file1, file2) < 0) - return (char *)file1; - else - return (char *)file2; - } else - return (char *)file1; /* Only file1 exists. */ - } else { - if (util_file_exists(file2)) - return (char *)file2; /* Only file2 exists. */ - else - return NULL; /* None of the files exist. */ - } -} - -bool util_file_update_required(const char *src_file, const char *target_file) { - if (util_file_difftime(src_file, target_file) < 0) - return true; - else - return false; -} - static void __util_set_timevalues_utc(time_t t, int *sec, int *min, int *hour, int *mday, int *month, int *year) { struct tm ts; @@ -2591,68 +1864,6 @@ bool util_is_first_day_in_month_utc(time_t t) { return false; } -/* - Expects date in the order YYYY-MM-DD. -*/ - -bool util_sscanf_isodate(const char *date_token, time_t *t) { - int day, month, year; - int year_digit1, year_digit2, year_digit3, year_digit4; - - if (date_token && - sscanf(date_token, "%1d%1d%1d%1d-%d-%d", &year_digit1, &year_digit2, - &year_digit3, &year_digit4, &month, &day) == 6 && - sscanf(date_token, "%d-%d-%d", &year, &month, &day) == 3) - return util_make_datetime_utc__(0, 0, 0, day, month, year, false, t); - - if (t) - *t = -1; - return false; -} - -/** - If the parsing fails the time_t pointer is set to -1; -*/ -bool util_sscanf_date_utc(const char *date_token, time_t *t) { - int day, month, year; - char sep1, sep2; - - if (date_token && sscanf(date_token, "%d%c%d%c%d", &day, &sep1, &month, - &sep2, &year) == 5) { - if (t) - *t = util_make_date_utc(day, month, year); - return true; - } else { - if (t) - *t = -1; - return false; - } -} - -bool util_sscanf_percent(const char *percent_token, double *value) { - if (!percent_token) - return false; - - char *percent_ptr; - double double_val = strtod(percent_token, &percent_ptr); - - if (0 == strcmp(percent_ptr, "%")) { - *value = double_val; - return true; - } else - return false; -} - -bool util_fscanf_date_utc(FILE *stream, time_t *t) { - int init_pos = util_ftell(stream); - char *date_token = util_fscanf_alloc_token(stream); - bool return_value = util_sscanf_date_utc(date_token, t); - if (!return_value) - util_fseek(stream, init_pos, SEEK_SET); - free(date_token); - return return_value; -} - /* The date format is HARD assumed to be @@ -2663,18 +1874,6 @@ bool util_fscanf_date_utc(FILE *stream, time_t *t) { */ -char *util_alloc_date_string_utc(time_t t) { - int mday, year, month; - - util_set_datetime_values_utc(t, NULL, NULL, NULL, &mday, &month, &year); - return util_alloc_sprintf("%02d/%02d/%4d", mday, month, year); -} - -char *util_alloc_date_stamp_utc() { - time_t now = time(NULL); - return util_alloc_date_string_utc(now); -} - void util_inplace_forward_seconds_utc(time_t *t, double seconds) { (*t) += seconds; } @@ -2799,12 +1998,6 @@ static bool util_make_datetime_utc__(int sec, int min, int hour, int mday, return valid; } -bool util_make_datetime_utc_validated(int sec, int min, int hour, int mday, - int month, int year, time_t *t) { - return util_make_datetime_utc__(sec, min, hour, mday, month, year, false, - t); -} - time_t util_make_datetime_utc(int sec, int min, int hour, int mday, int month, int year) { time_t t; @@ -2856,15 +2049,18 @@ char *util_alloc_strip_copy(const char *src) { return target; } -char **util_alloc_stringlist_copy(char const *const *src, int len) { - if (src != NULL) { - int i; - char **copy = (char **)util_calloc(len, sizeof *copy); - for (i = 0; i < len; i++) - copy[i] = util_alloc_string_copy(src[i]); - return copy; - } else - return NULL; +/** + This is nearly the same as util_stringlist_append_copy(), but for + this case only a refernce to the new string is appended. + + Slightly more dangerous to use ... +*/ +static char **util_stringlist_append_ref(char **string_list, int size, + const char *append_string) { + string_list = + (char **)util_realloc(string_list, (size + 1) * sizeof *string_list); + string_list[size] = (char *)append_string; + return string_list; } /** @@ -2890,31 +2086,6 @@ char **util_stringlist_append_copy(char **string_list, int size, util_alloc_string_copy(append_string)); } -/** - This is nearly the same as util_stringlist_append_copy(), but for - this case only a refernce to the new string is appended. - - Slightly more dangerous to use ... -*/ - -char **util_stringlist_append_ref(char **string_list, int size, - const char *append_string) { - string_list = - (char **)util_realloc(string_list, (size + 1) * sizeof *string_list); - string_list[size] = (char *)append_string; - return string_list; -} - -char *util_alloc_string_copy(const char *src) { - if (src != NULL) { - int byte_size = (strlen(src) + 1) * sizeof *src; - char *copy = (char *)util_calloc(byte_size, sizeof *copy); - memcpy(copy, src, byte_size); - return copy; - } else - return NULL; -} - char *util_realloc_string_copy(char *old_string, const char *src) { if (src != NULL) { /* Realloc can either (1) reuse or (2) allocate new memory (and free the @@ -2938,109 +2109,53 @@ char *util_realloc_string_copy(char *old_string, const char *src) { } } -char *util_realloc_substring_copy(char *old_string, const char *src, int len_) { - size_t len = len_; - if (src != NULL) { - size_t str_len; - char *copy; - if (strlen(src) < len) - str_len = strlen(src); - else - str_len = len; - - copy = (char *)realloc(old_string, (str_len + 1) * sizeof *copy); - strncpy(copy, src, str_len); - copy[str_len] = '\0'; - - return copy; - } else - return NULL; -} - /** - This function checks whether a string matches a pattern with - wildcard(s). The pattern can consist of plain string parts (which - must match verbatim), and an arbitrary number of '*' which will - match an arbitrary number (including zero) of arbitrary characters. - - Examples: - --------- - - util_string_match("Bjarne" , "Bjarne") ==> True - - util_string_match("Bjarne" , "jarn") ==> False - - util_string_match("Bjarne" , "*jarn*") ==> True - - util_string_match("Bjarne" , "B*e") ==> True - - util_string_match("Bjarne" , "B*n") ==> False - - util_string_match("Bjarne" , "*") ==> True - - util_string_match("Bjarne" , "B***jarne") ==> True - - util_string_match("Bjarne" , "B*r*e") ==> True - + sep_set = string with various characters, i.e. " \t" to split on. */ -bool util_string_match(const char *string, const char *pattern) { - const char wildcard = '*'; - const char *wildcard_st = "*"; +static void util_split_string(const char *line, const char *sep_set, + int *_tokens, char ***_token_list) { + int offset; + int tokens, token, token_length; + char **token_list; - if (strcmp(wildcard_st, pattern) == 0) - return true; - else { - bool match = true; - char **sub_pattern; - int num_patterns; - const char *string_ptr; - util_split_string(pattern, wildcard_st, &num_patterns, &sub_pattern); - - if (pattern[0] == '*') - string_ptr = strstr(string, sub_pattern[0]); - else - string_ptr = - (strncmp(string, sub_pattern[0], strlen(sub_pattern[0])) == 0) - ? (char *)string - : NULL; - - if (string_ptr != NULL) { - /* Inital part matched */ - int i; - string_ptr += strlen(sub_pattern[0]); - for (i = 1; i < num_patterns; i++) { - const char *match_ptr = strstr(string_ptr, sub_pattern[i]); - if (match_ptr != NULL) - string_ptr = match_ptr + strlen(sub_pattern[i]); - else { - match = false; - break; - } - } + offset = strspn(line, sep_set); + tokens = 0; + do { + /* + if (line[offset] == '\"') { + seek for terminating ". + } + */ + token_length = strcspn(&line[offset], sep_set); + if (token_length > 0) + tokens++; - /* - We have exhausted the complete pattern - matching all the way. - Does it match at the end? - */ - if (match) { - if (strlen(string_ptr) > 0) { - /* - There is more left at the end of the string; if the pattern - ends with '*' that is OK, otherwise the match result is - FALSE. - */ - if (pattern[(strlen(pattern) - 1)] != wildcard) - match = false; - } - } + offset += token_length; + offset += strspn(&line[offset], sep_set); + } while (line[offset] != '\0'); - } else - match = false; + if (tokens > 0) { + token_list = (char **)util_calloc(tokens, sizeof *token_list); + offset = strspn(line, sep_set); + token = 0; + do { + token_length = strcspn(&line[offset], sep_set); + if (token_length > 0) { + token_list[token] = + util_alloc_substring_copy(line, offset, token_length); + token++; + } else + token_list[token] = NULL; - util_free_stringlist(sub_pattern, num_patterns); - return match; - } + offset += token_length; + offset += strspn(&line[offset], sep_set); + } while (line[offset] != '\0'); + } else + token_list = NULL; + + *_tokens = tokens; + *_token_list = token_list; } /** @@ -3066,62 +2181,6 @@ void util_free_stringlist(char **list, int N) { } } -char *util_strstr_int_format(const char *string) { - // the function itself is essentially removing the const from *string - // so do it explicitly here to make C++ happy - char *percent_ptr = strchr((char *)string, '%'); - - if (percent_ptr) { - - percent_ptr++; - if (percent_ptr[0] == 'd') - return percent_ptr++; - else { - if (percent_ptr[0] == '0') { - - while (isdigit(percent_ptr[0])) - percent_ptr++; - - if (percent_ptr[0] == 'd') - return percent_ptr++; - else - return NULL; - } - return NULL; - } - } - return percent_ptr; -} - -int util_int_format_count(const char *string) { - int count = 0; - const char *str = util_strstr_int_format(string); - while (str) { - count++; - str = util_strstr_int_format(str); - } - return count; -} - -/** - Will free a list of strings where the last element is NULL. Will - go completely canacas if the list is not NULL terminated. -*/ - -void util_free_NULL_terminated_stringlist(char **string_list) { - if (string_list != NULL) { - int i = 0; - while (true) { - if (string_list[i] == NULL) - break; - else - free(string_list[i]); - i++; - } - free(string_list); - } -} - /** This function will reallocate the string s1 to become the sum of s1 and s2. If s1 == NULL it will just return a copy of s2. @@ -3144,128 +2203,6 @@ char *util_strcat_realloc(char *s1, const char *s2) { return s1; } -char *util_alloc_string_sum(const char **string_list, int N) { - int i, len; - char *buffer; - len = 0; - for (i = 0; i < N; i++) { - if (string_list[i] != NULL) - len += strlen(string_list[i]); - } - buffer = (char *)util_calloc(len + 1, sizeof *buffer); - buffer[0] = '\0'; - for (i = 0; i < N; i++) { - if (string_list[i] != NULL) - strcat(buffer, string_list[i]); - } - return buffer; -} - -/** - Allocates a new string consisting of all the elements in item_list, - joined together with sep as separator. Elements in item_list can be - NULL, this will be replaced with the empty string. -*/ - -char *util_alloc_joined_string(const char **item_list, int len, - const char *sep) { - if (len <= 0) - return NULL; - else { - char *joined_string; - int sep_length = strlen(sep); - int total_length = 0; - int eff_len = 0; - int i; - for (i = 0; i < len; i++) - if (item_list[i] != NULL) { - total_length += strlen(item_list[i]); - eff_len++; - } - - if (eff_len > 0) { - total_length += (eff_len - 1) * sep_length + 1; - joined_string = - (char *)util_calloc(total_length, sizeof *joined_string); - joined_string[0] = '\0'; - for (i = 0; i < len; i++) { - if (item_list[i] != NULL) { - if (i > 0) - strcat(joined_string, sep); - strcat(joined_string, item_list[i]); - } - } - return joined_string; - } else - return NULL; - } -} - -/** - New string is allocated by joining the elements in item_list, with - "\n" character as separator; an extra "\n" is also added at the end - of the list. -*/ -char *util_alloc_multiline_string(const char **item_list, int len) { - char *multiline_string = - util_alloc_joined_string(item_list, len, UTIL_NEWLINE_STRING); - multiline_string = (char *)util_realloc( - multiline_string, - (strlen(multiline_string) + strlen(UTIL_NEWLINE_STRING) + 1) * - sizeof *multiline_string); - strcat(multiline_string, UTIL_NEWLINE_STRING); - return multiline_string; -} - -/** - sep_set = string with various characters, i.e. " \t" to split on. -*/ - -void util_split_string(const char *line, const char *sep_set, int *_tokens, - char ***_token_list) { - int offset; - int tokens, token, token_length; - char **token_list; - - offset = strspn(line, sep_set); - tokens = 0; - do { - /* - if (line[offset] == '\"') { - seek for terminating ". - } - */ - token_length = strcspn(&line[offset], sep_set); - if (token_length > 0) - tokens++; - - offset += token_length; - offset += strspn(&line[offset], sep_set); - } while (line[offset] != '\0'); - - if (tokens > 0) { - token_list = (char **)util_calloc(tokens, sizeof *token_list); - offset = strspn(line, sep_set); - token = 0; - do { - token_length = strcspn(&line[offset], sep_set); - if (token_length > 0) { - token_list[token] = - util_alloc_substring_copy(line, offset, token_length); - token++; - } else - token_list[token] = NULL; - - offset += token_length; - offset += strspn(&line[offset], sep_set); - } while (line[offset] != '\0'); - } else - token_list = NULL; - - *_tokens = tokens; - *_token_list = token_list; -} - /** This function will split the input string in two parts, it will split on occurence of one or several of the characters in @@ -3388,81 +2325,6 @@ void util_binary_split_string(const char *__src, const char *sep_set, *__second_part = second_part; } -void util_binary_split_string_from_max_length(const char *__src, - const char *sep_set, - int max_length, - char **__first_part, - char **__second_part) { - char *first_part = NULL; - char *second_part = NULL; - if (__src != NULL) { - char *src; - size_t pos; - /* Removing leading separators. */ - pos = 0; - while ((pos < strlen(__src)) && (strchr(sep_set, __src[pos]) != NULL)) - pos += 1; - if (pos == strlen(__src)) /* The string consisted ONLY of separators. */ - src = NULL; - else - src = util_alloc_string_copy(&__src[pos]); - - /*Remove trailing separators. */ - pos = strlen(__src) - 1; - while ((pos >= 0) && (strchr(sep_set, __src[pos]) != NULL)) - pos -= 1; - if (pos < 0) - src = NULL; - else - src = util_alloc_substring_copy(__src, 0, pos + 1); - - /* - OK - we have removed all leading (or trailing) separators, and we have - a valid string which we can continue with. - */ - if (src != NULL) { - int pos; - int start_pos, delta, end_pos; - start_pos = max_length; - delta = -1; - end_pos = -1; - pos = start_pos; - while ((pos != end_pos) && (strchr(sep_set, src[pos]) == NULL)) - pos += delta; - /* - OK - now we have either iterated through the whole string - or - we hav found a character in the sep_set. - */ - if (pos == end_pos) { - /* There was no split. */ - first_part = util_alloc_string_copy(src); - second_part = NULL; - } else { - int sep_start = 0; - int sep_end = 0; - sep_end = pos; - /* Iterate through the separation string - can be e.g. many " " */ - while ((pos != end_pos) && (strchr(sep_set, src[pos]) != NULL)) - pos += delta; - - sep_start = pos; - if (sep_start == end_pos) { - // ":String" => (NULL , "String") - first_part = NULL; - second_part = util_alloc_string_copy(&src[sep_end + 1]); - } else { - first_part = - util_alloc_substring_copy(src, 0, sep_start + 1); - second_part = util_alloc_string_copy(&src[sep_end + 1]); - } - } - free(src); - } - } - *__first_part = first_part; - *__second_part = second_part; -} - /** The function will, in-place, update all occurencese: expr->subs. The return value is the number of substitutions which have been @@ -3522,78 +2384,6 @@ int util_string_replace_inplace(char **_buffer, const char *expr, return util_string_replace_inplace__(_buffer, expr, subs); } -/** - This allocates a copy of buff_org where occurences of the string expr are replaced with subs. -*/ -char *util_string_replace_alloc(const char *buff_org, const char *expr, - const char *subs) { - int buffer_size = strlen(buff_org) * 2; - char *new_buffer = (char *)util_calloc(buffer_size, sizeof *new_buffer); - memcpy(new_buffer, buff_org, strlen(buff_org) + 1); - util_string_replace_inplace__(&new_buffer, expr, subs); - - { - int size = strlen(new_buffer); - new_buffer = - (char *)util_realloc(new_buffer, (size + 1) * sizeof *new_buffer); - } - - return new_buffer; -} - -/** - This allocates a copy of buff_org where occurences of expr[i] are replaced with subs[i] for i=1,..,num_expr. -*/ -char *util_string_replacen_alloc(const char *buff_org, int num_expr, - const char **expr, const char **subs) { - int buffer_size = strlen(buff_org) * 2; - char *new_buffer = (char *)util_calloc(buffer_size, sizeof *new_buffer); - memcpy(new_buffer, buff_org, strlen(buff_org) + 1); - { - int i; - for (i = 0; i < num_expr; i++) - util_string_replace_inplace__(&new_buffer, expr[i], subs[i]); - } - - { - int size = strlen(new_buffer); - new_buffer = - (char *)util_realloc(new_buffer, (size + 1) * sizeof *new_buffer); - } - - return new_buffer; -} - -/** - This will alloc a copy of buff_org were char's in the last strings are removed. -*/ -char *util_string_strip_chars_alloc(const char *buff_org, const char *chars) { - int len_org = strlen(buff_org); - int pos_org = 0; - int pos_new = 0; - - char *buff_new = (char *)util_calloc((len_org + 1), sizeof *buff_new); - - while (pos_org < len_org) { - int pos_off = strcspn(buff_org + pos_org, chars); - if (pos_off > 0) { - memmove(buff_new + pos_new, buff_org + pos_org, - pos_off * sizeof *buff_new); - pos_org += pos_off; - pos_new += pos_off; - } - - pos_off = strspn(buff_org + pos_org, chars); - if (pos_off > 0) { - pos_org += pos_off; - } - } - buff_new[pos_new + 1] = '\0'; - buff_new = (char *)util_realloc(buff_new, (pos_new + 1) * sizeof buff_new); - - return buff_new; -} - void util_float_to_double(double *double_ptr, const float *float_ptr, int size) { int i; @@ -3647,45 +2437,12 @@ char *util_fread_alloc_string(FILE *stream) { return s; } -char *util_fread_realloc_string(char *old_s, FILE *stream) { - int len; - char *s = NULL; - util_fread(&len, sizeof len, 1, stream, __func__); - if (len > 0) { - s = (char *)util_realloc(old_s, len + 1); - util_fread(s, 1, len + 1, stream, __func__); - } else if (len == -1) /* Magic length for "" */ { - s = (char *)util_realloc(s, 1); - util_fread(s, 1, 1, stream, __func__); - } - return s; -} - -void util_fskip_string(FILE *stream) { - int len; - util_fread(&len, sizeof len, 1, stream, __func__); - if (len == 0) - return; /* The user has written NULL with util_fwrite_string(). */ - else if (len == -1) - util_fseek(stream, 1, - SEEK_CUR); /* Magic length for "" - skip the '\0' */ - else - util_fseek(stream, len + 1, - SEEK_CUR); /* Skip the data in a normal string. */ -} - void util_fwrite_offset(offset_type value, FILE *stream) { UTIL_FWRITE_SCALAR(value, stream); } -void util_fwrite_bool(bool value, FILE *stream) { - UTIL_FWRITE_SCALAR(value, stream); -} void util_fwrite_int(int value, FILE *stream) { UTIL_FWRITE_SCALAR(value, stream); } -void util_fwrite_time_t(time_t value, FILE *stream) { - UTIL_FWRITE_SCALAR(value, stream); -} void util_fwrite_size_t(size_t value, FILE *stream) { UTIL_FWRITE_SCALAR(value, stream); } @@ -3700,64 +2457,17 @@ void util_fwrite_int_vector(const int *value, int size, FILE *stream, const char *caller) { util_fwrite(value, sizeof *value, size, stream, caller); } -void util_fwrite_double_vector(const double *value, int size, FILE *stream, - const char *caller) { - util_fwrite(value, sizeof *value, size, stream, caller); -} void util_fwrite_char_vector(const char *value, int size, FILE *stream, const char *caller) { util_fwrite(value, sizeof *value, size, stream, caller); } -void util_fread_char_vector(char *ptr, int size, FILE *stream, - const char *caller) { - util_fread(ptr, sizeof *ptr, size, stream, caller); -} - -double util_fread_double(FILE *stream) { - double file_value; - UTIL_FREAD_SCALAR(file_value, stream); - return file_value; -} - int util_fread_int(FILE *stream) { int file_value; UTIL_FREAD_SCALAR(file_value, stream); return file_value; } -time_t util_fread_time_t(FILE *stream) { - time_t file_value; - UTIL_FREAD_SCALAR(file_value, stream); - return file_value; -} - -long util_fread_long(FILE *stream) { - long file_value; - UTIL_FREAD_SCALAR(file_value, stream); - return file_value; -} - -bool util_fread_bool(FILE *stream) { - bool file_value; - UTIL_FREAD_SCALAR(file_value, stream); - return file_value; -} - -void util_fskip_int(FILE *stream) { util_fseek(stream, sizeof(int), SEEK_CUR); } - -void util_fskip_long(FILE *stream) { - util_fseek(stream, sizeof(long), SEEK_CUR); -} - -void util_fskip_bool(FILE *stream) { - util_fseek(stream, sizeof(bool), SEEK_CUR); -} - -time_t util_time_t_min(time_t a, time_t b) { return (a < b) ? a : b; } - -time_t util_time_t_max(time_t a, time_t b) { return (a > b) ? a : b; } - size_t util_size_t_min(size_t a, size_t b) { return (a < b) ? a : b; } size_t util_size_t_max(size_t a, size_t b) { return (a > b) ? a : b; } @@ -3802,61 +2512,6 @@ void util_clamp_double(double *value, double limit1, double limit2) { *value = util_double_min(*value, max); } -/** - Scans through a vector of doubles, and finds min and max - values. They are returned by reference. -*/ - -void util_double_vector_max_min(int N, const double *vector, double *_max, - double *_min) { - double min = 1e100; /* How should this be done ??? */ - double max = -1e100; - int i; - for (i = 0; i < N; i++) { - if (vector[i] > max) - max = vector[i]; - - /* Can not have else here - because same item might succed on both tests. */ - - if (vector[i] < min) - min = vector[i]; - } - *_max = max; - *_min = min; -} - -double util_double_vector_mean(int N, const double *vector) { - double mean = 0.0; - int i; - for (i = 0; i < N; i++) - mean = mean + vector[i]; - - return mean / N; -} - -double util_double_vector_stddev(int N, const double *vector) { - if (N <= 1) - return 0.0; - { - double stddev = 0.0; - double mean = util_double_vector_mean(N, vector); - double *vector_shifted = - (double *)util_calloc(N, sizeof *vector_shifted); - - { - int i; - for (i = 0; i < N; i++) - vector_shifted[i] = vector[i] - mean; - - for (i = 0; i < N; i++) - stddev = stddev + vector_shifted[i] * vector_shifted[i]; - } - free(vector_shifted); - - return sqrt(stddev / (N - 1)); - } -} - FILE *util_fopen__(const char *filename, const char *mode) { return fopen(filename, mode); } @@ -4050,37 +2705,6 @@ char *util_alloc_sprintf(const char *fmt, ...) { return s; } -/* -char * util_realloc_sprintf(char * s , const char * fmt , ...) { - va_list ap; - va_start(ap , fmt); - printf("1: %s s:%s / p:%p \n",__func__ , s , s ); - { - int length; - va_list tmp_va; - va_copy(tmp_va , ap); - length = vsnprintf(s , 0 , fmt , tmp_va); - s = util_realloc(s , length + 1 , __func__); - printf("2: %s s:%s / p:%p length:%d\n",__func__ , s , s ); - } - vsprintf(s , fmt , ap); - va_end(ap); - return s; -} -*/ - -char *util_realloc_sprintf(char *s, const char *fmt, ...) { - char *new_s; - va_list ap; - va_start(ap, fmt); - - new_s = util_alloc_sprintf_va(fmt, ap); - free(s); - - va_end(ap); - return new_s; -} - /** This function is intended to be installed as a signal handler, so we can get a traceback from signals like SIGSEGV. @@ -4450,18 +3074,6 @@ char *util_alloc_filename(const char *path, const char *basename, return file; } -char *util_realloc_filename(char *filename, const char *path, - const char *basename, const char *extension) { - free(filename); - return util_alloc_filename(path, basename, extension); -} - -char *util_split_alloc_dirname(const char *input_path) { - char *path; - util_alloc_file_components(input_path, &path, NULL, NULL); - return path; -} - char *util_split_alloc_filename(const char *input_path) { char *filename = NULL; { @@ -4547,20 +3159,3 @@ int util_chdir(const char *path) { return chdir(path); } int util_chdir(const char *path) { return _chdir(path); } #endif - -bool util_chdir_file(const char *filename) { - if (!util_is_file(filename)) - return false; - - bool chdir_OK = false; - char *path; - char *abs_path; - util_alloc_file_components(filename, &path, NULL, NULL); - abs_path = util_alloc_abs_path(path); - if (util_is_directory(abs_path)) - chdir_OK = (0 == util_chdir(abs_path)); - - free(abs_path); - free(path); - return chdir_OK; -}