Skip to content

Commit

Permalink
More work on the reading of data and lines.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianFeldmann committed Dec 26, 2024
1 parent ed4267b commit dd06969
Show file tree
Hide file tree
Showing 17 changed files with 1,547 additions and 1,157 deletions.
55 changes: 26 additions & 29 deletions YUViewLib/src/dataSource/DataSourceLineReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,44 +35,41 @@
namespace datasource
{

DataSourceLineReader::DataSourceLineReader(std::unique_ptr<IDataSource> dataSource)
: dataSource(std::move(dataSource))
DataSourceLineReader::DataSourceLineReader(IDataSource *const dataSource) : dataSource(dataSource)
{
if (nullptr == dataSource)
throw std::runtime_error("Datasource must not be null");
}

std::vector<InfoItem> DataSourceLineReader::getInfoList() const
std::optional<std::string> DataSourceLineReader::readLine()
{
return this->dataSource->getInfoList();
};
if (!this->dataSource || !this->dataSource->isOk())
return {};

bool DataSourceLineReader::atEnd() const
{
return this->dataSource->atEnd();
}
if (this->dataBuffer.empty() && this->dataSource->atEnd())
return {};

bool DataSourceLineReader::isOk() const
{
return this->dataSource->isOk();
}
std::string line;

std::int64_t DataSourceLineReader::getPosition() const
{
return this->dataSource->getPosition();
}
while (true)
{
const auto nextNewline = std::find(this->dataPosition, this->dataBuffer.end(), '\n');

bool DataSourceLineReader::wasSourceModified() const
{
return this->dataSource->wasSourceModified();
}
const auto newLineFound = (nextNewline != this->dataBuffer.end());
if (newLineFound)
{
line.append(this->dataPosition, nextNewline);
this->dataPosition = nextNewline + 1;
return line;
}

bool DataSourceLineReader::seek(const std::int64_t pos)
{
return this->dataSource->seek(pos);
}
line.append(this->dataPosition, this->dataBuffer.end());

std::string DataSourceLineReader::readLine()
{
// Fill buffer and read ...
if (this->dataSource->read(this->dataBuffer, this->bufferSize) <= 0)
return line;

this->dataPosition = this->dataBuffer.begin();
}
}

} // namespace datasource
} // namespace datasource
22 changes: 8 additions & 14 deletions YUViewLib/src/dataSource/DataSourceLineReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,18 @@ namespace datasource
class DataSourceLineReader
{
public:
DataSourceLineReader(std::unique_ptr<IDataSource> dataSource);
DataSourceLineReader() = delete;
DataSourceLineReader(IDataSource *const dataSource);

[[nodiscard]] std::vector<InfoItem> getInfoList() const;
[[nodiscard]] bool atEnd() const;
[[nodiscard]] bool isOk() const;
[[nodiscard]] std::int64_t getPosition() const;

[[nodiscard]] bool wasSourceModified() const;

[[nodiscard]] bool seek(const std::int64_t pos);
[[nodiscard]] std::string readLine();

[[nodiscard]] std::optional<std::int64_t> getFileSize() const;
[[nodiscard]] std::optional<std::string> readLine();

protected:
std::unique_ptr<IDataSource> dataSource;
IDataSource *dataSource{};

ByteVector dataBuffer;
ByteVector::iterator dataPosition{this->dataBuffer.end()};

std::string textBuffer;
std::int64_t bufferSize{1048576};
};

} // namespace datasource
20 changes: 10 additions & 10 deletions YUViewLib/src/dataSource/DataSourceLocalFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ std::vector<InfoItem> DataSourceLocalFile::getInfoList() const
std::vector<InfoItem> infoList;
infoList.push_back(
InfoItem({"File Path", this->filePath.string(), "The absolute path of the local file"}));
if (const auto size = this->getFileSize())
if (const auto size = this->getSize())
infoList.push_back(InfoItem({"File Size", std::to_string(*size)}));

return infoList;
Expand All @@ -96,6 +96,15 @@ std::int64_t DataSourceLocalFile::getPosition() const
return this->filePosition;
}

std::optional<std::int64_t> DataSourceLocalFile::getSize() const
{
if (!this->isOk())
return {};

const auto size = std::filesystem::file_size(this->filePath);
return static_cast<std::int64_t>(size);
}

void DataSourceLocalFile::clearFileCache()
{
if (!this->isOk())
Expand Down Expand Up @@ -169,15 +178,6 @@ std::int64_t DataSourceLocalFile::read(ByteVector &buffer, const std::int64_t nr
return static_cast<std::int64_t>(bytesRead);
}

std::optional<std::int64_t> DataSourceLocalFile::getFileSize() const
{
if (!this->isOk())
return {};

const auto size = std::filesystem::file_size(this->filePath);
return static_cast<std::int64_t>(size);
}

[[nodiscard]] std::filesystem::path DataSourceLocalFile::getFilePath() const
{
return this->filePath;
Expand Down
12 changes: 6 additions & 6 deletions YUViewLib/src/dataSource/DataSourceLocalFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ class DataSourceLocalFile : public IDataSource
DataSourceLocalFile() = delete;
DataSourceLocalFile(const std::filesystem::path &filePath);

[[nodiscard]] std::vector<InfoItem> getInfoList() const override;
[[nodiscard]] bool atEnd() const override;
[[nodiscard]] bool isOk() const override;
[[nodiscard]] std::int64_t getPosition() const override;
[[nodiscard]] std::vector<InfoItem> getInfoList() const override;
[[nodiscard]] bool atEnd() const override;
[[nodiscard]] bool isOk() const override;
[[nodiscard]] std::int64_t getPosition() const override;
[[nodiscard]] std::optional<std::int64_t> getSize() const override;

void clearFileCache() override;
[[nodiscard]] bool wasSourceModified() const override;
Expand All @@ -59,8 +60,7 @@ class DataSourceLocalFile : public IDataSource
[[nodiscard]] bool seek(const std::int64_t pos) override;
[[nodiscard]] std::int64_t read(ByteVector &buffer, const std::int64_t nrBytes) override;

[[nodiscard]] std::optional<std::int64_t> getFileSize() const;
[[nodiscard]] std::filesystem::path getFilePath() const;
[[nodiscard]] std::filesystem::path getFilePath() const;

protected:
std::filesystem::path filePath{};
Expand Down
15 changes: 9 additions & 6 deletions YUViewLib/src/dataSource/IDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,22 @@ namespace datasource
class IDataSource
{
public:
[[nodiscard]] virtual std::vector<InfoItem> getInfoList() const = 0;
[[nodiscard]] virtual bool atEnd() const = 0;
[[nodiscard]] virtual bool isOk() const = 0;
[[nodiscard]] virtual std::int64_t getPosition() const = 0;
virtual ~IDataSource() = default;

[[nodiscard]] virtual std::vector<InfoItem> getInfoList() const = 0;
[[nodiscard]] virtual bool atEnd() const = 0;
[[nodiscard]] virtual bool isOk() const = 0;
[[nodiscard]] virtual std::int64_t getPosition() const = 0;
[[nodiscard]] virtual std::optional<std::int64_t> getSize() const = 0;

virtual void clearFileCache() = 0;
[[nodiscard]] virtual bool wasSourceModified() const = 0;
virtual void reloadAndResetDataSource() = 0;

explicit operator bool() const { return this->isOk(); }

[[nodiscard]] virtual bool seek(const std::int64_t pos) = 0;
[[nodiscard]] virtual std::int64_t read(ByteVector &buffer, const std::int64_t nrBytes) = 0;
virtual bool seek(const std::int64_t pos) = 0;
virtual std::int64_t read(ByteVector &buffer, const std::int64_t nrBytes) = 0;
};

} // namespace datasource
20 changes: 10 additions & 10 deletions YUViewLib/src/playlistitem/playlistItemStatisticsFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ bool playlistItemStatisticsFile::isSourceChanged()
void playlistItemStatisticsFile::updateSettings()
{
this->statisticsUIHandler.updateSettings();
if (this->file)
this->file->updateSettings();
// if (this->file)
// this->file->updateSettings();
}

void playlistItemStatisticsFile::getSupportedFileExtensions(QStringList &allExtensions,
Expand Down Expand Up @@ -275,14 +275,14 @@ void playlistItemStatisticsFile::openStatisticsFile()
}

auto suffix = QFileInfo(this->prop.name).suffix();
if (this->openMode == OpenMode::CSVFile ||
(this->openMode == OpenMode::Extension && suffix == "csv"))
this->file.reset(new stats::StatisticsFileCSV(this->prop.name, this->statisticsData));
else if (this->openMode == OpenMode::VTMBMSFile ||
(this->openMode == OpenMode::Extension && suffix == "vtmbmsstats"))
this->file.reset(new stats::StatisticsFileVTMBMS(this->prop.name, this->statisticsData));
else
assert(false);
// if (this->openMode == OpenMode::CSVFile ||
// (this->openMode == OpenMode::Extension && suffix == "csv"))
// this->file.reset(new stats::StatisticsFileCSV(this->prop.name, this->statisticsData));
// else if (this->openMode == OpenMode::VTMBMSFile ||
// (this->openMode == OpenMode::Extension && suffix == "vtmbmsstats"))
// this->file.reset(new stats::StatisticsFileVTMBMS(this->prop.name, this->statisticsData));
// else
// assert(false);

connect(this->file.get(),
&stats::StatisticsFileBase::readPOC,
Expand Down
Loading

0 comments on commit dd06969

Please sign in to comment.