From: Kevin Day Date: Thu, 12 May 2022 02:42:44 +0000 (-0500) Subject: Update: Add missing function f_path_is_absolute() and fix existing f_path_is_*()... X-Git-Tag: 0.5.10~151 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=e06ddcc2c763b9d941e373aea543dee130cf77e3;p=fll 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. --- 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.