2024-03-09 20:18:19 +01:00
|
|
|
#include <string.h>
|
2024-03-11 22:54:14 +01:00
|
|
|
#include <stdlib.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-09 20:46:15 +01:00
|
|
|
char *name;
|
2024-03-11 22:54:14 +01:00
|
|
|
char *stats;
|
2024-03-10 11:54:08 +01:00
|
|
|
/* put some more useful stat here */
|
2024-03-09 20:18:19 +01:00
|
|
|
struct file *next;
|
|
|
|
} file;
|
|
|
|
|
|
|
|
file *files = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get length of files linked list
|
|
|
|
*/
|
|
|
|
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-11 22:54:14 +01:00
|
|
|
long add_file(char *filename, char *stats)
|
2024-03-09 20:18:19 +01:00
|
|
|
{
|
|
|
|
file *current = files;
|
|
|
|
file *new_file = memalloc(sizeof(file));
|
|
|
|
char *buf = strdup(filename);
|
2024-03-11 22:54:14 +01:00
|
|
|
char *buf2 = strdup(stats);
|
|
|
|
if (buf == NULL || buf2 == NULL) {
|
2024-03-09 20:18:19 +01:00
|
|
|
perror("ccc");
|
|
|
|
}
|
2024-03-09 20:46:15 +01:00
|
|
|
new_file->name = buf;
|
2024-03-11 22:54:14 +01:00
|
|
|
new_file->stats = buf2;
|
2024-03-09 20:18:19 +01:00
|
|
|
new_file->next = NULL;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *get_filename(long index)
|
|
|
|
{
|
|
|
|
file *file = get_file(index);
|
2024-03-10 11:54:08 +01:00
|
|
|
if (file != NULL) {
|
2024-03-09 20:46:15 +01:00
|
|
|
char *name = strdup(file->name);
|
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
|
|
|
|
|
|
|
char *get_line(long index)
|
|
|
|
{
|
|
|
|
file *file = get_file(index);
|
|
|
|
if (file != NULL) {
|
|
|
|
char *name = strdup(file->name);
|
|
|
|
char *stats = strdup(file->stats);
|
|
|
|
if (name == NULL || stats == NULL) {
|
|
|
|
perror("ccc");
|
|
|
|
}
|
|
|
|
size_t length = strlen(name) + strlen(stats) + 2; /* one for space and one for nul */
|
|
|
|
char *line = memalloc(length * sizeof(char));
|
|
|
|
snprintf(line, length, "%s %s", stats, name);
|
|
|
|
return line;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|