-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynstuff.c
208 lines (169 loc) · 3.5 KB
/
dynstuff.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* $Id$ */
/*
* (C) Copyright 2001-2003 Wojtek Kaniewski <wojtekka@irc.pl>
* Dawid Jarosz <dawjar@poczta.onet.pl>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 2 as
* published by the Free Software Foundation.
*
* This program 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; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#ifndef _AIX
# include <string.h>
#endif
#include "dynstuff.h"
#include "xmalloc.h"
void *list_add(struct list **list, void *data) {
list_t new;
if (!list) {
errno = EFAULT;
return NULL;
}
new = xmalloc(sizeof(struct list));
new->next = *list;
*list = new;
new->data = data;
return new->data;
}
/*
* list_remove()
*
* usuwa z listy wpis z podanym elementem.
*
* - list - wska¼nik do listy,
* - data - element,
* - free_data - zwolniæ pamiêæ po elemencie.
*/
int list_remove(struct list **list, void *data, int free_data)
{
struct list *tmp, *last = NULL;
if (!list) {
errno = EFAULT;
return -1;
}
tmp = *list;
if (tmp->data == data) {
*list = tmp->next;
} else {
for (; tmp && tmp->data != data; tmp = tmp->next)
last = tmp;
if (!tmp) {
errno = ENOENT;
return -1;
}
last->next = tmp->next;
}
if (free_data)
free(tmp->data);
free(tmp);
return 0;
}
/*
* list_count()
*
* zwraca ilo¶æ elementów w danej li¶cie.
*
* - list - lista.
*/
int list_count(struct list *list)
{
int count = 0;
for (; list; list = list->next)
count++;
return count;
}
/*
* list_destroy()
*
* niszczy wszystkie elementy listy.
*
* - list - lista,
* - free_data - czy zwalniaæ bufor danych?
*/
int list_destroy(struct list *list, int free_data)
{
struct list *tmp;
while (list) {
if (free_data)
free(list->data);
tmp = list->next;
free(list);
list = tmp;
}
return 0;
}
/*
* string_realloc()
*
* upewnia siê, ¿e w stringu bêdzie wystarczaj±co du¿o miejsca.
*
* - s - ci±g znaków,
* - count - wymagana ilo¶æ znaków (bez koñcowego '\0').
*/
static void string_realloc(string_t s, int count)
{
char *tmp;
if (s->str && (count + 1) <= s->size)
return;
tmp = xrealloc(s->str, count + 81);
if (!s->str)
*tmp = 0;
tmp[count + 80] = 0;
s->size = count + 81;
s->str = tmp;
}
string_t string_init(const char *value) {
string_t tmp = xmalloc(sizeof(struct string));
size_t valuelen;
if (!value)
value = "";
valuelen = strlen(value);
tmp->str = xstrdup(value);
tmp->len = valuelen;
tmp->size = valuelen + 1;
return tmp;
}
int string_append_raw(string_t s, const char *str, int count)
{
if (!s || !str || count < 0) {
errno = EFAULT;
return -1;
}
string_realloc(s, s->len + count);
s->str[s->len + count] = 0;
memcpy(s->str + s->len, str, count);
s->len += count;
return 0;
}
void string_remove(string_t s, int count)
{
if (!s || count <= 0)
return;
if (count >= s->len) {
/* string_clear() */
s->str[0] = '\0';
s->len = 0;
} else {
memmove(s->str, s->str + count, s->len - count);
s->len -= count;
s->str[s->len] = '\0';
}
}
void string_free(string_t s)
{
if (s) {
free(s->str);
free(s);
}
}