Skip to content

Commit 0784db6

Browse files
authored
add is_decodable to files (#453)
1 parent 42e6ea6 commit 0784db6

31 files changed

Lines changed: 102 additions & 8 deletions

src/odr/exceptions.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,10 @@ NotEncryptedError::NotEncryptedError()
129129
InvalidPath::InvalidPath(const std::string &message)
130130
: std::runtime_error("invalid path: " + message) {}
131131

132+
UnsupportedFileEncoding::UnsupportedFileEncoding(const std::string &message)
133+
: std::runtime_error("unsupported file encoding: " + message) {}
134+
135+
FileEncryptedError::FileEncryptedError()
136+
: std::runtime_error("file encrypted error") {}
137+
132138
} // namespace odr

src/odr/exceptions.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,14 @@ struct InvalidPath final : std::runtime_error {
220220
explicit InvalidPath(const std::string &message);
221221
};
222222

223+
/// @brief Unsupported file encoding
224+
struct UnsupportedFileEncoding final : std::runtime_error {
225+
explicit UnsupportedFileEncoding(const std::string &message);
226+
};
227+
228+
/// @brief File is encrypted
229+
struct FileEncryptedError final : std::runtime_error {
230+
explicit FileEncryptedError();
231+
};
232+
223233
} // namespace odr

src/odr/file.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ DecodedFile DecodedFile::decrypt(const std::string &password) const {
125125
return DecodedFile(m_impl->decrypt(password));
126126
}
127127

128+
bool DecodedFile::is_decodable() const noexcept {
129+
return m_impl->is_decodable();
130+
}
131+
128132
bool DecodedFile::is_text_file() const {
129133
return std::dynamic_pointer_cast<internal::abstract::TextFile>(m_impl) !=
130134
nullptr;

src/odr/file.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ class DecodedFile {
200200
[[nodiscard]] EncryptionState encryption_state() const;
201201
[[nodiscard]] DecodedFile decrypt(const std::string &password) const;
202202

203+
[[nodiscard]] bool is_decodable() const noexcept;
204+
203205
[[nodiscard]] bool is_text_file() const;
204206
[[nodiscard]] bool is_image_file() const;
205207
[[nodiscard]] bool is_archive_file() const;

src/odr/internal/abstract/file.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class DecodedFile {
5050
(void)password;
5151
return nullptr;
5252
}
53+
54+
[[nodiscard]] virtual bool is_decodable() const noexcept = 0;
5355
};
5456

5557
class TextFile : public DecodedFile {

src/odr/internal/cfb/cfb_file.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ DecoderEngine CfbFile::decoder_engine() const noexcept {
2626
return DecoderEngine::odr;
2727
}
2828

29+
bool CfbFile::is_decodable() const noexcept { return true; }
30+
2931
std::shared_ptr<abstract::Archive> CfbFile::archive() const {
3032
return std::make_shared<CfbArchive>(m_cfb);
3133
}

src/odr/internal/cfb/cfb_file.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class CfbFile final : public abstract::ArchiveFile {
2727
[[nodiscard]] FileMeta file_meta() const noexcept override;
2828
[[nodiscard]] DecoderEngine decoder_engine() const noexcept override;
2929

30+
[[nodiscard]] bool is_decodable() const noexcept override;
31+
3032
[[nodiscard]] std::shared_ptr<abstract::Archive> archive() const override;
3133

3234
private:

src/odr/internal/common/image_file.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <odr/internal/common/image_file.hpp>
22

3+
#include <odr/exceptions.hpp>
4+
35
namespace odr::internal {
46

57
ImageFile::ImageFile(std::shared_ptr<abstract::File> file,
@@ -18,6 +20,10 @@ DecoderEngine ImageFile::decoder_engine() const noexcept {
1820
return DecoderEngine::odr;
1921
}
2022

21-
std::shared_ptr<abstract::Image> ImageFile::image() const { return {}; }
23+
bool ImageFile::is_decodable() const noexcept { return false; }
24+
25+
std::shared_ptr<abstract::Image> ImageFile::image() const {
26+
throw UnsupportedFileEncoding("generally unsupported");
27+
}
2228

2329
} // namespace odr::internal

src/odr/internal/common/image_file.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class ImageFile final : public abstract::ImageFile {
1414
[[nodiscard]] FileMeta file_meta() const noexcept override;
1515
[[nodiscard]] DecoderEngine decoder_engine() const noexcept override;
1616

17+
[[nodiscard]] bool is_decodable() const noexcept override;
18+
1719
[[nodiscard]] std::shared_ptr<abstract::Image> image() const override;
1820

1921
private:

src/odr/internal/csv/csv_file.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ DecoderEngine CsvFile::decoder_engine() const noexcept {
2828
return DecoderEngine::odr;
2929
}
3030

31+
bool CsvFile::is_decodable() const noexcept { return false; }
32+
3133
} // namespace odr::internal::csv

0 commit comments

Comments
 (0)