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
|
#include "l4array.h"
#include <stdlib.h>
#include <string.h>
L4DA* l4da_create_setmax(int itemsize, int len, int maxlen){
if(itemsize <= 0 || len < 0 || maxlen < len){
return NULL;
}
L4DA* arr = (L4DA*)malloc(sizeof(L4DA));
if(arr == NULL){
return NULL;
}
arr->arr_itemsize = itemsize;
arr->arr_curlen = len;
arr->arr_maxlen = maxlen;
if(maxlen != 0){
arr->arr_data = malloc(itemsize * maxlen);
if(arr->arr_data == NULL){
free(arr);
return NULL;
}
}else{
arr->arr_data = NULL;
}
return arr;
}
L4DA* l4da_create(int itemsize, int len){
return l4da_create_setmax(itemsize, len, len);
}
void l4da_free(L4DA* arr){
if(arr->arr_data != NULL){
free(arr->arr_data);
}
free(arr);
}
int l4da_pushback(L4DA* arr, const void* data){
if((arr->arr_maxlen) < (arr->arr_curlen + 1)){
if(arr->arr_maxlen != 0){
if(l4da_setmax(arr, arr->arr_maxlen*2) < 0){
return -1;
}
}else{
if(l4da_setmax(arr, 1) < 0){
return -1;
}
}
}
memcpy(l4da_vp(arr, arr->arr_curlen), data, arr->arr_itemsize);
arr->arr_curlen++;
return 0;
}
int l4da_setlen(L4DA* arr, int len){
if(len > (arr->arr_maxlen)){
if(l4da_setmax(arr, len) < 0){
return -1;
}else{
arr->arr_curlen = len;
}
}else{
arr->arr_curlen = len;
return 0;
}
return 0;
}
int l4da_setmax(L4DA* arr, int max){
void* newptr;
if(arr->arr_data == NULL){
newptr = malloc((arr->arr_itemsize)*max);
if(newptr == NULL){
return -1;
}else{
arr->arr_maxlen = max;
arr->arr_data = newptr;
return 0;
}
}
newptr = realloc(arr->arr_data, (arr->arr_itemsize)*max);
if(newptr == NULL){
return -1;
}else{
arr->arr_maxlen = max;
arr->arr_data = newptr;
}
return 0;
}
int l4da_strip(L4DA* arr){
if(arr->arr_data == NULL){
return 0;
}
L4DA* newptr;
/* 其實縮小空間營該一定會成功才對......
* 不過還是保險一點,加個判斷式,別說 memory leak 是我害的
* 當然也是避免編譯器一直跳 warning
*/
if((arr->arr_maxlen) > (arr->arr_curlen)){
arr->arr_maxlen = arr->arr_curlen;
newptr = realloc(arr->arr_data, (arr->arr_curlen)*(arr->arr_itemsize));
if(newptr == NULL){
return -1;
}
arr->arr_data = newptr;
}
return 0;
}
/* 基本上直接玩 struct 的函式還是少用吧 */
void* l4da_drop_struct(L4DA* arr){
void* toreturn = arr->arr_data;
free(arr);
return toreturn;
}
L4DA* l4da_make_struct(void* data, int itemsize, int len, int maxlen){
if(itemsize <= 0 || len < 0 || maxlen < len){
return NULL;
}
L4DA* arr = (L4DA*)malloc(sizeof(L4DA));
if(arr == NULL){
return NULL;
}
arr->arr_itemsize = itemsize;
arr->arr_curlen = len;
arr->arr_maxlen = maxlen;
arr->arr_data = data;
return arr;
}
L4DA* l4da_dup(const L4DA* arr){
L4DA* newarr = l4da_create_setmax(
l4da_itemsize(arr), l4da_getlen(arr), l4da_getmax(arr));
if(newarr == NULL){
return NULL;
}
memcpy(newarr->arr_data, arr->arr_data,
l4da_getlen(arr) * l4da_itemsize(arr));
return newarr;
}
int l4da_combine(L4DA* arr, const L4DA* att){
if(l4da_itemsize(arr) != l4da_itemsize(att)){
return -2;
}
if(l4da_setlen(arr, l4da_getlen(arr) + l4da_getlen(att)) < 0){
return -1;
}
memcpy(l4da_vp(arr, l4da_getlen(arr)), att->arr_data,
l4da_getlen(att) * l4da_itemsize(att));
return 0;
}
L4DA* l4da_filereadline_delim(FILE* infile, int chr){
L4DA* newarr = l4da_create(1, 0);
if(newarr == NULL){
return NULL;
}
int c;
char towrite;
while((c = getc(infile)) != chr && !feof(infile)){
towrite = c;
if(l4da_pushback(newarr, (void*)&towrite) < 0){
l4da_free(newarr);
return NULL;
}
}
towrite = '\0';
if(l4da_pushback(newarr, (void*)&towrite) < 0){
l4da_free(newarr);
return NULL;
}
return newarr;
}
|