summaryrefslogtreecommitdiff
path: root/fluid/tools/filename.cxx
blob: 1cecb387b81645913a9f8fe0b76d70ff37e7c6e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//
// 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 <FL/filename.H>
#include <FL/fl_utf8.h>
#include <FL/Fl.H>
#include "../src/flstring.h"

#include <string.h>

/**
 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;
}