-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchiveview.cpp
72 lines (51 loc) · 1.65 KB
/
archiveview.cpp
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
#include "archiveview.h"
int get_info_block_pos(FILE *encoded_file) {
int pos = 0, length = 0;
rewind(encoded_file);
if (!feof(encoded_file)) {
fseek(encoded_file, -(sizeof(int) * 2 + 2), SEEK_END);
fread(&pos, sizeof(int), 1, encoded_file);
fread(&length, sizeof(int), 1, encoded_file);
pos += length;
}
return pos;
}
void add_new_string(const char *new_str, Strings **head) {
Strings *cursor = *head;
Strings *new_item = (Strings*)malloc(sizeof(Strings));
new_item->string = (char*)malloc(MAX_PATH * sizeof(char));
strcpy(new_item->string, new_str);
new_item->next = NULL;
if (cursor == NULL) {
*head = new_item;
} else {
while (cursor->next != NULL) {
cursor = cursor->next;
}
cursor->next = new_item;
}
}
Strings* get_archived_file_names(const char *archive_name) {
FILE *archived_file = NULL;
fopen_s(&archived_file, archive_name, "rb");
Strings *head = NULL;
char *file_name = (char*)malloc(MAX_PATH * sizeof(char));
int pos = get_info_block_pos(archived_file);
if (pos > 0) {
fseek(archived_file, pos, SEEK_SET);
while (fgets(file_name, MAX_PATH, archived_file)) {
file_name[strlen(file_name) - 2] = '\0';
add_new_string((const char*)file_name, &head);
fseek(archived_file, 2 * sizeof(int) + 2, SEEK_CUR);
}
}
fclose(archived_file);
return head;
}
void display_list_contents(struct Strings *head) {
Strings *cursor = head;
while (cursor != NULL) {
printf("\n%s\n", cursor->string);
cursor = cursor->next;
}
}