-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainObject.cpp
More file actions
72 lines (66 loc) · 2.92 KB
/
MainObject.cpp
File metadata and controls
72 lines (66 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "MainObject.h"
#include <iostream>
#include <Matlab/MatlabEngineManager.h>
MainObject::MainObject(QObject *parent) {
Q_UNUSED(parent)
}
MainObject::~MainObject() {
}
void MainObject::run() {
std::cout << "Thread is starting" <<std::endl;
// Implementation
if (MManager::instance().isMatlabAvailable()) {
std::cout << "MATLAB library is available." << std::endl;
std::cout << "MATLAB C Library is found at:\""
<< QDir::toNativeSeparators(MManager::instance().matlabDir().absolutePath()).toStdString()
<< "\"" << std::endl;
if (MLibs::instance().mxEngine().loadLib() && MLibs::instance().mxMatrix().loadLib()) {
std::cout << "MATALB engine library loaded!" << std::endl;
std::cout << "\nStart task - Generating a strong password...\n" << std::endl;
runCommand();
std::cout << "Task completed." << std::endl;
MLibs::instance().mxEngine().unloadLib();
MLibs::instance().mxMatrix().unloadLib();
}
else {
std::cout << "Failed to load MATALB engine library!" << std::endl;
}
}
else {
std::cout << "MATLAB is not available." << std::endl;
}
// End of implementation
// End of run. Emit signal
emit finished();
std::cout << "Thread end" << std::endl;
}
int MainObject::runCommand() {
GET_FP(MLibs::instance().mxMatrix().lib(),mxArrayToString,mxArrayToString)
GET_FP(MLibs::instance().mxMatrix().lib(),mxDestroyArray,mxDestroyArray)
int engineId = 0;
std::cout << "\tSummon MATLAB computational engine..." << std::endl;
MInterface eng = MManager::instance().getEngineById(engineId);
std::cout << "\tLet's use MATLAB to generat a strong password" << std::endl;
eng.runCommand("rng(round(second(datetime('now'))));r = floor(rand(1,16)*93)+33;password=char(r);");
std::cout << "\tDone! Retriving output from MATLAB computational engine...." << std::endl;
mxArray* mPassword = eng.getVar("password");
std::string password = mxArrayToString(mPassword);
std::cout << "\tGenerated password: " << password << std::endl;
std::cout << "\tClosing MATLAB engine..." << std::endl;
MManager::instance().deleteEngineById(engineId);
std::cout << "\tPreparing to save the password to a MAT file \"savedPassword.mat\"..." << std::endl;
if (MLibs::instance().mxMat().loadLib()) { // Save the output to a MAT file
GET_FP(MLibs::instance().mxMat().lib(),matOpen,matOpen)
GET_FP(MLibs::instance().mxMat().lib(),matPutVariable,matPutVariable)
GET_FP(MLibs::instance().mxMat().lib(),matClose,matClose)
MATFile *pmat;
pmat = matOpen("savedPassword.mat","w");
if (pmat) {
matPutVariable(pmat,"password",mPassword);
matClose(pmat);
}
std::cout << "\tDone!" << std::endl;
}
mxDestroyArray(mPassword);
return 0;
}