Blender  V2.59
undofile.c
Go to the documentation of this file.
00001 /*
00002  * $Id: undofile.c 35245 2011-02-27 20:35:41Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (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 Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2004 Blender Foundation
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): none yet.
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  * .blend file reading entry point
00029  */
00030 
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include <stdio.h>
00039 #include <math.h>
00040 
00041 #include "MEM_guardedalloc.h"
00042 
00043 #include "DNA_listBase.h"
00044 
00045 
00046 #include "BLO_undofile.h"
00047 
00048 #include "BLI_blenlib.h"
00049 #include "BLI_linklist.h"
00050 
00051 
00052 
00053 /* **************** support for memory-write, for undo buffers *************** */
00054 
00055 /* not memfile itself */
00056 void BLO_free_memfile(MemFile *memfile)
00057 {
00058         MemFileChunk *chunk;
00059         
00060         while( (chunk = (memfile->chunks.first) ) ) {
00061                 if(chunk->ident==0) MEM_freeN(chunk->buf);
00062                 BLI_remlink(&memfile->chunks, chunk);
00063                 MEM_freeN(chunk);
00064         }
00065         memfile->size= 0;
00066 }
00067 
00068 /* to keep list of memfiles consistent, 'first' is always first in list */
00069 /* result is that 'first' is being freed */
00070 void BLO_merge_memfile(MemFile *first, MemFile *second)
00071 {
00072         MemFileChunk *fc, *sc;
00073         
00074         fc= first->chunks.first;
00075         sc= second->chunks.first;
00076         while (fc || sc) {
00077                 if(fc && sc) {
00078                         if(sc->ident) {
00079                                 sc->ident= 0;
00080                                 fc->ident= 1;
00081                         }
00082                 }
00083                 if(fc) fc= fc->next;
00084                 if(sc) sc= sc->next;
00085         }
00086         
00087         BLO_free_memfile(first);
00088 }
00089 
00090 static int my_memcmp(int *mem1, int *mem2, int len)
00091 {
00092         register int a= len, *mema= mem1, *memb= mem2;
00093         
00094         while(a--) {
00095                 if( *mema != *memb) return 1;
00096                 mema++;
00097                 memb++;
00098         }
00099         return 0;
00100 }
00101 
00102 void add_memfilechunk(MemFile *compare, MemFile *current, char *buf, unsigned int size)
00103 {
00104         static MemFileChunk *compchunk=NULL;
00105         MemFileChunk *curchunk;
00106         
00107         /* this function inits when compare != NULL or when current==NULL */
00108         if(compare) {
00109                 compchunk= compare->chunks.first;
00110                 return;
00111         }
00112         if(current==NULL) {
00113                 compchunk= NULL;
00114                 return;
00115         }
00116         
00117         curchunk= MEM_mallocN(sizeof(MemFileChunk), "MemFileChunk");
00118         curchunk->size= size;
00119         curchunk->buf= NULL;
00120         curchunk->ident= 0;
00121         BLI_addtail(&current->chunks, curchunk);
00122         
00123         /* we compare compchunk with buf */
00124         if(compchunk) {
00125                 if(compchunk->size == curchunk->size) {
00126                         if( my_memcmp((int *)compchunk->buf, (int *)buf, size/4)==0) {
00127                                 curchunk->buf= compchunk->buf;
00128                                 curchunk->ident= 1;
00129                         }
00130                 }
00131                 compchunk= compchunk->next;
00132         }
00133         
00134         /* not equal... */
00135         if(curchunk->buf==NULL) {
00136                 curchunk->buf= MEM_mallocN(size, "Chunk buffer");
00137                 memcpy(curchunk->buf, buf, size);
00138                 current->size += size;
00139         }
00140 }
00141