-
Notifications
You must be signed in to change notification settings - Fork 417
Expand file tree
/
Copy pathFileSource.cpp
More file actions
148 lines (121 loc) · 4.34 KB
/
FileSource.cpp
File metadata and controls
148 lines (121 loc) · 4.34 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
/* This file is part of YUView - The YUV player with advanced analytics toolset
* <https://github.com/IENT/YUView>
* Copyright (C) 2015 Institut für Nachrichtentechnik, RWTH Aachen University, GERMANY
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*
* You must obey the GNU General Public License in all respects for all
* of the code used other than OpenSSL. If you modify file(s) with this
* exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do
* so, delete this exception statement from your version. If you delete
* this exception statement from all source files in the program, then
* also delete it here.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "FileSource.h"
#include <common/Typedef.h>
#include <QFileInfo>
#include <QMutexLocker>
#ifdef Q_OS_WIN
#include <windows.h>
#endif
#define FILESOURCE_DEBUG_SIMULATESLOWLOADING 0
#if FILESOURCE_DEBUG_SIMULATESLOWLOADING && !NDEBUG
#include <QThread>
#endif
FileSource::FileSource() : FileSourceWithLocalFile()
{
}
bool FileSource::openFile(const QString &filePath)
{
QFileInfo fileInfo(filePath);
if (!fileInfo.exists() || !fileInfo.isFile())
return false;
if (this->srcFile.isOpen())
this->srcFile.close();
this->srcFile.setFileName(filePath);
this->isFileOpened = this->srcFile.open(QIODevice::ReadOnly);
if (!this->isFileOpened)
return false;
this->fullFilePath = filePath;
this->updateFileWatchSetting();
return true;
}
bool FileSource::atEnd() const
{
return !this->isFileOpened ? true : this->srcFile.atEnd();
}
QByteArray FileSource::readLine()
{
return !this->isFileOpened ? QByteArray() : this->srcFile.readLine();
}
bool FileSource::seek(int64_t pos)
{
return !this->isFileOpened ? false : this->srcFile.seek(pos);
}
int64_t FileSource::pos()
{
return !this->isFileOpened ? 0 : this->srcFile.pos();
}
#if SSE_CONVERSION
// Resize the target array if necessary and read the given number of bytes to the data array
void FileSource::readBytes(byteArrayAligned &targetBuffer, int64_t startPos, int64_t nrBytes)
{
if (!isOk())
return;
if (targetBuffer.size() < nrBytes)
targetBuffer.resize(nrBytes);
srcFile.seek(startPos);
srcFile.read(targetBuffer.data(), nrBytes);
}
#endif
int64_t FileSource::readBytes(QByteArray &targetBuffer, int64_t startPos, int64_t nrBytes)
{
if (!this->isOpened())
return 0;
if (targetBuffer.size() < nrBytes)
targetBuffer.resize(nrBytes);
#if FILESOURCE_DEBUG_SIMULATESLOWLOADING && !NDEBUG
QThread::msleep(50);
#endif
// lock the seek and read function
QMutexLocker locker(&this->readMutex);
this->srcFile.seek(startPos);
return this->srcFile.read(targetBuffer.data(), nrBytes);
}
void FileSource::clearFileCache()
{
if (!this->isFileOpened)
return;
#ifdef Q_OS_WIN
// Currently, we only support this on windows. But this is only used in performance testing.
// We will close the QFile, open it using the FILE_FLAG_NO_BUFFERING flags, close it and reopen
// the QFile. Suggested:
// http://stackoverflow.com/questions/478340/clear-file-cache-to-repeat-performance-testing
QMutexLocker locker(&this->readMutex);
this->srcFile.close();
LPCWSTR file = (const wchar_t *)this->fullFilePath.utf16();
HANDLE hFile =
CreateFile(file, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL);
CloseHandle(hFile);
this->srcFile.setFileName(this->fullFilePath);
this->srcFile.open(QIODevice::ReadOnly);
#endif
}