summaryrefslogtreecommitdiff
path: root/fluid/tools/filename.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'fluid/tools/filename.cxx')
-rw-r--r--fluid/tools/filename.cxx289
1 files changed, 101 insertions, 188 deletions
diff --git a/fluid/tools/filename.cxx b/fluid/tools/filename.cxx
index 72386f78b..1cecb387b 100644
--- a/fluid/tools/filename.cxx
+++ b/fluid/tools/filename.cxx
@@ -17,16 +17,6 @@
/** \file fluid/filename.cxx
\brief File names and URI utility functions for FLUID only.
-
- This file defines 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!
*/
#include "tools/filename.h"
@@ -35,226 +25,149 @@
#include <FL/fl_utf8.h>
#include <FL/Fl.H>
#include "../src/flstring.h"
-//#include <FL/fl_string_functions.h>
-//#include "../src/flstring.h"
-//
-//#include <stdlib.h>
-//#include <string>
+
+#include <string.h>
/**
Return a shortened filename for limited display width.
- Replace the start uf a path with "~" if it matches the home directory.
- If the remaining filename has more than the give number of characters, it will
+ Replace the start of a path with "~" if it matches the home directory.
+ If the remaining filename has more than the given number of characters, it will
be shortened by replacing parts of the path with an ellipsis ("...").
The shortened name can no longer be used to open a file. This is purely to
- make as much information visible while fitting into a give space.
+ make as much information visible while fitting into a given space.
+ \param[out] result buffer to store result
+ \param[in] result_size size of result buffer
\param[in] filename absolute path and name, UTF-8 aware
- \param[in[ max_chars maximum number of characters in result, including ellipsis
- \return shortened file path and name
+ \param[in] max_chars maximum number of characters in result, including ellipsis
+ \return pointer to result buffer
*/
-std::string fl_filename_shortened(const std::string &filename, int max_chars) {
+char *fl_filename_shortened(char *result, int result_size, const char *filename, int max_chars) {
// Insert this as the ellipsis
static const char *ell = "...";
static const int ell_bytes = 3;
// Replace the start of a path with "~" if it matches the home directory
- static std::string tilde = "~/";
- static std::string home;
+ static char home[FL_PATH_MAX] = "";
+ static int home_len = -1;
static int home_chars = -1;
- if (home_chars==-1) {
- home = fl_filename_expand_str(tilde);
- home_chars = fl_utf_nb_char((const uchar*)home.c_str(), (int)home.size());
+
+ if (home_chars == -1) {
+ fl_filename_expand(home, FL_PATH_MAX, "~/");
+ home_len = (int)strlen(home);
+ home_chars = fl_utf_nb_char((const unsigned char*)home, home_len);
}
- std::string homed_filename;
-#if defined(_WIN32) || defined(__APPLE__)
- bool starts_with_home = fl_utf_strncasecmp(home.c_str(), filename.c_str(), home_chars)==0;
-#else
- bool starts_with_home = ::strncmp(home.c_str(), filename.c_str(), home.size())==0;
-#endif
+
+ // Check if filename starts with home directory
+ char homed_filename[FL_PATH_MAX];
+ int starts_with_home = (strncmp(home, filename, home_len) == 0);
if (starts_with_home) {
- homed_filename = tilde + filename.substr(home.size());
+ snprintf(homed_filename, FL_PATH_MAX, "~/%s", filename + home_len);
} else {
- homed_filename = filename;
+ strlcpy(homed_filename, filename, FL_PATH_MAX);
}
- // C style pointer will stay valid until filename is modified.
- const unsigned char *u8str = reinterpret_cast<const unsigned char *>(homed_filename.c_str());
- // Count the number of UTF-8 characters in the name.
- int num_chars = fl_utf_nb_char(u8str, (int)homed_filename.size());
- if (num_chars+ell_bytes-1 > max_chars) {
- // Create a new string by replacing characters in the middle.
- int remove_chars = num_chars - max_chars + ell_bytes;
- int left_chars = (max_chars - ell_bytes)/2;
-// int right_chars = max_chars - left_chars - 3;
-// int right_start_char = num_chars - right_chars;
- // Convert character counts into byte counts.
- int left_bytes = fl_utf8strlen(homed_filename.c_str(), left_chars);
- int right_start_byte = fl_utf8strlen(homed_filename.c_str()+left_bytes, remove_chars) + left_bytes;
- return homed_filename.substr(0, left_bytes) + ell + homed_filename.substr(right_start_byte);
- } else {
- // Nothing to change.
- return homed_filename;
- }
-}
-/**
- Make sure that a path name ends with a forward slash.
- \param[in] str directory or path name
- \return a new string, ending with a '/'
- */
-std::string fld::end_with_slash(const std::string &str) {
- char last = str[str.size()-1];
- if (last !='/' && last != '\\')
- return str + "/";
- else
- return str;
-}
+ // Count the number of UTF-8 characters in the name
+ int homed_len = (int)strlen(homed_filename);
+ int num_chars = fl_utf_nb_char((const unsigned char*)homed_filename, homed_len);
-/**
- Replace Windows '\\' directory separator with UNix '/' separators.
- \param[in] fn a file path in Unix or Windows format
- \return a copy of the file path in Unix format.
- */
-std::string fld::fix_separators(const std::string &fn) {
- std::string ret = fn;
- for (size_t i=0; i<ret.size(); ++i) {
- if (ret[i] == '\\') {
- ret[i] = '/';
+ if (num_chars + ell_bytes - 1 > max_chars) {
+ // Create a new string by replacing characters in the middle
+ int remove_chars = num_chars - max_chars + ell_bytes;
+ int left_chars = (max_chars - ell_bytes) / 2;
+
+ // Convert character counts into byte counts
+ int left_bytes = fl_utf8strlen(homed_filename, left_chars);
+ int right_start_byte = fl_utf8strlen(homed_filename + left_bytes, remove_chars) + left_bytes;
+
+ // Build result: left part + ellipsis + right part
+ if (left_bytes < result_size - 1) {
+ memcpy(result, homed_filename, left_bytes);
+ int pos = left_bytes;
+ int remaining = result_size - pos - 1;
+ if (remaining > ell_bytes) {
+ memcpy(result + pos, ell, ell_bytes);
+ pos += ell_bytes;
+ remaining = result_size - pos - 1;
+ int right_len = homed_len - right_start_byte;
+ if (right_len > remaining) right_len = remaining;
+ if (right_len > 0) {
+ memcpy(result + pos, homed_filename + right_start_byte, right_len);
+ pos += right_len;
+ }
+ }
+ result[pos] = '\0';
+ } else {
+ strlcpy(result, homed_filename, result_size);
}
+ } else {
+ // Nothing to change
+ strlcpy(result, homed_filename, result_size);
}
- return ret;
-}
-
-// ---- FLUID-local wrapper functions for filename operations ----
-/**
- Get current working directory as std::string.
- \return current working directory path
- */
-std::string fl_getcwd_str() {
- char buf[FL_PATH_MAX];
- fl_getcwd(buf, FL_PATH_MAX);
- return std::string(buf);
+ return result;
}
/**
Get path component (directory) from a filename.
+ \param[out] result buffer to store result (ends with separator if path exists)
+ \param[in] result_size size of result buffer
\param[in] filename the full path
- \return directory portion ending with separator
- */
-std::string fl_filename_path_str(const char *filename) {
- if (!filename || !*filename) return std::string();
- const char *name = fl_filename_name(filename);
- if (name == filename) return std::string();
- return std::string(filename, name - filename);
-}
-
-std::string fl_filename_path_str(const std::string &filename) {
- return fl_filename_path_str(filename.c_str());
-}
-
-/**
- Make a filename absolute.
- \param[in] from relative or absolute filename
- \return absolute filename
+ \return pointer to result buffer
*/
-std::string fl_filename_absolute_str(const char *from) {
- char buf[FL_PATH_MAX];
- fl_filename_absolute(buf, FL_PATH_MAX, from);
- return std::string(buf);
-}
-
-std::string fl_filename_absolute_str(const std::string &from) {
- return fl_filename_absolute_str(from.c_str());
-}
-
-/**
- Make a filename absolute relative to a given directory.
- \param[in] from relative filename
- \param[in] cwd current working directory to use as base
- \return absolute filename
- */
-std::string fl_filename_absolute_str(const char *from, const char *cwd) {
- char buf[FL_PATH_MAX];
- fl_filename_absolute(buf, FL_PATH_MAX, from, cwd);
- return std::string(buf);
-}
+char *fl_filename_path(char *result, int result_size, const char *filename) {
+ result[0] = '\0';
+ if (!filename || !*filename) return result;
-std::string fl_filename_absolute_str(const std::string &from, const std::string &cwd) {
- return fl_filename_absolute_str(from.c_str(), cwd.c_str());
-}
+ const char *name = fl_filename_name(filename);
+ if (name == filename) return result; // no path component
-/**
- Make a filename relative to current directory.
- \param[in] from absolute filename
- \return relative filename
- */
-std::string fl_filename_relative_str(const char *from) {
- char buf[FL_PATH_MAX];
- fl_filename_relative(buf, FL_PATH_MAX, from);
- return std::string(buf);
-}
+ int path_len = (int)(name - filename);
+ if (path_len >= result_size) path_len = result_size - 1;
+ memcpy(result, filename, path_len);
+ result[path_len] = '\0';
-std::string fl_filename_relative_str(const std::string &from) {
- return fl_filename_relative_str(from.c_str());
+ return result;
}
/**
- Get filename portion from a path.
- \param[in] filename the full path
- \return filename without directory
+ Make sure that a path name ends with a forward slash.
+ \param[out] result buffer to store result
+ \param[in] result_size size of result buffer
+ \param[in] path directory or path name
+ \return pointer to result buffer
*/
-std::string fl_filename_name_str(const char *filename) {
- if (!filename) return std::string();
- return std::string(fl_filename_name(filename));
-}
-
-std::string fl_filename_name_str(const std::string &filename) {
- return fl_filename_name_str(filename.c_str());
-}
+char *fld_end_with_slash(char *result, int result_size, const char *path) {
+ if (!path || !*path) {
+ result[0] = '\0';
+ return result;
+ }
-/**
- Change the extension of a filename.
- \param[in] filename original filename
- \param[in] ext new extension (including dot)
- \return filename with new extension
- */
-std::string fl_filename_setext_str(const char *filename, const char *ext) {
- char buf[FL_PATH_MAX];
- if (!filename) return std::string();
- strlcpy(buf, filename, FL_PATH_MAX);
- fl_filename_setext(buf, FL_PATH_MAX, ext);
- return std::string(buf);
-}
+ int len = (int)strlen(path);
+ char last = path[len - 1];
-std::string fl_filename_setext_str(const std::string &filename, const std::string &ext) {
- return fl_filename_setext_str(filename.c_str(), ext.c_str());
-}
+ if (last == '/' || last == '\\') {
+ strlcpy(result, path, result_size);
+ } else {
+ snprintf(result, result_size, "%s/", path);
+ }
-/**
- Expand a filename with shell variables like ~ for home directory.
- \param[in] from filename with possible shell expansions
- \return expanded filename
- */
-std::string fl_filename_expand_str(const std::string &from) {
- char buf[FL_PATH_MAX];
- fl_filename_expand(buf, FL_PATH_MAX, from.c_str());
- return std::string(buf);
+ return result;
}
/**
- Get the extension of a filename.
- \param[in] filename the filename
- \return extension including the dot, or empty string if no extension
+ Replace Windows '\\' directory separator with Unix '/' separators in-place.
+ \param[in,out] path a file path to modify
+ \return pointer to path
*/
-std::string fl_filename_ext_str(const char *filename) {
- if (!filename) return std::string();
- const char *ext = fl_filename_ext(filename);
- if (!ext) return std::string();
- return std::string(ext);
-}
-
-std::string fl_filename_ext_str(const std::string &filename) {
- return fl_filename_ext_str(filename.c_str());
+char *fld_fix_separators(char *path) {
+ if (!path) return path;
+ char *p;
+ for (p = path; *p; p++) {
+ if (*p == '\\') {
+ *p = '/';
+ }
+ }
+ return path;
}