qtutils.cpp 21.5 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright 2012 The MITRE Corporation                                      *
 *                                                                           *
 * Licensed under the Apache License, Version 2.0 (the "License");           *
 * you may not use this file except in compliance with the License.          *
 * You may obtain a copy of the License at                                   *
 *                                                                           *
 *     http://www.apache.org/licenses/LICENSE-2.0                            *
 *                                                                           *
 * Unless required by applicable law or agreed to in writing, software       *
 * distributed under the License is distributed on an "AS IS" BASIS,         *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
 * See the License for the specific language governing permissions and       *
 * limitations under the License.                                            *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <QCryptographicHash>
#include <QDebug>
#ifndef BR_EMBEDDED
#include <QDesktopServices>
#endif // BR_EMBEDDED
#include <QFile>
#include <QFileInfo>
#include <QProcess>
#include <QProcessEnvironment>
#include <QRegExp>
#include <QRegularExpression>
#include <QStack>
#include <QUrl>
#include <openbr/openbr_plugin.h>

#include "alphanum.hpp"
#include "qtutils.h"
#include "opencvutils.h"

using namespace br;

namespace QtUtils
{

QStringList getFiles(QDir dir, bool recursive)
{
    dir = QDir(dir.canonicalPath());

    QStringList files;
    foreach (const QString &file, naturalSort(dir.entryList(QDir::Files)))
        files.append(dir.absoluteFilePath(file));

    if (!recursive) return files;

    foreach (const QString &folder, naturalSort(dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))) {
        QDir subdir(dir);
        bool success = subdir.cd(folder); if (!success) qFatal("cd failure.");
        files.append(getFiles(subdir, true));
    }
    return files;
}

QStringList getFiles(const QString &regexp)
{
    QFileInfo fileInfo(regexp);
    QDir dir(fileInfo.dir());
    QRegExp re(fileInfo.fileName());
    re.setPatternSyntax(QRegExp::Wildcard);

    QStringList files;
    foreach (const QString &fileName, dir.entryList(QDir::Files))
        if (re.exactMatch(fileName))
            files.append(dir.filePath(fileName));
    return files;
}

QStringList readLines(const QString &file)
{
    QStringList lines;
    readFile(file, lines);
    return lines;
}

void readFile(const QString &file, QStringList &lines)
{
    QByteArray data;
    readFile(file, data);
    lines = QString(data).split(QRegularExpression("[\n|\r\n|\r]"), QString::SkipEmptyParts);
    for (int i=0; i<lines.size(); i++)
        lines[i] = lines[i].simplified();
}

void readFile(const QString &file, QByteArray &data, bool uncompress)
{
    QFile f(file);
    if (!f.open(QFile::ReadOnly)) {
        if (f.exists()) qFatal("Unable to open %s for reading. Check file permissions.", qPrintable(file));
        else            qFatal("Unable to open %s for reading. File does not exist.", qPrintable(file));
    }
    data = f.readAll();
    if (uncompress) data = qUncompress(data);
    f.close();
}

void writeFile(const QString &file, const QStringList &lines)
{
    if (file.isEmpty()) return;
    const QString baseName = QFileInfo(file).baseName();

    if (baseName == "terminal") {
        printf("%s\n", qPrintable(lines.join("\n")));
    } else if (baseName == "buffer") {
        Globals->buffer = lines.join("\n").toStdString().c_str();
    } else {
        QFile f(file);
        touchDir(f);

        if (!f.open(QFile::WriteOnly))
            qFatal("Failed to open %s for writing.", qPrintable(file));

        foreach (const QString &line, lines)
            f.write((line+"\n").toLocal8Bit());

        f.close();
    }
}

void writeFile(const QString &file, const QString &data)
{
    writeFile(file, data.toLocal8Bit());
}

void writeFile(const QString &file, const QByteArray &data, int compression)
{
    if (file.isEmpty()) return;
    const QString baseName = QFileInfo(file).baseName();
    const QByteArray contents = (compression == 0) ? data : qCompress(data, compression);
    if (baseName == "terminal") {
        printf("%s\n", qPrintable(contents));
    } else if (baseName == "buffer") {
        Globals->buffer = data;
    } else {
        QFile f(file);
        touchDir(f);
        if (!f.open(QFile::WriteOnly))
            qFatal("Failed to open %s for writing.", qPrintable(file));
        f.write(contents);
        f.close();
    }
}

void copyFile(const QString &src, const QString &dst)
{
    touchDir(QFileInfo(dst));
    if (!QFile::copy(src, dst)) {
        if (QFileInfo(src).exists()) qFatal("Unable to copy %s to %s. Check file permissions.", qPrintable(src), qPrintable(dst));
        else                         qFatal("Unable to copy %s to %s. File does not exist.", qPrintable(src), qPrintable(dst));
    }
}

void touchDir(const QDir &dir)
{
    if (dir.exists(".")) return;
    if (!dir.mkpath("."))
        qFatal("Unable to create path to dir %s", qPrintable(dir.absolutePath()));
}

void touchDir(const QFile &file)
{
    touchDir(QFileInfo(file));
}

void touchDir(const QFileInfo &fileInfo)
{
    touchDir(fileInfo.dir());
}

void emptyDir(QDir &dir)
{
    foreach (const QString &folder, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks)) {
        QDir subdir(dir);
        bool success = subdir.cd(folder); if (!success) qFatal("cd failure.");
        emptyDir(subdir);
    }

    foreach (const QString &file, dir.entryList(QDir::Files))
        dir.remove(file);

    foreach (const QString &folder, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks))
        dir.rmdir(folder);

    foreach (const QString &symlink, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))
        dir.remove(symlink);
}

void deleteDir(QDir &dir)
{
    emptyDir(dir);
    dir.rmdir(".");
}

QString find(const QString &file, const QString &alt)
{
    if (QFileInfo(file).exists()) return file;
    if (QFileInfo(alt).exists()) return alt;
    qFatal("Can't find file %s or alt %s\n", qPrintable(file), qPrintable(alt));
    return "";
}

bool toBool(const QString &string)
{
    bool ok;
    bool result = (string.toFloat(&ok) != 0.f);
    if (ok) return result;
    else    return (string != "FALSE") && (string != "false") && (string != "F") && (string != "f");
}

int toInt(const QString &string)
{
    bool ok;
    int result = string.toInt(&ok); if (!ok) qFatal("Expected integer value, got %s.", qPrintable(string));
    return result;
}

float toFloat(const QString &string)
{
    bool ok;
    float result = string.toFloat(&ok); if (!ok) qFatal("Expected floating point value, got %s.", qPrintable(string));
    return result;
}

QList<float> toFloats(const QStringList &strings)
{
    QList<float> floats;
    bool ok;
    foreach (const QString &string, strings) {
        floats.append(string.toFloat(&ok));
        if (!ok) qFatal("Failed to convert %s to floating point format.", qPrintable(string));
    }
    return floats;
}

QStringList toStringList(const QList<float> &values)
{
    QStringList result; result.reserve(values.size());
    foreach (float value, values)
        result.append(QString::number(value));
    return result;
}

QStringList toStringList(const std::vector<std::string> &string_list)
{
    QStringList result;
    foreach (const std::string &string, string_list)
        result.append(QString::fromStdString(string));
    return result;
}

QStringList toStringList(int num_strings, const char *strings[])
{
    QStringList result;
    for (int i=0; i<num_strings; i++)
        result.append(strings[i]);
    return result;
}

QString shortTextHash(QString string)
{
    string.remove(QRegExp("[{}<>&]"));
    return QString(QCryptographicHash::hash(qPrintable(string), QCryptographicHash::Md5).toBase64()).remove(QRegExp("[^a-zA-Z1-9]")).left(6);
}

QStringList parse(QString args, char split, bool *ok)
{
    if (args.isEmpty()) return QStringList();

    QStringList words;
    int start = 0;
    bool inQuote = false;
    QStack<QChar> subexpressions;
    for (int i=0; i<args.size(); i++) {
        if (inQuote) {
            if (args[i] == '\'')
                inQuote = false;
        } else {
            if (args[i] == '\'') {
                inQuote = true;
            } else if ((args[i] == '(') || (args[i] == '[') || (args[i] == '<') || (args[i] == '{')) {
                subexpressions.push(args[i]);
            } else if (args[i] == ')') {
                if (subexpressions.isEmpty() || (subexpressions.pop() != '(')) {
                    if (ok) *ok = false;
                    else    qFatal("Unexpected ')'.");
                    return words;
                }
            } else if (args[i] == ']') {
                if (subexpressions.isEmpty() || (subexpressions.pop() != '[')) {
                    if (ok) *ok = false;
                    else    qFatal("Unexpected ']'.");
                    return words;
                }
            } else if (args[i] == '>') {
                if (subexpressions.isEmpty() || (subexpressions.pop() != '<')) {
                    if (ok) *ok = false;
                    else    qFatal("Unexpected '>'.");
                    return words;
                }
            } else if (args[i] == '}') {
                if (subexpressions.isEmpty() || (subexpressions.pop() != '{')) {
                    if (ok) *ok = false;
                    else    qFatal("Unexpected '}'.");
                    return words;
                }
            } else if (subexpressions.isEmpty() && (args[i] == split)) {
                words.append(args.mid(start, i-start).trimmed());
                start = i+1;
            }
        }
    }

    if (ok) *ok = true;
    words.append(args.mid(start).trimmed());
    return words;
}

void checkArgsSize(const QString &name, const QStringList &args, int min, int max)
{
    if (max == -1) max = std::numeric_limits<int>::max();
    if (max == 0) max = min;
    if (args.size() < min) qFatal("%s expects at least %d arguments, got %d", qPrintable(name), min, args.size());
    if (args.size() > max) qFatal("%s expects no more than %d arguments, got %d", qPrintable(name), max, args.size());
}

QPointF toPoint(const QString &string, bool *ok)
{
    if (string.startsWith('(') && string.endsWith(')')) {
        bool okParse;
        const QStringList words = parse(string.mid(1, string.size()-2), ',', &okParse);
        if (okParse && (words.size() == 2)) {
            float x, y;
            bool okX, okY;
            x = words[0].toFloat(&okX);
            y = words[1].toFloat(&okY);
            if (okX && okY) {
                if (ok) *ok = true;
                return QPointF(x, y);
            }
        }
    }

    if (ok) *ok = false;
    return QPointF();
}

QRectF toRect(const QString &string, bool *ok)
{
    if (string.startsWith('(') && string.endsWith(')')) {
        bool okParse;
        const QStringList words = parse(string.mid(1, string.size()-2), ',', &okParse);
        if (okParse && (words.size() == 4)) {
            float x, y, width, height;
            bool okX, okY, okWidth, okHeight;
            x = words[0].toFloat(&okX);
            y = words[1].toFloat(&okY);
            width = words[2].toFloat(&okWidth);
            height = words[3].toFloat(&okHeight);
            if (okX && okY && okWidth && okHeight) {
                if (ok) *ok = true;
                return QRectF(x, y, width, height);
            }
        }
    }

    if (ok) *ok = false;
    return QRectF();
}

QStringList naturalSort(const QStringList &strings)
{
    QList<std::string> stdStrings; stdStrings.reserve(strings.size());
    foreach (const QString &string, strings)
        stdStrings.append(string.toStdString());

    std::sort(stdStrings.begin(), stdStrings.end(), doj::alphanum_less<std::string>());

    QStringList result; result.reserve(strings.size());
    foreach (const std::string &stdString, stdStrings)
        result.append(QString::fromStdString(stdString));

    return result;
}

bool runRScript(const QString &file)
{
    QProcess RScript;
    RScript.start("Rscript", QStringList() << file);
    RScript.waitForFinished(-1);
    bool result = ((RScript.exitCode() == 0) && (RScript.error() == QProcess::UnknownError));
    if (!result) qDebug("Failed to run 'Rscript', did you forget to install R?  "
                        "See online documentation of 'br_plot' for required R packages.  "
                        "Otherwise, try running Rscript on %s to get the exact error.", qPrintable(file));
    return result;
}

bool runDot(const QString &file)
{
    QProcess dot;
    dot.start("dot -Tpdf -O " + file);
    dot.waitForFinished(-1);
    return ((dot.exitCode() == 0) && (dot.error() == QProcess::UnknownError));
}

void showFile(const QString &file)
{
#ifndef BR_EMBEDDED
    (void) file;
    // A bug in Qt5 currently prevents us from doing this:
    // QDesktopServices::openUrl(QUrl::fromLocalFile(file));
#else // BR_EMBEDDED
    (void) file;
#endif // BR_EMBEDDED
}

QString toString(const QVariant &variant)
{
    if (variant.canConvert(QVariant::List)) return toString(qvariant_cast<QVariantList>(variant));
    else if (variant.canConvert(QVariant::String)) return variant.toString();
    else if (variant.canConvert(QVariant::PointF)) {
        QPointF point = qvariant_cast<QPointF>(variant);
        return QString("(%1,%2)").arg(QString::number(point.x()),QString::number(point.y()));
    } else if (variant.canConvert(QVariant::RectF)) {
        QRectF rect = qvariant_cast<QRectF>(variant);
        return QString("(%1,%2,%3,%4)").arg(QString::number(rect.x()),
                                            QString::number(rect.y()),
                                            QString::number(rect.width()),
                                            QString::number(rect.height()));
    } else if (variant.canConvert<cv::Mat>()) return OpenCVUtils::matrixToString(variant.value<cv::Mat>());

    return QString();
}

QString toString(const QVariantList &variantList)
{
    QStringList variants;

    foreach (const QVariant &variant, variantList)
        variants.append(toString(variant));

    if (!variants.isEmpty()) return "[" + variants.join(", ") + "]";

    return QString();
}

QString toString(const QMap<QString,QVariant> &variantMap)
{
    QStringList variants;

    QMapIterator<QString, QVariant> i(variantMap);
    while (i.hasNext()) {
        i.next();
        variants.append(i.key() + "=" + toString(i.value()));
    }

    if (!variants.isEmpty()) return "[" + variants.join(", ") + "]";

    return QString();
}

QString toTime(int s)
{
    int h = s / (60*60);
    int m = (s - h*60*60) / 60;
    s = (s - h*60*60 - m*60);

    const QChar fillChar = QLatin1Char('0');

    return QString("%1:%2:%3").arg(h,2,10,fillChar).arg(m,2,10,fillChar).arg(s,2,10,fillChar);
}

float euclideanLength(const QPointF &point)
{
    return sqrt(pow(point.x(), 2) + pow(point.y(), 2));
}

float overlap(const QRectF &r, const QRectF &s) {
    QRectF intersection = r & s;

    return (intersection.width()*intersection.height())/(r.width()*r.height());
}


QString getAbsolutePath(const QString &filename)
{
    // Try adding the global path, if present
    QString withPath = (Globals->path.isEmpty() ? "" : Globals->path + "/") + filename;

    // we weren't necessarily using it to begin with, so see if that file
    // exists
    QFileInfo wpInfo(withPath);
    if (wpInfo.exists() )
        return wpInfo.absoluteFilePath();
    
    // If no, just use the nominal filename
    return QFileInfo(filename).absoluteFilePath();
}

const int base_block = 100000000;

BlockCompression::BlockCompression(QIODevice *_basis)
{
    blockSize = base_block;
    setBasis(_basis);
}

BlockCompression::BlockCompression() { blockSize = base_block;};

bool BlockCompression::open(QIODevice::OpenMode mode)
{
    this->setOpenMode(mode);
    bool res = basis->open(mode);

    if (!res)
        return false;

    blockReader.setDevice(basis);
    blockWriter.setDevice(basis);

    if (mode & QIODevice::WriteOnly) {
        precompressedBlockWriter.open(QIODevice::WriteOnly);
    }
    else if (mode & QIODevice::ReadOnly) {

        // Read an initial compressed block from the underlying QIODevice,
        // decompress, and set up a reader on it
        QByteArray compressedBlock;
        quint32 block_size;
        blockReader >> block_size;
        compressedBlock.resize(block_size);
        int read_count = blockReader.readRawData(compressedBlock.data(), block_size);
        if (read_count != block_size)
            qFatal("Failed to read initial block");

        decompressedBlock = qUncompress(compressedBlock);

        decompressedBlockReader.setBuffer(&decompressedBlock);
        decompressedBlockReader.open(QIODevice::ReadOnly);
    }

    return true;
}

void BlockCompression::close()
{
    // flush output buffer, since we may have a partial block which hasn't been 
    // written to disk yet.
    if ((openMode() & QIODevice::WriteOnly) && precompressedBlockWriter.isOpen()) {
        QByteArray compressedBlock = qCompress(precompressedBlockWriter.buffer());
        precompressedBlockWriter.close();

        quint32 bsize=  compressedBlock.size();
        blockWriter << bsize;
        blockWriter.writeRawData(compressedBlock.data(), compressedBlock.size());
    }
    // close the underlying device.
    basis->close();
}

void BlockCompression::setBasis(QIODevice *_basis)
{
    basis = _basis;
    blockReader.setDevice(basis);
    blockWriter.setDevice(basis);
}

// read from current decompressed block, if out of space, read and decompress another
// block from basis
qint64 BlockCompression::readData(char *data, qint64 remaining)
{
    qint64 initial = remaining;
    qint64 read = 0;
    while (remaining > 0) {
        // attempt to read the target amount of data
        qint64 single_read = decompressedBlockReader.read(data, remaining);
        if (single_read == -1)
            qFatal("miss read");

        remaining -= single_read;
        read += single_read;
        data += single_read;

        // need a new block if we didn't get enough bytes from the previous read
        if (remaining > 0) {
            QByteArray compressedBlock;

            // read the size of the next block
            quint32 block_size;
            blockReader >> block_size;
            if (block_size == 0)
                break;

            compressedBlock.resize(block_size);
            int actualRead = blockReader.readRawData(compressedBlock.data(), block_size);
            if (actualRead != block_size)
                qFatal("Bad read on nominal block size: %d, only got %d", block_size, remaining);

            decompressedBlock = qUncompress(compressedBlock);

            decompressedBlockReader.close();
            decompressedBlockReader.setBuffer(&decompressedBlock);
            decompressedBlockReader.open(QIODevice::ReadOnly);
        }
    }

    bool condition = blockReader.atEnd() && !basis->isReadable() ;
    if (condition)
        qWarning("Returning -1 from read");

    return condition ? -1 : read;
}

bool BlockCompression::isSequential() const
{
    return true;
}

qint64 BlockCompression::writeData(const char *data, qint64 remaining)
{
    const char * endPoint = data + remaining;
    qint64 initial = remaining;

    qint64 written = 0;

    while (remaining > 0) {
        // how much more can be put in this buffer?
        qint64 capacity = blockSize - precompressedBlockWriter.pos();
        if (capacity < 0)
            qFatal("Negative capacity!!!");

        // don't try to write beyond capacity 
        qint64 write_size = qMin(capacity, remaining);

        qint64 singleWrite = precompressedBlockWriter.write(data, write_size);

        if (singleWrite == -1)
            qFatal("matrix write failure?");

        remaining -= singleWrite;
        data += singleWrite;
        written += singleWrite;
        if (data > endPoint)
            qFatal("Wrote past the end");

        if (remaining > 0) {
            QByteArray compressedBlock = qCompress(precompressedBlockWriter.buffer(), -1);

            if (precompressedBlockWriter.buffer().size() != 0) {
                quint32 block_size = compressedBlock.size();
                blockWriter << block_size;

                int write_count = blockWriter.writeRawData(compressedBlock.data(), block_size);
                if (write_count != block_size)
                    qFatal("Didn't write enough data");
            }
            else
                qFatal("serialized empty compressed block (?)");

            precompressedBlockWriter.close();
            precompressedBlockWriter.open(QIODevice::WriteOnly);
        }
    }

    if (written != initial)
        qFatal("didn't write enough bytes");

    bool condition = basis->isWritable();
    if (!condition)
        qWarning("Returning -1 from write");

    return basis->isWritable() ? written : -1;
}



}  // namespace QtUtils