Skip to content

Commit

Permalink
Fix opening videos with single quotes in the file name
Browse files Browse the repository at this point in the history
  • Loading branch information
cantabile committed Jun 12, 2020
1 parent 0a0c067 commit 0494f7e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
58 changes: 58 additions & 0 deletions src/shared/RandomStuff.h
Original file line number Diff line number Diff line change
@@ -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 <string>
#include <vector>


static std::string handleSingleQuotes(const std::string &path) {
// Turn <afkjhg'sgsh'fhdfh> into <afkjhg r"'" r'sgsh' r"'" r'fhdfh>
// 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<size_t> 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
3 changes: 2 additions & 1 deletion src/shared/WobblyProject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ SOFTWARE.
#include "rapidjson/prettywriter.h"
#include "rapidjson/error/en.h"

#include "RandomStuff.h"
#include "WobblyException.h"
#include "WobblyProject.h"

Expand Down Expand Up @@ -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 +=
Expand Down
11 changes: 7 additions & 4 deletions src/wibbly/WibblyJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ SOFTWARE.
*/


#include "RandomStuff.h"
#include "WibblyJob.h"


Expand Down Expand Up @@ -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";
}

Expand Down
3 changes: 2 additions & 1 deletion src/wobbly/WobblyWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ SOFTWARE.

#include "CombedFramesCollector.h"
#include "ProgressDialog.h"
#include "RandomStuff.h"
#include "ScrollArea.h"
#include "WobblyException.h"
#include "WobblyWindow.h"
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 0494f7e

Please sign in to comment.