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-09 20:18:19 +01:00
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
2024-03-10 11:54:08 +01:00
|
|
|
/* files in a link list data structure */
|
2024-03-09 20:18:19 +01:00
|
|
|
typedef struct file {
|
2024-03-12 21:59:36 +01:00
|
|
|
char *path;
|
2024-03-11 22:54:14 +01:00
|
|
|
char *stats;
|
2024-03-13 11:18:35 +01:00
|
|
|
char *type;
|
2024-03-14 15:58:03 +01:00
|
|
|
int color;
|
2024-03-09 20:18:19 +01:00
|
|
|
struct file *next;
|
|
|
|
} file;
|
|
|
|
|
|
|
|
file *files = NULL;
|
2024-03-13 11:18:35 +01:00
|
|
|
file *marked = NULL;
|
2024-03-09 20:18:19 +01:00
|
|
|
|
|
|
|
/*
|
2024-03-13 11:18:35 +01:00
|
|
|
* Get length of files linked list
|
2024-03-09 20:18:19 +01:00
|
|
|
*/
|
|
|
|
long files_len()
|
|
|
|
{
|
|
|
|
file *current = files;
|
|
|
|
int count = 0;
|
2024-03-10 11:54:08 +01:00
|
|
|
while (current != NULL) {
|
2024-03-09 20:18:19 +01:00
|
|
|
count++;
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2024-03-13 11:18:35 +01:00
|
|
|
/*
|
|
|
|
* Get length of marked files
|
|
|
|
*/
|
|
|
|
long marked_len()
|
|
|
|
{
|
|
|
|
file *current = marked;
|
|
|
|
int count = 0;
|
|
|
|
while (current != NULL) {
|
|
|
|
count++;
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-03-15 19:28:37 +01:00
|
|
|
void free_file(file *toremove)
|
|
|
|
{
|
|
|
|
if (toremove->type != NULL)
|
|
|
|
free(toremove->type);
|
|
|
|
if (toremove->path != NULL)
|
|
|
|
free(toremove->path);
|
|
|
|
if (toremove->stats != NULL)
|
|
|
|
free(toremove->stats);
|
|
|
|
free(toremove);
|
|
|
|
}
|
|
|
|
|
2024-03-12 21:59:36 +01:00
|
|
|
void clear_files()
|
|
|
|
{
|
|
|
|
file *tmp;
|
|
|
|
while (files != NULL) {
|
|
|
|
tmp = files;
|
|
|
|
files = files->next;
|
2024-03-15 19:28:37 +01:00
|
|
|
free_file(tmp);
|
2024-03-12 21:59:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 11:18:35 +01:00
|
|
|
void clear_marked()
|
|
|
|
{
|
|
|
|
file *tmp;
|
|
|
|
while (marked != NULL) {
|
|
|
|
tmp = marked;
|
|
|
|
files = marked->next;
|
2024-03-15 19:28:37 +01:00
|
|
|
free_file(tmp);
|
2024-03-13 11:18:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-14 15:58:03 +01:00
|
|
|
long add_file(char *filepath, char *stats, char *type, int color)
|
2024-03-09 20:18:19 +01:00
|
|
|
{
|
|
|
|
file *current = files;
|
|
|
|
file *new_file = memalloc(sizeof(file));
|
2024-03-12 21:59:36 +01:00
|
|
|
char *buf = strdup(filepath);
|
2024-03-11 22:54:14 +01:00
|
|
|
char *buf2 = strdup(stats);
|
2024-03-13 11:18:35 +01:00
|
|
|
char *buf3 = strdup(type);
|
2024-03-14 15:58:03 +01:00
|
|
|
int buf4 = color;
|
|
|
|
|
|
|
|
if (buf == NULL || buf2 == NULL || buf3 == NULL)
|
2024-03-09 20:18:19 +01:00
|
|
|
perror("ccc");
|
2024-03-14 15:58:03 +01:00
|
|
|
|
2024-03-12 21:59:36 +01:00
|
|
|
new_file->path = buf;
|
2024-03-11 22:54:14 +01:00
|
|
|
new_file->stats = buf2;
|
2024-03-13 11:18:35 +01:00
|
|
|
new_file->type = buf3;
|
2024-03-14 15:58:03 +01:00
|
|
|
new_file->color = buf4;
|
2024-03-09 20:18:19 +01:00
|
|
|
new_file->next = NULL;
|
2024-03-14 15:58:03 +01:00
|
|
|
|
2024-03-10 11:54:08 +01:00
|
|
|
if (current == NULL) {
|
2024-03-09 20:18:19 +01:00
|
|
|
files = new_file;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
long index = 1;
|
2024-03-10 11:54:08 +01:00
|
|
|
while (current->next != NULL) {
|
2024-03-09 20:18:19 +01:00
|
|
|
current = current->next;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
current->next = new_file;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2024-03-15 19:28:37 +01:00
|
|
|
void remove_marked(file *marked_file)
|
|
|
|
{
|
|
|
|
/* If the head node itself is marked for removal */
|
|
|
|
if (marked == marked_file) {
|
|
|
|
marked = marked->next;
|
|
|
|
free_file(marked_file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Search for the marked file node in the list */
|
|
|
|
file* temp = marked;
|
|
|
|
while (temp != NULL && temp->next != marked_file) {
|
|
|
|
temp = temp->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the marked file node is found, remove it from the list */
|
|
|
|
if (temp != NULL) {
|
|
|
|
temp->next = marked_file->next;
|
|
|
|
free_file(marked_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-18 00:21:58 +01:00
|
|
|
/*
|
|
|
|
* force will not remove duplicate marked files, instead it just skip adding
|
|
|
|
*/
|
|
|
|
long add_marked(char *filepath, char *type, bool force)
|
2024-03-14 16:29:03 +01:00
|
|
|
{
|
|
|
|
file *current = marked;
|
|
|
|
file *new_file = memalloc(sizeof(file));
|
|
|
|
char *buf = strdup(filepath);
|
|
|
|
char *buf2 = strdup(type);
|
|
|
|
if (buf == NULL || buf2 == NULL) {
|
|
|
|
perror("ccc");
|
|
|
|
}
|
|
|
|
new_file->path = buf;
|
|
|
|
new_file->type = buf2;
|
2024-03-15 19:28:37 +01:00
|
|
|
new_file->stats = NULL;
|
|
|
|
new_file->color = 0;
|
2024-03-14 16:29:03 +01:00
|
|
|
new_file->next = NULL;
|
|
|
|
if (current == NULL) {
|
2024-03-14 20:57:03 +01:00
|
|
|
marked = new_file;
|
2024-03-14 16:29:03 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2024-03-15 19:28:37 +01:00
|
|
|
|
2024-03-14 16:29:03 +01:00
|
|
|
long index = 1;
|
|
|
|
while (current->next != NULL) {
|
2024-03-15 19:28:37 +01:00
|
|
|
if (strcmp(current->path, new_file->path) == 0) {
|
2024-03-18 00:21:58 +01:00
|
|
|
if (force) {
|
|
|
|
return index;
|
|
|
|
} else {
|
|
|
|
remove_marked(current);
|
|
|
|
free_file(new_file);
|
|
|
|
return -1;
|
|
|
|
}
|
2024-03-15 19:28:37 +01:00
|
|
|
}
|
2024-03-14 16:29:03 +01:00
|
|
|
current = current->next;
|
|
|
|
index++;
|
|
|
|
}
|
2024-03-15 19:28:37 +01:00
|
|
|
if (strcmp(current->path, new_file->path) == 0){
|
2024-03-18 00:21:58 +01:00
|
|
|
if (force) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
remove_marked(current);
|
|
|
|
free_file(new_file);
|
|
|
|
return -1;
|
|
|
|
}
|
2024-03-15 19:28:37 +01:00
|
|
|
}
|
2024-03-14 16:29:03 +01:00
|
|
|
current->next = new_file;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2024-03-15 19:28:37 +01:00
|
|
|
file *get_marked(long index)
|
|
|
|
{
|
|
|
|
file *current = marked;
|
|
|
|
if (index == 0) {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
if (index > files_len()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (long i = 0; i < index; i++) {
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool in_marked(char *path)
|
|
|
|
{
|
|
|
|
file *tmp = marked;
|
|
|
|
if (tmp == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (tmp != NULL) {
|
|
|
|
if (strcmp(path, tmp->path) == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-09 20:18:19 +01:00
|
|
|
file *get_file(long index)
|
|
|
|
{
|
|
|
|
file *current = files;
|
2024-03-10 11:54:08 +01:00
|
|
|
if (index == 0) {
|
2024-03-09 20:18:19 +01:00
|
|
|
return current;
|
|
|
|
}
|
2024-03-10 11:54:08 +01:00
|
|
|
if (index > files_len()) {
|
2024-03-09 20:18:19 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2024-03-10 11:54:08 +01:00
|
|
|
for (long i = 0; i < index; i++) {
|
2024-03-09 20:18:19 +01:00
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2024-03-12 21:59:36 +01:00
|
|
|
char *get_filepath(long index)
|
2024-03-09 20:18:19 +01:00
|
|
|
{
|
|
|
|
file *file = get_file(index);
|
2024-03-10 11:54:08 +01:00
|
|
|
if (file != NULL) {
|
2024-03-12 21:59:36 +01:00
|
|
|
char *name = strdup(file->path);
|
2024-03-10 11:54:08 +01:00
|
|
|
if (!name) {
|
2024-03-09 20:46:15 +01:00
|
|
|
perror("ccc");
|
|
|
|
}
|
|
|
|
return name;
|
2024-03-10 11:54:08 +01:00
|
|
|
} else {
|
2024-03-09 20:18:19 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2024-03-11 22:54:14 +01:00
|
|
|
|
2024-03-14 15:58:03 +01:00
|
|
|
int get_color(long index)
|
|
|
|
{
|
|
|
|
file *file = get_file(index);
|
|
|
|
if (file != NULL) {
|
|
|
|
int color = file->color;
|
|
|
|
if (!color) {
|
|
|
|
perror("ccc");
|
|
|
|
}
|
|
|
|
return color;
|
|
|
|
} else {
|
2024-03-15 19:28:37 +01:00
|
|
|
return 8; /* white */
|
2024-03-14 15:58:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 11:18:35 +01:00
|
|
|
/*
|
|
|
|
* Construct a formatted line for display
|
|
|
|
*/
|
2024-03-11 22:54:14 +01:00
|
|
|
char *get_line(long index)
|
|
|
|
{
|
|
|
|
file *file = get_file(index);
|
|
|
|
if (file != NULL) {
|
2024-03-12 21:59:36 +01:00
|
|
|
char *name = strdup(file->path);
|
2024-03-11 22:54:14 +01:00
|
|
|
char *stats = strdup(file->stats);
|
2024-03-14 15:58:03 +01:00
|
|
|
size_t length = strlen(name) + strlen(stats) + 2; /* one for space and one for null */
|
2024-03-11 22:54:14 +01:00
|
|
|
char *line = memalloc(length * sizeof(char));
|
2024-03-14 15:58:03 +01:00
|
|
|
|
|
|
|
name = basename(name);
|
|
|
|
if (name == NULL || stats == NULL)
|
|
|
|
perror("ccc");
|
|
|
|
|
2024-03-11 22:54:14 +01:00
|
|
|
snprintf(line, length, "%s %s", stats, name);
|
2024-03-14 15:58:03 +01:00
|
|
|
|
2024-03-11 22:54:14 +01:00
|
|
|
return line;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|