Blender V2.61 - r43446

undofile.c

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