1 /*
2   mkvmerge -- utility for splicing together matroska files
3   from component media subtypes
4 
5   Distributed under the GPL v2
6   see the file COPYING for details
7   or visit https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
8 
9   Exceptios for the I/O callback class
10 
11   Written by Moritz Bunkus <moritz@bunkus.org>.
12 */
13 
14 #pragma once
15 
16 #include "common/common_pch.h"
17 
18 #include <system_error>
19 
20 namespace mtx::mm_io {
21 
22 std::error_code make_error_code();
23 
24 class exception: public mtx::exception {
25 private:
26   std::error_code m_error_code;
27 
28 public:
29   exception(std::error_code const &error_code = std::error_code())
exception()30     : mtx::exception()
31     , m_error_code{error_code}
32   {
33   }
34 
what()35   virtual const char *what() const noexcept {
36     return "unspecified I/O error";
37   }
38 
code()39   std::error_code const &code() const {
40     return m_error_code;
41   }
42 
43   virtual std::string error() const noexcept;
44 };
45 
46 class end_of_file_x: public exception {
47 public:
exception(error_code)48   end_of_file_x(std::error_code const &error_code = std::error_code()) : exception(error_code) {}
49 
what()50   virtual char const *what() const noexcept {
51     return "end of file error";
52   }
53 };
54 
55 class seek_x: public exception {
56 public:
exception(error_code)57   seek_x(std::error_code const &error_code = std::error_code()) : exception(error_code) {}
58 
what()59   virtual char const *what() const noexcept {
60     return "seek in file error";
61   }
62 };
63 
64 class read_write_x: public exception {
65 public:
exception(error_code)66   read_write_x(std::error_code const &error_code = std::error_code()) : exception(error_code) {}
67 
what()68   virtual char const *what() const noexcept {
69     return "reading from/writing to the file error";
70   }
71 };
72 
73 class open_x: public exception {
74 public:
exception(error_code)75   open_x(std::error_code const &error_code = std::error_code()) : exception(error_code) {}
76 
what()77   virtual char const *what() const noexcept {
78     return "open file error";
79   }
80 };
81 
82 class wrong_read_write_access_x: public exception {
83 public:
exception(error_code)84   wrong_read_write_access_x(std::error_code const &error_code = std::error_code()) : exception(error_code) {}
85 
what()86   virtual char const *what() const noexcept {
87     return "write operation to read-only file or vice versa";
88   }
89 };
90 
91 class insufficient_space_x: public exception {
92 public:
exception(error_code)93   insufficient_space_x(std::error_code const &error_code = std::error_code()) : exception(error_code) {}
94 
what()95   virtual char const *what() const noexcept {
96     return "insufficient space for write operation";
97   }
98 };
99 
100 class create_directory_x: public exception {
101 protected:
102   std::string m_path;
103 public:
104   create_directory_x(std::string const &path,
105                      std::error_code const &error_code = std::error_code())
exception(error_code)106     : exception(error_code)
107     , m_path{path}
108   {
109   }
110 
~create_directory_x()111   virtual ~create_directory_x() noexcept { }
112 
what()113   virtual char const *what() const noexcept {
114     return "create_directory() failed";
115   }
error()116   virtual std::string error() const noexcept {
117     return fmt::format(Y("Creating directory '{0}' failed: {1}"), m_path, code().message());
118   }
119 };
120 
121 namespace text {
122 class exception: public mtx::mm_io::exception {
123 public:
exception(error_code)124   exception(std::error_code const &error_code = std::error_code()) : mtx::mm_io::exception(error_code) {}
125 
what()126   virtual char const *what() const noexcept {
127     return "unspecified text I/O error";
128   }
129 };
130 
131 class invalid_utf8_char_x: public exception {
132 protected:
133   char m_first_char;
134 public:
135   invalid_utf8_char_x(char first_char, std::error_code const &error_code = std::error_code())
exception(error_code)136     : exception(error_code)
137     , m_first_char{first_char}
138   {
139   }
140 
what()141   virtual char const *what() const noexcept {
142     return "invalid UTF-8 char";
143   }
144 
error()145   virtual std::string error() const noexcept {
146     return fmt::format(Y("Invalid UTF-8 char. First byte: 0x{0:02x}"), static_cast<unsigned int>(m_first_char));
147   }
148 };
149 
150 inline std::ostream &
151 operator <<(std::ostream &out,
152             exception const &ex) {
153   out << ex.error();
154   return out;
155 }
156 
157 }}
158