wowlib 0.0.0
Read & write World of Warcraft client files — a C++26 core
Loading...
Searching...
No Matches
locstring.hpp
Go to the documentation of this file.
1#pragma once
2
8
9#include <array>
10#include <cstddef>
11#include <cstdint>
12#include <optional>
13#include <string>
14#include <string_view>
15
16#include <welder/vocabulary.hpp>
17
19#include <wowlib/core/error.hpp>
20
21namespace wowlib::db {
28 constexpr std::optional<std::size_t> localeColumnSlot(Locale locale) {
29 switch (locale) {
30 case Locale::enUS:
31 case Locale::enGB: return 0;
32 case Locale::koKR: return 1;
33 case Locale::frFR: return 2;
34 case Locale::deDE: return 3;
35 case Locale::zhCN: return 4;
36 case Locale::zhTW: return 5;
37 case Locale::esES: return 6;
38 case Locale::esMX: return 7;
39 case Locale::ruRU: return 8;
40 case Locale::ptBR: return 10;
41 case Locale::itIT: return 11;
42 }
43 return std::nullopt;
44 }
45
51 template <std::size_t Langs>
52 struct [[
53 =welder::weld,
54 =welder::doc(R"(
55 A localized string column of a pre-Cataclysm client database: one string
56 per language slot in the client's fixed column order, plus the locale
57 flags field. Use at()/set() to address slots by Locale.)")
58 ]] LocString {
59 [[=welder::mark::no_reassign,
60 =welder::doc(
61 "The language slot values in client column order (0 enUS, 1 koKR, "
62 "2 frFR, 3 deDE, 4 zhCN, 5 zhTW, 6 esES, 7 esMX, then ruRU/ptBR/"
63 "itIT for 16-slot eras); empty for languages the file does not "
64 "carry.")]]
65 std::array<std::string, Langs> values{};
66
67 [[=welder::doc(
68 "The trailing locale flags field; a bitmask the client never "
69 "reads, preserved verbatim.")]]
70 std::uint32_t flags = 0;
71
73
76 [[nodiscard]]
77 [[=welder::doc(
78 "The string stored for a locale; empty when the locale has no "
79 "slot in this client era."),
80 =welder::returns("the slot value, empty when absent")]]
81 std::string_view at(Locale locale [[=welder::doc("the locale to look up")]]) const {
82 const auto slot = localeColumnSlot(locale);
83 if (!slot || *slot >= Langs) return {};
84 return values[*slot];
85 }
86
92 [[=welder::doc("Store a string in a locale's slot."),
93 =welder::returns("nothing; raises when the locale has no slot in this "
94 "client era")]]
95 Result<void> set(Locale locale [[=welder::doc("the locale to write")]],
96 std::string_view value [[=welder::doc(
97 "the string to store")]]) {
98 const auto slot = localeColumnSlot(locale);
99 if (!slot || *slot >= Langs)
101 std::string{"locale "} + std::string{localeCode(locale)} +
102 " has no langstringref slot in this client era");
103 values[*slot] = value;
104 return {};
105 }
106
107 constexpr bool operator==(const LocString&) const = default;
108 };
109
111 using LocString8 = LocString<8>;
112
115}
Client version identity, the flavor axis that separates a client's CONTENT version from the engine ge...
The error-handling vocabulary: ErrorCode, Error and the Result<T> alias every fallible wowlib operati...
constexpr std::optional< std::size_t > localeColumnSlot(Locale locale)
The langstringref column slot locale occupies, in the client's fixed column order (0 enUS/enGB,...
Definition locstring.hpp:28
LocString< 16 > LocString16
The TBC..WotLK column shape: 16 language slots (2.1.0.6692+).
LocString< 8 > LocString8
The vanilla-era column shape: 8 language slots (pre-2.1.0.6692).
Definition locstring.hpp:98
std::expected< T, Error > Result
Every fallible wowlib operation returns Result<T>; bindings translate the error branch into a target-...
Definition error.hpp:100
std::string_view localeCode(Locale locale)
The four-letter code of locale ("enUS", ...) as used in MPQ locale directory and archive names.
std::unexpected< Error > makeError(ErrorCode code, std::string message, std::uint32_t nativeError=0)
Shorthand for constructing the error branch of a Result.
Definition error.hpp:107
@ NotSupported
Operation not supported by this backend/provider.
Definition error.hpp:32
A decoded pre-Cataclysm localized string column: Langs language slots (8 before TBC 2....
Definition locstring.hpp:52
std::array< std::string, Langs > values
Definition locstring.hpp:54
std::string_view at(Locale locale) const
The string stored for locale.
Definition locstring.hpp:68
constexpr bool operator==(const LocString &) const =default
Result< void > set(Locale locale, std::string_view value)
Store value in the slot of locale.
Definition locstring.hpp:83