libgeda

s_undo.c

Go to the documentation of this file.
00001 /* gEDA - GPL Electronic Design Automation
00002  * libgeda - gEDA's library
00003  * Copyright (C) 1998-2010 Ales Hvezda
00004  * Copyright (C) 1998-2010 gEDA Contributors (see ChangeLog for details)
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 #include <config.h>
00021 
00022 #include <stdio.h>
00023 #include <ctype.h>
00024 #ifdef HAVE_STDLIB_H
00025 #include <stdlib.h>
00026 #endif
00027 #ifdef HAVE_UNISTD_H
00028 #include <unistd.h>
00029 #endif
00030 
00031 #include "libgeda_priv.h"
00032 
00033 #ifdef HAVE_LIBDMALLOC
00034 #include <dmalloc.h>
00035 #endif
00036 
00042 UNDO *s_undo_return_tail(UNDO *head)
00043 {
00044   UNDO *u_current=NULL;
00045   UNDO *ret_struct=NULL;
00046 
00047   u_current = head;
00048   while ( u_current != NULL ) { /* goto end of list */
00049     ret_struct = u_current;
00050     u_current = u_current->next;
00051   }
00052 
00053   return(ret_struct);
00054 }
00055 
00061 UNDO *s_undo_return_head(UNDO *tail)
00062 {
00063   UNDO *u_current=NULL;
00064   UNDO *ret_struct=NULL;
00065 
00066   u_current = tail;
00067   while ( u_current != NULL ) { /* goto end of list */
00068     ret_struct = u_current;
00069     u_current = u_current->prev;
00070   }
00071 
00072   return(ret_struct);
00073 }
00074 
00080 UNDO *s_undo_new_head(void)
00081 {
00082   UNDO *u_new;
00083 
00084   u_new = (UNDO *) g_malloc(sizeof(UNDO));
00085   u_new->type = -1;
00086   u_new->filename = NULL;
00087   u_new->object_list = NULL;
00088   u_new->left = u_new->right = u_new->top = u_new->bottom = -1;
00089 
00090   u_new->page_control = 0;
00091   u_new->up = -2;
00092 
00093   u_new->prev = NULL;
00094   u_new->next = NULL;
00095 
00096   return(u_new);
00097 }
00098 
00104 void s_undo_destroy_head(UNDO *u_head)
00105 {
00106   g_free(u_head);
00107 }
00108 
00114 UNDO *s_undo_add (UNDO *head, int type, char *filename, GList *object_list,
00115          int left, int top, int right, int bottom, int page_control,
00116          int up)
00117 {
00118   UNDO *tail;
00119   UNDO *u_new;
00120 
00121   u_new = (UNDO *) g_malloc(sizeof(UNDO));
00122 
00123   u_new->filename = g_strdup (filename);
00124     
00125   u_new->object_list = object_list;
00126 
00127   u_new->type = type;
00128 
00129   u_new->left = left;
00130   u_new->top = top;
00131   u_new->right = right;
00132   u_new->bottom = bottom;
00133 
00134   u_new->page_control = page_control;
00135   u_new->up = up;
00136 
00137   if (head == NULL) {
00138     u_new->prev = NULL; /* setup previous link */
00139     u_new->next = NULL;
00140     return(u_new);
00141   } else {
00142     tail = s_undo_return_tail(head);
00143     u_new->prev = tail; /* setup previous link */
00144     u_new->next = NULL;
00145     tail->next = u_new;
00146     return(tail->next);
00147   }
00148 }
00149 
00155 void s_undo_print_all( UNDO *head )
00156 {
00157   UNDO *u_current;
00158 
00159   u_current = head;
00160 
00161   printf("START printing undo ********************\n");
00162   printf("BOTTOM\n");
00163   while(u_current != NULL) {
00164 
00165     if (u_current->filename) printf("%s\n", u_current->filename);
00166         
00167     if (u_current->object_list) {
00168       print_struct_forw (u_current->object_list);
00169     }
00170         
00171     printf("\t%d %d %d %d\n", u_current->left, u_current->top,
00172            u_current->right, u_current->bottom);
00173     u_current = u_current->next;
00174   }
00175   printf("TOS\n");
00176   printf("Number of levels: %d\n", s_undo_levels(head));
00177   printf("DONE printing undo ********************\n");
00178   printf("\n");
00179 
00180 }
00181 
00187 void s_undo_destroy_all(TOPLEVEL *toplevel, UNDO *head)
00188 {
00189   UNDO *u_current;
00190   UNDO *u_prev;
00191 
00192   u_current = s_undo_return_tail(head);
00193 
00194   while (u_current != NULL) {
00195     u_prev = u_current->prev;   
00196     g_free(u_current->filename);
00197         
00198     if (u_current->object_list) {
00199       s_delete_object_glist (toplevel, u_current->object_list);
00200       u_current->object_list = NULL;
00201     }
00202 
00203     g_free(u_current);
00204     u_current = u_prev;
00205   }
00206 }
00207 
00213 void s_undo_remove(TOPLEVEL *toplevel, UNDO *head, UNDO *u_tos)
00214 {
00215   UNDO *u_current;
00216 
00217   if (u_tos == NULL) {
00218     fprintf(stderr, "Got NULL for u_tos in s_undo_remove\n");
00219     return;
00220   }
00221 
00222   u_current = head; 
00223 
00224   while (u_current != NULL) {
00225     if (u_current == u_tos) {
00226       if (u_current->next)
00227         u_current->next->prev = u_current->prev;
00228       else
00229         u_current->next = NULL;
00230 
00231       if (u_current->prev)
00232         u_current->prev->next = u_current->next;
00233       else
00234         u_current->prev = NULL;
00235 
00236       g_free(u_current->filename);  
00237 
00238       if (u_current->object_list) {
00239         s_delete_object_glist (toplevel, u_current->object_list);
00240         u_current->object_list = NULL;
00241       }
00242 
00243       g_free(u_current);
00244       return;
00245     }
00246     u_current = u_current->next;
00247   }
00248 }
00249 
00255 void s_undo_remove_rest(TOPLEVEL *toplevel, UNDO *head)
00256 {
00257   UNDO *u_current;
00258   UNDO *u_next;
00259 
00260   u_current = head;
00261 
00262   while (u_current != NULL) {
00263     u_next = u_current->next;   
00264 
00265     if (u_current->filename) {
00266       unlink(u_current->filename);
00267       g_free(u_current->filename);
00268     }
00269 
00270     if (u_current->object_list) {
00271       s_delete_object_glist (toplevel, u_current->object_list);
00272       u_current->object_list = NULL;
00273     }
00274 
00275     g_free(u_current);
00276     u_current = u_next;
00277   }
00278 }
00279 
00285 int s_undo_levels(UNDO *head)
00286 {
00287   UNDO *u_current;
00288   int count = 0;
00289     
00290   u_current = head;
00291   while (u_current != NULL) {
00292     if (u_current->filename || u_current->object_list) {
00293       count++;  
00294     }   
00295         
00296     u_current = u_current->next;
00297   }
00298     
00299   return(count);
00300 }
00301 
00307 void s_undo_init(PAGE *p_current)
00308 {
00309     p_current->undo_tos = p_current->undo_bottom = NULL;
00310     p_current->undo_current = NULL;
00311 }
00312 
00318 void s_undo_free_all(TOPLEVEL *toplevel, PAGE *p_current)
00319 {
00320   s_undo_destroy_all(toplevel, p_current->undo_bottom);
00321   p_current->undo_bottom = NULL;
00322   p_current->undo_tos = NULL;
00323   p_current->undo_current = NULL;
00324 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines