summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKohei Yoshida <kohei.yoshida@gmail.com>2022-06-07 20:31:59 -0400
committerKohei Yoshida <kohei.yoshida@gmail.com>2022-06-07 20:31:59 -0400
commit068dc7d7661ab9d829671447b988a42991431598 (patch)
treeba0cb38ba67b418f255563eead2a3519916201ae
parent7d54608dc334406349d10de9d1c919c6fd69131b (diff)
downloadorcus-068dc7d7661ab9d829671447b988a42991431598.tar.gz
Simplify the hor and ver alignment extraction function implementations
-rw-r--r--src/liborcus/odf_helper.cpp71
-rw-r--r--src/liborcus/odf_helper.hpp4
-rw-r--r--src/liborcus/odf_styles_context.cpp6
3 files changed, 43 insertions, 38 deletions
diff --git a/src/liborcus/odf_helper.cpp b/src/liborcus/odf_helper.cpp
index 3dc81098..530b317d 100644
--- a/src/liborcus/odf_helper.cpp
+++ b/src/liborcus/odf_helper.cpp
@@ -75,26 +75,48 @@ const map_type& get()
} // namespace underline_style
-typedef mdds::sorted_string_map<spreadsheet::hor_alignment_t> odf_horizontal_alignment_map;
+namespace hor_align {
-odf_horizontal_alignment_map::entry odf_horizontal_alignment_entries[] =
+typedef mdds::sorted_string_map<spreadsheet::hor_alignment_t> map_type;
+
+// Keys must be sorted.
+constexpr map_type::entry entries[] =
{
- { MDDS_ASCII("center"), spreadsheet::hor_alignment_t::center},
- { MDDS_ASCII("end"), spreadsheet::hor_alignment_t::right},
- { MDDS_ASCII("justified"), spreadsheet::hor_alignment_t::justified},
- { MDDS_ASCII("start"), spreadsheet::hor_alignment_t::left}
+ { MDDS_ASCII("center"), spreadsheet::hor_alignment_t::center },
+ { MDDS_ASCII("end"), spreadsheet::hor_alignment_t::right },
+ { MDDS_ASCII("justified"), spreadsheet::hor_alignment_t::justified },
+ { MDDS_ASCII("start"), spreadsheet::hor_alignment_t::left }
};
-typedef mdds::sorted_string_map<spreadsheet::ver_alignment_t> odf_vertical_alignment_map;
+const map_type& get()
+{
+ static map_type mt(entries, std::size(entries), ss::hor_alignment_t::unknown);
+ return mt;
+}
+
+} // namespace hor_align
-odf_vertical_alignment_map::entry odf_vertical_alignment_entries[] =
+namespace ver_align {
+
+typedef mdds::sorted_string_map<spreadsheet::ver_alignment_t> map_type;
+
+// Keys must be sorted.
+constexpr map_type::entry entries[] =
{
- { MDDS_ASCII("bottom"), spreadsheet::ver_alignment_t::bottom},
- { MDDS_ASCII("justified"), spreadsheet::ver_alignment_t::justified},
- { MDDS_ASCII("middle"), spreadsheet::ver_alignment_t::middle},
- { MDDS_ASCII("top"), spreadsheet::ver_alignment_t::top}
+ { MDDS_ASCII("bottom"), spreadsheet::ver_alignment_t::bottom },
+ { MDDS_ASCII("justified"), spreadsheet::ver_alignment_t::justified },
+ { MDDS_ASCII("middle"), spreadsheet::ver_alignment_t::middle },
+ { MDDS_ASCII("top"), spreadsheet::ver_alignment_t::top }
};
+const map_type& get()
+{
+ static map_type mt(entries, std::size(entries), ss::ver_alignment_t::unknown);
+ return mt;
+}
+
+} // namespace ver_align
+
bool is_valid_hex_digit(const char& character, orcus::spreadsheet::color_elem_t& val)
{
if ('0' <= character && character <= '9')
@@ -209,31 +231,14 @@ orcus::spreadsheet::underline_t odf::extract_underline_style(std::string_view va
return underline_style::get().find(value.data(), value.size());
}
-bool odf::extract_hor_alignment_style(std::string_view value, spreadsheet::hor_alignment_t& alignment)
+ss::hor_alignment_t odf::extract_hor_alignment_style(std::string_view value)
{
- odf_horizontal_alignment_map horizontal_alignment_map(odf_horizontal_alignment_entries,
- ORCUS_N_ELEMENTS(odf_horizontal_alignment_entries),
- spreadsheet::hor_alignment_t::unknown);
-
- alignment = horizontal_alignment_map.find(value.data(), value.size());
-
- if (alignment == spreadsheet::hor_alignment_t::unknown)
- return false;
-
- return true;
+ return hor_align::get().find(value.data(), value.size());
}
-bool odf::extract_ver_alignment_style(std::string_view value, spreadsheet::ver_alignment_t& alignment)
+spreadsheet::ver_alignment_t odf::extract_ver_alignment_style(std::string_view value)
{
- odf_vertical_alignment_map vertical_alignment_map(odf_vertical_alignment_entries,
- ORCUS_N_ELEMENTS(odf_vertical_alignment_entries),
- spreadsheet::ver_alignment_t::unknown);
- alignment = vertical_alignment_map.find(value.data(), value.size());
-
- if (alignment == spreadsheet::ver_alignment_t::unknown)
- return false;
-
- return true;
+ return ver_align::get().find(value.data(), value.size());
}
}
diff --git a/src/liborcus/odf_helper.hpp b/src/liborcus/odf_helper.hpp
index 5786d573..eb95692d 100644
--- a/src/liborcus/odf_helper.hpp
+++ b/src/liborcus/odf_helper.hpp
@@ -44,9 +44,9 @@ spreadsheet::underline_width_t extract_underline_width(std::string_view value);
spreadsheet::underline_t extract_underline_style(std::string_view value);
-bool extract_hor_alignment_style(std::string_view value, spreadsheet::hor_alignment_t& alignment);
+spreadsheet::hor_alignment_t extract_hor_alignment_style(std::string_view value);
-bool extract_ver_alignment_style(std::string_view value, spreadsheet::ver_alignment_t& alignment);
+spreadsheet::ver_alignment_t extract_ver_alignment_style(std::string_view value);
}}
diff --git a/src/liborcus/odf_styles_context.cpp b/src/liborcus/odf_styles_context.cpp
index b5ec1d5f..80daa473 100644
--- a/src/liborcus/odf_styles_context.cpp
+++ b/src/liborcus/odf_styles_context.cpp
@@ -213,8 +213,8 @@ void styles_context::start_element(xmlns_id_t ns, xml_token_t name, const xml_at
switch (attr.name)
{
case XML_text_align:
- odf::extract_hor_alignment_style(
- attr.value, m_current_style->cell_data->hor_align);
+ m_current_style->cell_data->hor_align =
+ odf::extract_hor_alignment_style(attr.value);
break;
default:
;
@@ -639,7 +639,7 @@ void styles_context::start_table_cell_properties(const xml_token_pair_t& parent,
break;
}
case XML_vertical_align:
- odf::extract_ver_alignment_style(attr.value, ver_alignment);
+ ver_alignment = odf::extract_ver_alignment_style(attr.value);
break;
default:
;