Skip to content

Commit 518f922

Browse files
committed
using xtd::helpers::throw_helper class
1 parent a8bc071 commit 518f922

File tree

10 files changed

+283
-256
lines changed

10 files changed

+283
-256
lines changed

src/xtd.core/include/xtd/helpers/exception_case.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ namespace xtd {
3434
argument_null,
3535
/// @brief The argument is out of range.
3636
argument_out_of_range,
37+
/// @brief The directory is not found.
38+
directory_not_found,
3739
/// @brief The end of stream is reached.
3840
end_of_stream,
3941
/// @brief The file is not found.
@@ -64,8 +66,14 @@ namespace xtd {
6466
null_pointer,
6567
/// @brief Arithmetic overflow.
6668
overflow,
69+
/// @brief The path is too long.
70+
path_too_long,
71+
/// @brief The platform is not supported.
72+
platform_not_supported,
6773
/// @brief The rank is not valid.
6874
rank,
75+
/// @brief The access is denied.
76+
unauthorized_access,
6977
};
7078
}
7179
}

src/xtd.core/include/xtd/unauthorized_access_exception.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
/// @brief The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
99
namespace xtd {
10-
/// @brief The exception that is thrown when one of the arguments provided to a method is not valid.
10+
/// @brief The exception that is thrown when the operating system denies access because of an I/O error or a specific type of security error.
1111
/// @par Namespace
1212
/// xtd
1313
/// @par Library

src/xtd.core/src/xtd/helpers/throw_helper.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
#undef __XTD_CORE_INTERNAL__
44
#include "../../../include/xtd/collections/generic/key_not_found_exception.hpp"
55
#include "../../../include/xtd/diagnostics/stack_frame.hpp"
6+
#include "../../../include/xtd/io/directory_not_found_exception.hpp"
67
#include "../../../include/xtd/io/end_of_stream_exception.hpp"
78
#include "../../../include/xtd/io/file_not_found_exception.hpp"
89
#include "../../../include/xtd/io/io_exception.hpp"
10+
#include "../../../include/xtd/io/path_too_long_exception.hpp"
911
#include "../../../include/xtd/argument_exception.hpp"
1012
#include "../../../include/xtd/argument_null_exception.hpp"
1113
#include "../../../include/xtd/argument_out_of_range_exception.hpp"
@@ -16,8 +18,10 @@
1618
#include "../../../include/xtd/not_implemented_exception.hpp"
1719
#include "../../../include/xtd/null_pointer_exception.hpp"
1820
#include "../../../include/xtd/overflow_exception.hpp"
21+
#include "../../../include/xtd/platform_not_supported_exception.hpp"
1922
#include "../../../include/xtd/rank_exception.hpp"
2023
#include "../../../include/xtd/typeof.hpp"
24+
#include "../../../include/xtd/unauthorized_access_exception.hpp"
2125

2226
using namespace xtd;
2327
using namespace xtd::collections::generic;
@@ -36,6 +40,7 @@ void throw_helper::throws(enum exception_case exception_case, const source_locat
3640
case exception_case::argument: throw argument_exception {to_stack_frame(location)};
3741
case exception_case::argument_null: throw argument_null_exception {to_stack_frame(location)};
3842
case exception_case::argument_out_of_range: throw argument_out_of_range_exception {to_stack_frame(location)};
43+
case exception_case::directory_not_found: throw directory_not_found_exception {to_stack_frame(location)};
3944
case exception_case::end_of_stream: throw end_of_stream_exception {to_stack_frame(location)};
4045
case exception_case::file_not_found: throw file_not_found_exception {to_stack_frame(location)};
4146
case exception_case::format: throw format_exception {to_stack_frame(location)};
@@ -51,7 +56,10 @@ void throw_helper::throws(enum exception_case exception_case, const source_locat
5156
case exception_case::not_implemented: throw not_implemented_exception {to_stack_frame(location)};
5257
case exception_case::null_pointer: throw null_pointer_exception {to_stack_frame(location)};
5358
case exception_case::overflow: throw overflow_exception {to_stack_frame(location)};
59+
case exception_case::path_too_long: throw path_too_long_exception {to_stack_frame(location)};
60+
case exception_case::platform_not_supported: throw platform_not_supported_exception {to_stack_frame(location)};
5461
case exception_case::rank: throw rank_exception {to_stack_frame(location)};
62+
case exception_case::unauthorized_access: throw unauthorized_access_exception {to_stack_frame(location)};
5563
default: throw argument_exception {"Invalid xtd::helpers::exception_case value"};
5664
}
5765
}
@@ -62,6 +70,7 @@ void throw_helper::throws(enum exception_case exception_case, const char* messag
6270
case exception_case::argument: throw argument_exception {message, to_stack_frame(location)};
6371
case exception_case::argument_null: throw argument_null_exception {message, to_stack_frame(location)};
6472
case exception_case::argument_out_of_range: throw argument_out_of_range_exception {message, to_stack_frame(location)};
73+
case exception_case::directory_not_found: throw directory_not_found_exception {message, to_stack_frame(location)};
6574
case exception_case::end_of_stream: throw end_of_stream_exception {message, to_stack_frame(location)};
6675
case exception_case::file_not_found: throw file_not_found_exception {message, to_stack_frame(location)};
6776
case exception_case::format: throw format_exception {message, to_stack_frame(location)};
@@ -77,7 +86,10 @@ void throw_helper::throws(enum exception_case exception_case, const char* messag
7786
case exception_case::not_implemented: throw not_implemented_exception {message, to_stack_frame(location)};
7887
case exception_case::null_pointer: throw null_pointer_exception {message, to_stack_frame(location)};
7988
case exception_case::overflow: throw overflow_exception {message, to_stack_frame(location)};
89+
case exception_case::path_too_long: throw path_too_long_exception {message, to_stack_frame(location)};
90+
case exception_case::platform_not_supported: throw platform_not_supported_exception {message, to_stack_frame(location)};
8091
case exception_case::rank: throw rank_exception {message, to_stack_frame(location)};
92+
case exception_case::unauthorized_access: throw unauthorized_access_exception {message, to_stack_frame(location)};
8193
default: throw argument_exception {"Invalid xtd::helpers::exception_case value"};
8294
}
8395
}

src/xtd.core/src/xtd/io/binary_reader.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
#include "../../../include/xtd/bit_converter.hpp"
88

99
using namespace xtd;
10+
using namespace xtd::helpers;
1011
using namespace xtd::io;
1112

1213
binary_reader::binary_reader(const string& path) : stream_(new std::ifstream(path, std::ios::binary)), delete_when_destroy_(true) {
13-
if (path.trim(' ').length() == 0 || path.index_of_any(io::path::get_invalid_path_chars()) != string::npos) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument);
14-
if (!file::exists(path)) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::file_not_found);
14+
if (path.trim(' ').length() == 0 || path.index_of_any(io::path::get_invalid_path_chars()) != string::npos) throw_helper::throws(exception_case::argument);
15+
if (!file::exists(path)) throw_helper::throws(exception_case::file_not_found);
1516
}
1617

1718
binary_reader::binary_reader(std::istream& stream) : stream_(&stream) {
@@ -63,7 +64,7 @@ int32 binary_reader::read() {
6364
}
6465

6566
size_t binary_reader::read(std::vector<xtd::byte>& buffer, size_t index, size_t count) {
66-
if (index + count > buffer.size()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument);
67+
if (index + count > buffer.size()) throw_helper::throws(exception_case::argument);
6768
for (auto i = 0_z; i < count; i++) {
6869
auto current = read();
6970
if (current == EOF) return i;
@@ -73,7 +74,7 @@ size_t binary_reader::read(std::vector<xtd::byte>& buffer, size_t index, size_t
7374
}
7475

7576
size_t binary_reader::read(std::vector<char>& buffer, size_t index, size_t count) {
76-
if (index + count > buffer.size()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument);
77+
if (index + count > buffer.size()) throw_helper::throws(exception_case::argument);
7778
for (auto i = 0_z; i < count; i++) {
7879
auto current = read();
7980
if (current == EOF) return i;
@@ -93,7 +94,7 @@ xtd::byte binary_reader::read_byte() {
9394
std::vector<xtd::byte> binary_reader::read_bytes(size_t count) {
9495
auto result = std::vector<xtd::byte>(count);
9596
if (read(result, 0, count) != count)
96-
throw end_of_stream_exception {};
97+
throw_helper::throws(exception_case::end_of_stream);
9798
return result;
9899
}
99100

@@ -104,7 +105,7 @@ char binary_reader::read_char() {
104105
std::vector<char> binary_reader::read_chars(size_t count) {
105106
auto result = std::vector<char>(count);
106107
if (read(result, 0, count) != count)
107-
throw end_of_stream_exception {};
108+
throw_helper::throws(exception_case::end_of_stream);
108109
return result;
109110
}
110111

@@ -174,7 +175,7 @@ int32 binary_reader::read_7bit_encoded_int() {
174175
}
175176

176177
byte_read_just_now = read_byte();
177-
if (byte_read_just_now > 0b1111u) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::format);
178+
if (byte_read_just_now > 0b1111u) throw_helper::throws(exception_case::format);
178179

179180
result |= static_cast<uint32>(byte_read_just_now) << (max_bytes_without_overflow * 7);
180181
return static_cast<int32>(result);

src/xtd.core/src/xtd/io/binary_writer.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@
88
#include "../../../include/xtd/io/binary_writer.hpp"
99

1010
using namespace xtd;
11+
using namespace xtd::helpers;
1112
using namespace xtd::io;
1213

1314
binary_writer::binary_writer(const string& path) : stream_(new std::ofstream(path, std::ios::out | std::ios::binary | std::ios_base::trunc)), delete_when_destroy_(true) {
14-
if (path.index_of_any(path::get_invalid_path_chars()) != string::npos) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument);
15-
if (path.empty() || path.trim(' ').empty()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument);
16-
if (!file::exists(path)) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::file_not_found);
17-
if ((file::get_attributes(path) & file_attributes::read_only) == file_attributes::read_only) throw unauthorized_access_exception {};
18-
if (!dynamic_cast<std::ofstream*>(stream_)->is_open() || !stream_->good()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::io);
15+
if (path.index_of_any(path::get_invalid_path_chars()) != string::npos) throw_helper::throws(exception_case::argument);
16+
if (path.empty() || path.trim(' ').empty()) throw_helper::throws(exception_case::argument);
17+
if (!file::exists(path)) throw_helper::throws(exception_case::file_not_found);
18+
if ((file::get_attributes(path) & file_attributes::read_only) == file_attributes::read_only) throw_helper::throws(exception_case::unauthorized_access);
19+
if (!dynamic_cast<std::ofstream*>(stream_)->is_open() || !stream_->good()) throw_helper::throws(exception_case::io);
1920
}
2021

2122
binary_writer::binary_writer(std::ostream& stream) : stream_(&stream) {
22-
if (!stream_->good()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::io);
23+
if (!stream_->good()) throw_helper::throws(exception_case::io);
2324
stream_->flush();
2425
}
2526

@@ -47,7 +48,7 @@ void binary_writer::flush() {
4748
}
4849

4950
size_t binary_writer::seek(size_t offset, std::ios::seekdir origin) {
50-
if (!stream_) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::io);
51+
if (!stream_) throw_helper::throws(exception_case::io);
5152
stream_->seekp(offset, origin);
5253
return static_cast<size_t>(stream_->tellp());
5354
}
@@ -57,12 +58,12 @@ void binary_writer::write(bool value) {
5758
}
5859

5960
void binary_writer::write(xtd::byte value) {
60-
if (!stream_) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::io);
61+
if (!stream_) throw_helper::throws(exception_case::io);
6162
stream_->put(static_cast<char>(value));
6263
}
6364

6465
void binary_writer::write(char value) {
65-
if (!stream_) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::io);
66+
if (!stream_) throw_helper::throws(exception_case::io);
6667
stream_->put(value);
6768
}
6869

@@ -71,8 +72,8 @@ void binary_writer::write(const std::vector<xtd::byte>& buffer) {
7172
}
7273

7374
void binary_writer::write(const std::vector<xtd::byte>& buffer, size_t index, size_t count) {
74-
if (!stream_) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::io);
75-
if (index + count > buffer.size()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument);
75+
if (!stream_) throw_helper::throws(exception_case::io);
76+
if (index + count > buffer.size()) throw_helper::throws(exception_case::argument);
7677
for (auto i = index; i < (index + count); ++i)
7778
write(buffer[i]);
7879
}
@@ -82,8 +83,8 @@ void binary_writer::write(const std::vector<char>& buffer) {
8283
}
8384

8485
void binary_writer::write(const std::vector<char>& buffer, size_t index, size_t count) {
85-
if (!stream_) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::io);
86-
if (index + count > buffer.size()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument);
86+
if (!stream_) throw_helper::throws(exception_case::io);
87+
if (index + count > buffer.size()) throw_helper::throws(exception_case::argument);
8788
for (auto i = index; i < (index + count); ++i)
8889
write(buffer[i]);
8990
}
@@ -105,7 +106,7 @@ void binary_writer::write(int64 value) {
105106
}
106107

107108
void binary_writer::write(sbyte value) {
108-
if (!stream_) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::io);
109+
if (!stream_) throw_helper::throws(exception_case::io);
109110
stream_->put(static_cast<char>(value));
110111
}
111112

0 commit comments

Comments
 (0)