Arkanjo 0.2
A tool for find code duplicated functions in codebases
Loading...
Searching...
No Matches
path.cpp
Go to the documentation of this file.
1
3
4#include <iostream>
5
6std::vector<std::string> Path::split_path(const fs::path& path) {
7 std::vector<std::string> parts;
8 for (const auto& part : path) {
9 std::string s = part.string();
10 if (!s.empty()) {
11 parts.push_back(s);
12 }
13 }
14 return parts;
15}
16
17size_t Path::find_position_start_relative_path() const {
18 size_t sz = tokens.size();
19 size_t ret = sz;
20 for (size_t i = 1; i < sz - 1; i++) {
21 if (tokens[i - 1] == BASE_INIT_STRING && tokens[i + 1] == SOURCE_STRING) {
22 ret = i + 2;
23 }
24 }
25 assert(ret != sz && "PATH NOT VALID FOR THE TOOL");
26 return ret;
27}
28
29bool Path::is_empty() const {
30 return tokens.empty();
31}
32
33Path::Path(const fs::path& path) {
34 tokens = split_path(path);
35 position_start_relative_path = find_position_start_relative_path();
36}
37
38std::string Path::build_string_path(const std::vector<std::string>& path_tokens) {
39 fs::path p;
40 for (const auto& token : path_tokens) {
41 p /= token;
42 }
43 return p.string();
44}
45
46std::string Path::build_base_path(const std::string& base) const {
47 if (is_empty()) {
48 return "";
49 }
50 std::vector<std::string> path = tokens;
51 size_t pos_change = position_start_relative_path - 1;
52 path[pos_change] = base;
53 std::string string_path = build_string_path(path);
54 return string_path;
55}
56
57std::string Path::build_source_path() const {
58 return build_base_path(SOURCE_STRING);
59}
60
61std::string Path::build_header_path() const {
62 return build_base_path(HEADER_STRING);
63}
64
65std::string Path::build_info_path() const {
66 std::string ret = build_base_path(INFO_STRING);
67 if (ret == "") {
68 return "";
69 }
70 ret = remove_extension(ret);
71 if (ret == "") {
72 return "";
73 }
74 ret += JSON_EXTENSION;
75 return ret;
76}
77
78std::vector<std::string> Path::get_tokens_from_relative_path() const {
79 std::vector<std::string> token_relative_path = tokens;
80 token_relative_path.pop_back();
81 size_t to_remove = position_start_relative_path;
82 reverse(token_relative_path.begin(), token_relative_path.end());
83 for (size_t i = 0; i < to_remove; i++) {
84 token_relative_path.pop_back();
85 }
86 reverse(token_relative_path.begin(), token_relative_path.end());
87 return token_relative_path;
88}
89
90std::string Path::remove_extension(std::string& token) {
91 while (!token.empty()) {
92 auto c = token.back();
93 token.pop_back();
94 if (c == '.') {
95 break;
96 }
97 }
98 return token;
99}
100
101std::string Path::build_relative_path() const {
102 if (is_empty()) {
103 return "";
104 }
105 std::vector<std::string> token_relative_path = get_tokens_from_relative_path();
106 std::string string_path = build_string_path(token_relative_path);
107 return string_path;
108}
109
110std::string Path::build_function_name() const {
111 if (is_empty()) {
112 return "";
113 }
114 std::string function_name = tokens.back();
115 function_name = remove_extension(function_name);
116 return function_name;
117}
118
119std::vector<std::string> Path::get_common_folders(const Path& path) const {
120 std::vector<std::string> tokens_relative_1 = get_tokens_from_relative_path();
121 std::vector<std::string> tokens_relative_2 = path.get_tokens_from_relative_path();
122 size_t minimum_size_tokens = std::min(tokens_relative_1.size(), tokens_relative_2.size());
123 std::vector<std::string> common_folders;
124 for (size_t i = 0; i < minimum_size_tokens; i++) {
125 auto token_1 = tokens_relative_1[i];
126 auto token_2 = tokens_relative_2[i];
127 if (token_1 == token_2) {
128 common_folders.push_back(token_1);
129 } else {
130 break;
131 }
132 }
133 return common_folders;
134}
135
136bool Path::operator<(const Path& path) const {
137 return tokens < path.tokens;
138}
139
140bool Path::contains_given_pattern(const std::string& pattern) const {
141 std::string relative_path_plus_function_name = build_relative_path() + BAR + build_function_name();
142 return relative_path_plus_function_name.find(pattern) != std::string::npos;
143}
144
146 return build_relative_path() + BETWEEN_RELATIVE_AND_FUNCTION_NAME + build_function_name();
147}
Path manipulation class for tool-specific directory structure.
Definition path.hpp:27
std::string format_path_message_in_pair() const
Formats path for display.
Definition path.cpp:145
std::string build_relative_path() const
Builds relative path portion.
Definition path.cpp:101
std::string build_info_path() const
Builds metadata file path.
Definition path.cpp:65
std::vector< std::string > get_common_folders(const Path &path) const
Finds common folders with another path.
Definition path.cpp:119
std::string build_function_name() const
Extracts function name from path.
Definition path.cpp:110
bool contains_given_pattern(const std::string &pattern) const
Checks for pattern in path.
Definition path.cpp:140
Path()=default
Default constructor.
bool operator<(const Path &path) const
Path comparison operator.
Definition path.cpp:136
std::string build_header_path() const
Builds header file path.
Definition path.cpp:61
std::string build_source_path() const
Builds source file path.
Definition path.cpp:57
bool is_empty() const
Checks if path is empty.
Definition path.cpp:29
Path abstraction for temporary codebase.