Arkanjo 0.1
A tool for find code duplicated functions in codebases
Loading...
Searching...
No Matches
parser.cpp
Go to the documentation of this file.
1#include <bits/stdc++.h>
2using namespace std;
3
4
5double CAP = 0;
6
7
8vector<string> parser_line(string line){
9 string at = "";
10 vector<string> ret;
11 for(auto c : line){
12 if(c <= 20 || c == ' '){ //an space on non-printable char
13 if(!at.empty())
14 ret.push_back(at);
15 at = "";
16 }else{
17 at += c;
18 }
19 }
20 if(!at.empty())
21 ret.push_back(at);
22
23 return ret;
24}
25
26bool is_an_file(string s){
27 return !s.empty() && s[0] == '/';
28}
29
31 for(int i = 0; i < 4; i++){
32 s.pop_back();
33 }
34 reverse(s.begin(),s.end());
35 while(s.back() != 'm'){
36 s.pop_back();
37 }
38 s.pop_back();
39 reverse(s.begin(),s.end());
40 return s;
41}
42
43double retrive_similarity(string s){
45 char *cs = s.data();
46 float similarity = stod(s);
47 return similarity;
48}
49
50
51struct Comparation{
52 string path1, path2;
53 double similarity;
54
55
57 }
58
59 Comparation(string _path1, string _path2, double _sim){
60 if(_path1 > _path2) swap(_path1,_path2);
61 path1 = _path1;
62 path2 = _path2;
63 similarity = _sim;
64 }
65
66 bool operator<(const Comparation &com) const{
67 if(similarity != com.similarity){
68 return similarity > com.similarity;
69 }
70 if(path1 == com.path1){
71 return path2 < com.path2;
72 }
73 return path1 < com.path1;
74 }
75
76 bool operator==(const Comparation &com) const{
77 return path1 == com.path1 && path2 == com.path2;
78 }
79};
80
81
82void parser_block(string path, set<Comparation> &comparations){
83 string line;
84 while(getline(cin,line)){
85 vector<string> tokens = parser_line(line);
86 if(tokens.empty()){
87 break;
88 }
89 if( int32_t(tokens.size()) != 2 || !is_an_file(tokens[0])){
90 continue;
91 }
92 string path_compared = tokens[0];
93
94 double similarity = retrive_similarity(tokens[1]);
95 Comparation com(path,path_compared,similarity);
96 if(similarity >= CAP)
97 comparations.insert(com);
98 }
99}
100
101
102void parser(){
103 string line;
104 set<Comparation> comparations;
105
106 while(getline(cin,line)){
107 vector<string> tokens = parser_line(line);
108 if(tokens.empty()) continue;
109
110 string path;
111 for(auto token : tokens){
112 if(is_an_file(token)){
113 path = token;
114 break;
115 }
116 }
117 //Removing some formatation that shows on terminal
118 for(int i = 0; i < 4; i++) path.pop_back();
119 parser_block(path,comparations);
120 }
121 cout << comparations.size() << '\n';
122 for(auto com : comparations){
123 cout << com.path1 << ' ' << com.path2 << ' ';
124 cout << fixed << setprecision(2) << com.similarity << '\n';
125 }
126}
127
128int main(int argc, char *argv[]){
129 if(argc >= 2){
130 CAP = atof(argv[1]);
131 }
132 parser();
133 return 0;
134}
return ret
Definition sum.c:3
vector< string > parser_line(string line)
Definition parser.cpp:8
void parser_block(string path, set< Comparation > &comparations)
Definition parser.cpp:82
int main(int argc, char *argv[])
Definition parser.cpp:128
double CAP
Definition parser.cpp:5
string remove_formatation_from_similarity(string s)
Definition parser.cpp:30
void parser()
Definition parser.cpp:102
bool is_an_file(string s)
Definition parser.cpp:26
double retrive_similarity(string s)
Definition parser.cpp:43
Structure representing a code comparison result.
Definition parser.hpp:25
bool operator<(const Comparation &com) const
Definition parser.cpp:66
double similarity
Similarity score between the files (0-100)
Definition parser.hpp:28
Comparation(string _path1, string _path2, double _sim)
Definition parser.cpp:59
string path1
Path to first code file being compared.
Definition parser.hpp:26
bool operator==(const Comparation &com) const
Definition parser.cpp:76
string path2
Path to second code file being compared.
Definition parser.hpp:27