1 /**
2  * Copyright (c) 2018, Timothy Stack
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither the name of Timothy Stack nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * @file unique_path.hh
30  */
31 
32 #ifndef LNAV_UNIQUE_PATH_HH
33 #define LNAV_UNIQUE_PATH_HH
34 
35 #include <map>
36 #include <memory>
37 #include <string>
38 #include <vector>
39 
40 #include "ghc/filesystem.hpp"
41 
42 /**
43  * A source of a path for the unique_path_generator.
44  */
45 class unique_path_source {
46 public:
47     virtual ~unique_path_source() = default;
48 
set_unique_path(const std::string & path)49     virtual void set_unique_path(const std::string &path) {
50         this->ups_unique_path = path;
51     }
52 
get_unique_path() const53     virtual std::string get_unique_path() const {
54         return this->ups_unique_path;
55     }
56 
57     virtual ghc::filesystem::path get_path() const = 0;
58 
get_path_prefix()59     ghc::filesystem::path& get_path_prefix() {
60         return this->ups_prefix;
61     }
62 
set_path_prefix(const ghc::filesystem::path & prefix)63     void set_path_prefix(const ghc::filesystem::path &prefix) {
64         this->ups_prefix = prefix;
65     }
66 private:
67     ghc::filesystem::path ups_prefix;
68     std::string ups_unique_path;
69 };
70 
71 /**
72  * Given a collection of filesystem paths, this class will generate a shortened
73  * and unique path for each of the given paths.
74  */
75 class unique_path_generator {
76 public:
77     void add_source(const std::shared_ptr<unique_path_source>& path_source);
78 
79     void generate();
80 
81     std::map<std::string, std::vector<std::shared_ptr<unique_path_source>>> upg_unique_paths;
82     size_t upg_max_len{0};
83 };
84 
85 #endif //LNAV_UNIQUE_PATH_HH
86