Debian Codegraph @

BambooTracker/io/io_utils.hpp

raw
1 /*
2 * Copyright (C) 2018-2021 Rerrah
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #pragma once
27
28 #include <string>
29 #include <vector>
30 #include <memory>
31 #include <unordered_map>
32 #include <algorithm>
33 #include "enum_hash.hpp"
34
35 enum class FMEnvelopeParameter;
36 enum class FMOperatorType;
37
38 namespace io
39 {
40 class BinaryContainer;
41
42 template<class T>
43 class FileIOHandlerMap
44 {
45 public:
46 FileIOHandlerMap()
47 {
48 const std::string all = "All files (*)";
49 ldFilters_.push_back(all);
50 svFilters_.push_back(all);
51 }
52 void add(T* handler)
53 {
54 const std::string ext = handler->getExtension();
55 map_[ext].reset(handler);
56 const std::string filter = handler->getFilterText();
57 if (handler->isLoadable()) {
58 ldFilters_.insert(ldFilters_.end() - 1, filter);
59 if (ldFilters_.size() > 2) ldExts_ += " ";
60 ldExts_ += ("*." + ext);
61 }
62 if (handler->isSavable()) {
63 svFilters_.insert(svFilters_.end() - 1, filter);
64 if (svFilters_.size() > 2) svExts_ += " ";
65 svExts_ += ("*." + ext);
66 }
67 }
68 bool containExtension(const std::string& ext) const { return map_.count(ext); }
69 bool testLoadableExtension(const std::string& ext) const { return (map_.count(ext) && map_.at(ext)->isLoadable()); }
70 bool testSavableExtension(const std::string& ext) const { return (map_.count(ext) && map_.at(ext)->isSavable()); }
71 const std::unique_ptr<T>& at(const std::string& ext) const { return map_.at(ext); }
72 std::vector<std::string> getLoadFilterList() const noexcept
73 {
74 std::vector<std::string> filters = ldFilters_;
75 filters.insert(filters.begin(), "All supported formats (" + ldExts_ + ")");
76 return filters;
77 }
78 std::vector<std::string> getSaveFilterList() const noexcept { return svFilters_; }
79
80 private:
81 std::unordered_map<std::string, std::unique_ptr<T>> map_;
82 std::vector<std::string> ldFilters_, svFilters_;
83 std::string ldExts_, svExts_;
84 };
85
86 enum class FMOperatorParameter
87 {
88 AR, DR, SR, RR, SL, TL, KS, ML, DT, SSGEG
89 };
90
91 extern const std::unordered_map<FMOperatorParameter, FMEnvelopeParameter> FM_OP_PARAMS[4];
92 extern const FMEnvelopeParameter FM_OPSEQ_PARAMS[38];
93 extern const FMOperatorType FM_OP_TYPES[4];
94
95 inline std::string getExtension(const std::string& path)
96 {
97 std::string ext = path.substr(path.find_last_of(".") + 1);
98 std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
99 return ext;
100 }
101
102 int convertDtFromDmpTfiVgi(int dt);
103 }