Arkanjo 0.1
A tool for find code duplicated functions in codebases
Loading...
Searching...
No Matches
preprocessor.cpp
Go to the documentation of this file.
1#include "preprocessor.hpp"
2#include <iostream>
3#include <iomanip>
4#include <cassert>
5#include <filesystem>
6namespace fs = std::filesystem;
7
8void Preprocessor::save_current_run_params(string path){
9 vector<string> config_content;
10
11 string path_message = PATH_MESSAGE + path;
12
13 auto end = std::chrono::system_clock::now();
14 std::time_t end_time = std::chrono::system_clock::to_time_t(end);
15 string time_message = TIME_MESSAGE + std::ctime(&end_time);
16
17 config_content.push_back(path_message);
18 config_content.push_back(time_message);
19
20 Utils::write_file_generic(Config::config()->getBasePath() + "/" + CONFIG_PATH,config_content);
21}
22
23tuple<string,double,bool> Preprocessor::read_parameters(){
24 cout << INITIAL_MESSAGE << '\n';
25 string path,similarity_message;
26
27 cout << PROJECT_PATH_MESSAGE << '\n';
28 cin >> path;
29
30 cout << MINIMUM_SIMILARITY_MESSAGE << '\n';
31 cin >> similarity_message;
32 double similarity = stod(similarity_message);
33
34 bool use_duplication_finder_by_tool=false;
35
36 while(true){
37 cout << MESSAGE_DUPLICATION_FINDER_TYPE_1 << '\n';
38 cout << MESSAGE_DUPLICATION_FINDER_TYPE_2 << '\n';
39 cout << MESSAGE_DUPLICATION_FINDER_TYPE_3 << '\n';
40 int x;
41 cin >> x;
42 if(x == 1){
43 use_duplication_finder_by_tool = true;
44 }else if(x == 2){
45 use_duplication_finder_by_tool = false;
46 }else{
47 cout << INVALID_CODE_DUPLICATION_FINDER << '\n';
48 exit(0);
49 continue;
50 }
51 break;
52 }
53
54 return {path,similarity,use_duplication_finder_by_tool};
55}
56
57void Preprocessor::preprocess(string path, double similarity, bool use_duplication_finder_by_tool){
58 cout << BREAKER_MESSAGE << '\n';
59
60 Config *config = Config::config();
61 string base_path = config->getBasePath();
62
63 fs::path dir_to_remove = base_path;
64 if (fs::exists(dir_to_remove)) {
65 fs::remove_all(dir_to_remove);
66 }
67
68 string command_rm_tmp = "rm -r -f " + base_path + "/";
69 system(command_rm_tmp.c_str());
70 FunctionBreaker function_breaker(path);
71
72 cout << DUPLICATION_MESSAGE << '\n';
73
74 if(use_duplication_finder_by_tool){
75 DuplicationFinderTool duplicationFinder(base_path,similarity);
76 duplicationFinder.execute();
77 }else{
78 DuplicationFinderDiff duplicationFinder(base_path,similarity);
79 duplicationFinder.execute();
80 }
81
82 save_current_run_params(path);
83
84 cout << END_MESSAGE << '\n';
85}
86
87Preprocessor::Preprocessor(bool force_preprocess){
88 Config *config = Config::config();
89 string base_path = config->getBasePath();
90 if(force_preprocess || !Utils::does_file_exist(base_path+"/"+CONFIG_PATH)){
91 auto [path,similarity,use_duplication_finder_by_tool] = read_parameters();
92 preprocess(path,similarity,use_duplication_finder_by_tool);
93 }
94}
95
96Preprocessor::Preprocessor(bool force_preprocess, string path, double similarity){
97 Config *config = Config::config();
98 string base_path = config->getBasePath();
99 if(force_preprocess || !Utils::does_file_exist(base_path+"/"+CONFIG_PATH)){
100 preprocess(path,similarity,true);
101 }
102}
Singleton configuration manager class.
Definition config.hpp:26
static Config * config()
Gets the singleton configuration instance.
Definition config.cpp:18
string getBasePath()
Gets the current base path.
Definition config.cpp:4
Code duplication preprocessor.
Main code duplication detection tool.
Main function extraction processor.
Preprocessor(bool force_preprocess)
Constructs preprocessor with optional forcing.
bool does_file_exist(string file_path)
Checks if a file exists at the given path.
Definition utils.cpp:68
void write_file_generic(string file_path, vector< string > content)
Writes content to a file at specified path.
Definition utils.cpp:32
Codebase preprocessing interface.