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
172
173
174
175
176
177
178
179
180
|
/* cklist.h - Custom list data structure for ck ------------------------*- C -*-
*
* This file is part of ck, the config keeper
*
* -----------------------------------------------------------------------------
*
* Copyright (C) 2018 Anastasis Grammenos
* GPLv3 (see LICENCE for the full notice)
*
* -------------------------------------------------------------------------- */
#include "cklist.h"
#include "ckutil.h"
cklist* list_make_new() {
cklist *ckl = malloc(sizeof(cklist));
ckl->size = 0;
ckl->pos = 0;
ckl->arr = NULL;
return ckl;
}
void list_add(cklist *ckl, const char *str) {
ckl->size++;
ckl->arr = (char **)
realloc(ckl->arr, ckl->size*sizeof(char *));
if (!ckl->arr) {
return;
}
ckl->arr[ckl->size-1] = strdup(str);
}
cklist* list_make_and_add(const char *str) {
cklist *ckl = list_make_new();
list_add(ckl,str);
return ckl;
}
int list_next(cklist *ckl) {
if (ckl->size < 1) {
return 0;
}
if (ckl->pos >= ckl->size - 1) {
return 0;
}
ckl->pos++;
return 1;
}
char* list_get(cklist *ckl) {
if (ckl->pos == -1) {
return NULL;
}
if (ckl->pos >= ckl->size) {
return NULL;
}
return ckl->arr[ckl->pos];
}
char* list_get_at(cklist *ckl, int pos) {
if (ckl->pos == -1) {
return NULL;
}
if (ckl->pos >= ckl->size
|| pos >= ckl->size) {
return NULL;
}
return ckl->arr[pos];
}
void list_rewind(cklist *ckl) {
ckl->pos = 0;
}
cklist* list_duplicate(cklist *ckl) {
list_rewind(ckl);
cklist *_ckl = list_make_and_add(list_get(ckl));
while (list_next(ckl)) {
list_add(_ckl, list_get(ckl));
}
list_rewind(ckl);
return _ckl;
}
cklist* list_move(cklist *ckl) {
list_rewind(ckl);
cklist *_ckl = list_make_and_add(list_get(ckl));
while (list_next(ckl)) {
list_add(_ckl, list_get(ckl));
}
list_free(ckl);
return _ckl;
}
cklist* list_copy_from(cklist *ckl, int index) {
list_rewind(ckl);
cklist *_ckl = list_make_new();
if (ckl->pos >= index) {
list_add(_ckl, list_get(ckl));
}
while(list_next(ckl)) {
if (ckl->pos >= index) {
list_add(_ckl, list_get(ckl));
}
}
list_rewind(ckl);
return _ckl;
}
cklist* list_copy_until(cklist *ckl, int index) {
list_rewind(ckl);
cklist *_ckl = list_make_and_add(list_get(ckl));
while(list_next(ckl)) {
if (ckl->pos < index) {
list_add(_ckl, list_get(ckl));
}
}
list_rewind(ckl);
return _ckl;
}
cklist* list_copy_part(cklist *ckl, int from, int until) {
list_rewind(ckl);
cklist *_ckl = list_make_new();
if (ckl->pos >= from && ckl->pos < until) {
list_add(_ckl, list_get(ckl));
}
while(list_next(ckl)) {
if (ckl->pos >= from && ckl->pos < until) {
list_add(_ckl, list_get(ckl));
}
}
list_rewind(ckl);
return _ckl;
}
void list_print_lisp(cklist *ckl) {
if (ckl->size > 0) {
list_rewind(ckl);
printf("( \"%s\"", list_get(ckl));
if (ckl->size == 1) {
printf(" )\n");
}
while (list_next(ckl)) {
if (ckl->pos == ckl->size-1) {
printf(", \"%s\" )\n", list_get(ckl));
}
else {
printf(", \"%s\"", list_get(ckl));
}
}
list_rewind(ckl);
}
}
void list_print(cklist *ckl) {
if (ckl->size > 0) {
list_rewind(ckl);
while (list_next(ckl)) {
printf("%s\n", list_get(ckl));
}
list_rewind(ckl);
}
}
int list_size(cklist *ckl) {
return ckl->size;
}
void list_free(cklist *ckl) {
int i;
for (i=0; i<ckl->size; i++) {
free(ckl->arr[i]);
}
free(ckl->arr);
free(ckl);
}
|