// // Filename handling code for the Fast Light Tool Kit (FLTK). // // 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 // file is missing or damaged, see the license at: // // https://www.fltk.org/COPYING.php // // Please see the following page on how to report bugs and issues: // // https://www.fltk.org/bugs.php // /** \file fluid/filename.cxx \brief File names and URI utility functions for FLUID only. */ #include "tools/filename.h" #include #include #include #include "../src/flstring.h" #include /** Return a shortened filename for limited display width. 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 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 pointer to result buffer */ 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 char home[FL_PATH_MAX] = ""; static int home_len = -1; static int home_chars = -1; 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); } // 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) { snprintf(homed_filename, FL_PATH_MAX, "~/%s", filename + home_len); } else { strlcpy(homed_filename, filename, FL_PATH_MAX); } // 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); 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 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 pointer to result buffer */ char *fl_filename_path(char *result, int result_size, const char *filename) { result[0] = '\0'; if (!filename || !*filename) return result; const char *name = fl_filename_name(filename); if (name == filename) return result; // no path component 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'; return result; } /** 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 */ char *fld_end_with_slash(char *result, int result_size, const char *path) { if (!path || !*path) { result[0] = '\0'; return result; } int len = (int)strlen(path); char last = path[len - 1]; if (last == '/' || last == '\\') { strlcpy(result, path, result_size); } else { snprintf(result, result_size, "%s/", path); } return result; } /** Replace Windows '\\' directory separator with Unix '/' separators in-place. \param[in,out] path a file path to modify \return pointer to path */ char *fld_fix_separators(char *path) { if (!path) return path; char *p; for (p = path; *p; p++) { if (*p == '\\') { *p = '/'; } } return path; }