Making fl_filename_... public for std::string.

New functions append "_str" to the function name to
avoid ambiguities when calling them. So
'char *fl_filename_name(const char *)' becomes
'std::string fl_filename_name_str(const std::string &)'
This commit is contained in:
Matthias Melcher
2025-03-07 20:26:03 +01:00
parent 70e5dc23ae
commit 1186b4e255
8 changed files with 67 additions and 226 deletions

View File

@@ -72,20 +72,18 @@ FL_EXPORT int fl_filename_isdir(const char *name);
FL_EXPORT int fl_filename_absolute(char *to, int tolen, const char *from, const char *cwd);
FL_EXPORT int fl_filename_relative(char *to, int tolen, const char *from, const char *cwd);
#include <string>
// FIXME: We can't do this in 1.4.x - enable this block in 1.5 or higher.
// See fluid/fluid_filename.{h|cxx} for an implementation using Fl_String.
// FL_EXPORT std::string fl_filename_name(const std::string &filename);
// FL_EXPORT std::string fl_filename_path(const std::string &filename);
// FL_EXPORT std::string fl_filename_ext(const std::string &filename);
// FL_EXPORT std::string fl_filename_setext(const std::string &filename, const std::string &new_extension);
// FL_EXPORT std::string fl_filename_expand(const std::string &from);
// FL_EXPORT std::string fl_filename_absolute(const std::string &from);
// FL_EXPORT std::string fl_filename_absolute(const std::string &from, const std::string &base);
// FL_EXPORT std::string fl_filename_relative(const std::string &from);
// FL_EXPORT std::string fl_filename_relative(const std::string &from, const std::string &base);
// FL_EXPORT std::string fl_getcwd();
FL_EXPORT std::string fl_filename_name_str(const std::string &filename);
FL_EXPORT std::string fl_filename_path_str(const std::string &filename);
FL_EXPORT std::string fl_filename_ext_str(const std::string &filename);
FL_EXPORT std::string fl_filename_setext_str(const std::string &filename, const std::string &new_extension);
FL_EXPORT std::string fl_filename_expand_str(const std::string &from);
FL_EXPORT std::string fl_filename_absolute_str(const std::string &from);
FL_EXPORT std::string fl_filename_absolute_str(const std::string &from, const std::string &base);
FL_EXPORT std::string fl_filename_relative_str(const std::string &from);
FL_EXPORT std::string fl_filename_relative_str(const std::string &from, const std::string &base);
FL_EXPORT std::string fl_getcwd_str();
# endif /* defined(__cplusplus) */

View File

@@ -462,9 +462,9 @@ void enter_project_dir() {
return;
}
// store the current working directory for later
app_work_dir = fl_getcwd();
app_work_dir = fl_getcwd_str();
// set the current directory to the path of our .fl file
std::string project_path = fl_filename_path(fl_filename_absolute(filename));
std::string project_path = fl_filename_path_str(fl_filename_absolute_str(filename));
if (fl_chdir(project_path.c_str()) == -1) {
fprintf(stderr, "** Fluid internal error: enter_project_dir() can't chdir to %s: %s\n",
project_path.c_str(), strerror(errno));
@@ -609,7 +609,7 @@ void save_cb(Fl_Widget *, void *v) {
if (fnfc.show() != 0) return;
c = fnfc.filename();
if (!fl_access(c, 0)) {
std::string basename = fl_filename_name(std::string(c));
std::string basename = fl_filename_name_str(std::string(c));
if (fl_choice("The file \"%s\" already exists.\n"
"Do you want to replace it?", "Cancel",
"Replace", NULL, basename.c_str()) == 0) return;
@@ -978,8 +978,8 @@ std::string open_project_filechooser(const std::string &title) {
dialog.filter("FLUID Files\t*.f[ld]\n");
if (filename) {
std::string current_project_file = filename;
dialog.directory(fl_filename_path(current_project_file).c_str());
dialog.preset_file(fl_filename_name(current_project_file).c_str());
dialog.directory(fl_filename_path_str(current_project_file).c_str());
dialog.preset_file(fl_filename_name_str(current_project_file).c_str());
}
if (dialog.show() != 0)
return std::string();
@@ -1086,7 +1086,7 @@ void apple_open_cb(const char *c) {
\return the path ending in '/'
*/
std::string Fluid_Project::projectfile_path() const {
return end_with_slash(fl_filename_absolute(fl_filename_path(filename), g_launch_path));
return end_with_slash(fl_filename_absolute_str(fl_filename_path_str(filename), g_launch_path));
}
/**
@@ -1102,11 +1102,11 @@ std::string Fluid_Project::projectfile_name() const {
\return the path ending in '/'
*/
std::string Fluid_Project::codefile_path() const {
std::string path = fl_filename_path(code_file_name);
std::string path = fl_filename_path_str(code_file_name);
if (batch_mode)
return end_with_slash(fl_filename_absolute(path, g_launch_path));
return end_with_slash(fl_filename_absolute_str(path, g_launch_path));
else
return end_with_slash(fl_filename_absolute(path, projectfile_path()));
return end_with_slash(fl_filename_absolute_str(path, projectfile_path()));
}
/**
@@ -1114,11 +1114,11 @@ std::string Fluid_Project::codefile_path() const {
\return the file name without path
*/
std::string Fluid_Project::codefile_name() const {
std::string name = fl_filename_name(code_file_name);
std::string name = fl_filename_name_str(code_file_name);
if (name.empty()) {
return fl_filename_setext(fl_filename_name(filename), ".cxx");
return fl_filename_setext_str(fl_filename_name(filename), ".cxx");
} else if (name[0] == '.') {
return fl_filename_setext(fl_filename_name(filename), code_file_name);
return fl_filename_setext_str(fl_filename_name(filename), code_file_name);
} else {
return name;
}
@@ -1129,11 +1129,11 @@ std::string Fluid_Project::codefile_name() const {
\return the path ending in '/'
*/
std::string Fluid_Project::headerfile_path() const {
std::string path = fl_filename_path(header_file_name);
std::string path = fl_filename_path_str(header_file_name);
if (batch_mode)
return end_with_slash(fl_filename_absolute(path, g_launch_path));
return end_with_slash(fl_filename_absolute_str(path, g_launch_path));
else
return end_with_slash(fl_filename_absolute(path, projectfile_path()));
return end_with_slash(fl_filename_absolute_str(path, projectfile_path()));
}
/**
@@ -1141,11 +1141,11 @@ std::string Fluid_Project::headerfile_path() const {
\return the file name without path
*/
std::string Fluid_Project::headerfile_name() const {
std::string name = fl_filename_name(header_file_name);
std::string name = fl_filename_name_str(header_file_name);
if (name.empty()) {
return fl_filename_setext(fl_filename_name(filename), ".h");
return fl_filename_setext_str(fl_filename_name_str(filename), ".h");
} else if (name[0] == '.') {
return fl_filename_setext(fl_filename_name(filename), header_file_name);
return fl_filename_setext_str(fl_filename_name_str(filename), header_file_name);
} else {
return name;
}
@@ -1172,9 +1172,9 @@ std::string Fluid_Project::stringsfile_path() const {
*/
std::string Fluid_Project::stringsfile_name() const {
switch (i18n_type) {
default: return fl_filename_setext(fl_filename_name(filename), ".txt");
case FD_I18N_GNU: return fl_filename_setext(fl_filename_name(filename), ".po");
case FD_I18N_POSIX: return fl_filename_setext(fl_filename_name(filename), ".msg");
default: return fl_filename_setext_str(fl_filename_name(filename), ".txt");
case FD_I18N_GNU: return fl_filename_setext_str(fl_filename_name(filename), ".po");
case FD_I18N_POSIX: return fl_filename_setext_str(fl_filename_name(filename), ".msg");
}
}
@@ -1183,7 +1183,7 @@ std::string Fluid_Project::stringsfile_name() const {
\return the file name without path or extension
*/
std::string Fluid_Project::basename() const {
return fl_filename_setext(fl_filename_name(filename), "");
return fl_filename_setext_str(fl_filename_name(filename), "");
}
/**
@@ -1223,8 +1223,8 @@ int write_code_files(bool dont_show_completion_dialog)
// -- write the code and header files
if (!batch_mode) enter_project_dir();
int x = f.write_code(code_filename.c_str(), header_filename.c_str());
std::string code_filename_rel = fl_filename_relative(code_filename);
std::string header_filename_rel = fl_filename_relative(header_filename);
std::string code_filename_rel = fl_filename_relative_str(code_filename);
std::string header_filename_rel = fl_filename_relative_str(header_filename);
if (!batch_mode) leave_project_dir();
// -- print error message in batch mode or pop up an error or confirmation dialog box
@@ -1625,7 +1625,7 @@ void print_menu_cb(Fl_Widget *, void *) {
fl_draw(date, w - (int)fl_width(date), fl_height());
// Get the base filename...
std::string basename = fl_filename_name(std::string(filename));
std::string basename = fl_filename_name_str(std::string(filename));
fl_draw(basename.c_str(), 0, fl_height());
// print centered and scaled to fit in the page
@@ -2073,7 +2073,7 @@ void set_modflag(int mf, int mfc) {
if (main_window) {
std::string basename;
if (!filename) basename = "Untitled.fl";
else basename = fl_filename_name(std::string(filename));
else basename = fl_filename_name_str(std::string(filename));
code_ext = fl_filename_ext(g_project.code_file_name.c_str());
char mod_star = modflag ? '*' : ' ';
char mod_c_star = modflag_c ? '*' : ' ';
@@ -2199,7 +2199,7 @@ int fluid_main(int argc,char **argv) {
setlocale(LC_ALL, ""); // enable multi-language errors in file chooser
setlocale(LC_NUMERIC, "C"); // make sure numeric values are written correctly
g_launch_path = end_with_slash(fl_getcwd()); // store the current path at launch
g_launch_path = end_with_slash(fl_getcwd_str()); // store the current path at launch
Fl::args_to_utf8(argc, argv); // for MSYS2/MinGW
if ( (Fl::args(argc,argv,i,arg) == 0) // unsupported argument found

View File

@@ -21,6 +21,7 @@
#include <FL/Fl_Preferences.H>
#include <FL/Fl_Menu_Item.H>
#include <FL/filename.H>
#include <string>

View File

@@ -600,8 +600,8 @@ static void cb_w_layout_menu_save(Fl_Menu_*, void*) {
fnfc.options(Fl_Native_File_Chooser::SAVEAS_CONFIRM | Fl_Native_File_Chooser::USE_FILTER_EXT);
fnfc.filter("FLUID Layouts\t*.fll\n");
std::string filename = g_layout_list.filename_;
fnfc.directory(fl_filename_path(filename).c_str());
fnfc.preset_file(fl_filename_name(filename).c_str());
fnfc.directory(fl_filename_path_str(filename).c_str());
fnfc.preset_file(fl_filename_name_str(filename).c_str());
if (fnfc.show() != 0) return;
const char *new_filename = fnfc.filename();
if (!new_filename) return;

View File

@@ -574,8 +574,8 @@ g_layout_list.update_dialogs();}
fnfc.options(Fl_Native_File_Chooser::SAVEAS_CONFIRM | Fl_Native_File_Chooser::USE_FILTER_EXT);
fnfc.filter("FLUID Layouts\\t*.fll\\n");
std::string filename = g_layout_list.filename_;
fnfc.directory(fl_filename_path(filename).c_str());
fnfc.preset_file(fl_filename_name(filename).c_str());
fnfc.directory(fl_filename_path_str(filename).c_str());
fnfc.preset_file(fl_filename_name_str(filename).c_str());
if (fnfc.show() != 0) return;
const char *new_filename = fnfc.filename();
if (!new_filename) return;

View File

@@ -29,137 +29,15 @@
the next release after 1.4.x will be. We'll use std::string instead!
*/
#include "tools/fluid_filename.h"
#include <FL/filename.H>
#include <FL/Fl.H>
#include <FL/fl_string_functions.h>
#include "../src/flstring.h"
#include <stdlib.h>
#include <string>
/**
Return a new string that contains the name part of the filename.
\param[in] filename file path and name
\return the name part of a filename
\see fl_filename_name(const char *filename)
*/
std::string fl_filename_name(const std::string &filename) {
return std::string(fl_filename_name(filename.c_str()));
}
/**
Return a new string that contains the path part of the filename.
\param[in] filename file path and name
\return the path part of a filename without the name
\see fl_filename_name(const char *filename)
*/
std::string fl_filename_path(const std::string &filename) {
const char *base = filename.c_str();
const char *name = fl_filename_name(base);
if (name) {
return std::string(base, (int)(name-base));
} else {
return std::string();
}
}
/**
Return a new string that contains the filename extension.
\param[in] filename file path and name
\return the filename extension including the prepending '.', or an empty
string if the filename has no extension
\see fl_filename_ext(const char *buf)
*/
std::string fl_filename_ext(const std::string &filename) {
return std::string(fl_filename_ext(filename.c_str()));
}
/**
Return a copy of the old filename with the new extension.
\param[in] filename file path and name
\param[in] new_extension new filename extension, starts with a '.'
\return the new filename
\see fl_filename_setext(char *to, int tolen, const char *ext)
*/
std::string fl_filename_setext(const std::string &filename, const std::string &new_extension) {
char buffer[FL_PATH_MAX];
fl_strlcpy(buffer, filename.c_str(), FL_PATH_MAX);
fl_filename_setext(buffer, FL_PATH_MAX, new_extension.c_str());
return std::string(buffer);
}
/**
Expands a filename containing shell variables and tilde (~).
\param[in] from file path and name
\return the new, expanded filename
\see fl_filename_expand(char *to, int tolen, const char *from)
*/
std::string fl_filename_expand(const std::string &from) {
char buffer[FL_PATH_MAX];
fl_filename_expand(buffer, FL_PATH_MAX, from.c_str());
return std::string(buffer);
}
/**
Makes a filename absolute from a filename relative to the current working directory.
\param[in] from relative filename
\return the new, absolute filename
\see fl_filename_absolute(char *to, int tolen, const char *from)
*/
std::string fl_filename_absolute(const std::string &from) {
char buffer[FL_PATH_MAX];
fl_filename_absolute(buffer, FL_PATH_MAX, from.c_str());
return std::string(buffer);
}
/**
Append the relative filename `from` to the absolute filename `base` to form
the new absolute path.
\param[in] from relative filename
\param[in] base `from` is relative to this absolute file path
\return the new, absolute filename
\see fl_filename_absolute(char *to, int tolen, const char *from, const char *base)
*/
std::string fl_filename_absolute(const std::string &from, const std::string &base) {
char buffer[FL_PATH_MAX];
fl_filename_absolute(buffer, FL_PATH_MAX, from.c_str(), base.c_str());
return std::string(buffer);
}
/**
Makes a filename relative to the current working directory.
\param[in] from file path and name
\return the new, relative filename
\see fl_filename_relative(char *to, int tolen, const char *from)
*/
std::string fl_filename_relative(const std::string &from) {
char buffer[FL_PATH_MAX];
fl_filename_relative(buffer, FL_PATH_MAX, from.c_str());
return std::string(buffer);
}
/**
Makes a filename relative to any directory.
\param[in] from file path and name
\param[in] base relative to this absolute path
\return the new, relative filename
\see fl_filename_relative(char *to, int tolen, const char *from, const char *base)
*/
std::string fl_filename_relative(const std::string &from, const std::string &base) {
char buffer[FL_PATH_MAX];
fl_filename_relative(buffer, FL_PATH_MAX, from.c_str(), base.c_str());
return std::string(buffer);
}
/** Cross-platform function to get the current working directory
as a UTF-8 encoded value in an std::string.
\return the CWD encoded as UTF-8
*/
std::string fl_getcwd() {
char buffer[FL_PATH_MAX];
fl_getcwd(buffer, FL_PATH_MAX);
return std::string(buffer);
}
//#include <FL/fl_string_functions.h>
//#include "../src/flstring.h"
//
//#include <stdlib.h>
//#include <string>
/**
Return a shortened filename for limited display width.
@@ -184,7 +62,7 @@ std::string fl_filename_shortened(const std::string &filename, int max_chars) {
static std::string home;
static int home_chars = -1;
if (home_chars==-1) {
home = fl_filename_expand(tilde);
home = fl_filename_expand_str(tilde);
home_chars = fl_utf_nb_char((const uchar*)home.c_str(), (int)home.size());
}
std::string homed_filename;

View File

@@ -1,7 +1,7 @@
/*
* Filename header file for the Fast Light Tool Kit (FLTK).
*
* Copyright 1998-2023 by Bill Spitzak and others.
* Copyright 1998-2025 by Bill Spitzak and others.
*
* This library is free software. Distribution and use rights are outlined in
* the file "COPYING" which should have been included with this file. If this
@@ -15,45 +15,14 @@
*/
/** \file fluid/fluid_filename.h
\brief File names and URI utility functions for FLUID only.
This file declares all fl_filename* functions using std::string and also
includes the main header file <FL/filename.H>.
\note This file contains some filename functions using std::string which
which are used in FLTK 1.4.x but will be removed in the next minor
or major release after 1.4.x (i.e. 1.5 or maybe 4.0).
\note This entire file should become obsolete in 1.5 or higher, whatever
the next release after 1.4.x will be. We'll use std::string instead!
\brief Handling file names operations that are not in the core library.
*/
#ifndef FLUID_FILENAME_H
# define FLUID_FILENAME_H
#include <FL/Fl_Export.H>
#include <FL/platform_types.h>
#include <FL/filename.H>
# if defined(__cplusplus)
#ifndef FLUID_TOOLS_FILENAME_H
#define FLUID_TOOLS_FILENAME_H
#include <string>
std::string fl_filename_shortened(const std::string &filename, int maxchars);
std::string fl_filename_name(const std::string &filename);
std::string fl_filename_path(const std::string &filename);
std::string fl_filename_ext(const std::string &filename);
std::string fl_filename_setext(const std::string &filename, const std::string &new_extension);
std::string fl_filename_expand(const std::string &from);
std::string fl_filename_absolute(const std::string &from);
std::string fl_filename_absolute(const std::string &from, const std::string &base);
std::string fl_filename_relative(const std::string &from);
std::string fl_filename_relative(const std::string &from, const std::string &base);
std::string fl_getcwd();
# endif
/** @} */
#endif /* FLUID_FILENAME_H */
#endif // FLUID_TOOLS_FILENAME_H

View File

@@ -285,10 +285,6 @@ int Fl_System_Driver::filename_relative(char *to, int tolen, const char *dest_di
\endcond
*/
// FIXME: '0 &&' => We can't do that in 1.4.x, enable this block in 1.5 or higher.
// There would be too many naming conflicts with fluid's usage of these functions.
#if (0 && FLTK_USE_STD)
/**
Return a new string that contains the name part of the filename.
@@ -296,7 +292,7 @@ int Fl_System_Driver::filename_relative(char *to, int tolen, const char *dest_di
\return the name part of a filename
\see fl_filename_name(const char *filename)
*/
std::string fl_filename_name(const std::string &filename) {
std::string fl_filename_name_str(const std::string &filename) {
return std::string(fl_filename_name(filename.c_str()));
}
@@ -306,7 +302,7 @@ std::string fl_filename_name(const std::string &filename) {
\return the path part of a filename without the name
\see fl_filename_name(const char *filename)
*/
std::string fl_filename_path(const std::string &filename) {
std::string fl_filename_path_str(const std::string &filename) {
const char *base = filename.c_str();
const char *name = fl_filename_name(base);
if (name) {
@@ -323,7 +319,7 @@ std::string fl_filename_path(const std::string &filename) {
string if the filename has no extension
\see fl_filename_ext(const char *buf)
*/
std::string fl_filename_ext(const std::string &filename) {
std::string fl_filename_ext_str(const std::string &filename) {
return std::string(fl_filename_ext(filename.c_str()));
}
@@ -334,7 +330,7 @@ std::string fl_filename_ext(const std::string &filename) {
\return the new filename
\see fl_filename_setext(char *to, int tolen, const char *ext)
*/
std::string fl_filename_setext(const std::string &filename, const std::string &new_extension) {
std::string fl_filename_setext_str(const std::string &filename, const std::string &new_extension) {
char buffer[FL_PATH_MAX];
fl_strlcpy(buffer, filename.c_str(), FL_PATH_MAX);
fl_filename_setext(buffer, FL_PATH_MAX, new_extension.c_str());
@@ -347,7 +343,7 @@ std::string fl_filename_setext(const std::string &filename, const std::string &n
\return the new, expanded filename
\see fl_filename_expand(char *to, int tolen, const char *from)
*/
std::string fl_filename_expand(const std::string &from) {
std::string fl_filename_expand_str(const std::string &from) {
char buffer[FL_PATH_MAX];
fl_filename_expand(buffer, FL_PATH_MAX, from.c_str());
return std::string(buffer);
@@ -359,7 +355,7 @@ std::string fl_filename_expand(const std::string &from) {
\return the new, absolute filename
\see fl_filename_absolute(char *to, int tolen, const char *from)
*/
std::string fl_filename_absolute(const std::string &from) {
std::string fl_filename_absolute_str(const std::string &from) {
char buffer[FL_PATH_MAX];
fl_filename_absolute(buffer, FL_PATH_MAX, from.c_str());
return std::string(buffer);
@@ -373,7 +369,7 @@ std::string fl_filename_absolute(const std::string &from) {
\return the new, absolute filename
\see fl_filename_absolute(char *to, int tolen, const char *from, const char *base)
*/
std::string fl_filename_absolute(const std::string &from, const std::string &base) {
std::string fl_filename_absolute_str(const std::string &from, const std::string &base) {
char buffer[FL_PATH_MAX];
fl_filename_absolute(buffer, FL_PATH_MAX, from.c_str(), base.c_str());
return std::string(buffer);
@@ -385,7 +381,7 @@ std::string fl_filename_absolute(const std::string &from, const std::string &bas
\return the new, relative filename
\see fl_filename_relative(char *to, int tolen, const char *from)
*/
std::string fl_filename_relative(const std::string &from) {
std::string fl_filename_relative_str(const std::string &from) {
char buffer[FL_PATH_MAX];
fl_filename_relative(buffer, FL_PATH_MAX, from.c_str());
return std::string(buffer);
@@ -398,7 +394,7 @@ std::string fl_filename_relative(const std::string &from) {
\return the new, relative filename
\see fl_filename_relative(char *to, int tolen, const char *from, const char *base)
*/
std::string fl_filename_relative(const std::string &from, const std::string &base) {
std::string fl_filename_relative_str(const std::string &from, const std::string &base) {
char buffer[FL_PATH_MAX];
fl_filename_relative(buffer, FL_PATH_MAX, from.c_str(), base.c_str());
return std::string(buffer);
@@ -408,10 +404,9 @@ std::string fl_filename_relative(const std::string &from, const std::string &bas
as a UTF-8 encoded value in an std::string.
\return the CWD encoded as UTF-8
*/
std::string fl_getcwd() {
std::string fl_getcwd_str() {
char buffer[FL_PATH_MAX];
buffer[0] = 0;
fl_getcwd(buffer, FL_PATH_MAX);
return std::string(buffer);
}
#endif // FLTK_USE_STD