Debian Codegraph @

src/autoloaderwriter.h

raw symbols SCIP
1 // Copyright (C) 2014 Sacha Refshauge
2
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 3.0.
6
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 3.0 for more details.
11
12 // A copy of the GPL 3.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14
15 // Official GIT repository and contact information can be found at
16 // http://github.com/xsacha/Sachesi
17
18 #pragma once
19 #include <QApplication>
20 #include <QList>
21 #include <QFile>
22 #include "fs/fs.h" // QNXStream
23 #include "ports.h"
24
25 class AutoloaderWriter: public QFile {
26 Q_OBJECT
27 public:
28 AutoloaderWriter(QList<QIODevice*> devices)
29 : _devHandle(devices)
30 , _finished(false)
31 {
32
33 }
34
35 void kill() {
36 _finished = true;
37 if (isOpen())
38 close();
39 remove();
40 }
41
42 void create(QString name) {
43 _maxSize = 0x10000000; // A very large number :)
44 // Find potential file
45 QString append = ".exe";
46 for (int f = 2; QFile::exists(name + append); f++) {
47 append = QString("-%1.exe").arg(QString::number(f));
48 }
49 // Start the autoloader as a cap file
50 setFileName(name + append);
51 open(QIODevice::WriteOnly);
52 QFile cap(capPath());
53 cap.open(QIODevice::ReadOnly);
54 appendFile(&cap);
55
56 // This code is used as a separator
57 write(QByteArray::fromBase64("at9dFE5LT0dJSE5JTk1TDRAMBRceERhTLUY8T0crSzk5OVNOT1FNT09RTU9RSEhwnNXFl5zVxZec1cWX").constData(), 60);
58 // This is a placeholder for a password
59 write(QByteArray(80, 0), 80);
60
61 QByteArray dataHeader;
62 QNXStream dataStream(&dataHeader, QIODevice::WriteOnly);
63 dataStream << (quint64)_devHandle.count();
64 quint64 counter = pos() + 64;
65 foreach (QIODevice* file, _devHandle)
66 {
67 dataStream << counter;
68 counter += file->size();
69 }
70 for (int i = _devHandle.count() - 1; i < 6; i++)
71 dataStream << (qint64)0;
72 write(dataHeader);
73 _read = 100 * pos();
74 _maxSize = counter;
75 resize(_maxSize);
76 foreach (QIODevice* file, _devHandle)
77 if (!_finished) appendFile(file);
78 if (isOpen())
79 close();
80 }
81
82 void appendFile(QIODevice* file) {
83 while (!file->atEnd())
84 {
85 // Check if we are being told to leave
86 qApp->processEvents();
87 if (_finished)
88 return;
89 int writeSize = write(file->read(FAST_BUFFER_LEN));
90 if (writeSize < 0) {
91 kill();
92 return;
93 }
94 _read += 100 * writeSize;
95 emit newProgress((int)(_read / _maxSize));
96 }
97 file->close();
98 }
99 signals:
100 void newProgress(int percent);
101 private:
102 qint64 _read, _maxSize;
103 QList<QIODevice*> _devHandle;
104 bool _finished;
105 };