mirror of
https://github.com/miek/inspectrum.git
synced 2026-03-03 15:04:19 +01:00
Remove mmap dependencies with QFile
QFile implements cross platform file mapping and removes this dependency entirely. Also simplifies the file size and error to string handling logic.
This commit is contained in:
@@ -20,12 +20,9 @@
|
||||
|
||||
#include "inputsource.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
@@ -107,13 +104,12 @@ InputSource::~InputSource()
|
||||
void InputSource::cleanup()
|
||||
{
|
||||
if (mmapData != nullptr) {
|
||||
munmap(mmapData, fileSize);
|
||||
inputFile->unmap(mmapData);
|
||||
mmapData = nullptr;
|
||||
fileSize = 0;
|
||||
}
|
||||
|
||||
if (inputFile != nullptr) {
|
||||
fclose(inputFile);
|
||||
delete inputFile;
|
||||
inputFile = nullptr;
|
||||
}
|
||||
}
|
||||
@@ -138,28 +134,21 @@ void InputSource::openFile(const char *filename)
|
||||
sampleAdapter = std::unique_ptr<SampleAdapter>(new ComplexF32SampleAdapter());
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
FILE *file = fopen(filename, "rb");
|
||||
if (file == nullptr) {
|
||||
std::stringstream ss;
|
||||
ss << "Error opening file: " << strerror(errno) << " (" << errno << ")";
|
||||
throw std::runtime_error(ss.str());
|
||||
std::unique_ptr<QFile> file(new QFile(filename));
|
||||
if (!file->open(QFile::ReadOnly)) {
|
||||
throw std::runtime_error(file->errorString().toStdString());
|
||||
}
|
||||
|
||||
struct stat sb;
|
||||
if (fstat(fileno(file), &sb) != 0)
|
||||
throw std::runtime_error("Error fstating file");
|
||||
off_t size = sb.st_size;
|
||||
auto size = file->size();
|
||||
sampleCount = size / sampleAdapter->sampleSize();
|
||||
|
||||
auto data = mmap(NULL, size, PROT_READ, MAP_SHARED, fileno(file), 0);
|
||||
auto data = file->map(0, size);
|
||||
if (data == nullptr)
|
||||
throw std::runtime_error("Error mmapping file");
|
||||
|
||||
cleanup();
|
||||
|
||||
inputFile = file;
|
||||
fileSize = size;
|
||||
inputFile = file.release();
|
||||
mmapData = data;
|
||||
|
||||
invalidate();
|
||||
|
||||
Reference in New Issue
Block a user