Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OpenFlipper-Free
OpenFlipper-Free
Commits
123e27be
Commit
123e27be
authored
Mar 17, 2016
by
Jan Möbius
Browse files
Added include statement for scripts
parent
bfd9c4f1
Pipeline
#907
passed with stage
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
OpenFlipper/Documentation/DeveloperHelpSources/scripting.docu
View file @
123e27be
...
@@ -111,6 +111,16 @@
...
@@ -111,6 +111,16 @@
// The path of the currently executed script is stored in the ScriptPath variable.
// The path of the currently executed script is stored in the ScriptPath variable.
print(ScriptPath)
print(ScriptPath)
\endcode
\endcode
\subsection scripting_misc_includes Can i include additional script files?
Yes, just use the keyword include. You can also use paths relative to the current script.
The scripts will be joined internally so variables are available across the scripts.
\code
// Include with full path:
include <Path/script1.ofs>
// Relative to current script:
include <ScriptPath/script2.ofs>
\endcode
\section scripting_examples Scripting Examples
\section scripting_examples Scripting Examples
\subsection scripting_examples_qstringlist Iterating over a QStringList
\subsection scripting_examples_qstringlist Iterating over a QStringList
...
...
Plugin-Scripting/ScriptingPlugin.cc
View file @
123e27be
...
@@ -352,6 +352,55 @@ void ScriptingPlugin::slotExecuteScript( QString _script ) {
...
@@ -352,6 +352,55 @@ void ScriptingPlugin::slotExecuteScript( QString _script ) {
// Get the filename of the script and set it in the scripting environment
// Get the filename of the script and set it in the scripting environment
engine
->
globalObject
().
setProperty
(
"ScriptPath"
,
OpenFlipper
::
Options
::
currentScriptDirStr
());
engine
->
globalObject
().
setProperty
(
"ScriptPath"
,
OpenFlipper
::
Options
::
currentScriptDirStr
());
// Check if the script contains include statements
if
(
_script
.
contains
(
QRegExp
(
"^include <"
))
)
{
// Split input script into lines
QStringList
script
=
_script
.
split
(
QRegExp
(
"[
\r\n
]"
),
QString
::
SkipEmptyParts
);
// Find first include statement
int
include_index
=
script
.
indexOf
(
QRegExp
(
"^include.*"
));
while
(
include_index
!=
-
1
)
{
QString
include_statement
=
script
[
include_index
];
// Extract the file path of the include
include_statement
.
remove
(
QRegExp
(
"^include"
)
);
include_statement
.
remove
(
"<"
);
include_statement
.
remove
(
">"
);
include_statement
=
include_statement
.
trimmed
();
// Replace the ScriptPath component
include_statement
.
replace
(
"ScriptPath"
,
OpenFlipper
::
Options
::
currentScriptDirStr
());
QFile
includeFile
(
include_statement
);
if
(
!
includeFile
.
exists
()
)
{
emit
log
(
LOGERR
,
"Script file include not found : "
+
include_statement
+
" from "
+
script
[
include_index
]
);
return
;
}
else
{
if
(
!
includeFile
.
open
(
QFile
::
ReadOnly
|
QFile
::
Text
))
{
emit
log
(
LOGERR
,
"Unable to open file : "
+
include_statement
);
return
;
}
QTextStream
in
(
&
includeFile
);
script
[
include_index
]
=
in
.
readAll
();
includeFile
.
close
();
}
// Recombine all script components
_script
=
script
.
join
(
"
\n
"
);
// Check for next occurence of an include statement
include_index
=
script
.
indexOf
(
QRegExp
(
"^include.*"
));
}
}
// Execute the script
// Execute the script
engine
->
evaluate
(
_script
);
engine
->
evaluate
(
_script
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment