Skip to content

Commit

Permalink
Fix text keywords do not work with non-ascii filenames on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
bmatherly committed Dec 2, 2023
1 parent b82f2af commit c5c199e
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/framework/mlt_producer.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* \brief abstraction for all producer services
* \see mlt_producer_s
*
* Copyright (C) 2003-2022 Meltytech, LLC
* Copyright (C) 2003-2023 Meltytech, LLC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -1256,7 +1256,7 @@ int64_t mlt_producer_get_creation_time(mlt_producer self)
}
if (resource) {
struct stat file_info;
if (!stat(resource, &file_info)) {
if (!mlt_stat(resource, &file_info)) {
return (int64_t) file_info.st_mtime * 1000;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/framework/mlt_properties.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* \brief Properties class definition
* \see mlt_properties_s
*
* Copyright (C) 2003-2022 Meltytech, LLC
* Copyright (C) 2003-2023 Meltytech, LLC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -288,7 +288,7 @@ int mlt_properties_preset(mlt_properties self, const char *name)
return 1;

// See if name is an explicit file
if (!stat(name, &stat_buff)) {
if (!mlt_stat(name, &stat_buff)) {
return load_properties(self, name);
} else {
// Look for profile-specific preset before a generic one.
Expand Down
2 changes: 1 addition & 1 deletion src/framework/mlt_repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ static void list_presets(mlt_properties properties, const char *path, const char
struct stat info;

snprintf(fullname, sizeof(fullname), "%s/%s", dirname, de->d_name);
stat(fullname, &info);
mlt_stat(fullname, &info);
if (S_ISDIR(info.st_mode)) {
// recurse into subdirectories
char sub[PATH_MAX];
Expand Down
5 changes: 5 additions & 0 deletions src/framework/mlt_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,17 @@ extern int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
extern int setenv(const char *name, const char *value, int overwrite);
extern char *getlocale();
extern FILE *win32_fopen(const char *filename_utf8, const char *mode_utf8);
#include <sys/stat.h>
#include <sys/types.h>
extern int win32_stat(const char *filename_utf8, struct stat *buffer);
#include <time.h>
extern char *strptime(const char *buf, const char *fmt, struct tm *tm);
#define mlt_fopen win32_fopen
#define mlt_stat win32_stat
#define MLT_DIRLIST_DELIMITER ";"
#else
#define mlt_fopen fopen
#define mlt_stat stat
#define MLT_DIRLIST_DELIMITER ":"
#endif /* ifdef _WIN32 */

Expand Down
2 changes: 1 addition & 1 deletion src/modules/frei0r/factory.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ static mlt_properties fill_param_info(mlt_service_type type, const char *service
servicetype,
service_name);
memset(&stat_buff, 0, sizeof(stat_buff));
stat(file, &stat_buff);
mlt_stat(file, &stat_buff);

if (S_ISREG(stat_buff.st_mode)) {
return mlt_properties_parse_yaml(file);
Expand Down
6 changes: 3 additions & 3 deletions src/modules/gdk/producer_pixbuf.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* producer_pixbuf.c -- raster image loader based upon gdk-pixbuf
* Copyright (C) 2003-2021 Meltytech, LLC
* Copyright (C) 2003-2023 Meltytech, LLC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -212,7 +212,7 @@ static int load_sequence_sprintf(producer_pixbuf self,
while (gap < 100) {
struct stat buf;
snprintf(full, 1023, filename, i++);
if (stat(full, &buf) == 0) {
if (mlt_stat(full, &buf) == 0) {
sprintf(key, "%d", keyvalue++);
mlt_properties_set(self->filenames, key, full);
gap = 0;
Expand Down Expand Up @@ -341,7 +341,7 @@ static int load_sequence_csv(producer_pixbuf self, mlt_properties properties, co
break;
}

if (stat(line, &buf) != 0) {
if (mlt_stat(line, &buf) != 0) {
break;
}

Expand Down
7 changes: 4 additions & 3 deletions src/modules/opencv/filter_opencv_tracker.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* filter_tracker.cpp -- Motion tracker
* Copyright (C) 2016 Jean-Baptiste Mardelle
* Copyright (C) 2023 Meltytech, LLC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -175,8 +176,8 @@ static void analyze(mlt_filter filter,
// - kernel_r1: https://www.dropbox.com/s/999cqx5zrfi7w4p/dasiamrpn_kernel_r1.onnx?dl=0
// - kernel_cls1: https://www.dropbox.com/s/qvmtszx5h339a0w/dasiamrpn_kernel_cls1.onnx?dl=0
struct stat file_info;
if (stat(model1, &file_info) == 0 && stat(model2, &file_info) == 0
&& stat(model3, &file_info) == 0) {
if (mlt_stat(model1, &file_info) == 0 && mlt_stat(model2, &file_info) == 0
&& mlt_stat(model3, &file_info) == 0) {
// Models found, process
parameters.model = model1;
parameters.kernel_cls1 = model2;
Expand Down Expand Up @@ -205,7 +206,7 @@ static void analyze(mlt_filter filter,
// Models can be downloaded from:
// https://github.com/HonglinChu/SiamTrackers/tree/master/NanoTrack/models/nanotrackv2
struct stat file_info;
if (stat(model1, &file_info) == 0 && stat(model2, &file_info) == 0) {
if (mlt_stat(model1, &file_info) == 0 && mlt_stat(model2, &file_info) == 0) {
// Models found, process
parameters.backbone = model1;
parameters.neckhead = model2;
Expand Down
10 changes: 6 additions & 4 deletions src/modules/plus/filter_dynamictext.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static void get_filedate_str(const char *keyword, mlt_filter filter, mlt_frame f
char *filename = mlt_properties_get(producer_properties, "resource");
struct stat file_info;

if (!stat(filename, &file_info)) {
if (!mlt_stat(filename, &file_info)) {
const char *format = "%Y/%m/%d";
int n = strlen("filedate") + 1;
struct tm *time_info = gmtime(&(file_info.st_mtime));
Expand All @@ -124,7 +124,7 @@ static void get_localfiledate_str(const char *keyword,
char *filename = mlt_properties_get(producer_properties, "resource");
struct stat file_info;

if (!stat(filename, &file_info)) {
if (!mlt_stat(filename, &file_info)) {
const char *format = "%Y/%m/%d";
int n = strlen("localfiledate") + 1;
struct tm *time_info = localtime(&(file_info.st_mtime));
Expand Down Expand Up @@ -167,7 +167,8 @@ static void get_filename_str(mlt_filter filter, mlt_frame frame, char *text)
mlt_producer producer = mlt_producer_cut_parent(mlt_frame_get_original_producer(frame));
mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES(producer);
char *filename = mlt_properties_get(producer_properties, "resource");
if (access(filename, F_OK) == 0) {
struct stat file_info;
if (!mlt_stat(filename, &file_info)) {
strncat(text, basename(filename), MAX_TEXT_LEN - strlen(text) - 1);
}
}
Expand All @@ -177,7 +178,8 @@ static void get_basename_str(mlt_filter filter, mlt_frame frame, char *text)
mlt_producer producer = mlt_producer_cut_parent(mlt_frame_get_original_producer(frame));
mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES(producer);
char *filename = strdup(mlt_properties_get(producer_properties, "resource"));
if (access(filename, F_OK) == 0) {
struct stat file_info;
if (!mlt_stat(filename, &file_info)) {
char *bname = basename(filename);
char *ext = strrchr(bname, '.');
if (ext) {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/qt/qimage_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* qimage_wrapper.cpp -- a Qt/QImage based producer for MLT
* Copyright (C) 2006-2022 Meltytech, LLC
* Copyright (C) 2006-2023 Meltytech, LLC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -500,7 +500,7 @@ int load_sequence_sprintf(producer_qimage self, mlt_properties properties, const
for (int gap = 0; gap < 100;) {
struct stat buf;
snprintf(full, 1023, filename, i++);
if (stat(full, &buf) == 0) {
if (mlt_stat(full, &buf) == 0) {
sprintf(key, "%d", keyvalue++);
mlt_properties_set(self->filenames, key, full);
gap = 0;
Expand Down
24 changes: 23 additions & 1 deletion src/win32/win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* \file win32.c
* \brief Miscellaneous utility functions for Windows.
*
* Copyright (C) 2003-2016 Meltytech, LLC
* Copyright (C) 2003-2023 Meltytech, LLC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand All @@ -28,6 +28,8 @@
#include <locale.h>
#include <ctype.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "../framework/mlt_properties.h"
#include "../framework/mlt_log.h"

Expand Down Expand Up @@ -265,3 +267,23 @@ FILE* win32_fopen(const char *filename_utf8, const char *mode_utf8)
// Try with regular old fopen.
return fopen(filename_utf8, mode_utf8);
}

int win32_stat(const char *filename_utf8, struct stat *buffer)
{
int ret = stat(filename_utf8, buffer);
if (!ret) {
return ret;
}
// Convert UTF-8 to wide chars.
int n = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename_utf8, -1, NULL, 0);
if (n > 0) {
wchar_t *filename_w = (wchar_t *) calloc(n, sizeof(wchar_t));
if (filename_w) {
MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, filename_w, n);
ret = _wstat(filename_w, (struct _stat *)buffer);
free(filename_w);
return ret;
}
}
return ret;
}

0 comments on commit c5c199e

Please sign in to comment.