libgeda: Remove some exit() calls and assertions.
[geda-gaf/peter-b.git] / libgeda / src / s_undo.c
blob444933fffcb3eeeb93c7214abe013a3c052fb4b9
1 /* gEDA - GPL Electronic Design Automation
2 * libgeda - gEDA's library
3 * Copyright (C) 1998-2010 Ales Hvezda
4 * Copyright (C) 1998-2010 gEDA Contributors (see ChangeLog for details)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <config.h>
22 #include <stdio.h>
23 #include <ctype.h>
24 #ifdef HAVE_STDLIB_H
25 #include <stdlib.h>
26 #endif
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
31 #include "libgeda_priv.h"
33 #ifdef HAVE_LIBDMALLOC
34 #include <dmalloc.h>
35 #endif
37 /*! \todo Finish function documentation!!!
38 * \brief
39 * \par Function Description
42 UNDO *s_undo_return_tail(UNDO *head)
44 UNDO *u_current=NULL;
45 UNDO *ret_struct=NULL;
47 u_current = head;
48 while ( u_current != NULL ) { /* goto end of list */
49 ret_struct = u_current;
50 u_current = u_current->next;
53 return(ret_struct);
56 /*! \todo Finish function documentation!!!
57 * \brief
58 * \par Function Description
61 UNDO *s_undo_return_head(UNDO *tail)
63 UNDO *u_current=NULL;
64 UNDO *ret_struct=NULL;
66 u_current = tail;
67 while ( u_current != NULL ) { /* goto end of list */
68 ret_struct = u_current;
69 u_current = u_current->prev;
72 return(ret_struct);
75 /*! \todo Finish function documentation!!!
76 * \brief
77 * \par Function Description
80 UNDO *s_undo_new_head(void)
82 UNDO *u_new;
84 u_new = (UNDO *) g_malloc(sizeof(UNDO));
85 u_new->type = -1;
86 u_new->filename = NULL;
87 u_new->object_list = NULL;
88 u_new->left = u_new->right = u_new->top = u_new->bottom = -1;
90 u_new->page_control = 0;
91 u_new->up = -2;
93 u_new->prev = NULL;
94 u_new->next = NULL;
96 return(u_new);
99 /*! \todo Finish function documentation!!!
100 * \brief
101 * \par Function Description
104 void s_undo_destroy_head(UNDO *u_head)
106 g_free(u_head);
109 /*! \todo Finish function documentation!!!
110 * \brief
111 * \par Function Description
114 UNDO *s_undo_add (UNDO *head, int type, char *filename, GList *object_list,
115 int left, int top, int right, int bottom, int page_control,
116 int up)
118 UNDO *tail;
119 UNDO *u_new;
121 u_new = (UNDO *) g_malloc(sizeof(UNDO));
123 u_new->filename = g_strdup (filename);
125 u_new->object_list = object_list;
127 u_new->type = type;
129 u_new->left = left;
130 u_new->top = top;
131 u_new->right = right;
132 u_new->bottom = bottom;
134 u_new->page_control = page_control;
135 u_new->up = up;
137 if (head == NULL) {
138 u_new->prev = NULL; /* setup previous link */
139 u_new->next = NULL;
140 return(u_new);
141 } else {
142 tail = s_undo_return_tail(head);
143 u_new->prev = tail; /* setup previous link */
144 u_new->next = NULL;
145 tail->next = u_new;
146 return(tail->next);
150 /*! \todo Finish function documentation!!!
151 * \brief
152 * \par Function Description
155 void s_undo_print_all( UNDO *head )
157 UNDO *u_current;
159 u_current = head;
161 printf("START printing undo ********************\n");
162 printf("BOTTOM\n");
163 while(u_current != NULL) {
165 if (u_current->filename) printf("%s\n", u_current->filename);
167 if (u_current->object_list) {
168 print_struct_forw (u_current->object_list);
171 printf("\t%d %d %d %d\n", u_current->left, u_current->top,
172 u_current->right, u_current->bottom);
173 u_current = u_current->next;
175 printf("TOS\n");
176 printf("Number of levels: %d\n", s_undo_levels(head));
177 printf("DONE printing undo ********************\n");
178 printf("\n");
182 /*! \todo Finish function documentation!!!
183 * \brief
184 * \par Function Description
187 void s_undo_destroy_all(TOPLEVEL *toplevel, UNDO *head)
189 UNDO *u_current;
190 UNDO *u_prev;
192 u_current = s_undo_return_tail(head);
194 while (u_current != NULL) {
195 u_prev = u_current->prev;
196 g_free(u_current->filename);
198 if (u_current->object_list) {
199 s_delete_object_glist (toplevel, u_current->object_list);
200 u_current->object_list = NULL;
203 g_free(u_current);
204 u_current = u_prev;
208 /*! \todo Finish function documentation!!!
209 * \brief
210 * \par Function Description
213 void s_undo_remove(TOPLEVEL *toplevel, UNDO *head, UNDO *u_tos)
215 UNDO *u_current;
217 if (u_tos == NULL) {
218 fprintf(stderr, "Got NULL for u_tos in s_undo_remove\n");
219 return;
222 u_current = head;
224 while (u_current != NULL) {
225 if (u_current == u_tos) {
226 if (u_current->next)
227 u_current->next->prev = u_current->prev;
228 else
229 u_current->next = NULL;
231 if (u_current->prev)
232 u_current->prev->next = u_current->next;
233 else
234 u_current->prev = NULL;
236 g_free(u_current->filename);
238 if (u_current->object_list) {
239 s_delete_object_glist (toplevel, u_current->object_list);
240 u_current->object_list = NULL;
243 g_free(u_current);
244 return;
246 u_current = u_current->next;
250 /*! \todo Finish function documentation!!!
251 * \brief
252 * \par Function Description
255 void s_undo_remove_rest(TOPLEVEL *toplevel, UNDO *head)
257 UNDO *u_current;
258 UNDO *u_next;
260 u_current = head;
262 while (u_current != NULL) {
263 u_next = u_current->next;
265 if (u_current->filename) {
266 unlink(u_current->filename);
267 g_free(u_current->filename);
270 if (u_current->object_list) {
271 s_delete_object_glist (toplevel, u_current->object_list);
272 u_current->object_list = NULL;
275 g_free(u_current);
276 u_current = u_next;
280 /*! \todo Finish function documentation!!!
281 * \brief
282 * \par Function Description
285 int s_undo_levels(UNDO *head)
287 UNDO *u_current;
288 int count = 0;
290 u_current = head;
291 while (u_current != NULL) {
292 if (u_current->filename || u_current->object_list) {
293 count++;
296 u_current = u_current->next;
299 return(count);
302 /*! \todo Finish function documentation!!!
303 * \brief
304 * \par Function Description
307 void s_undo_init(PAGE *p_current)
309 p_current->undo_tos = p_current->undo_bottom = NULL;
310 p_current->undo_current = NULL;
313 /*! \todo Finish function documentation!!!
314 * \brief
315 * \par Function Description
318 void s_undo_free_all(TOPLEVEL *toplevel, PAGE *p_current)
320 s_undo_destroy_all(toplevel, p_current->undo_bottom);
321 p_current->undo_bottom = NULL;
322 p_current->undo_tos = NULL;
323 p_current->undo_current = NULL;