Line data Source code
1 : // Copyright (c) 2013-2024 Made to Order Software Corp. All Rights Reserved 2 : // 3 : // This program is free software: you can redistribute it and/or modify 4 : // it under the terms of the GNU General Public License as published by 5 : // the Free Software Foundation, either version 3 of the License, or 6 : // (at your option) any later version. 7 : // 8 : // This program is distributed in the hope that it will be useful, 9 : // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 : // GNU General Public License for more details. 12 : // 13 : // You should have received a copy of the GNU General Public License 14 : // along with this program. If not, see <https://www.gnu.org/licenses/>. 15 : 16 : // self 17 : // 18 : #include "edhttp/compression/archiver.h" 19 : 20 : 21 : 22 : // C++ 23 : // 24 : #include <ranges> 25 : 26 : 27 : // last include 28 : // 29 : #include <snapdev/poison.h> 30 : 31 : 32 : 33 : namespace edhttp 34 : { 35 : 36 : 37 : namespace 38 : { 39 : 40 : 41 : 42 : typedef std::map<std::string, archiver *> archiver_map_t; 43 : 44 : // IMPORTANT NOTE: 45 : // This list only makes use of bare pointers for many good reasons. 46 : // (i.e. all archivers are defined statitcally, not allocated) 47 : // Do not try to change it! Thank you. 48 : // 49 : archiver_map_t * g_archivers; 50 : 51 : 52 : 53 : } // no name namespace 54 : 55 : 56 : 57 2 : archiver::archiver(char const * name) 58 : { 59 2 : if(g_archivers == nullptr) 60 : { 61 2 : g_archivers = new archiver_map_t; 62 : } 63 2 : (*g_archivers)[name] = this; 64 2 : } 65 : 66 : 67 2 : archiver::~archiver() 68 : { 69 2 : for(auto it(g_archivers->begin()); it != g_archivers->end(); ++it) 70 : { 71 2 : if(it->second == this) 72 : { 73 2 : g_archivers->erase(it); 74 2 : break; 75 : } 76 : } 77 : 78 2 : if(g_archivers->empty()) 79 : { 80 2 : delete g_archivers; 81 2 : g_archivers = nullptr; 82 : } 83 2 : } 84 : 85 : 86 1 : advgetopt::string_list_t archiver_list() 87 : { 88 1 : if(g_archivers == nullptr) 89 : { 90 : return advgetopt::string_list_t(); // LCOV_EXCL_LINE 91 : } 92 : 93 1 : auto kv = std::views::keys(*g_archivers); 94 1 : return advgetopt::string_list_t(kv.begin(), kv.end()); 95 : } 96 : 97 : 98 13 : archiver * get_archiver(std::string const & archiver_name) 99 : { 100 13 : if(g_archivers != nullptr) 101 : { 102 13 : auto it(g_archivers->find(archiver_name)); 103 13 : if(it != g_archivers->end()) 104 : { 105 12 : return it->second; 106 : } 107 : } 108 : 109 1 : return nullptr; 110 : } 111 : 112 : 113 : 114 : } // namespace edhttp 115 : // vim: ts=4 sw=4 et