Skip to content

Commit cd9ec5a

Browse files
Merge pull request #596 from IENT/newDataSourceInterface
Add new DataSourceLocal file
2 parents ede5275 + d8100bc commit cd9ec5a

File tree

5 files changed

+402
-0
lines changed

5 files changed

+402
-0
lines changed

YUViewLib/src/common/InfoItemAndData.h

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ struct InfoItem
5656
: name(name), text(text), description(description)
5757
{
5858
}
59+
60+
bool operator==(const InfoItem &other) const
61+
{
62+
return this->name == other.name && this->text == other.text &&
63+
this->description == other.description;
64+
}
5965
};
6066

6167
struct InfoData
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/* This file is part of YUView - The YUV player with advanced analytics toolset
2+
* <https://github.com/IENT/YUView>
3+
* Copyright (C) 2015 Institut für Nachrichtentechnik, RWTH Aachen University, GERMANY
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* In addition, as a special exception, the copyright holders give
11+
* permission to link the code of portions of this program with the
12+
* OpenSSL library under certain conditions as described in each
13+
* individual source file, and distribute linked combinations including
14+
* the two.
15+
*
16+
* You must obey the GNU General Public License in all respects for all
17+
* of the code used other than OpenSSL. If you modify file(s) with this
18+
* exception, you may extend this exception to your version of the
19+
* file(s), but you are not obligated to do so. If you do not wish to do
20+
* so, delete this exception statement from your version. If you delete
21+
* this exception statement from all source files in the program, then
22+
* also delete it here.
23+
*
24+
* This program is distributed in the hope that it will be useful,
25+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27+
* GNU General Public License for more details.
28+
*
29+
* You should have received a copy of the GNU General Public License
30+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
31+
*/
32+
33+
#include "DataSourceLocalFile.h"
34+
35+
namespace filesource
36+
{
37+
38+
DataSourceLocalFile::DataSourceLocalFile(const std::filesystem::path &filePath)
39+
{
40+
this->file.open(filePath.string(), std::ios_base::in | std::ios_base::binary);
41+
if (this->isOk())
42+
this->path = filePath;
43+
}
44+
45+
std::vector<InfoItem> DataSourceLocalFile::getInfoList() const
46+
{
47+
if (!this->isOk())
48+
return {};
49+
50+
std::vector<InfoItem> infoList;
51+
infoList.push_back(
52+
InfoItem({"File Path", this->path.string(), "The absolute path of the local file"}));
53+
if (const auto size = this->fileSize())
54+
infoList.push_back(InfoItem({"File Size", std::to_string(*size)}));
55+
56+
return infoList;
57+
}
58+
59+
bool DataSourceLocalFile::atEnd() const
60+
{
61+
return this->file.eof();
62+
}
63+
64+
bool DataSourceLocalFile::isOk() const
65+
{
66+
if (this->file.fail())
67+
return this->file.eof();
68+
return true;
69+
}
70+
71+
std::int64_t DataSourceLocalFile::position() const
72+
{
73+
return this->filePosition;
74+
}
75+
76+
bool DataSourceLocalFile::seek(const std::int64_t pos)
77+
{
78+
if (!this->isOk())
79+
return false;
80+
81+
this->file.clear();
82+
this->file.seekg(static_cast<std::streampos>(pos));
83+
this->filePosition = this->file.tellg();
84+
85+
return this->isOk();
86+
}
87+
88+
std::int64_t DataSourceLocalFile::read(ByteVector &buffer, const std::int64_t nrBytes)
89+
{
90+
if (!this->isOk())
91+
return 0;
92+
93+
const auto usize = static_cast<size_t>(nrBytes);
94+
if (static_cast<std::int64_t>(buffer.size()) < nrBytes)
95+
buffer.resize(usize);
96+
97+
this->file.read(reinterpret_cast<char *>(buffer.data()), usize);
98+
99+
const auto bytesRead = this->file.gcount();
100+
buffer.resize(bytesRead);
101+
102+
this->filePosition += bytesRead;
103+
return static_cast<std::int64_t>(bytesRead);
104+
}
105+
106+
std::optional<std::int64_t> DataSourceLocalFile::fileSize() const
107+
{
108+
if (this->path.empty())
109+
return {};
110+
111+
const auto size = std::filesystem::file_size(this->path);
112+
return static_cast<std::int64_t>(size);
113+
}
114+
115+
[[nodiscard]] std::filesystem::path DataSourceLocalFile::filePath() const
116+
{
117+
return this->path;
118+
}
119+
120+
} // namespace filesource
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* This file is part of YUView - The YUV player with advanced analytics toolset
2+
* <https://github.com/IENT/YUView>
3+
* Copyright (C) 2015 Institut für Nachrichtentechnik, RWTH Aachen University, GERMANY
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* In addition, as a special exception, the copyright holders give
11+
* permission to link the code of portions of this program with the
12+
* OpenSSL library under certain conditions as described in each
13+
* individual source file, and distribute linked combinations including
14+
* the two.
15+
*
16+
* You must obey the GNU General Public License in all respects for all
17+
* of the code used other than OpenSSL. If you modify file(s) with this
18+
* exception, you may extend this exception to your version of the
19+
* file(s), but you are not obligated to do so. If you do not wish to do
20+
* so, delete this exception statement from your version. If you delete
21+
* this exception statement from all source files in the program, then
22+
* also delete it here.
23+
*
24+
* This program is distributed in the hope that it will be useful,
25+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27+
* GNU General Public License for more details.
28+
*
29+
* You should have received a copy of the GNU General Public License
30+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
31+
*/
32+
33+
#pragma once
34+
35+
#include "IDataSource.h"
36+
37+
#include <filesystem>
38+
#include <fstream>
39+
40+
namespace filesource
41+
{
42+
43+
class DataSourceLocalFile : public IDataSource
44+
{
45+
public:
46+
DataSourceLocalFile(const std::filesystem::path &filePath);
47+
48+
[[nodiscard]] std::vector<InfoItem> getInfoList() const override;
49+
[[nodiscard]] bool atEnd() const override;
50+
[[nodiscard]] bool isOk() const override;
51+
[[nodiscard]] std::int64_t position() const override;
52+
53+
[[nodiscard]] bool seek(const std::int64_t pos) override;
54+
[[nodiscard]] std::int64_t read(ByteVector &buffer, const std::int64_t nrBytes) override;
55+
56+
[[nodiscard]] std::optional<std::int64_t> fileSize() const;
57+
[[nodiscard]] std::filesystem::path filePath() const;
58+
59+
protected:
60+
std::filesystem::path path{};
61+
bool isFileOpened{};
62+
63+
std::ifstream file{};
64+
std::int64_t filePosition{};
65+
};
66+
67+
} // namespace filesource
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* This file is part of YUView - The YUV player with advanced analytics toolset
2+
* <https://github.com/IENT/YUView>
3+
* Copyright (C) 2015 Institut für Nachrichtentechnik, RWTH Aachen University, GERMANY
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* In addition, as a special exception, the copyright holders give
11+
* permission to link the code of portions of this program with the
12+
* OpenSSL library under certain conditions as described in each
13+
* individual source file, and distribute linked combinations including
14+
* the two.
15+
*
16+
* You must obey the GNU General Public License in all respects for all
17+
* of the code used other than OpenSSL. If you modify file(s) with this
18+
* exception, you may extend this exception to your version of the
19+
* file(s), but you are not obligated to do so. If you do not wish to do
20+
* so, delete this exception statement from your version. If you delete
21+
* this exception statement from all source files in the program, then
22+
* also delete it here.
23+
*
24+
* This program is distributed in the hope that it will be useful,
25+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27+
* GNU General Public License for more details.
28+
*
29+
* You should have received a copy of the GNU General Public License
30+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
31+
*/
32+
33+
#pragma once
34+
35+
#include <common/InfoItemAndData.h>
36+
#include <common/Typedef.h>
37+
38+
namespace filesource
39+
{
40+
41+
/* The data source interface defines a something that can provide data.
42+
* The source of the data could be a local file, a remote file or maybe
43+
* something that generates data. It does not matter
44+
*/
45+
class IDataSource
46+
{
47+
public:
48+
[[nodiscard]] virtual std::vector<InfoItem> getInfoList() const = 0;
49+
[[nodiscard]] virtual bool atEnd() const = 0;
50+
[[nodiscard]] virtual bool isOk() const = 0;
51+
[[nodiscard]] virtual std::int64_t position() const = 0;
52+
53+
explicit operator bool() const { return this->isOk(); }
54+
55+
[[nodiscard]] virtual bool seek(const std::int64_t pos) = 0;
56+
[[nodiscard]] virtual std::int64_t read(ByteVector &buffer, const std::int64_t nrBytes) = 0;
57+
};
58+
59+
} // namespace filesource

0 commit comments

Comments
 (0)