MCPcopy Create free account
hub / github.com/TeXworks/texworks / readFile

Method readFile

src/scripting/ScriptAPI.cpp:324–357  ·  view source on GitHub ↗

Q_INVOKABLE

Source from the content-addressed store, hash-verified

322
323//Q_INVOKABLE
324QMap<QString, QVariant> ScriptAPI::readFile(const QString& filename) const
325{
326 // relative paths are taken to be relative to the folder containing the
327 // executing script's file
328 QMap<QString, QVariant> retVal;
329
330 retVal[QString::fromLatin1("status")] = SystemAccess_PermissionDenied;
331 retVal[QString::fromLatin1("result")] = QVariant();
332 retVal[QString::fromLatin1("message")] = QVariant();
333
334 QDir scriptDir(QFileInfo(m_script->getFilename()).dir());
335 QString path = scriptDir.absoluteFilePath(filename);
336
337 if (!mayReadFile(path, m_target)) {
338 retVal[QString::fromLatin1("message")] = tr("Reading all files is disabled (see Preferences)");
339 retVal[QString::fromLatin1("status")] = ScriptAPI::SystemAccess_PermissionDenied;
340 return retVal;
341 }
342
343 QFile fin(path);
344
345 if (!fin.open(QIODevice::ReadOnly | QIODevice::Text)) {
346 retVal[QString::fromLatin1("message")] = tr("The file \"%1\" could not be opened for reading").arg(path);
347 retVal[QString::fromLatin1("status")] = ScriptAPI::SystemAccess_Failed;
348 return retVal;
349 }
350
351 // with readAll, there's no way to detect an error during the actual read
352 retVal[QString::fromLatin1("result")] = QString::fromUtf8(fin.readAll().constData());
353 retVal[QString::fromLatin1("status")] = ScriptAPI::SystemAccess_OK;
354 fin.close();
355
356 return retVal;
357}
358
359int ScriptAPI::fileExists(const QString& filename) const
360{

Callers

nothing calls this directly

Calls 3

getFilenameMethod · 0.45
absoluteFilePathMethod · 0.45
openMethod · 0.45

Tested by

no test coverage detected