-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdir_list.c
112 lines (94 loc) · 2.34 KB
/
dir_list.c
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
/*
DeskLink2
Extensions and enhancements Copyright (C) 2005 John R. Hogerhuis
Copyright (c) 2022 Gabriele Gorla
DeskLink2 is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License version 2 or any
later as version as published by the Free Software Foundation.
DeskLink2 is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (in the file "COPYING"); if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111, USA.
*/
#include <stdint.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>
#include "dir_list.h"
static uint16_t allocated;
static uint16_t ndx;
static uint16_t cur;
static FILE_ENTRY* tblp = 0;
static FILE_ENTRY* current_record(void);
int file_list_init() {
tblp = malloc(sizeof(FILE_ENTRY)*FEQ);
if (!tblp) return -1;
allocated = FEQ;
ndx = 0;
cur = 0;
return 0;
}
int file_list_cleanup() {
allocated = 0;
ndx = 0;
cur = 0;
if (tblp) free(tblp);
tblp = NULL;
return 0;
}
void file_list_clear_all() {
cur = ndx = 0;
}
int add_file(FILE_ENTRY* fe) {
/* allocate FEQ more records if out of space */
if (ndx >= allocated) {
/* resize the array */
tblp = realloc(tblp, (allocated+FEQ)*sizeof(FILE_ENTRY));
if (!tblp) return -1;
allocated += FEQ;
}
/* reference the entry */
if (!tblp) return -1;
memcpy(tblp+ndx, fe, sizeof(FILE_ENTRY));
/* adjust cur to address this record, ndx to next avail */
cur = ndx;
ndx++;
return 0;
}
FILE_ENTRY* find_file(char* client_fname, uint8_t attr) {
int i;
for (i=0;i<ndx;i++) {
if (
!strcmp(client_fname,tblp[i].client_fname)
&&
tblp[i].attr==attr
) return &tblp[i];
}
return 0;
}
FILE_ENTRY* get_first_file(void) {
cur = 0;
return current_record();
}
FILE_ENTRY* get_next_file(void) {
if (cur + 1 > ndx) return NULL;
cur++;
return current_record();
}
FILE_ENTRY* get_prev_file(void) {
if (cur==0) return NULL;
cur--;
return current_record();
}
static FILE_ENTRY* current_record(void) {
FILE_ENTRY* ep;
if (cur >= ndx) return NULL;
if (!tblp) return NULL;
ep = tblp + cur;
return ep;
}