1 | #ifndef XMLCUSTOMCODE_H
|
---|
2 | #define XMLCUSTOMCODE_H
|
---|
3 |
|
---|
4 | #include "utilxmltools.h"
|
---|
5 |
|
---|
6 | #include <omp.h> // OpenMP support
|
---|
7 | #include <ctime> // script execution time calculation
|
---|
8 | #include <QScriptEngine>
|
---|
9 | #include <QTextStream>
|
---|
10 | #include <QCoreApplication>
|
---|
11 |
|
---|
12 | #define SLOW_SCRIPT_TIME 0.1 // if a user script takes more than 0.1 seconds to execute give a warning.
|
---|
13 | #define CUSTOM_FILES_PER_THREAD 2
|
---|
14 |
|
---|
15 | class XmlCustomCode
|
---|
16 | {
|
---|
17 | public:
|
---|
18 | XmlCustomCode();
|
---|
19 | void executeCustomCode(const QString &jsString, const QVector<QString> &filesToProcess, const bool backupsEnabled, const bool verboseEnabled);
|
---|
20 | private:
|
---|
21 | const int numThreads;
|
---|
22 | static QVector<QScriptEngine*> scriptEngines; // allows each thread to keep one script engine without always create/destruct them
|
---|
23 | static QVector<QScriptValue*> jsFunctions;
|
---|
24 | static QVector<QScriptValue*> getXmlDataFunctions;
|
---|
25 | static QVector<QScriptValue*> setXmlDataFunctions;
|
---|
26 |
|
---|
27 | void displayJsException(QScriptEngine &engine, QScriptValue &engineResult);
|
---|
28 |
|
---|
29 | __attribute__((always_inline)) inline void customCodeUnwinding(const QString &fileName, QString &currXmlFileString,
|
---|
30 | QScriptEngine &engine, clock_t &begin, double elapsed_secs, QScriptValue &engineResult, QScriptValue &jsFunction,
|
---|
31 | QScriptValue &getXmlDataFunction, QScriptValue &setXmlDataFunction, const bool &backupsEnabled, const bool &verboseEnabled){
|
---|
32 | if(backupsEnabled){
|
---|
33 | UtilXmlTools::backupFile(fileName, verboseEnabled);
|
---|
34 | }
|
---|
35 |
|
---|
36 | QFile currXmlFile(fileName);
|
---|
37 |
|
---|
38 | if(!currXmlFile.open(QFile::ReadOnly | QFile::Text)){
|
---|
39 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Error loading '" + fileName + "' file for read operation.");
|
---|
40 | }
|
---|
41 |
|
---|
42 | currXmlFileString=QTextStream(&currXmlFile).readAll();
|
---|
43 |
|
---|
44 | currXmlFile.close(); // close reading
|
---|
45 |
|
---|
46 | setXmlDataFunction.call(setXmlDataFunction,QScriptValueList() << currXmlFileString);
|
---|
47 |
|
---|
48 | begin = clock();
|
---|
49 |
|
---|
50 | engineResult=jsFunction.call(); // main funtion allows to use return to exit prematurely from user code
|
---|
51 |
|
---|
52 | if(verboseEnabled){
|
---|
53 | elapsed_secs = double(clock() - begin) / CLOCKS_PER_SEC;
|
---|
54 |
|
---|
55 | // Warn the user if the script took much time
|
---|
56 | if(elapsed_secs>SLOW_SCRIPT_TIME){
|
---|
57 | std::cout << "Warning: Slow javascript code detected.\n" <<
|
---|
58 | "Warning: Script execution seconds: " << elapsed_secs
|
---|
59 | << std::endl;
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (engine.hasUncaughtException()) {
|
---|
64 | displayJsException(engine,engineResult);
|
---|
65 | }
|
---|
66 |
|
---|
67 | if(!currXmlFile.open(QFile::WriteOnly | QFile::Text | QIODevice::Truncate)){
|
---|
68 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Error loading '" + fileName + "' file for @CUSTOM_CODE write operation.");
|
---|
69 | }
|
---|
70 |
|
---|
71 | engineResult=getXmlDataFunction.call();
|
---|
72 |
|
---|
73 | if (engine.hasUncaughtException()) {
|
---|
74 | displayJsException(engine,engineResult);
|
---|
75 | }
|
---|
76 |
|
---|
77 | QTextStream(&currXmlFile) << engineResult.toString(); // retreive the modified xml by javascript and save it to the file
|
---|
78 |
|
---|
79 | currXmlFile.close();
|
---|
80 | }
|
---|
81 | };
|
---|
82 |
|
---|
83 | #endif // XMLCUSTOMCODE_H
|
---|