From e90ff462412902a1e06878a0f34dc2e0323e738c Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Wed, 17 Aug 2022 17:45:35 +0200 Subject: [PATCH] Extract Collator class. --- src/lib/collator.cc | 69 +++++++++++++++++++++++++++++++++++++++++ src/lib/collator.h | 49 +++++++++++++++++++++++++++++ src/lib/wscript | 1 + src/wx/screens_panel.cc | 41 ++---------------------- src/wx/screens_panel.h | 5 ++- 5 files changed, 124 insertions(+), 41 deletions(-) create mode 100644 src/lib/collator.cc create mode 100644 src/lib/collator.h diff --git a/src/lib/collator.cc b/src/lib/collator.cc new file mode 100644 index 000000000..4f52597c2 --- /dev/null +++ b/src/lib/collator.cc @@ -0,0 +1,69 @@ +/* + Copyright (C) 2022 Carl Hetherington + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see . + +*/ + + +#include "collator.h" +#include +#include +#include +#include +#include +#include +#include + + +using std::string; + + +Collator::Collator() +{ + UErrorCode status = U_ZERO_ERROR; + _collator = ucol_open(nullptr, &status); + if (_collator) { + ucol_setAttribute(_collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status); + ucol_setAttribute(_collator, UCOL_STRENGTH, UCOL_PRIMARY, &status); + ucol_setAttribute(_collator, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status); + } +} + + +Collator::~Collator() +{ + if (_collator) { + ucol_close (_collator); + } +} + + +int +Collator::compare (string const& utf8_a, string const& utf8_b) +{ + if (_collator) { + UErrorCode error = U_ZERO_ERROR; + boost::scoped_array utf16_a(new uint16_t[utf8_a.size() + 1]); + u_strFromUTF8(reinterpret_cast(utf16_a.get()), utf8_a.size() + 1, nullptr, utf8_a.c_str(), -1, &error); + boost::scoped_array utf16_b(new uint16_t[utf8_b.size() + 1]); + u_strFromUTF8(reinterpret_cast(utf16_b.get()), utf8_b.size() + 1, nullptr, utf8_b.c_str(), -1, &error); + return ucol_strcoll(_collator, reinterpret_cast(utf16_a.get()), -1, reinterpret_cast(utf16_b.get()), -1); + } else { + return strcoll(utf8_a.c_str(), utf8_b.c_str()); + } +} + diff --git a/src/lib/collator.h b/src/lib/collator.h new file mode 100644 index 000000000..3762f2400 --- /dev/null +++ b/src/lib/collator.h @@ -0,0 +1,49 @@ +/* + Copyright (C) 2022 Carl Hetherington + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see . + +*/ + + +#ifndef DCPOMATIC_COLLATOR_H +#define DCPOMATIC_COLLATOR_H + + +#include + + +struct UCollator; + + +class Collator +{ +public: + Collator(); + ~Collator(); + + Collator(Collator const &) = delete; + Collator& operator=(Collator const&) = delete; + + int compare(std::string const& utf8_a, std::string const& utf8_b); + +private: + UCollator* _collator = nullptr; +}; + + +#endif + diff --git a/src/lib/wscript b/src/lib/wscript index 26fcb21e5..d3aec7769 100644 --- a/src/lib/wscript +++ b/src/lib/wscript @@ -51,6 +51,7 @@ sources = """ check_content_job.cc cinema.cc cinema_sound_processor.cc + collator.cc colour_conversion.cc config.cc content.cc diff --git a/src/wx/screens_panel.cc b/src/wx/screens_panel.cc index 562ea8f38..a726030ad 100644 --- a/src/wx/screens_panel.cc +++ b/src/wx/screens_panel.cc @@ -29,11 +29,6 @@ #include "lib/scope_guard.h" #include "lib/screen.h" #include "lib/timer.h" -#include -#include -#include -#include -#include using std::cout; @@ -120,14 +115,6 @@ ScreensPanel::ScreensPanel (wxWindow* parent) SetSizer (sizer); - UErrorCode status = U_ZERO_ERROR; - _collator = ucol_open(nullptr, &status); - if (_collator) { - ucol_setAttribute(_collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status); - ucol_setAttribute(_collator, UCOL_STRENGTH, UCOL_PRIMARY, &status); - ucol_setAttribute(_collator, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status); - } - _config_connection = Config::instance()->Changed.connect(boost::bind(&ScreensPanel::config_changed, this, _1)); } @@ -136,10 +123,6 @@ ScreensPanel::~ScreensPanel () { _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this); _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this); - - if (_collator) { - ucol_close (_collator); - } } @@ -239,7 +222,7 @@ ScreensPanel::add_cinema_clicked () auto cinemas = Config::instance()->cinemas(); cinemas.sort( - [this](shared_ptr a, shared_ptr b) { return compare(a->name, b->name) < 0; } + [this](shared_ptr a, shared_ptr b) { return _collator.compare(a->name, b->name) < 0; } ); try { @@ -254,7 +237,7 @@ ScreensPanel::add_cinema_clicked () wxTreeListItem previous = wxTLI_FIRST; bool found = false; for (auto existing_cinema: cinemas) { - if (compare(dialog->name(), existing_cinema->name) < 0) { + if (_collator.compare(dialog->name(), existing_cinema->name) < 0) { /* existing_cinema should be after the one we're inserting */ found = true; break; @@ -502,7 +485,7 @@ ScreensPanel::add_cinemas () { auto cinemas = Config::instance()->cinemas(); cinemas.sort( - [this](shared_ptr a, shared_ptr b) { return compare(a->name, b->name) < 0; } + [this](shared_ptr a, shared_ptr b) { return _collator.compare(a->name, b->name) < 0; } ); for (auto cinema: cinemas) { @@ -667,22 +650,6 @@ ScreensPanel::screen_to_item (shared_ptr screen) const } -int -ScreensPanel::compare (string const& utf8_a, string const& utf8_b) -{ - if (_collator) { - UErrorCode error = U_ZERO_ERROR; - boost::scoped_array utf16_a(new uint16_t[utf8_a.size() + 1]); - u_strFromUTF8(reinterpret_cast(utf16_a.get()), utf8_a.size() + 1, nullptr, utf8_a.c_str(), -1, &error); - boost::scoped_array utf16_b(new uint16_t[utf8_b.size() + 1]); - u_strFromUTF8(reinterpret_cast(utf16_b.get()), utf8_b.size() + 1, nullptr, utf8_b.c_str(), -1, &error); - return ucol_strcoll(_collator, reinterpret_cast(utf16_a.get()), -1, reinterpret_cast(utf16_b.get()), -1); - } else { - return strcoll(utf8_a.c_str(), utf8_b.c_str()); - } -} - - bool ScreensPanel::notify_cinemas_changed() { @@ -707,5 +674,3 @@ ScreensPanel::config_changed(Config::Property property) clear_and_re_add(); } } - - diff --git a/src/wx/screens_panel.h b/src/wx/screens_panel.h index a91fbc0e0..e9cd8454d 100644 --- a/src/wx/screens_panel.h +++ b/src/wx/screens_panel.h @@ -19,6 +19,7 @@ */ +#include "lib/collator.h" #include "lib/config.h" #include LIBDCP_DISABLE_WARNINGS @@ -38,7 +39,6 @@ namespace dcpomatic { class Cinema; -struct UCollator; class ScreensPanel : public wxPanel @@ -69,7 +69,6 @@ private: std::shared_ptr cinema_for_operation () const; void set_screen_checked (wxTreeListItem item, bool checked); void setup_cinema_checked_state (wxTreeListItem screen); - int compare (std::string const& utf8_a, std::string const& utf8_b); void check_all (); void uncheck_all (); bool notify_cinemas_changed(); @@ -111,7 +110,7 @@ private: bool _ignore_selection_change = false; bool _ignore_check_change = false; - UCollator* _collator = nullptr; + Collator _collator; boost::signals2::scoped_connection _config_connection; bool _ignore_cinemas_changed = false; -- 2.30.2