-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpack.cpp
More file actions
189 lines (159 loc) · 5.9 KB
/
Copy pathpack.cpp
File metadata and controls
189 lines (159 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <filesystem>
#include <direct.h>
#define MAX 300
//tar is in ascii
const char temp_location[] = "constant files/test files/temp.tar";
const char typed_end_of_file[] = "\n\n*EOF_no_more_content_to_be_displayed*\n\n";
const char typed_end_of_directory[] = "EOD_no_more_files_to_be_analysed_";
void change_extension(char* file_path, const char* extension) //are ca input un fisier tar corect
{
std::filesystem::path f_p(file_path);
if(std::filesystem::is_directory(f_p) && extension[0] == '.')
strcpy(strstr(file_path, "."), extension);
}
char* get_file_name(char source_path[])
{
size_t length = strlen(source_path), index_context = 0;
for(size_t i = 0; i < length - 1; i++)
if(source_path[i]=='/' || source_path[i] == 92) // '\' == 92
index_context = i;
return source_path + index_context + 1;
}
//output is in temp
void help_build(FILE* p_bar, FILE* p_components, char path_of_file[], char identation[])
{
const std::filesystem::path file_path(path_of_file); std::error_code ec;
if (std::filesystem::is_directory(file_path, ec)) //directory
{
if(identation[0])
strcat(path_of_file, "/\0");
fprintf(p_bar, "%s%s\n", identation, get_file_name(path_of_file));
strcat(identation, "\t\0");
for (auto& p : std::filesystem::directory_iterator(file_path)) //alphabetical order
if (p.path().empty() == false)
help_build(p_bar, p_components, (char*)p.path().string().c_str(), identation);
identation[strlen(identation) - 1] = '\0';
fprintf(p_bar, "%s%s%s\n", identation, typed_end_of_directory, get_file_name(path_of_file));
}
else //normal file
{
p_components = fopen(path_of_file, "rb");
if (p_components == nullptr)
{
std::cout << "error - composition: can not open:" << path_of_file <<" .\n";
return;
}
fprintf(p_bar, "%s%s", identation, get_file_name(path_of_file));
char temp = fgetc(p_components); if(temp!=EOF) fprintf(p_bar, "\n\n%c", temp);
while ((temp = fgetc(p_components)) != EOF)
fputc(temp, p_bar);
fputs(typed_end_of_file, p_bar);
fclose(p_components);
}
}
void build_tar(short nr_paths, char** paths_input)
{
char identation[MAX] = { 0 };
FILE* p_bar = nullptr, * p_components = nullptr;
p_bar = fopen("constant files/test files/temp.tar", "wb");
if (p_bar == nullptr)
{
std::cout << "error - composition: can not create temp file.\n";
return;
}
for(int i=0; i < nr_paths; i++)
help_build(p_bar, p_components, paths_input[i], identation);
fclose(p_bar);
}
void read_antet(FILE* pointer_to_file, char my_string[]) // was not used in the end
{
char* character = new char{ 0 };
for(size_t i = 0; i < strlen(my_string); i++)
my_string[i] = '\0';
size_t index = 0;
fread(character, 1, 1, pointer_to_file);
while(*character != '\n')
{
my_string[index++] = *character;
fread(character, 1, 1, pointer_to_file);
}
}
void help_decompose_content(FILE* p_bar, FILE* p_components, char* path_output)
{
p_components = fopen(path_output, "wb");
if(p_components == nullptr)
std::cout << "error - decomposition: can not write the files.\n";
fseek(p_bar, -1, SEEK_CUR);
char check_empty[MAX] = { 0 };
fread(check_empty, sizeof(char), strlen(typed_end_of_file), p_bar);
if(strcmp(check_empty, typed_end_of_file) == 0)
return;
fseek(p_bar, -strlen(typed_end_of_file), SEEK_CUR); // rewind pointer
fseek(p_bar, 2, SEEK_CUR); // remove file_name_antent's "\n"
char* character = new char{ 0 };
while(true)
{
fread(character, 1, 1, p_bar);
if(*character == typed_end_of_file[0])
{
bool condition_to_continue = false;
char temp[MAX] = { *character }, temp_index = 1;
for(size_t i = 1; i < strlen(typed_end_of_file) && condition_to_continue == false; i++)
{
fread(character, 1, 1, p_bar);
temp[temp_index++] = *character;
if(*character != typed_end_of_file[i])
condition_to_continue = true;
}
if(condition_to_continue == false)
break;
fwrite(temp, sizeof(char), (size_t)temp_index, p_components);
}
else fwrite(character, 1, 1, p_components);
}
delete character;
fclose(p_components);
}
void help_decompose_antet(FILE* p_bar, FILE* p_components, char* path_output, char line[])
{
line[strlen(line) - 1] = '\0'; //delete '\n'
while (line[0] == '\t')
strcpy(line, line + 1);
char new_destination[MAX];
strcpy(new_destination, path_output);
strcat(new_destination, line);
if (line[strlen(line) - 1] == '/') // directory name
{
mkdir(new_destination);
char previous_line[MAX];
strcpy(previous_line, line);
fgets(line, MAX, p_bar);
while(strstr(line, typed_end_of_directory) == nullptr && strstr(line, previous_line) == nullptr)
{
help_decompose_antet(p_bar, p_components, new_destination, line);
fgets(line, MAX, p_bar);
}
}
else help_decompose_content(p_bar, p_components, new_destination);
}
void decompose_tar(char* path_output, char* output_name)
{
char final_path_output[MAX] = {0};
strcpy(final_path_output, path_output);
strcat(final_path_output, output_name);
mkdir(final_path_output);
FILE* p_bar = nullptr, * p_components = nullptr;
p_bar = fopen(temp_location, "rb");
if (p_bar == nullptr)
{
std::cout << "error - decomposition: missing temp file.\n";
return;
}
char line[MAX] = {0};
while(fgets(line, MAX, p_bar))
help_decompose_antet(p_bar, p_components, final_path_output, line);
fclose(p_bar);
}