-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.c
129 lines (114 loc) · 2.75 KB
/
file.c
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
#include <string.h>
#include <stdlib.h>
#include "util.h"
#include "file.h"
ArrayList *arraylist_init(size_t capacity)
{
ArrayList *list = memalloc(sizeof(ArrayList));
list->length = 0;
list->capacity = capacity;
list->items = memalloc(capacity * sizeof(file));
return list;
}
void arraylist_free(ArrayList *list)
{
for (size_t i = 0; i < list->length; i++) {
if (list->items[i].name != NULL)
free(list->items[i].name);
if (list->items[i].path != NULL)
free(list->items[i].path);
if (list->items[i].stats != NULL)
free(list->items[i].stats);
}
free(list->items);
free(list);
}
/*
* Check if the file is in the arraylist
* Treat filepath as base name if bname is 1
*/
long arraylist_search(ArrayList *list, char *filepath, int bname)
{
for (long i = 0; i < list->length; i++) {
if (!bname && strcmp(list->items[i].path, filepath) == 0) {
return i;
}
if (bname) {
if (strcmp(list->items[i].name, filepath) == 0) {
return i;
}
}
}
return -1;
}
void arraylist_remove(ArrayList *list, long index)
{
if (index >= list->length)
return;
/* marked stuff doesn't work with this
free(list->items[index].name);
free(list->items[index].path);
free(list->items[index].stats);
*/
for (long i = index; i < list->length - 1; i++)
list->items[i] = list->items[i + 1];
list->length--;
}
/*
* Force will not remove duplicate marked files, instead it just skip adding
*/
void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int type, char *icon, int color, int marked, int force)
{
file new_file = { name, path, type, stats, color };
strcpy(new_file.icon, icon);
if (list->capacity != list->length) {
if (marked) {
for (int i = 0; i < list->length; i++) {
if (strcmp(list->items[i].path, new_file.path) == 0) {
if (!force)
arraylist_remove(list, i);
return;
}
}
}
list->items[list->length] = new_file;
} else {
int new_cap = list->capacity * 2;
file *new_items = memalloc(new_cap * sizeof(file));
file *old_items = list->items;
list->capacity = new_cap;
list->items = new_items;
for (int i = 0; i < list->length; i++)
new_items[i] = old_items[i];
free(old_items);
list->items[list->length] = new_file;
}
list->length++;
}
/*
* Construct a formatted line for display
*/
char *get_line(ArrayList *list, long index, int detail, int icons)
{
file f = list->items[index];
size_t length = strlen(f.name) + 1;
if (detail) {
length += strlen(f.stats) + 1; /* 1 for space */
}
if (icons) {
length += 5; /* 4 for icon, 1 for space */
}
char *line = memalloc(length);
line[0] = '\0';
if (detail) {
strcat(line, f.stats);
strcat(line, " ");
}
if (icons) {
strcat(line, f.icon);
strcat(line, " ");
}
strcat(line, f.name);
line[length] = '\0';
return line;
}