From 0494f7eacc414f4170f850afe3f52856f9ab36e0 Mon Sep 17 00:00:00 2001 From: cantabile Date: Wed, 2 May 2018 16:10:51 +0300 Subject: [PATCH] Fix opening videos with single quotes in the file name --- Makefile.am | 1 + src/shared/RandomStuff.h | 58 ++++++++++++++++++++++++++++++++++++ src/shared/WobblyProject.cpp | 3 +- src/wibbly/WibblyJob.cpp | 11 ++++--- src/wobbly/WobblyWindow.cpp | 3 +- 5 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 src/shared/RandomStuff.h diff --git a/Makefile.am b/Makefile.am index d895ac0..cacd93e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -98,6 +98,7 @@ shared_sources = $(rapidjson_sources) \ src/shared/PresetsModel.h \ src/shared/ProgressDialog.cpp \ src/shared/ProgressDialog.h \ + src/shared/RandomStuff.h \ src/shared/ScrollArea.cpp \ src/shared/ScrollArea.h \ src/shared/SectionsModel.cpp \ diff --git a/src/shared/RandomStuff.h b/src/shared/RandomStuff.h new file mode 100644 index 0000000..8f67c66 --- /dev/null +++ b/src/shared/RandomStuff.h @@ -0,0 +1,58 @@ +/* + +Copyright (c) 2018, John Smith + +Permission to use, copy, modify, and/or distribute this software for +any purpose with or without fee is hereby granted, provided that the +above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR +BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES +OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +SOFTWARE. + +*/ + + +#ifndef RANDOMSTUFF_H +#define RANDOMSTUFF_H + +#include +#include + + +static std::string handleSingleQuotes(const std::string &path) { + // Turn into + // Replace every ' in the string with ' r"'" r' so that the file name can be passed to Python. + // Opening and closing single quotes are provided by the caller. + + char needle = '\''; + std::string replacement("' r\"'\" r'"); + + std::vector positions; + + size_t position = 0; + while (true) { + position = path.find(needle, position); + + if (position == std::string::npos) + break; + else { + positions.push_back(position); + position++; + } + } + + std::string fixed_path = path; + + for (int i = positions.size() - 1; i >= 0; i--) + fixed_path.replace(positions[i], 1, replacement); + + return fixed_path; +} + +#endif // RANDOMSTUFF_H diff --git a/src/shared/WobblyProject.cpp b/src/shared/WobblyProject.cpp index c5e39e9..10d2b1d 100644 --- a/src/shared/WobblyProject.cpp +++ b/src/shared/WobblyProject.cpp @@ -36,6 +36,7 @@ SOFTWARE. #include "rapidjson/prettywriter.h" #include "rapidjson/error/en.h" +#include "RandomStuff.h" #include "WobblyException.h" #include "WobblyProject.h" @@ -3332,7 +3333,7 @@ void WobblyProject::presetsToScript(std::string &script) const { void WobblyProject::sourceToScript(std::string &script, bool save_node) const { - std::string src = "src = c." + source_filter + "(r'" + input_file + "')\n"; + std::string src = "src = c." + source_filter + "(r'" + handleSingleQuotes(input_file) + "')\n"; if (save_node) { script += diff --git a/src/wibbly/WibblyJob.cpp b/src/wibbly/WibblyJob.cpp index 8a29c40..454cc51 100644 --- a/src/wibbly/WibblyJob.cpp +++ b/src/wibbly/WibblyJob.cpp @@ -18,6 +18,7 @@ SOFTWARE. */ +#include "RandomStuff.h" #include "WibblyJob.h" @@ -222,20 +223,22 @@ void WibblyJob::headerToScript(std::string &script) const { void WibblyJob::sourceToScript(std::string &script) const { + std::string fixed_input_file = handleSingleQuotes(input_file); + script += - "if wibbly_last_input_file == r'" + input_file + "':\n" + "if wibbly_last_input_file == r'" + fixed_input_file + "':\n" " try:\n" " src = vs.get_output(index=1)\n" // Since VapourSynth R41 get_output returns the alpha as well. " if isinstance(src, tuple):\n" " src = src[0]\n" " except KeyError:\n" - " src = c." + source_filter + "(r'" + input_file + "')\n" + " src = c." + source_filter + "(r'" + fixed_input_file + "')\n" " src.set_output(index=1)\n" "else:\n" - " src = c." + source_filter + "(r'" + input_file + "')\n" + " src = c." + source_filter + "(r'" + fixed_input_file + "')\n" " src.set_output(index=1)\n" - " wibbly_last_input_file = r'" + input_file + "'\n" + " wibbly_last_input_file = r'" + fixed_input_file + "'\n" "\n"; } diff --git a/src/wobbly/WobblyWindow.cpp b/src/wobbly/WobblyWindow.cpp index a4611da..42038af 100644 --- a/src/wobbly/WobblyWindow.cpp +++ b/src/wobbly/WobblyWindow.cpp @@ -45,6 +45,7 @@ SOFTWARE. #include "CombedFramesCollector.h" #include "ProgressDialog.h" +#include "RandomStuff.h" #include "ScrollArea.h" #include "WobblyException.h" #include "WobblyWindow.h" @@ -3681,7 +3682,7 @@ void WobblyWindow::realOpenVideo(const QString &path) { "c = vs.get_core()\n" "\n" "c.%1(r'%2').set_output()\n"); - script = script.arg(source_filter).arg(path); + script = script.arg(source_filter).arg(QString::fromStdString(handleSingleQuotes(path.toStdString()))); QApplication::setOverrideCursor(Qt::WaitCursor);