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