-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchlist.c
More file actions
171 lines (144 loc) · 4.48 KB
/
Copy pathsearchlist.c
File metadata and controls
171 lines (144 loc) · 4.48 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Pablo Portas López pablo.portas
// Pablo Míguez Muiño pablo.miguez.moino
#include "searchlist.h"
#include "auxiliar.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Variable global del buscatal
tSearchList buscatal = {-1,NULL};
tPosSearchL SList_first() {
return SList_aux_first(buscatal);
}
tPosSearchL SList_next(tPosSearchL pos) {
return SList_aux_next(pos, buscatal);
}
void SList_add(tPathL path) {
SList_aux_insertItem(path,LNULL, &buscatal);
}
void SList_delete(tPathL path) {
tPosSearchL aux;
if ((aux = SList_first()) == NULL) return;
while (strcmp(aux->path, path) != 0) {
aux = SList_aux_next(aux, buscatal);
if (aux == NULL) return;
}
SList_aux_deleteAtPosition(aux, &buscatal);
}
void SList_show_all() {
tPosSearchL posaux = SList_aux_first(buscatal);
for (int i = 1; posaux != LNULL; i++) {
printf("%s\n", posaux->path);
posaux = SList_aux_next(posaux, buscatal);
}
}
void SList_import_path() {
char *path = getenv("PATH");
int i = 0, j = -1;
tPathL aux;
if (path == NULL) {
Aux_general_Imprimir_Error("No se ha podido importar el PATH");
return;
}
do {
j++;
for (i = 0; path[j] != '\0' && path[j] != ':'; i++, j++) {
aux[i] = path[j];
}
aux[i] = '\0';
SList_add(aux);
} while (path[j] != '\0');
}
void SList_show_n(int n) {
tPosSearchL posaux = SList_aux_first(buscatal);
for (int i = n; i > 0; i--) {
if (i == 1) {
printf("%d. %s\n", n, posaux->path);
}
posaux = SList_aux_next(posaux, buscatal);
}
}
void SList_show_last_n(int n) {
tPosSearchL posaux = SList_aux_last(buscatal);
for (int i = buscatal.contador; i > buscatal.contador + n && posaux != LNULL; i--) {
printf("%d. %s\n", i + 1, posaux->path);
posaux = SList_aux_previous(posaux, buscatal);
}
}
int SList_total() {
return buscatal.contador + 1;
}
void SList_delete_all(void) {
if (buscatal.start == NULL) return;
while (!SList_aux_isEmptyList(buscatal)) {
SList_aux_deleteAtPosition(SList_aux_first(buscatal), &buscatal);
}
}
// AUXILIARES / INTERNAS
void SList_aux_createEmptyList(tSearchList *lista) {
lista->contador = -1;
lista->start = LNULL;
}
bool SList_aux_isEmptyList(tSearchList lista) {
if (lista.contador == -1) return true;
return false;
}
tPosSearchL SList_aux_first(tSearchList lista) {
return lista.start;
}
tPosSearchL SList_aux_last(tSearchList lista) {
tPosSearchL puntero;
for (puntero = lista.start; puntero->siguiente != LNULL; puntero = puntero->siguiente);
return puntero;
}
tPosSearchL SList_aux_next(tPosSearchL posicion, tSearchList lista) {
return posicion->siguiente;
}
tPosSearchL SList_aux_previous(tPosSearchL posicion, tSearchList lista) {
if (posicion == lista.start) return LNULL;
tPosSearchL aux;
for (aux = lista.start; aux->siguiente != posicion; aux = aux->siguiente);
return aux;
}
bool SList_aux_insertItem(tPathL path, tPosSearchL posicion, tSearchList *lista) {
if (!SList_aux_isEmptyList(*lista) && lista->contador == MAXSIZE) return false;
tPosSearchL aux1 = malloc(sizeof(struct tNodeSearch));
if (aux1 == NULL) return false;
strcpy(aux1->path, path);
aux1->siguiente = LNULL;
if (lista->start == LNULL)
lista->start = aux1;
else if (posicion == LNULL) {
tPosSearchL aux2 = SList_aux_last(*lista);
aux2->siguiente = aux1;
} else if (posicion == SList_aux_first(*lista)) {
aux1->siguiente = posicion;
lista->start = aux1;
} else {
tPosSearchL aux2 = SList_aux_previous(posicion, *lista);
aux2->siguiente = aux1;
aux1->siguiente = posicion;
}
lista->contador += 1;
return true;
}
void SList_aux_deleteAtPosition(tPosSearchL posicion, tSearchList *lista) {
tPosSearchL aux, anterior;
if (SList_aux_next(posicion, *lista) == LNULL) {
if (posicion == lista->start) {
lista->start = LNULL;
} else {
aux = SList_aux_previous(posicion, *lista);
aux->siguiente = LNULL;
}
} else {
if (posicion == lista->start) {
lista->start = SList_aux_next(posicion, *lista);
} else {
anterior = SList_aux_previous(posicion, *lista);
anterior->siguiente = SList_aux_next(posicion, *lista);
}
}
lista->contador -= 1;
free(posicion);
};