BLI_path: add BLI_path_extension_strip as an alternative to replace

While replacing the extension with an empty string works,
it required a redundant string-size argument which took a dummy
value in some cases. Avoid having to pass in a redundant string size by
adding a function that strips the extension.
This commit is contained in:
Campbell Barton
2023-04-19 12:53:47 +10:00
parent 9e6757f20f
commit 26a194abbd
10 changed files with 24 additions and 10 deletions
+5
View File
@@ -278,6 +278,11 @@ bool BLI_path_extension_glob_validate(char *ext_fnmatch) ATTR_NONNULL();
* \return false if there was no room.
*/
bool BLI_path_extension_replace(char *path, size_t maxlen, const char *ext) ATTR_NONNULL();
/**
* Remove the file extension.
* \return true if a change was made to `path`.
*/
bool BLI_path_extension_strip(char *path) ATTR_NONNULL();
/**
* Strip's trailing '.'s and adds the extension only when needed
*/
+11 -2
View File
@@ -820,8 +820,7 @@ void BLI_path_to_display_name(char *display_name, int maxlen, const char *name)
/* Replace underscores with spaces. */
BLI_str_replace_char(display_name, '_', ' ');
/* Strip extension. */
BLI_path_extension_replace(display_name, maxlen, "");
BLI_path_extension_strip(display_name);
/* Test if string has any upper case characters. */
bool all_lower = true;
@@ -1279,6 +1278,16 @@ bool BLI_path_extension_replace(char *path, size_t maxlen, const char *ext)
return true;
}
bool BLI_path_extension_strip(char *path)
{
char *path_ext = (char *)BLI_path_extension(path);
if (path_ext == NULL) {
return false;
}
*path_ext = '\0';
return true;
}
bool BLI_path_extension_ensure(char *path, size_t maxlen, const char *ext)
{
#ifdef DEBUG_STRSIZE
@@ -80,7 +80,7 @@ static int volume_import_exec(bContext *C, wmOperator *op)
LISTBASE_FOREACH (ImageFrameRange *, range, &ranges) {
char filename[FILE_MAX];
BLI_split_file_part(range->filepath, filename, sizeof(filename));
BLI_path_extension_replace(filename, sizeof(filename), "");
BLI_path_extension_strip(filename);
Object *object = object_volume_add(C, op, filename);
Volume *volume = (Volume *)object->data;
+1 -1
View File
@@ -170,7 +170,7 @@ static int screenshot_invoke(bContext *C, wmOperator *op, const wmEvent *event)
const char *blendfile_path = BKE_main_blendfile_path_from_global();
if (blendfile_path[0] != '\0') {
BLI_strncpy(filepath, blendfile_path, sizeof(filepath));
BLI_path_extension_replace(filepath, sizeof(filepath), ""); /* strip '.blend' */
BLI_path_extension_strip(filepath); /* Strip `.blend`. */
}
else {
/* As the file isn't saved, only set the name and let the file selector pick a directory. */
@@ -3030,7 +3030,7 @@ static int filelist_readjob_list_dir(FileListReadJob *job_params,
target = entry->redirection_path;
#ifdef WIN32
/* On Windows don't show `.lnk` extension for valid shortcuts. */
BLI_path_extension_replace(entry->relpath, FILE_MAXDIR, "");
BLI_path_extension_strip(entry->relpath);
#endif
}
else {
+1 -1
View File
@@ -171,7 +171,7 @@ void importer_main(Main *bmain,
/* File base name used for both mesh and object. */
char ob_name[FILE_MAX];
BLI_strncpy(ob_name, BLI_path_basename(import_params.filepath), FILE_MAX);
BLI_path_extension_replace(ob_name, FILE_MAX, "");
BLI_path_extension_strip(ob_name);
/* Parse header. */
PlyReadBuffer file(import_params.filepath, 64 * 1024);
+1 -1
View File
@@ -76,7 +76,7 @@ void importer_main(Main *bmain,
/* Name used for both mesh and object. */
char ob_name[FILE_MAX];
BLI_strncpy(ob_name, BLI_path_basename(import_params.filepath), FILE_MAX);
BLI_path_extension_replace(ob_name, FILE_MAX, "");
BLI_path_extension_strip(ob_name);
Mesh *mesh = nullptr;
if (is_ascii_stl) {
@@ -304,7 +304,7 @@ void export_frame(Depsgraph *depsgraph, const OBJExportParams &export_params, co
bool append_frame_to_filename(const char *filepath, const int frame, char *r_filepath_with_frames)
{
BLI_strncpy(r_filepath_with_frames, filepath, FILE_MAX);
BLI_path_extension_replace(r_filepath_with_frames, FILE_MAX, "");
BLI_path_extension_strip(r_filepath_with_frames);
const int digits = frame == 0 ? 1 : integer_digits_i(abs(frame));
BLI_path_frame(r_filepath_with_frames, frame, digits);
return BLI_path_extension_replace(r_filepath_with_frames, FILE_MAX, ".obj");
@@ -489,7 +489,7 @@ void OBJParser::parse(Vector<std::unique_ptr<Geometry>> &r_all_geometries,
/* Use the filename as the default name given to the initial object. */
char ob_name[FILE_MAXFILE];
BLI_strncpy(ob_name, BLI_path_basename(import_params_.filepath), FILE_MAXFILE);
BLI_path_extension_replace(ob_name, FILE_MAXFILE, "");
BLI_path_extension_strip(ob_name);
Geometry *curr_geom = create_geometry(nullptr, GEOM_MESH, ob_name, r_all_geometries);
@@ -852,7 +852,7 @@ static void render_result_exr_file_cache_path(Scene *sce,
const char *blendfile_path = BKE_main_blendfile_path_from_global();
if (blendfile_path[0] != '\0') {
BLI_split_dirfile(blendfile_path, dirname, filename, sizeof(dirname), sizeof(filename));
BLI_path_extension_replace(filename, sizeof(filename), ""); /* strip '.blend' */
BLI_path_extension_strip(filename); /* Strip `.blend`. */
BLI_hash_md5_buffer(blendfile_path, strlen(blendfile_path), path_digest);
}
else {