2024-03-09 20:18:19 +01:00
|
|
|
#include <string.h>
|
2024-03-11 22:54:14 +01:00
|
|
|
#include <stdlib.h>
|
2024-03-15 19:28:37 +01:00
|
|
|
#include <stdbool.h>
|
2024-03-12 21:59:36 +01:00
|
|
|
#include <libgen.h>
|
2024-03-29 23:34:26 +01:00
|
|
|
#include <wchar.h>
|
2024-03-09 20:18:19 +01:00
|
|
|
|
|
|
|
#include "util.h"
|
2024-03-29 23:34:26 +01:00
|
|
|
#include "file.h"
|
2024-03-09 20:18:19 +01:00
|
|
|
|
2024-03-20 21:29:04 +01:00
|
|
|
ArrayList *arraylist_init(size_t capacity)
|
2024-03-09 20:18:19 +01:00
|
|
|
{
|
2024-03-20 21:29:04 +01:00
|
|
|
ArrayList *list = memalloc(sizeof(ArrayList));
|
|
|
|
list->length = 0;
|
|
|
|
list->capacity = capacity;
|
|
|
|
list->items = memalloc(capacity * sizeof(file));
|
2024-03-29 23:34:26 +01:00
|
|
|
|
2024-03-20 21:29:04 +01:00
|
|
|
return list;
|
2024-03-12 21:59:36 +01:00
|
|
|
}
|
|
|
|
|
2024-03-20 21:29:04 +01:00
|
|
|
void arraylist_free(ArrayList *list)
|
2024-03-13 11:18:35 +01:00
|
|
|
{
|
2024-03-20 21:29:04 +01:00
|
|
|
for (size_t i = 0; i < list->length; i++) {
|
2024-04-01 22:53:19 +02:00
|
|
|
if (list->items[i].name != NULL)
|
|
|
|
free(list->items[i].name);
|
2024-03-20 21:29:04 +01:00
|
|
|
if (list->items[i].path != NULL)
|
|
|
|
free(list->items[i].path);
|
2024-03-31 22:44:05 +02:00
|
|
|
if (list->items[i].type != NULL)
|
|
|
|
free(list->items[i].type);
|
2024-03-20 21:29:04 +01:00
|
|
|
if (list->items[i].stats != NULL)
|
|
|
|
free(list->items[i].stats);
|
2024-03-30 04:39:02 +01:00
|
|
|
if (list->items[i].icon != NULL)
|
|
|
|
free(list->items[i].icon);
|
2024-03-20 21:29:04 +01:00
|
|
|
}
|
2024-03-29 23:34:26 +01:00
|
|
|
|
2024-03-20 21:29:04 +01:00
|
|
|
free(list->items);
|
2024-04-07 00:46:14 +02:00
|
|
|
free(list);
|
2024-03-13 11:18:35 +01:00
|
|
|
}
|
|
|
|
|
2024-03-31 22:44:05 +02:00
|
|
|
/*
|
|
|
|
* Check if the file is in the arraylist
|
2024-04-01 22:53:19 +02:00
|
|
|
* Treat filepath as base name if bname is true
|
2024-03-31 22:44:05 +02:00
|
|
|
*/
|
2024-04-01 22:53:19 +02:00
|
|
|
long arraylist_search(ArrayList *list, char *filepath, bool bname)
|
2024-03-09 20:18:19 +01:00
|
|
|
{
|
2024-03-31 01:20:29 +01:00
|
|
|
for (long i = 0; i < list->length; i++) {
|
2024-04-01 22:53:19 +02:00
|
|
|
if (!bname && strcmp(list->items[i].path, filepath) == 0) {
|
2024-03-31 01:20:29 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
if (bname) {
|
2024-04-01 22:53:19 +02:00
|
|
|
if (strcmp(list->items[i].name, filepath) == 0) {
|
2024-03-31 01:20:29 +01:00
|
|
|
return i;
|
|
|
|
}
|
2024-03-20 21:29:04 +01:00
|
|
|
}
|
2024-03-09 20:18:19 +01:00
|
|
|
}
|
2024-03-31 01:20:29 +01:00
|
|
|
return -1;
|
2024-03-09 20:18:19 +01:00
|
|
|
}
|
|
|
|
|
2024-03-20 21:29:04 +01:00
|
|
|
void arraylist_remove(ArrayList *list, long index)
|
2024-03-15 19:28:37 +01:00
|
|
|
{
|
2024-03-29 23:34:26 +01:00
|
|
|
if (index >= list->length)
|
2024-03-15 19:28:37 +01:00
|
|
|
return;
|
|
|
|
|
2024-10-11 21:03:31 +02:00
|
|
|
/* marked stuff doesn't work with this
|
2024-04-01 22:53:19 +02:00
|
|
|
free(list->items[index].name);
|
2024-03-20 21:29:04 +01:00
|
|
|
free(list->items[index].path);
|
|
|
|
free(list->items[index].type);
|
2024-04-01 22:53:19 +02:00
|
|
|
free(list->items[index].stats);
|
2024-03-29 23:34:26 +01:00
|
|
|
free(list->items[index].icon);
|
2024-10-11 21:03:31 +02:00
|
|
|
*/
|
2024-03-29 23:34:26 +01:00
|
|
|
for (long i = index; i < list->length - 1; i++)
|
2024-03-20 21:29:04 +01:00
|
|
|
list->items[i] = list->items[i + 1];
|
|
|
|
|
|
|
|
list->length--;
|
2024-03-15 19:28:37 +01:00
|
|
|
}
|
|
|
|
|
2024-03-18 00:21:58 +01:00
|
|
|
/*
|
2024-03-29 23:34:26 +01:00
|
|
|
* Force will not remove duplicate marked files, instead it just skip adding
|
2024-03-18 00:21:58 +01:00
|
|
|
*/
|
2024-04-01 22:53:19 +02:00
|
|
|
void arraylist_add(ArrayList *list, char *name, char *path, char *stats, char *type, wchar_t *icon, int color, bool marked, bool force)
|
2024-03-14 16:29:03 +01:00
|
|
|
{
|
2024-04-01 22:53:19 +02:00
|
|
|
/* name, path, stats, type, icon, color */
|
2024-04-06 22:52:31 +02:00
|
|
|
file new_file = { name, path, type, stats, icon, color };
|
2024-03-20 21:29:04 +01:00
|
|
|
|
|
|
|
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) {
|
2024-03-29 23:34:26 +01:00
|
|
|
if (!force)
|
2024-03-20 21:29:04 +01:00
|
|
|
arraylist_remove(list, i);
|
2024-03-20 21:32:43 +01:00
|
|
|
return;
|
2024-03-20 21:29:04 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-14 15:58:03 +01:00
|
|
|
}
|
2024-03-20 21:29:04 +01:00
|
|
|
list->items[list->length] = new_file;
|
2024-03-14 15:58:03 +01:00
|
|
|
} else {
|
2024-03-20 21:29:04 +01:00
|
|
|
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;
|
2024-03-29 23:34:26 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < list->length; i++)
|
2024-03-20 21:29:04 +01:00
|
|
|
new_items[i] = old_items[i];
|
2024-03-29 23:34:26 +01:00
|
|
|
|
2024-03-20 21:29:04 +01:00
|
|
|
free(old_items);
|
|
|
|
list->items[list->length] = new_file;
|
2024-03-14 15:58:03 +01:00
|
|
|
}
|
2024-03-20 21:29:04 +01:00
|
|
|
list->length++;
|
2024-03-14 15:58:03 +01:00
|
|
|
}
|
|
|
|
|
2024-03-13 11:18:35 +01:00
|
|
|
/*
|
|
|
|
* Construct a formatted line for display
|
|
|
|
*/
|
2024-04-02 02:18:34 +02:00
|
|
|
char *get_line(ArrayList *list, long index, bool detail, bool icons)
|
2024-03-11 22:54:14 +01:00
|
|
|
{
|
2024-03-20 21:29:04 +01:00
|
|
|
file file = list->items[index];
|
2024-03-31 00:47:51 +01:00
|
|
|
|
2024-04-07 00:46:14 +02:00
|
|
|
size_t name_len = strlen(file.name);
|
2024-03-29 15:05:01 +01:00
|
|
|
size_t length;
|
|
|
|
if (detail) {
|
2024-04-07 00:46:14 +02:00
|
|
|
length = name_len + strlen(file.stats) + 7; /* 4 for icon, 2 for space and 1 for null */
|
2024-03-29 15:05:01 +01:00
|
|
|
} else {
|
2024-04-01 22:53:19 +02:00
|
|
|
length = name_len + 6; /* 4 for icon, 1 for space and 1 for null */
|
2024-03-29 15:05:01 +01:00
|
|
|
}
|
2024-03-29 23:34:26 +01:00
|
|
|
|
2024-03-20 21:29:04 +01:00
|
|
|
char *line = memalloc(length * sizeof(char));
|
2024-04-02 02:18:34 +02:00
|
|
|
line[0] = '\0';
|
2024-03-29 15:05:01 +01:00
|
|
|
if (detail) {
|
2024-04-07 00:46:14 +02:00
|
|
|
strcat(line, file.stats);
|
2024-04-02 02:18:34 +02:00
|
|
|
strcat(line, " ");
|
2024-03-29 15:05:01 +01:00
|
|
|
}
|
2024-04-02 02:18:34 +02:00
|
|
|
if (icons) {
|
|
|
|
char *tmp = memalloc(8 * sizeof(char));
|
2024-04-07 00:46:14 +02:00
|
|
|
snprintf(tmp, 8, "%ls", file.icon);
|
2024-04-02 02:18:34 +02:00
|
|
|
strcat(line, tmp);
|
|
|
|
strcat(line, " ");
|
|
|
|
free(tmp);
|
|
|
|
}
|
2024-04-07 00:46:14 +02:00
|
|
|
strcat(line, file.name);
|
2024-04-02 02:18:34 +02:00
|
|
|
|
2024-03-20 21:29:04 +01:00
|
|
|
return line;
|
2024-03-11 22:54:14 +01:00
|
|
|
}
|