<commit_before>#include "qhexeditdata.h"

const qint64 QHexEditData::BUFFER_SIZE = 8192;

QHexEditData::QHexEditData(QIODevice *iodevice, QObject *parent): QObject(parent), _iodevice(iodevice), _length(iodevice->size()), _devicelength(iodevice->size()), _lastpos(-1), _lastaction(QHexEditData::None)
{
    qRegisterMetaType<QHexEditData::ActionType>("QHexEditData::ActionType");
    this->_modlist.append(new ModifiedItem(0, iodevice->size(), false));
}

QHexEditData::~QHexEditData()
{
    foreach(ModifiedItem* mi, this->_modlist)
        delete mi;

    this->_modlist.clear();

    if(this->_iodevice->isOpen())
        this->_iodevice->close();
}

QUndoStack *QHexEditData::undoStack()
{
    return &this->_undostack;
}

qint64 QHexEditData::length() const
{
    return this->_length;
}

bool QHexEditData::isReadOnly() const
{
    return !this->_iodevice->isWritable();
}

bool QHexEditData::save()
{
    return this->saveTo(this->_iodevice);
}

bool QHexEditData::saveTo(QIODevice *iodevice)
{
    if(!iodevice->isOpen())
        iodevice->open(QIODevice::WriteOnly);

    if(!iodevice->isWritable())
        return false;

    qint64 outseekpos = 0;
    this->_iodevice->reset();

    for(int i = 0; i < this->_modlist.length(); i++)
    {
        ModifiedItem* mi = this->_modlist[i];
        iodevice->seek(outseekpos);

        if(mi->modified())
            iodevice->write(this->_modbuffer.mid(mi->pos(), mi->length()));
        else if(iodevice != this->_iodevice)
        {
            this->_iodevice->seek(mi->pos());
            QByteArray ba = this->_iodevice->read(mi->length());
            iodevice->write(ba);
        }

        outseekpos += mi->length();
    }

    return true;
}

QHexEditData *QHexEditData::fromDevice(QIODevice *iodevice, QObject *parent)
{
    if(!iodevice->isOpen())
        iodevice->open(QFile::ReadWrite);

    return new QHexEditData(iodevice, parent);
}

QHexEditData *QHexEditData::fromFile(QString filename, QObject *parent)
{
    QFileInfo fi(filename);
    QFile* f = new QFile(filename);

    if(fi.isWritable())
        f->open(QFile::ReadWrite);
    else
        f->open(QFile::ReadOnly);

    return QHexEditData::fromDevice(f, parent);
}

QHexEditData *QHexEditData::fromMemory(const QByteArray &ba, QObject *parent)
{
    QBuffer* b = new QBuffer();
    b->setData(ba);
    b->open(QFile::ReadOnly);

    return QHexEditData::fromDevice(b, parent);<commit_msg>Stop leaking implicitly created QFile/QBuffer<commit_after>#include "qhexeditdata.h"

const qint64 QHexEditData::BUFFER_SIZE = 8192;

QHexEditData::QHexEditData(QIODevice *iodevice, QObject *parent): QObject(parent), _iodevice(iodevice), _length(iodevice->size()), _devicelength(iodevice->size()), _lastpos(-1), _lastaction(QHexEditData::None)
{
    qRegisterMetaType<QHexEditData::ActionType>("QHexEditData::ActionType");
    this->_modlist.append(new ModifiedItem(0, iodevice->size(), false));
}

QHexEditData::~QHexEditData()
{
    foreach(ModifiedItem* mi, this->_modlist)
        delete mi;

    this->_modlist.clear();

    if(this->_iodevice->isOpen())
        this->_iodevice->close();
}

QUndoStack *QHexEditData::undoStack()
{
    return &this->_undostack;
}

qint64 QHexEditData::length() const
{
    return this->_length;
}

bool QHexEditData::isReadOnly() const
{
    return !this->_iodevice->isWritable();
}

bool QHexEditData::save()
{
    return this->saveTo(this->_iodevice);
}

bool QHexEditData::saveTo(QIODevice *iodevice)
{
    if(!iodevice->isOpen())
        iodevice->open(QIODevice::WriteOnly);

    if(!iodevice->isWritable())
        return false;

    qint64 outseekpos = 0;
    this->_iodevice->reset();

    for(int i = 0; i < this->_modlist.length(); i++)
    {
        ModifiedItem* mi = this->_modlist[i];
        iodevice->seek(outseekpos);

        if(mi->modified())
            iodevice->write(this->_modbuffer.mid(mi->pos(), mi->length()));
        else if(iodevice != this->_iodevice)
        {
            this->_iodevice->seek(mi->pos());
            QByteArray ba = this->_iodevice->read(mi->length());
            iodevice->write(ba);
        }

        outseekpos += mi->length();
    }

    return true;
}

QHexEditData *QHexEditData::fromDevice(QIODevice *iodevice, QObject *parent)
{
    if(!iodevice->isOpen())
        iodevice->open(QFile::ReadWrite);

    return new QHexEditData(iodevice, parent);
}

QHexEditData *QHexEditData::fromFile(QString filename, QObject *parent)
{
    QFileInfo fi(filename);
    QFile* f = new QFile(filename);

    if(fi.isWritable())
        f->open(QFile::ReadWrite);
    else
        f->open(QFile::ReadOnly);

    QHexEditData* data = QHexEditData::fromDevice(f, parent);
    f->setParent(data);
    return data;
}

QHexEditData *QHexEditData::fromMemory(const QByteArray &ba, QObject *parent)
{
    QBuffer* b = new QBuffer();
    b->setData(ba);
    b->open(QFile::ReadOnly);

    QHexEditData* data = QHexEditData::fromDevice(b, parent);
    b->setParent(data);
    return data;
{'fraction_non_alphanumeric': 0.16064981949458484, 'fraction_numerical': 0.005615724027276374, 'mean_word_length': 3.758587786259542, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 95, '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': '2591968', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1794, 'n_words': 269}