File: flist.c

package info (click to toggle)
fvwm 1:2.6.8-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 15,804 kB
  • sloc: ansic: 145,770; xml: 17,093; perl: 7,302; sh: 4,921; makefile: 1,094; yacc: 688; lex: 187; sed: 11
file content (179 lines) | stat: -rw-r--r-- 3,413 bytes parent folder | download
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
/* -*-c-*- */
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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, see: <http://www.gnu.org/licenses/>
 */

/* code for parsing the fvwm style command */

/* ---------------------------- included header files ---------------------- */

#include "config.h"
#include <stdio.h>

#include "safemalloc.h"
#include "flist.h"

/* ---------------------------- local definitions -------------------------- */

/* ---------------------------- local macros ------------------------------- */

/* ---------------------------- imports ------------------------------------ */

/* ---------------------------- included code files ------------------------ */

/* ---------------------------- local types -------------------------------- */

/* ---------------------------- forward declarations ----------------------- */

/* ---------------------------- local variables ---------------------------- */

/* ---------------------------- exported variables (globals) --------------- */

/* ---------------------------- local functions ---------------------------- */

/* ---------------------------- interface functions ------------------------ */

flist *flist_append_obj(flist *list, void *object)
{
	flist *new = (flist *)safemalloc(sizeof(flist));
	flist *tl = list;

	new->object = object;
	new->next = NULL;
	new->prev = NULL;

	if (list == NULL)
	{
		return new;
	}
	while(tl->next)
	{
		tl = tl->next;
	}
	tl->next = new;
	new->prev = tl;

	return list;
}

flist *flist_prepend_obj(flist *list, void *object)
{
	flist *new = (flist *)safemalloc(sizeof(flist));

	new->object = object;
	new->next = NULL;
	new->prev = NULL;

	if (list == NULL)
	{
		return new;
	}

	if (list->prev)
	{
		list->prev->next = new;
		new->prev = list->prev;
	}
	list->prev = new;
	new->next = list;

	return new;
}

flist *flist_insert_obj(flist *list, void *object, int position)
{
	flist *new;
	flist *tl;

	if (position < 0)
	{
		return flist_append_obj(list, object);
	}
	if (position == 0)
	{
		return flist_prepend_obj(list, object);
	}
	tl = list;
	while(tl && position-- > 0)
	{
		tl = tl->next;
	}

	if (!tl)
	{
		return flist_append_obj(list, object);
	}

	new = (flist *)safemalloc(sizeof(flist));;
	new->object = object;
	new->prev = NULL;

	if (tl->prev)
	{
		tl->prev->next = new;
		new->prev = tl->prev;
	}
	new->next = tl;
	tl->prev = new;

	if (tl == list)
	{
		return new;
	}

	return list;
}

flist *flist_remove_obj(flist *list, void *object)
{
	flist *tl = list;

	while (tl && tl->object != object)
	{
		tl = tl->next;
	}

	if (tl == NULL)
	{
		return NULL;
	}

	if (tl->prev)
	{
		tl->prev->next = tl->next;
	}
	if (tl->next)
	{
		tl->next->prev = tl->prev;
	}
	if (list == tl)
	{
		list = list->next;
	}
	free(tl);

	return list;
}

flist *flist_free_list(flist *list)
{
	flist *tl;

	while (list)
	{
		tl = list;
		list = list->next;
		free(tl);
	}
	return NULL;
}