From c407dd5fb1029825430a75454e2be4ba9921d9f5 Mon Sep 17 00:00:00 2001 From: Charles Otto Date: Thu, 29 May 2014 14:25:40 -0400 Subject: [PATCH] Update txtGallery and flatGallery to operate incrementally --- openbr/core/qtutils.cpp | 11 ++++++++++- openbr/plugins/gallery.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 73 insertions(+), 18 deletions(-) diff --git a/openbr/core/qtutils.cpp b/openbr/core/qtutils.cpp index eedf226..cd1f915 100644 --- a/openbr/core/qtutils.cpp +++ b/openbr/core/qtutils.cpp @@ -99,7 +99,16 @@ void readFile(const QString &file, QByteArray &data, bool uncompress) void writeFile(const QString &file, const QStringList &lines) { - writeFile(file, lines.join("\n")); + 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) diff --git a/openbr/plugins/gallery.cpp b/openbr/plugins/gallery.cpp index 5b0c2a6..bf1174c 100644 --- a/openbr/plugins/gallery.cpp +++ b/openbr/plugins/gallery.cpp @@ -574,32 +574,55 @@ class txtGallery : public Gallery Q_PROPERTY(QString label READ get_label WRITE set_label RESET reset_label STORED false) BR_PROPERTY(QString, label, "") - QStringList lines; - + QFile f; ~txtGallery() { - if (!lines.isEmpty()) - QtUtils::writeFile(file.name, lines); + f.close(); } TemplateList readBlock(bool *done) { + *done = false; + if (f.atEnd()) + f.seek(0); + TemplateList templates; - foreach (const QString &line, QtUtils::readLines(file)) { - int splitIndex = line.lastIndexOf(' '); - if (splitIndex == -1) templates.append(File(line)); - else templates.append(File(line.mid(0, splitIndex), line.mid(splitIndex+1))); + + for (qint64 i = 0; i < readBlockSize; i++) + { + QByteArray lineBytes = f.readLine(); + QString line(lineBytes); + + if (!line.isEmpty()){ + int splitIndex = line.lastIndexOf(' '); + if (splitIndex == -1) templates.append(File(line)); + else templates.append(File(line.mid(0, splitIndex), line.mid(splitIndex+1))); + } + + if (f.atEnd()) { + *done=true; + break; + } } - *done = true; + return templates; } + void init() + { + f.setFileName(file); + QtUtils::touchDir(f); + if (!f.open(QFile::ReadWrite)) + qFatal("Failed to open %s for read/write.", qPrintable(file)); + } + void write(const Template &t) { QString line = t.file.name; if (!label.isEmpty()) line += " " + t.file.get(label); - lines.append(line); + + f.write((line+"\n").toLocal8Bit() ); } }; @@ -612,26 +635,49 @@ BR_REGISTER(Gallery, txtGallery) class flatGallery : public Gallery { Q_OBJECT - QStringList lines; + QFile f; ~flatGallery() { - if (!lines.isEmpty()) - QtUtils::writeFile(file.name, lines); + f.close(); } TemplateList readBlock(bool *done) { + *done = false; + if (f.atEnd()) + f.seek(0); + TemplateList templates; - foreach (const QString &line, QtUtils::readLines(file)) - templates.append(File(line)); - *done = true; + + for (qint64 i = 0; i < readBlockSize; i++) + { + QByteArray line = f.readLine(); + + if (!line.isEmpty()) + templates.append(File(QString(line))); + + if (f.atEnd()) { + *done=true; + break; + } + } + return templates; } + void init() + { + f.setFileName(file); + QtUtils::touchDir(f); + if (!f.open(QFile::ReadWrite)) + qFatal("Failed to open %s for read/write.", qPrintable(file)); + + } + void write(const Template &t) { - lines.append(t.file.flat()); + f.write((t.file.flat()+"\n").toLocal8Bit() ); } }; -- libgit2 0.21.4