Promote fl_strlcpy to <FL/fl_string_functions.h>

This commit is contained in:
Matthias Melcher 2024-03-02 23:49:35 +01:00
parent 4ccadff4b9
commit f1c9b198bb
6 changed files with 53 additions and 47 deletions

View File

@ -28,6 +28,8 @@
extern "C" {
#endif
#include <stddef.h> // size_t
/** \defgroup fl_string String handling functions
String handling functions declared in <FL/fl_string_functions.h>
@{
@ -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

View File

@ -52,7 +52,6 @@ marked in the source code as `TUTORIAL_CHAPTER = 1`.
\code
#include <FL/Fl_Double_Window.H>
#include <FL/Fl.H>
#include <string.h>
Fl_Double_Window *app_window = NULL;
@ -113,6 +112,7 @@ a buffer for the current filename.
#include <FL/Fl_Menu_Bar.H>
#include <FL/fl_ask.H>
#include <FL/filename.H>
#include <FL/fl_string_functions.h>
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<Replace_Dialog*>(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);
}

View File

@ -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);
}

View File

@ -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) )
/**

View File

@ -35,6 +35,7 @@
# include <strings.h>
# endif /* HAVE_STRINGS_H */
# include <ctype.h>
# include <FL/fl_string_functions.h>
/*
* 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/fl_string_functions.h>
//FL_EXPORT extern size_t fl_strlcpy(char *, const char *, size_t);
# ifndef HAVE_STRLCPY
# define strlcpy fl_strlcpy
# endif /* !HAVE_STRLCPY */

View File

@ -26,7 +26,6 @@
#include <FL/Fl_Double_Window.H>
#include <FL/Fl.H>
#include <string.h>
Fl_Double_Window *app_window = NULL;
@ -50,6 +49,7 @@ int main (int argc, char **argv) {
#include <FL/Fl_Menu_Bar.H>
#include <FL/fl_ask.H>
#include <FL/filename.H>
#include <FL/fl_string_functions.h>
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<Replace_Dialog*>(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);
}