From f1c9b198bba52d19a840efc7a77a9752d711ee57 Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Sat, 2 Mar 2024 23:49:35 +0100 Subject: [PATCH] Promote fl_strlcpy to --- FL/fl_string_functions.h | 4 ++++ documentation/src/editor.dox | 14 +++++++------- src/fl_string_functions.cxx | 32 ++++++++++++++++++++++++++++++++ src/flstring.c | 32 -------------------------------- src/flstring.h | 4 +++- test/editor.cxx | 14 +++++++------- 6 files changed, 53 insertions(+), 47 deletions(-) diff --git a/FL/fl_string_functions.h b/FL/fl_string_functions.h index 16e7a5ada..b88dc4973 100644 --- a/FL/fl_string_functions.h +++ b/FL/fl_string_functions.h @@ -28,6 +28,8 @@ extern "C" { #endif +#include // size_t + /** \defgroup fl_string String handling functions String handling functions declared in @{ @@ -35,6 +37,8 @@ extern "C" { FL_EXPORT char* fl_strdup(const char *s); +FL_EXPORT size_t fl_strlcpy(char *, const char *, size_t); + /** @} */ #ifdef __cplusplus diff --git a/documentation/src/editor.dox b/documentation/src/editor.dox index 13b23b58d..299af9ff6 100644 --- a/documentation/src/editor.dox +++ b/documentation/src/editor.dox @@ -52,7 +52,6 @@ marked in the source code as `TUTORIAL_CHAPTER = 1`. \code #include #include -#include Fl_Double_Window *app_window = NULL; @@ -113,6 +112,7 @@ a buffer for the current filename. #include #include #include +#include Fl_Menu_Bar *app_menu_bar = NULL; bool text_changed = false; @@ -161,7 +161,7 @@ NULL, the window title will revert to "FLTK Editor". \code void set_filename(const char *new_filename) { if (new_filename) { - strlcpy(app_filename, new_filename, FL_PATH_MAX); + fl_strlcpy(app_filename, new_filename, FL_PATH_MAX); } else { app_filename[0] = 0; } @@ -366,7 +366,7 @@ little chunk of code will separate the file name from the path before we call // insert before `if (file_chooser.show()...` if (app_filename[0]) { char temp_filename[FL_PATH_MAX]; - strlcpy(temp_filename, app_filename, FL_PATH_MAX); + fl_strlcpy(temp_filename, app_filename, FL_PATH_MAX); const char *name = fl_filename_name(temp_filename); if (name) { file_chooser.preset_file(name); @@ -479,7 +479,7 @@ name: ... if (app_filename[0]) { char temp_filename[FL_PATH_MAX]; - strlcpy(temp_filename, app_filename, FL_PATH_MAX); + fl_strlcpy(temp_filename, app_filename, FL_PATH_MAX); const char *name = fl_filename_name(temp_filename); if (name) { file_chooser.preset_file(name); @@ -648,7 +648,7 @@ char last_find_text[1024] = ""; void menu_find_callback(Fl_Widget*, void* v) { const char *find_text = fl_input("Find in text:", last_find_text); if (find_text) { - strlcpy(last_find_text, find_text, sizeof(last_find_text)); + fl_strlcpy(last_find_text, find_text, sizeof(last_find_text)); find_next(find_text); } } @@ -812,8 +812,8 @@ function: \code void Replace_Dialog::find_next_callback(Fl_Widget*, void* my_dialog) { Replace_Dialog *dlg = static_cast(my_dialog); - strlcpy(last_find_text, dlg->find_text_input->value(), sizeof(last_find_text)); - strlcpy(last_replace_text, dlg->replace_text_input->value(), sizeof(last_replace_text)); + fl_strlcpy(last_find_text, dlg->find_text_input->value(), sizeof(last_find_text)); + fl_strlcpy(last_replace_text, dlg->replace_text_input->value(), sizeof(last_replace_text)); if (last_find_text[0]) find_next(last_find_text); } diff --git a/src/fl_string_functions.cxx b/src/fl_string_functions.cxx index 6b7fcead7..2e5e5d5d4 100644 --- a/src/fl_string_functions.cxx +++ b/src/fl_string_functions.cxx @@ -32,3 +32,35 @@ char *fl_strdup(const char *s) { return Fl::system_driver()->strdup(s); } + +/* + * 'fl_strlcpy()' - Safely copy two strings. + */ +size_t /* O - Length of string */ +fl_strlcpy(char *dst, /* O - Destination string */ + const char *src, /* I - Source string */ + size_t size) { /* I - Size of destination string buffer */ + size_t srclen; /* Length of source string */ + + + /* + * Figure out how much room is needed... + */ + + size --; + + srclen = strlen(src); + + /* + * Copy the appropriate amount... + */ + + if (srclen > size) srclen = size; + + memcpy(dst, src, srclen); + dst[srclen] = '\0'; + + return (srclen); +} + + diff --git a/src/flstring.c b/src/flstring.c index 6298a6d70..fab89feed 100644 --- a/src/flstring.c +++ b/src/flstring.c @@ -56,38 +56,6 @@ fl_strlcat(char *dst, /* O - Destination string */ return (dstlen + srclen); } - -/* - * 'fl_strlcpy()' - Safely copy two strings. - */ - -size_t /* O - Length of string */ -fl_strlcpy(char *dst, /* O - Destination string */ - const char *src, /* I - Source string */ - size_t size) { /* I - Size of destination string buffer */ - size_t srclen; /* Length of source string */ - - - /* - * Figure out how much room is needed... - */ - - size --; - - srclen = strlen(src); - - /* - * Copy the appropriate amount... - */ - - if (srclen > size) srclen = size; - - memcpy(dst, src, srclen); - dst[srclen] = '\0'; - - return (srclen); -} - #define C_RANGE(c,l,r) ( (c) >= (l) && (c) <= (r) ) /** diff --git a/src/flstring.h b/src/flstring.h index d902c0a9f..e4c7ea491 100644 --- a/src/flstring.h +++ b/src/flstring.h @@ -35,6 +35,7 @@ # include # endif /* HAVE_STRINGS_H */ # include +# include /* * Apparently Unixware defines "index" to strchr (!) rather than @@ -81,7 +82,8 @@ FL_EXPORT extern size_t fl_strlcat(char *, const char *, size_t); # define strlcat fl_strlcat # endif /* !HAVE_STRLCAT */ -FL_EXPORT extern size_t fl_strlcpy(char *, const char *, size_t); +// promoted to +//FL_EXPORT extern size_t fl_strlcpy(char *, const char *, size_t); # ifndef HAVE_STRLCPY # define strlcpy fl_strlcpy # endif /* !HAVE_STRLCPY */ diff --git a/test/editor.cxx b/test/editor.cxx index a6ab7734c..360e3eed7 100644 --- a/test/editor.cxx +++ b/test/editor.cxx @@ -26,7 +26,6 @@ #include #include -#include Fl_Double_Window *app_window = NULL; @@ -50,6 +49,7 @@ int main (int argc, char **argv) { #include #include #include +#include Fl_Menu_Bar *app_menu_bar = NULL; bool text_changed = false; @@ -81,7 +81,7 @@ void set_changed(bool v) { void set_filename(const char *new_filename) { if (new_filename) { - strlcpy(app_filename, new_filename, FL_PATH_MAX); + fl_strlcpy(app_filename, new_filename, FL_PATH_MAX); } else { app_filename[0] = 0; } @@ -188,7 +188,7 @@ void menu_save_as_callback(Fl_Widget*, void*) { file_chooser.type(Fl_Native_File_Chooser::BROWSE_SAVE_FILE); if (app_filename[0]) { char temp_filename[FL_PATH_MAX]; - strlcpy(temp_filename, app_filename, FL_PATH_MAX); + fl_strlcpy(temp_filename, app_filename, FL_PATH_MAX); const char *name = fl_filename_name(temp_filename); if (name) { file_chooser.preset_file(name); @@ -263,7 +263,7 @@ void menu_open_callback(Fl_Widget*, void*) { file_chooser.type(Fl_Native_File_Chooser::BROWSE_FILE); if (app_filename[0]) { char temp_filename[FL_PATH_MAX]; - strlcpy(temp_filename, app_filename, FL_PATH_MAX); + fl_strlcpy(temp_filename, app_filename, FL_PATH_MAX); const char *name = fl_filename_name(temp_filename); if (name) { file_chooser.preset_file(name); @@ -399,7 +399,7 @@ bool find_next(const char *needle) { void menu_find_callback(Fl_Widget*, void* v) { const char *find_text = fl_input("Find in text:", last_find_text); if (find_text) { - strlcpy(last_find_text, find_text, sizeof(last_find_text)); + fl_strlcpy(last_find_text, find_text, sizeof(last_find_text)); find_next(find_text); } } @@ -497,8 +497,8 @@ void Replace_Dialog::show() { void Replace_Dialog::find_next_callback(Fl_Widget*, void* my_dialog) { Replace_Dialog *dlg = static_cast(my_dialog); - strlcpy(last_find_text, dlg->find_text_input->value(), sizeof(last_find_text)); - strlcpy(last_replace_text, dlg->replace_text_input->value(), sizeof(last_replace_text)); + fl_strlcpy(last_find_text, dlg->find_text_input->value(), sizeof(last_find_text)); + fl_strlcpy(last_replace_text, dlg->replace_text_input->value(), sizeof(last_replace_text)); if (last_find_text[0]) find_next(last_find_text); }