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