<commit_before>#include "BtTorrent.h"
#include <QByteArray>
#include <QIODevice>
#include <QDebug>
#include <QTextDecoder>
#include <QTextCodec>
#include <QMetaType>
#include <QTextStream>

using namespace BtQt;

static QTextStream& qStdOut(int tabs = 0, bool useBlank = true) {
    static QTextStream ts(stdout);
    for (auto i = 0; i < tabs; i ++) {
        if(useBlank) ts << "  ";
        else ts << "\t";
    }
    return ts;
}

static void displayQMap(QMap<QString, QVariant> const &, int = 0);
static void displayQList(QList<QVariant> const &, int = 0);

static void displayQMap(QMap<QString, QVariant> const &map, int tabs)
{
    qStdOut(tabs) << "{" << endl;
    ++ tabs;
    for(auto key : map.keys()) {
        qStdOut(tabs) << key << ": ";
        QVariant value = map.value(key);

        if(value.canConvert(QMetaType::QString)) {
            qStdOut() << value.toString() << endl;
        } else if(value.canConvert(QMetaType::QByteArray)) {
            qStdOut() << value.toByteArray() << endl;
        } else if(value.canConvert(QMetaType::QVariantList)) {
            qStdOut() << endl;
            displayQList(value.toList(), tabs);
        } else if(value.canConvert(QMetaType::QVariantMap)) {
            qStdOut() << endl;
            displayQMap(value.toMap(), tabs);
        } else {
            qDebug() << "There are illegal types.";
        }
    }
    -- tabs;
    qStdOut(tabs) << "}" << endl;
}

static void displayQList(QList<QVariant> const &list, int tabs)
{
    qStdOut(tabs) << "[" << endl;
    ++ tabs;
    for(auto value : list) {
        if(value.canConvert(QMetaType::QString)) {
            qStdOut(tabs) << value.toString() << endl;
        } else if(value.canConvert(QMetaType::QByteArray)) {
            qStdOut(tabs) << value.toByteArray() << endl;
        } else if(value.canConvert(QMetaType::QVariantList)) {
            displayQList(value.toList(), tabs);
        } else if(value.canConvert(QMetaType::QVariantMap)) {
            displayQMap(value.toMap(), tabs);
        } else {
            qDebug() << "There are illegal types.";
        }
    }
    -- tabs;
    qStdOut(tabs) << "]" << endl;
}

void BtTorrent::display() const
{
    if(!isParsed) {
        qDebug() << "BtTorrent object is empty.";
        return;
    }

    displayQMap(torrentObject);
    /*
     *qInfo() << torrentObject;
     */
}

bool BtTorrent::encodeTorrentFile(QFile &torrentFile)
{
    if(!isParsed) {
        qDebug() << "Can not encode an empty object.";
        return false;
    }

    QByteArray encoded;
    try {
        /* The pieces's value is sha-1 lists (hex encoded),
         * so we should join the lists together */
        QMap<QString, QVariant> tmpInfo = torrentInfo;
        QByteArray piecesHashList;
        for(auto i : torrentPieces) {
            auto pieceHash = QByteArray::fromHex(i.toByteArray());
            piecesHashList.append(pieceHash);
        }
        qDebug() << torrentPieces.size();
        qDebug() << piecesHashList.size();
        tmpInfo["pieces"].setValue(piecesHashList);
        qDebug() << tmpInfo["pieces"].toByteArray().size();

        /* Temperarily change pieces to hashlist */
        QVariant &tInfo = torrentObject["info"];
        tInfo.setValue(tmpInfo);

        qDebug() << torrentObject["info"].toMap().value("pieces").toByteArray().size();

        BtEncodeBencodeMap(torrentObject, encoded);
        qDebug() << encoded.size();
        /* Change back */
        tInfo.setValue(torrentInfo);
    } catch (std::exception e) {
        qDebug() << "Can not encode this object. Error occurs!";
        return false;
    }

    if(!torrentFile.open(QIODevice::WriteOnly)) {
        qDebug() << "Can not open file " << torrentFile.fileName() << " in write-only mode.";
        return false;
    }

    if(torrentFile.write(encoded) == -1) {
        qDebug() << "Can not write to file " << torrentFile.fileName();
        torrentFile.close();
        return false;
    }
<commit_msg>Remove harmful/useless operations in torrent encoding!<commit_after>#include "BtTorrent.h"
#include <QByteArray>
#include <QIODevice>
#include <QDebug>
#include <QTextDecoder>
#include <QTextCodec>
#include <QMetaType>
#include <QTextStream>

using namespace BtQt;

static QTextStream& qStdOut(int tabs = 0, bool useBlank = true) {
    static QTextStream ts(stdout);
    for (auto i = 0; i < tabs; i ++) {
        if(useBlank) ts << "  ";
        else ts << "\t";
    }
    return ts;
}

static void displayQMap(QMap<QString, QVariant> const &, int = 0);
static void displayQList(QList<QVariant> const &, int = 0);

static void displayQMap(QMap<QString, QVariant> const &map, int tabs)
{
    qStdOut(tabs) << "{" << endl;
    ++ tabs;
    for(auto key : map.keys()) {
        qStdOut(tabs) << key << ": ";
        QVariant value = map.value(key);

        if(value.canConvert(QMetaType::QString)) {
            qStdOut() << value.toString() << endl;
        } else if(value.canConvert(QMetaType::QByteArray)) {
            qStdOut() << value.toByteArray() << endl;
        } else if(value.canConvert(QMetaType::QVariantList)) {
            qStdOut() << endl;
            displayQList(value.toList(), tabs);
        } else if(value.canConvert(QMetaType::QVariantMap)) {
            qStdOut() << endl;
            displayQMap(value.toMap(), tabs);
        } else {
            qDebug() << "There are illegal types.";
        }
    }
    -- tabs;
    qStdOut(tabs) << "}" << endl;
}

static void displayQList(QList<QVariant> const &list, int tabs)
{
    qStdOut(tabs) << "[" << endl;
    ++ tabs;
    for(auto value : list) {
        if(value.canConvert(QMetaType::QString)) {
            qStdOut(tabs) << value.toString() << endl;
        } else if(value.canConvert(QMetaType::QByteArray)) {
            qStdOut(tabs) << value.toByteArray() << endl;
        } else if(value.canConvert(QMetaType::QVariantList)) {
            displayQList(value.toList(), tabs);
        } else if(value.canConvert(QMetaType::QVariantMap)) {
            displayQMap(value.toMap(), tabs);
        } else {
            qDebug() << "There are illegal types.";
        }
    }
    -- tabs;
    qStdOut(tabs) << "]" << endl;
}

void BtTorrent::display() const
{
    if(!isParsed) {
        qDebug() << "BtTorrent object is empty.";
        return;
    }

    displayQMap(torrentObject);
    /*
     *qInfo() << torrentObject;
     */
}

bool BtTorrent::encodeTorrentFile(QFile &torrentFile)
{
    if(!isParsed) {
        qDebug() << "Can not encode an empty object.";
        return false;
    }

    QByteArray encoded;
    try {
        /* The pieces's value is sha-1 lists (hex encoded),
         * so we should join the lists together */
        QMap<QString, QVariant> tmpInfo = torrentInfo;
        QByteArray piecesHashList;
        for(auto i : torrentPieces) {
            auto pieceHash = QByteArray::fromHex(i.toByteArray());
            piecesHashList.append(pieceHash);
        }
        tmpInfo["pieces"].setValue(piecesHashList);

        /* Make a copy of torrentObject,
         * and change pieces to hashlist */
        QMap<QString, QVariant> tmpObject = torrentObject;
        QVariant &tInfo = tmpObject["info"];
        tInfo.setValue(tmpInfo);

        BtEncodeBencodeMap(tmpObject, encoded);
    } catch (std::exception e) {
        qDebug() << "Can not encode this object. Error occurs!";
        return false;
    }

    if(!torrentFile.open(QIODevice::WriteOnly)) {
        qDebug() << "Can not open file " << torrentFile.fileName() << " in write-only mode.";
        return false;
    }

    if(torrentFile.write(encoded) == -1) {
        qDebug() << "Can not write to file " << torrentFile.fileName();
        torrentFile.close();
        return false;
    }

{'fraction_non_alphanumeric': 0.14908197569175072, 'fraction_numerical': 0.0015515903801396431, 'mean_word_length': 2.515909090909091, 'pattern_counts': {'":': 2, '<': 172, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4466375', 'n_tokens_mistral': 2520, 'n_tokens_neox': 2453, 'n_words': 529}