remove assignments which are unused.
[blender.git] / source / blender / blenloader / intern / readblenentry.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
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.
15  *
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 Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * .blend file reading entry point
29  */
30
31 #include <stddef.h>
32 #include "BLI_storage.h" /* _LARGEFILE_SOURCE */
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <math.h>
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_ghash.h"
42 #include "BLI_linklist.h"
43
44 #include "DNA_genfile.h"
45 #include "DNA_sdna_types.h"
46
47
48 #include "BKE_main.h"
49 #include "BKE_library.h" // for free_main
50 #include "BKE_idcode.h"
51 #include "BKE_report.h"
52
53 #include "BLO_readfile.h"
54 #include "BLO_undofile.h"
55
56 #include "readfile.h"
57
58 #include "BLO_readblenfile.h"
59
60 #include "BLO_sys_types.h" // needed for intptr_t
61
62 #ifdef _WIN32
63 #include "BLI_winstuff.h"
64 #endif
65
66 /* local prototypes --------------------- */
67 void BLO_blendhandle_print_sizes(BlendHandle *, void *); 
68
69         /* Access routines used by filesel. */
70          
71 BlendHandle *BLO_blendhandle_from_file(char *file) 
72 {
73         BlendHandle *bh;
74
75         bh= (BlendHandle*)blo_openblenderfile(file, NULL);
76
77         return bh;
78 }
79
80 BlendHandle *BLO_blendhandle_from_memory(void *mem, int memsize)
81 {
82         BlendHandle *bh;
83
84         bh= (BlendHandle*)blo_openblendermemory(mem, memsize, NULL);
85
86         return bh;
87 }
88
89 void BLO_blendhandle_print_sizes(BlendHandle *bh, void *fp) 
90 {
91         FileData *fd= (FileData*) bh;
92         BHead *bhead;
93
94         fprintf(fp, "[\n");
95         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
96                 if (bhead->code==ENDB)
97                         break;
98                 else {
99                         short *sp= fd->filesdna->structs[bhead->SDNAnr];
100                         char *name= fd->filesdna->types[ sp[0] ];
101                         char buf[4];
102                         
103                         buf[0]= (bhead->code>>24)&0xFF;
104                         buf[1]= (bhead->code>>16)&0xFF;
105                         buf[2]= (bhead->code>>8)&0xFF;
106                         buf[3]= (bhead->code>>0)&0xFF;
107                         
108                         buf[0]= buf[0]?buf[0]:' ';
109                         buf[1]= buf[1]?buf[1]:' ';
110                         buf[2]= buf[2]?buf[2]:' ';
111                         buf[3]= buf[3]?buf[3]:' ';
112                         
113                         fprintf(fp, "['%.4s', '%s', %d, %ld ], \n", buf, name, bhead->nr, (long int)bhead->len+sizeof(BHead));
114                 }
115         }
116         fprintf(fp, "]\n");
117 }
118
119 LinkNode *BLO_blendhandle_get_datablock_names(BlendHandle *bh, int ofblocktype) 
120 {
121         FileData *fd= (FileData*) bh;
122         LinkNode *names= NULL;
123         BHead *bhead;
124
125         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
126                 if (bhead->code==ofblocktype) {
127                         char *idname= bhead_id_name(fd, bhead);
128                         
129                         BLI_linklist_prepend(&names, strdup(idname+2));
130                 } else if (bhead->code==ENDB)
131                         break;
132         }
133         
134         return names;
135 }
136
137 LinkNode *BLO_blendhandle_get_previews(BlendHandle *bh, int ofblocktype) 
138 {
139         FileData *fd= (FileData*) bh;
140         LinkNode *previews= NULL;
141         BHead *bhead;
142         int looking=0;
143         int npreviews = 0;
144         PreviewImage* prv = NULL;
145         PreviewImage* new_prv = NULL;
146         
147         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
148                 if (bhead->code==ofblocktype) {
149                         ID *id= (ID*) (bhead+1);
150                         switch(GS(id->name))
151                         {
152                                 case ID_MA: /* fall through */
153                                 case ID_TE: /* fall through */
154                                 case ID_IM: /* fall through */
155                                 case ID_WO: /* fall through */
156                                 case ID_LA: /* fall through */
157                                         new_prv = MEM_callocN(sizeof(PreviewImage), "newpreview");
158                                         BLI_linklist_prepend(&previews, new_prv);
159                                         looking = 1;
160                                         break;
161                                 default:
162                                         break;
163                         }
164                 } else if (bhead->code==DATA) {
165                         if (looking) {
166                                 if (bhead->SDNAnr == DNA_struct_find_nr(fd->filesdna, "PreviewImage") ) {
167                                         prv = (PreviewImage*) (bhead+1);
168                                         npreviews = 0;                          
169                                         memcpy(new_prv, prv, sizeof(PreviewImage));
170                                         if (prv->rect[0]) {
171                                                 unsigned int *rect = NULL;
172                                                 // int rectlen = 0;
173                                                 new_prv->rect[0] = MEM_callocN(new_prv->w[0]*new_prv->h[0]*sizeof(unsigned int), "prvrect");
174                                                 bhead= blo_nextbhead(fd, bhead);
175                                                 rect = (unsigned int*)(bhead+1);
176                                                 // rectlen = new_prv->w[0]*new_prv->h[0]*sizeof(unsigned int);
177                                                 memcpy(new_prv->rect[0], rect, bhead->len);                                     
178                                         } else {
179                                                 new_prv->rect[0] = NULL;
180                                         }
181                                         
182                                         if (prv->rect[1]) {
183                                                 unsigned int *rect = NULL;
184                                                 // int rectlen = 0;
185                                                 new_prv->rect[1] = MEM_callocN(new_prv->w[1]*new_prv->h[1]*sizeof(unsigned int), "prvrect");
186                                                 bhead= blo_nextbhead(fd, bhead);
187                                                 rect = (unsigned int*)(bhead+1);
188                                                 // rectlen = new_prv->w[1]*new_prv->h[1]*sizeof(unsigned int);
189                                                 memcpy(new_prv->rect[1], rect, bhead->len);                                                     
190                                         } else {
191                                                 new_prv->rect[1] = NULL;
192                                         }
193                                 }
194                         }
195                 } else if (bhead->code==ENDB) {
196                         break;
197                 } else if (bhead->code==DATA) {
198                         /* DATA blocks between IDBlock and Preview */
199                 } else {
200                         looking = 0;
201                         new_prv = NULL;
202                         prv = NULL;
203                 }
204                 
205         }
206         
207         return previews;
208 }
209
210 LinkNode *BLO_blendhandle_get_linkable_groups(BlendHandle *bh) 
211 {
212         FileData *fd= (FileData*) bh;
213         GHash *gathered= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "linkable_groups gh");
214         LinkNode *names= NULL;
215         BHead *bhead;
216         
217         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
218                 if (bhead->code==ENDB) {
219                         break;
220                 } else if (BKE_idcode_is_valid(bhead->code)) {
221                         if (BKE_idcode_is_linkable(bhead->code)) {
222                                 const char *str= BKE_idcode_to_name(bhead->code);
223                                 
224                                 if (!BLI_ghash_haskey(gathered, (void *)str)) {
225                                         BLI_linklist_prepend(&names, strdup(str));
226                                         BLI_ghash_insert(gathered, (void *)str, NULL);
227                                 }
228                         }
229                 }
230         }
231         
232         BLI_ghash_free(gathered, NULL, NULL);
233         
234         return names;
235 }               
236
237 void BLO_blendhandle_close(BlendHandle *bh) {
238         FileData *fd= (FileData*) bh;
239         
240         blo_freefiledata(fd);
241 }
242
243         /**********/
244
245 BlendFileData *BLO_read_from_file(const char *file, ReportList *reports)
246 {
247         BlendFileData *bfd = NULL;
248         FileData *fd;
249                 
250         fd = blo_openblenderfile(file, reports);
251         if (fd) {
252                 fd->reports= reports;
253                 bfd= blo_read_file_internal(fd, file);
254                 blo_freefiledata(fd);                   
255         }
256
257         return bfd;     
258 }
259
260 BlendFileData *BLO_read_from_memory(void *mem, int memsize, ReportList *reports)
261 {
262         BlendFileData *bfd = NULL;
263         FileData *fd;
264                 
265         fd = blo_openblendermemory(mem, memsize,  reports);
266         if (fd) {
267                 fd->reports= reports;
268                 bfd= blo_read_file_internal(fd, "");
269                 blo_freefiledata(fd);                   
270         }
271
272         return bfd;     
273 }
274
275 BlendFileData *BLO_read_from_memfile(Main *oldmain, const char *filename, MemFile *memfile, ReportList *reports)
276 {
277         BlendFileData *bfd = NULL;
278         FileData *fd;
279         ListBase mainlist;
280         
281         fd = blo_openblendermemfile(memfile, reports);
282         if (fd) {
283                 fd->reports= reports;
284                 strcpy(fd->relabase, filename);
285                 
286                 /* clear ob->proxy_from pointers in old main */
287                 blo_clear_proxy_pointers_from_lib(oldmain);
288
289                 /* separate libraries from old main */
290                 blo_split_main(&mainlist, oldmain);
291                 /* add the library pointers in oldmap lookup */
292                 blo_add_library_pointer_map(&mainlist, fd);
293                 
294                 /* makes lookup of existing images in old main */
295                 blo_make_image_pointer_map(fd, oldmain);
296                 
297                 bfd= blo_read_file_internal(fd, filename);
298                 
299                 /* ensures relinked images are not freed */
300                 blo_end_image_pointer_map(fd, oldmain);
301                 
302                 /* move libraries from old main to new main */
303                 if(bfd && mainlist.first!=mainlist.last) {
304                         
305                         /* Library structs themselves */
306                         bfd->main->library= oldmain->library;
307                         oldmain->library.first= oldmain->library.last= NULL;
308                         
309                         /* add the Library mainlist to the new main */
310                         BLI_remlink(&mainlist, oldmain);
311                         BLI_addhead(&mainlist, bfd->main);
312                 }
313                 blo_join_main(&mainlist);
314                 
315                 blo_freefiledata(fd);                   
316         }
317
318         return bfd;     
319 }
320
321 void BLO_blendfiledata_free(BlendFileData *bfd)
322 {
323         if (bfd->main) {
324                 free_main(bfd->main);
325         }
326         
327         if (bfd->user) {
328                 MEM_freeN(bfd->user);
329         }
330
331         MEM_freeN(bfd);
332 }
333