From e06ddcc2c763b9d941e373aea543dee130cf77e3 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Wed, 11 May 2022 21:42:44 -0500 Subject: [PATCH] Update: Add missing function f_path_is_absolute() and fix existing f_path_is_*() functions. The f_path_is_absolute() function, being the compliment of f_path_is_relative() is now added. I noticed multiple problems when looking at this code. - The f_path_is_relative() and f_path_is_relative_current() functions are not checking that the max length is reached before comparing. - the f_path_is_relative_current() is not incrementing the counter when attempting to check for the next character resulting in invalid results. The f_path project clearly needs unit testing. I intend to write unit tests and fix problems found before the next stable release is made. --- level_0/f_path/c/path.c | 44 ++++++++++++++++++++++++++++++-------------- level_0/f_path/c/path.h | 21 +++++++++++++++++++++ 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/level_0/f_path/c/path.c b/level_0/f_path/c/path.c index 84d9524..23932f3 100644 --- a/level_0/f_path/c/path.c +++ b/level_0/f_path/c/path.c @@ -173,6 +173,26 @@ extern "C" { } #endif // _di_f_path_is_ +#ifndef _di_f_path_is_absolute_ + f_status_t f_path_is_absolute(const f_string_static_t path) { + + if (!path.string || !path.used) { + return F_data_not; + } + + f_array_length_t i = 0; + + for (; i < path.used; ++i) { + if (path.string[i]) break; + } // for + + if (i == path.used) return F_data_not; + if (path.string[i] == f_path_separator_s.string[0]) return F_true; + + return F_false; + } +#endif // _di_f_path_is_absolute_ + #ifndef _di_f_path_is_relative_ f_status_t f_path_is_relative(const f_string_static_t path) { @@ -186,9 +206,8 @@ extern "C" { if (path.string[i]) break; } // for - if (path.string[i] == f_path_separator_s.string[0]) { - return F_false; - } + if (i == path.used) return F_data_not; + if (path.string[i] == f_path_separator_s.string[0]) return F_false; return F_true; } @@ -207,27 +226,24 @@ extern "C" { if (path.string[i]) break; } // for - if (path.string[i] == f_path_separator_s.string[0]) { - return F_false; - } + if (i == path.used) return F_data_not; + if (path.string[i] == f_path_separator_s.string[0]) return F_false; if (path.string[i] == f_path_separator_current_s.string[0]) { - for (; i < path.used; ++i) { + for (++i; i < path.used; ++i) { if (path.string[i]) break; } // for - if (path.string[i] == f_path_separator_s.string[0]) { - return F_true; - } + if (i == path.used) return F_false; + if (path.string[i] == f_path_separator_s.string[0]) return F_true; if (path.string[i] == f_path_separator_current_s.string[0]) { - for (; i < path.used; ++i) { + for (++i; i < path.used; ++i) { if (path.string[i]) break; } // for - if (path.string[i] == f_path_separator_s.string[0]) { - return F_true; - } + if (i == path.used) return F_false; + if (path.string[i] == f_path_separator_s.string[0]) return F_true; } } diff --git a/level_0/f_path/c/path.h b/level_0/f_path/c/path.h index 39ebd8f..5b69b31 100644 --- a/level_0/f_path/c/path.h +++ b/level_0/f_path/c/path.h @@ -169,6 +169,27 @@ extern "C" { #endif // _di_f_path_is_ /** + * Identify whether or not a string represents an absolute path string. + * + * This does not check to see if the path exists or not. + * This does not stop on NULL (NULL characters are ignored). + * + * A string with path parts that start with '/' is an absolute path. + * That is to say "/my_file.txt" is an absolute path string. + * + * @param path + * The string that may or may not represent an absolute path. + * + * @return + * F_true if the string is an absolute path string. + * F_false if the string is not an absolute path string. + * F_data_not if the string has a length of 0 or the string is NULL. + */ +#ifndef _di_f_path_is_absolute_ + extern f_status_t f_path_is_absolute(const f_string_static_t path); +#endif // _di_f_path_is_absolute_ + +/** * Identify whether or not a string represents a relative path string. * * This does not check to see if the path exists or not. -- 1.8.3.1