summaryrefslogtreecommitdiff
path: root/fluid/tools/filename.cxx
blob: 72386f78b4b5570da95db92e838448969d97721d (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
// 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.

  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"

#include <FL/filename.H>
#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>

/**
 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
 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.

 \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
 */
std::string fl_filename_shortened(const std::string &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 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());
  }
  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
  if (starts_with_home) {
    homed_filename = tilde + filename.substr(home.size());
  } else {
    homed_filename = filename;
  }
  // 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;
}

/**
 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] = '/';
    }
  }
  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);
}

/**
 Get path component (directory) from a filename.
 \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
 */
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);
}

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

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

std::string fl_filename_relative_str(const std::string &from) {
  return fl_filename_relative_str(from.c_str());
}

/**
 Get filename portion from a path.
 \param[in] filename the full path
 \return filename without directory
 */
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());
}

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

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

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

/**
 Get the extension of a filename.
 \param[in] filename the filename
 \return extension including the dot, or empty string if no extension
 */
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());
}