commit 5db423cf8d4146fa61091ae9253c18a5a132ecb7 Author: Ahmed Siam Date: Tue Jul 11 21:42:46 2023 +0300 i18n of lttoolbox patch 3 diff --git a/locales/root.txt b/locales/root.txt index 3631776..bc81a28 100644 --- a/locales/root.txt +++ b/locales/root.txt @@ -142,4 +142,14 @@ root{ LTTB1055{"ERROR LTTB1055: Trying to link nonexistent states ({source}, {target}, {tag})."} LTTB1056{"ERROR LTTB1056: Empty set of final states."} LTTB1057{"ERROR LTTB1057: Couldn't find {f_src}, {g_src} in state map."} + LTTB1058{"ERROR LTTB1058: Failed to write uint64_t."} + LTTB1059{"ERROR LTTB1059: Transducer has features that are unknown to this version of lttoolbox - upgrade!"} + LTTB1060{"ERROR LTTB1060: Unable to parse {type}."} + LTTB1061{"ERROR LTTB1061: Malformed input stream."} + LTTB1062{"ERROR LTTB1062: FST has features that are unknown to this version of lttoolbox - upgrade!"} + LTTB1063{"ERROR LTTB1063: Could not read {number} expected bytes from stream."} + LTTB1064{"ERROR LTTB1064: Can't deserialise {size} byte integer type: Can't deserialise size."} + LTTB1065{"ERROR LTTB1065: Can't deserialise {size} byte integer type: Can't deserialise byte."} + LTTB1066{"ERROR LTTB1066: Can't serialise const {size_a} byte integer type: Can't serialise size {size_b}."} + LTTB1067{"ERROR LTTB1067: Can't serialise const {size} byte integer type: Can't serialise byte {byte}."} } diff --git a/lttoolbox/compression.h b/lttoolbox/compression.h index 4bcba58..3e7ea40 100644 --- a/lttoolbox/compression.h +++ b/lttoolbox/compression.h @@ -23,6 +23,7 @@ #include #include #include +#include // Global lttoolbox features constexpr char HEADER_LTTOOLBOX[4]{'L', 'T', 'T', 'B'}; @@ -43,7 +44,7 @@ enum TD_FEATURES : uint64_t { inline auto write_u64(FILE *out, uint64_t value) { auto rv = fwrite_unlocked(reinterpret_cast(&value), 1, sizeof(value), out); if (rv != sizeof(value)) { - throw std::runtime_error("Failed to write uint64_t"); + I18n(LOCALES_DATA).error("LTTB1058", {}, {}, true); } return rv; } @@ -77,7 +78,7 @@ inline auto write_le(Stream& out, uint64_t value) { inline auto read_u64(FILE *in) { uint64_t value = 0; if (fread_unlocked(reinterpret_cast(&value), 1, sizeof(value), in) != sizeof(value)) { - throw std::runtime_error("Failed to read uint64_t"); + I18n(LOCALES_DATA).error("LTTB1058", {}, {}, true); } return value; } diff --git a/lttoolbox/deserialiser.h b/lttoolbox/deserialiser.h index 9062930..0b99265 100644 --- a/lttoolbox/deserialiser.h +++ b/lttoolbox/deserialiser.h @@ -34,6 +34,7 @@ #include #include +#include template class Deserialiser; @@ -124,28 +125,22 @@ Deserialiser >::deserialise( template integer_type int_deserialise(std::istream &Stream_) { - try { - integer_type SerialisedType_ = 0; - unsigned char SerialisedTypeSize = Stream_.get(); + integer_type SerialisedType_ = 0; + unsigned char SerialisedTypeSize = Stream_.get(); + + if (!Stream_) + I18n(LOCALES_DATA).error("LTTB1064", {"size"}, {std::to_string(sizeof(integer_type)).c_str()}, true); + + for (; SerialisedTypeSize != 0;) { + SerialisedType_ += + static_cast(Stream_.get()) + << std::numeric_limits::digits * --SerialisedTypeSize; if (!Stream_) - throw DeserialisationException("can't deserialise size"); - - for (; SerialisedTypeSize != 0;) { - SerialisedType_ += - static_cast(Stream_.get()) - << std::numeric_limits::digits * --SerialisedTypeSize; - - if (!Stream_) - throw DeserialisationException("can't deserialise byte"); - } - - return SerialisedType_; - } catch (const std::exception &exc) { - std::stringstream what_; - what_ << "can't deserialise " << sizeof(integer_type) << " byte integer type: " << exc.what(); - throw DeserialisationException(what_.str().c_str()); + I18n(LOCALES_DATA).error("LTTB1065", {"size"}, {std::to_string(sizeof(integer_type)).c_str()}, true); } + + return SerialisedType_; } #ifdef SIZET_NOT_CSTDINT diff --git a/lttoolbox/file_utils.cc b/lttoolbox/file_utils.cc index b618360..9f4348f 100644 --- a/lttoolbox/file_utils.cc +++ b/lttoolbox/file_utils.cc @@ -102,7 +102,7 @@ readShared(FILE* input, std::set& letters, Alphabet& alpha) if (strncmp(header, HEADER_LTTOOLBOX, 4) == 0) { auto features = read_le(input); if (features >= LTF_UNKNOWN) { - throw std::runtime_error("FST has features that are unknown to this version of lttoolbox - upgrade!"); + I18n(LOCALES_DATA).error("LTTB1062", {}, {}, true); } } else { // Old binary format diff --git a/lttoolbox/fst_processor.cc b/lttoolbox/fst_processor.cc index 38248b9..c38b8d1 100644 --- a/lttoolbox/fst_processor.cc +++ b/lttoolbox/fst_processor.cc @@ -25,6 +25,7 @@ #include #include #include +#include FSTProcessor::FSTProcessor() @@ -51,7 +52,7 @@ FSTProcessor::FSTProcessor() void FSTProcessor::streamError() { - throw Exception("Error: Malformed input stream."); + I18n(LOCALES_DATA).error("LTTB1061", {}, {}, true); } void diff --git a/lttoolbox/input_file.cc b/lttoolbox/input_file.cc index c95300b..6f33f0d 100644 --- a/lttoolbox/input_file.cc +++ b/lttoolbox/input_file.cc @@ -96,17 +96,17 @@ InputFile::internal_read() if ((first & 0xF0) == 0xF0) { i += 3; if (fread_unlocked(cbuffer+1, 1, 3, infile) != 3) { - throw std::runtime_error("Could not read 3 expected bytes from stream"); + I18n(LOCALES_DATA).error("LTTB1063", {"number"}, {3}, true); } } else if ((first & 0xE0) == 0xE0) { i += 2; if (fread_unlocked(cbuffer+1, 1, 2, infile) != 2) { - throw std::runtime_error("Could not read 2 expected bytes from stream"); + I18n(LOCALES_DATA).error("LTTB1063", {"number"}, {2}, true); } } else if ((first & 0xC0) == 0xC0) { i += 1; if (fread_unlocked(cbuffer+1, 1, 1, infile) != 1) { - throw std::runtime_error("Could not read 1 expected byte from stream"); + I18n(LOCALES_DATA).error("LTTB1063", {"number"}, {1}, true); } } memset(ubuffer, 0, 3*sizeof(UChar)); diff --git a/lttoolbox/serialiser.h b/lttoolbox/serialiser.h index a155d91..44d7715 100644 --- a/lttoolbox/serialiser.h +++ b/lttoolbox/serialiser.h @@ -154,35 +154,29 @@ void Serialiser >::serialise( template void int_serialise(const integer_type &SerialisedType_, std::ostream &Output) { - try { - Output.put(compressedSize(SerialisedType_)); + Output.put(compressedSize(SerialisedType_)); + if (!Output) { + std::stringstream what_; + what_ << std::hex << /* [1] */ +compressedSize(SerialisedType_) << std::dec; + I18n(LOCALES_DATA).error("LTTB1066", {"size_a", "size_b"}, + {std::to_string(sizeof(integer_type)).c_str(), what_.str().c_str()}, true); + } + + for (unsigned char CompressedSize = compressedSize(SerialisedType_); + CompressedSize != 0; Output.put(static_cast( + SerialisedType_ >> + std::numeric_limits::digits * --CompressedSize))) { if (!Output) { std::stringstream what_; - what_ << "can't serialise size " << std::hex - << /* [1] */ +compressedSize(SerialisedType_) << std::dec; - throw SerialisationException(what_.str().c_str()); + what_ << std::hex << /* [1] */ +static_cast( + SerialisedType_ >> + std::numeric_limits::digits * + CompressedSize) << std::dec; + + I18n(LOCALES_DATA).error("LTTB1067", {"size", "byte"}, + {std::to_string(sizeof(integer_type)).c_str(), what_.str().c_str()}, true); } - - for (unsigned char CompressedSize = compressedSize(SerialisedType_); - CompressedSize != 0; Output.put(static_cast( - SerialisedType_ >> - std::numeric_limits::digits * --CompressedSize))) { - if (!Output) { - std::stringstream what_; - what_ << "can't serialise byte " << std::hex - << /* [1] */ +static_cast( - SerialisedType_ >> - std::numeric_limits::digits * - CompressedSize) << std::dec; - throw SerialisationException(what_.str().c_str()); - } - } - } catch (const SerialisationException &exc) { - std::stringstream what_; - what_ << "can't serialise const " << sizeof(integer_type) << " byte integer type: " - << exc.what(); - throw SerialisationException(what_.str().c_str()); } } diff --git a/lttoolbox/string_utils.cc b/lttoolbox/string_utils.cc index 99f85db..3eca057 100644 --- a/lttoolbox/string_utils.cc +++ b/lttoolbox/string_utils.cc @@ -148,7 +148,7 @@ StringUtils::stoi(const UString& str) int ret; int c = u_sscanf(str.c_str(), "%d", &ret); if (c != 1) { - throw std::invalid_argument("unable to parse int"); + I18n(LOCALES_DATA).error("LTTB1060", {"type"}, {"int"}, true); } return ret; } @@ -167,7 +167,7 @@ StringUtils::stod(const UString& str) c = 1; } if (c != 1) { - throw std::invalid_argument("unable to parse float"); + I18n(LOCALES_DATA).error("LTTB1060", {"type"}, {"float"}, true); } return ret; } diff --git a/lttoolbox/trans_exe.cc b/lttoolbox/trans_exe.cc index a1b2c29..2fc8d1f 100644 --- a/lttoolbox/trans_exe.cc +++ b/lttoolbox/trans_exe.cc @@ -75,7 +75,7 @@ TransExe::read(FILE *input, Alphabet const &alphabet) if (strncmp(header, HEADER_TRANSDUCER, 4) == 0) { auto features = read_le(input); if (features >= TDF_UNKNOWN) { - throw std::runtime_error("Transducer has features that are unknown to this version of lttoolbox - upgrade!"); + I18n(LOCALES_DATA).error("LTTB1059", {}, {}, true); } read_weights = (features & TDF_WEIGHTS); } diff --git a/lttoolbox/transducer.cc b/lttoolbox/transducer.cc index bd96389..c91aebc 100644 --- a/lttoolbox/transducer.cc +++ b/lttoolbox/transducer.cc @@ -589,7 +589,7 @@ Transducer::read(FILE *input, int const decalage) if (strncmp(header, HEADER_TRANSDUCER, 4) == 0) { auto features = read_le(input); if (features >= TDF_UNKNOWN) { - throw std::runtime_error("Transducer has features that are unknown to this version of lttoolbox - upgrade!"); + I18n(LOCALES_DATA).error("LTTB1059", {}, {}, true); } read_weights = (features & TDF_WEIGHTS); }