Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | |
3 | | // included header files |
4 | | #include "error.hpp" |
5 | | #include "i18n.h" // NLS support. |
6 | | |
7 | | // + standard includes |
8 | | #include <array> |
9 | | #include <iostream> |
10 | | #include <string> |
11 | | #include <utility> |
12 | | |
13 | | namespace { |
14 | | //! Complete list of Exiv2 exception error messages |
15 | | constexpr std::array errList{ |
16 | | N_("Success"), // kerSuccess |
17 | | N_("Error %0: arg2=%2, arg3=%3, arg1=%1."), // KerGeneralError |
18 | | "%1", // kerErrorMessage, |
19 | | "%1: Call to `%3' failed: %2", // KerCallFailed, %1=path, %2=strerror, %3=function that failed |
20 | | N_("This does not look like a %1 image"), // kerNotAnImage, %1=Image type |
21 | | N_("Invalid dataset name '%1'"), // kerInvalidDataset %1=dataset name |
22 | | N_("Invalid record name '%1'"), // kerInvalidRecord %1=record name |
23 | | N_("Invalid key '%1'"), // kerInvalidKey %1=key |
24 | | N_("Invalid tag name or ifdId `%1', ifdId %2"), // kerInvalidTag %1=tag name, %2=ifdId |
25 | | N_("%1 value not set"), // kerValueNotSet %1=key name |
26 | | N_("%1: Failed to open the data source: %2"), // kerDataSourceOpenFailed %1=path, %2=strerror |
27 | | N_("%1: Failed to open file (%2): %3"), // kerFileOpenFailed %1=path, %2=mode, %3=strerror |
28 | | N_("%1: The file contains data of an unknown image type"), // kerFileContainsUnknownImageType %1=path |
29 | | N_("The memory contains data of an unknown image type"), // kerMemoryContainsUnknownImageType |
30 | | N_("Image type %1 is not supported"), // kerUnsupportedImageType %1=image type |
31 | | N_("Failed to read image data"), // kerFailedToReadImageData |
32 | | N_("This does not look like a JPEG image"), // kerNotAJpeg |
33 | | N_("%1: Failed to map file for reading and writing: %2"), // kerFailedToMapFileForReadWrite %1=path, |
34 | | // %2=strerror |
35 | | N_("%1: Failed to rename file to %2: %3"), // kerFileRenameFailed %1=old path, %2=new path, %3=strerror |
36 | | N_("%1: Transfer failed: %2"), // kerTransferFailed %1=path, %2=strerror |
37 | | N_("Memory transfer failed: %1"), // kerMemoryTransferFailed %1=strerror |
38 | | N_("Failed to read input data"), // kerInputDataReadFailed |
39 | | N_("Failed to write image"), // kerImageWriteFailed |
40 | | N_("Input data does not contain a valid image"), // kerNoImageInInputData |
41 | | N_("Invalid ifdId %1"), // kerInvalidIfdId %1=ifdId |
42 | | N_("Entry::setValue: Value too large (tag=%1, size=%2, requested=%3)"), // kerValueTooLarge %1=tag, |
43 | | // %2=dataSize, %3=required size |
44 | | N_("Entry::setDataArea: Value too large (tag=%1, size=%2, requested=%3)"), // kerDataAreaValueTooLarge %1=tag, |
45 | | // %2=dataAreaSize, %3=required size |
46 | | N_("Offset out of range"), // kerOffsetOutOfRange |
47 | | N_("Unsupported data area offset type"), // kerUnsupportedDataAreaOffsetType |
48 | | N_("Invalid charset: `%1'"), // kerInvalidCharset %1=charset name |
49 | | N_("Unsupported date format"), // kerUnsupportedDateFormat |
50 | | N_("Unsupported time format"), // kerUnsupportedTimeFormat |
51 | | N_("Writing to %1 images is not supported"), // kerWritingImageFormatUnsupported %1=image format |
52 | | N_("Setting %1 in %2 images is not supported"), // kerInvalidSettingForImage %1=metadata type, %2=image format |
53 | | N_("This does not look like a CRW image"), // kerNotACrwImage |
54 | | N_("%1: Not supported"), // kerFunctionNotSupported %1=function |
55 | | N_("No namespace info available for XMP prefix `%1'"), // kerNoNamespaceInfoForXmpPrefix %1=prefix |
56 | | N_("No prefix registered for namespace `%2', needed for property path `%1'"), // kerNoPrefixForNamespace |
57 | | // %1=namespace |
58 | | N_("Size of %1 JPEG segment is larger than 65535 bytes"), // kerTooLargeJpegSegment %1=type of metadata (Exif, |
59 | | // IPTC, JPEG comment) |
60 | | N_("Unhandled Xmpdatum %1 of type %2"), // kerUnhandledXmpdatum %1=key, %2=value type |
61 | | N_("Unhandled XMP node %1 with opt=%2"), // kerUnhandledXmpNode %1=key, %2=XMP Toolkit option flags |
62 | | N_("XMP Toolkit error %1: %2"), // kerXMPToolkitError %1=XMP_Error::GetID(), %2=XMP_Error::GetErrMsg() |
63 | | N_("Failed to decode Lang Alt property %1 with opt=%2"), // kerDecodeLangAltPropertyFailed %1=property path, |
64 | | // %3=XMP Toolkit option flags |
65 | | N_("Failed to decode Lang Alt qualifier %1 with opt=%2"), // kerDecodeLangAltQualifierFailed %1=qualifier path, |
66 | | // %3=XMP Toolkit option flags |
67 | | N_("Failed to encode Lang Alt property %1"), // kerEncodeLangAltPropertyFailed %1=key |
68 | | N_("Failed to determine property name from path %1, namespace %2"), // kerPropertyNameIdentificationFailed |
69 | | // %1=property path, %2=namespace |
70 | | N_("Schema namespace %1 is not registered with the XMP Toolkit"), // kerSchemaNamespaceNotRegistered |
71 | | // %1=namespace |
72 | | N_("No namespace registered for prefix `%1'"), // kerNoNamespaceForPrefix %1=prefix |
73 | | N_("Aliases are not supported. Please send this XMP packet to ahuggel@gmx.net `%1', `%2', `%3'"), // kerAliasesNotSupported |
74 | | // %1=namespace, |
75 | | // %2=property |
76 | | // path, |
77 | | // %3=value |
78 | | N_("Invalid XmpText type `%1'"), // kerInvalidXmpText %1=type |
79 | | N_("TIFF directory %1 has too many entries"), // kerTooManyTiffDirectoryEntries %1=TIFF directory name |
80 | | N_("Multiple TIFF array element tags %1 in one directory"), // kerMultipleTiffArrayElementTagsInDirectory |
81 | | // %1=tag number |
82 | | N_("TIFF array element tag %1 has wrong type"), // kerWrongTiffArrayElementTagType %1=tag number |
83 | | N_("%1 has invalid XMP value type `%2'"), // kerInvalidKeyXmpValue %1=key, %2=value type |
84 | | N_("Not a valid ICC Profile"), // kerInvalidIccProfile |
85 | | N_("Not valid XMP"), // kerInvalidXMP |
86 | | N_("tiff directory length is too large"), // kerTiffDirectoryTooLarge |
87 | | N_("invalid type in tiff structure"), // kerInvalidTypeValue |
88 | | N_("Invalid LangAlt value `%1'"), // kerInvalidLangAltValue %1=value |
89 | | N_("invalid memory allocation request"), // kerInvalidMalloc |
90 | | N_("corrupted image metadata"), // kerCorruptedMetadata |
91 | | N_("Arithmetic operation overflow"), // kerArithmeticOverflow |
92 | | N_("Memory allocation failed"), // kerMallocFailed |
93 | | N_("Cannot convert text encoding from '%1' to '%2'"), // kerInvalidIconvEncoding |
94 | | N_("%1: File access disabled in exiv2 build options"), // kerFileAccessDisabled %1=path |
95 | | }; |
96 | | static_assert(errList.size() == static_cast<size_t>(Exiv2::ErrorCode::kerErrorCount), |
97 | | "errList needs to contain a error msg for every ErrorCode defined in error.hpp"); |
98 | | |
99 | | } // namespace |
100 | | |
101 | | // ***************************************************************************** |
102 | | // class member definitions |
103 | | namespace Exiv2 { |
104 | | LogMsg::Level LogMsg::level_ = LogMsg::warn; // Default output level |
105 | | LogMsg::Handler LogMsg::handler_ = LogMsg::defaultHandler; |
106 | | |
107 | 1.10M | LogMsg::LogMsg(LogMsg::Level msgType) : msgType_(msgType) { |
108 | 1.10M | } |
109 | | |
110 | 1.10M | LogMsg::~LogMsg() { |
111 | 1.10M | if (msgType_ >= level_ && handler_) |
112 | 1.10M | handler_(msgType_, os_.str().c_str()); |
113 | 1.10M | } |
114 | | |
115 | 1.10M | std::ostringstream& LogMsg::os() { |
116 | 1.10M | return os_; |
117 | 1.10M | } |
118 | | |
119 | 0 | void LogMsg::setLevel(LogMsg::Level level) { |
120 | 0 | level_ = level; |
121 | 0 | } |
122 | | |
123 | 38.8k | void LogMsg::setHandler(LogMsg::Handler handler) { |
124 | 38.8k | handler_ = handler; |
125 | 38.8k | } |
126 | | |
127 | 1.11M | LogMsg::Level LogMsg::level() { |
128 | 1.11M | return level_; |
129 | 1.11M | } |
130 | | |
131 | 1.10M | LogMsg::Handler LogMsg::handler() { |
132 | 1.10M | return handler_; |
133 | 1.10M | } |
134 | | |
135 | 0 | void LogMsg::defaultHandler(int level, const char* s) { |
136 | 0 | switch (static_cast<LogMsg::Level>(level)) { |
137 | 0 | case LogMsg::debug: |
138 | 0 | std::cerr << "Debug: "; |
139 | 0 | break; |
140 | 0 | case LogMsg::info: |
141 | 0 | std::cerr << "Info: "; |
142 | 0 | break; |
143 | 0 | case LogMsg::warn: |
144 | 0 | std::cerr << "Warning: "; |
145 | 0 | break; |
146 | 0 | case LogMsg::error: |
147 | 0 | std::cerr << "Error: "; |
148 | 0 | break; |
149 | 0 | default: |
150 | 0 | break; |
151 | 0 | } |
152 | 0 | std::cerr << s; |
153 | 0 | } |
154 | | |
155 | 31.8k | Error::Error(ErrorCode code) : code_(code) { |
156 | 31.8k | setMsg(0); |
157 | 31.8k | } |
158 | | |
159 | 14.2k | ErrorCode Error::code() const noexcept { |
160 | 14.2k | return code_; |
161 | 14.2k | } |
162 | | |
163 | 39.1k | const char* Error::what() const noexcept { |
164 | 39.1k | return msg_.c_str(); |
165 | 39.1k | } |
166 | | |
167 | 43.2k | void Error::setMsg(int count) { |
168 | 43.2k | std::string msg{_(errList.at(static_cast<size_t>(code_)))}; |
169 | 43.2k | auto pos = msg.find("%0"); |
170 | 43.2k | if (pos != std::string::npos) { |
171 | 0 | msg.replace(pos, 2, std::to_string(static_cast<int>(code_))); |
172 | 0 | } |
173 | 43.2k | if (count > 0) { |
174 | 11.4k | pos = msg.find("%1"); |
175 | 11.4k | if (pos != std::string::npos) { |
176 | 11.4k | msg.replace(pos, 2, arg1_); |
177 | 11.4k | } |
178 | 11.4k | } |
179 | 43.2k | if (count > 1) { |
180 | 10.5k | pos = msg.find("%2"); |
181 | 10.5k | if (pos != std::string::npos) { |
182 | 10.5k | msg.replace(pos, 2, arg2_); |
183 | 10.5k | } |
184 | 10.5k | } |
185 | 43.2k | if (count > 2) { |
186 | 0 | pos = msg.find("%3"); |
187 | 0 | if (pos != std::string::npos) { |
188 | 0 | msg.replace(pos, 2, arg3_); |
189 | 0 | } |
190 | 0 | } |
191 | 43.2k | msg_ = std::move(msg); |
192 | 43.2k | } |
193 | | |
194 | | } // namespace Exiv2 |