summaryrefslogtreecommitdiff
path: root/entry.c
blob: af3a5c8870fbb76cd1373d449130bd273487cb2e (plain)
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
130
131
#include <assert.h>
#include <dirent.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "entry.h"
#include "group.h"
#include "read_cfg.h"
#define BUF_LEN 1024

typedef struct entry{
	char name[BUF_LEN];
	char path[BUF_LEN];
	bool path_force;
	struct entry *next;
} ENTRY;

ENTRY *create_entry(char *new_name, char *new_path, bool force);
void entry_rm(ENTRY *e, ENTRY *prev);
void clear_entries(ENTRY *head);
int entry_add(ENTRY *head, ENTRY *tail, ENTRY *add);
ENTRY **get_entries(ENTRY *head, int count);
char *get_ename(ENTRY *e);
char *get_epath(ENTRY *e);
bool get_eforce(ENTRY *e);
void entry_debug(ENTRY *trav);

ENTRY *create_entry(char *new_name, char *new_path, bool force){
	ENTRY *new;

	new = malloc(sizeof(ENTRY));

	strcpy(new->name, new_name);
	strcpy(new->path, new_path);
	new->path_force = force;
	new->next = NULL;

	return new;
}

void entry_rm(ENTRY *e, ENTRY *prev){
	assert(e != NULL);
	if(prev != NULL) prev->next = e->next; //maintain linked structure
	free(e);
}

void clear_entries(ENTRY *head){
	ENTRY *temp;
	
	while(head != NULL){
		temp = head;
		head = head->next;
		free(temp);
	}

	return;
}

//returns 0 if in the middle, 1 if new head, 2 if new tail, or 3 if both new head and tail
//TODO this is kind of a stupid way of handling things
int entry_add(ENTRY *head, ENTRY *tail, ENTRY *add){
	assert(add != NULL);
	ENTRY *ahead;

	//Empty group (no need to sort)
	if(head == NULL) return 3;

	//add is the new tail
	if(!get_sort() || strcmp(tail->name, add->name) <= 0){
		tail->next = add;
		return 2;
	}

	//add is the new head
	if(strcmp(add->name, head->name) <= 0){
		add->next = head;
		return 1;
	}

	ahead = head->next;

	while(ahead != NULL){
		if(strcmp(head->name, add->name) <= 0 && strcmp(add->name, ahead->name) <= 0) break;
		head = head->next;
		ahead = ahead->next;
	}

	head->next = add;
	add->next = ahead;

	return 0;
}

ENTRY **get_entries(ENTRY *head, int count){
	ENTRY **arr = malloc(sizeof(ENTRY *) * count);
	ENTRY *trav = head;
	int i;

	for(i = 0; i < count; i++){
		arr[i] = trav;
		trav = trav->next;
	}

	return arr;
}

char *get_ename(ENTRY *e){
	assert(e != NULL);
	return e->name;
}
char *get_epath(ENTRY *e){
	assert(e != NULL);
	return e->path;
}

bool get_eforce(ENTRY *e){
	assert(e != NULL);
	return e->path_force;
}

void entry_debug(ENTRY *trav){

	while(trav != NULL){
		printf("%s, \n", trav->name);
		trav = trav->next;
	}

	return;
}