how to parse result from MsProcess::readAllStandardOutput() ?

• Sep 15, 2019 - 21:12

Hello.
I'd like to get the last line of the result for an external process execution in MuseScore. The process is called via plugin API (MsProcess), and its execution result I collect using readAllStandardOutput()

  console.log("generating MIDI file with command: "+cmd);
  proc.start(cmd);
  var val = proc.waitForFinished(30000);
  if (val) {
     var res = proc.readAllStandardOutput();
     console.log(res);
     console.log("last line of process result is: "+res.lastIndexOf("\n"));  //doesn't work, how can I parse res ?
     return(res);
  } else {
     console.log("Generation of MIDI file failed, please check (blah blah)");
     return;
  }

While the console.log(res); displays the multiple lines of execution log just fine, the next call with res.lastIndexOf("\n") fails miserably stating:

182:-1: TypeError: Property 'lastIndexOf' of object {full content of 'res' displayed} is not a function

How can I parse the content of readAllStandardOutput() or cast it into a usable (multiline) String or do anything with it a bit more complex (regexp?) than log.

Thank you!


Comments

Without being aware of what has already been said in this interesting conversation above ;-) ...

The return type is a QByteArray, which should be converted into an ArrayBuffer by the QML engine (https://doc.qt.io/qt-5/qtqml-cppintegration-data.html#qbytearray-to-jav…)

This does support the toString() prototype method (probably how the console.log processes it) so you can likely work from there. (See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Globa…). Another way is to turn it into a typed array (UInt8Array for example) and then parse those back into chars; this assumes of course that you also handle eventual encoding of the characters.

In reply to by jeetee

indeed, readAllStandardOutput().toString() does work, thanks !

What is the type of the object I get therefrom? QML QString, javascript string, or something else? (I'm not understanding exactly what's happening between the C++, the QML and the language)

In reply to by berteh

JavaScript string.

Call and language order:
1. QML proc.readAllStandardOutput()
2. C++::QMLEngine turns this into MSProcess::readAllStandardOutput()
3. C++ function is executed, returns a QByteArray within C++
4. C++::QMLEngine turns this into an ArrayBuffer compatible object QML/JavaScript
5. QML/JavaScript ends up with the result of the call, being now an ArrayBuffer JavaScript
6. JavaScript toString() is executed
7. JavaScript resulting String object is created JavaScript

The boundaries between JavaScript and QML are quite fuzzy and exist mainly of additional JavaScript objects that have a C++ "code-behind" part (such as QtQuick) and the translation layer between JavaScript/QML object types and their C++ counter parts (the QMLEngine)

Do you still have an unanswered question? Please log in first to post your question.