Merged changes in the trunk up to revision 28536.
[blender.git] / source / blender / makesdna / intern / makesdna.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  *
29  * Struct muncher for making SDNA
30  *
31  * Originally by Ton, some mods by Frank, and some cleaning and
32  * extension by Nzc.
33  *
34  * Makesdna creates a .c file with a long string of numbers that
35  * encode the Blender file format. It is fast, because it is basically
36  * a binary dump. There are some details to mind when reconstructing
37  * the file (endianness and byte-alignment).
38  *
39  * This little program scans all structs that need to be serialized,
40  * and determined the names and types of all members. It calculates
41  * how much memory (on disk or in ram) is needed to store that struct,
42  * and the offsets for reaching a particular one.
43  *
44  * There is a facility to get verbose output from sdna. Search for
45  * debugSDNA. This int can be set to 0 (no output) to some int. Higher
46  * numbers give more output.
47  * */
48
49 #define DNA_VERSION_DATE "$Id$"
50
51 #include <string.h>
52 #include <stdlib.h>
53 #include <stdio.h>
54
55 #include "MEM_guardedalloc.h"
56 #include "DNA_sdna_types.h"
57
58 #include "BLO_sys_types.h" // for intptr_t support
59
60 #define SDNA_MAX_FILENAME_LENGTH 255
61
62
63 /* Included the path relative from /source/blender/ here, so we can move     */
64 /* headers around with more freedom.                                         */
65 char *includefiles[] = {
66
67         // if you add files here, please add them at the end
68         // of makesdna.c (this file) as well
69
70         "DNA_listBase.h",
71         "DNA_vec_types.h",
72         "DNA_ID.h",
73         "DNA_ipo_types.h",
74         "DNA_key_types.h",
75         "DNA_text_types.h",
76         "DNA_packedFile_types.h",
77         "DNA_camera_types.h",
78         "DNA_image_types.h",
79         "DNA_texture_types.h",
80         "DNA_lamp_types.h",
81         "DNA_wave_types.h",
82         "DNA_material_types.h",
83         "DNA_vfont_types.h",
84         // if you add files here, please add them at the end
85         // of makesdna.c (this file) as well
86         "DNA_meta_types.h",
87         "DNA_curve_types.h",
88         "DNA_mesh_types.h",
89         "DNA_meshdata_types.h",
90         "DNA_modifier_types.h",
91         "DNA_lattice_types.h",  
92         "DNA_object_types.h",
93         "DNA_object_force.h",
94         "DNA_object_fluidsim.h",
95         "DNA_world_types.h",
96         "DNA_scene_types.h",
97         "DNA_view3d_types.h",
98         "DNA_view2d_types.h",   
99         "DNA_space_types.h",
100         "DNA_userdef_types.h",
101         "DNA_screen_types.h",
102         "DNA_sdna_types.h",
103         // if you add files here, please add them at the end
104         // of makesdna.c (this file) as well
105         "DNA_fileglobal_types.h",
106         "DNA_sequence_types.h",
107         "DNA_effect_types.h",
108         "DNA_outliner_types.h",
109         "DNA_property_types.h",
110         "DNA_sensor_types.h",
111         "DNA_controller_types.h",
112         "DNA_actuator_types.h",
113         "DNA_sound_types.h",
114         "DNA_group_types.h",
115         "DNA_armature_types.h",
116         "DNA_action_types.h",
117         "DNA_constraint_types.h",
118         "DNA_nla_types.h",
119         "DNA_node_types.h",
120         "DNA_color_types.h",
121         "DNA_brush_types.h",
122         "DNA_customdata_types.h",
123         "DNA_particle_types.h",
124         "DNA_cloth_types.h",
125         "DNA_gpencil_types.h",
126         // if you add files here, please add them at the end
127         // of makesdna.c (this file) as well
128         "DNA_windowmanager_types.h",
129         "DNA_anim_types.h",
130         "DNA_boid_types.h",
131         "DNA_smoke_types.h",
132         "DNA_freestyle_types.h",
133
134         // empty string to indicate end of includefiles
135         ""
136 };
137
138 int maxdata= 500000, maxnr= 50000;
139 int nr_names=0;
140 int nr_types=0;
141 int nr_structs=0;
142 char **names, *namedata;                /* at adress names[a] is string a */
143 char **types, *typedata;                /* at adress types[a] is string a */
144 short *typelens;                                /* at typelens[a] is de length of type a */
145 short *alphalens;                           /* contains sizes as they are calculated on the DEC Alpha (64 bits) */
146 short **structs, *structdata;   /* at sp= structs[a] is the first adress of a struct definition
147                                                                    sp[0] is type number
148                                                                    sp[1] is amount of elements
149                                                                    sp[2] sp[3] is typenr,  namenr (etc) */
150 /*
151  * debugSDNA:
152  *  - 0 = no output, except errors
153  *  - 1 = detail actions
154  *  - 2 = full trace, tell which names and types were found
155  *  - 4 = full trace, plus all gritty details
156  */
157 int debugSDNA = 0;
158 int additional_slen_offset;
159
160 /* ************************************************************************** */
161 /* Functions                                                                  */
162 /* ************************************************************************** */
163
164 /**
165  * Add type <str> to struct indexed by <len>, if it was not yet found.
166  */
167 int add_type(char *str, int len);
168
169 /**
170  * Add variable <str> to 
171  */
172 int add_name(char *str);
173
174 /**
175  * Search whether this structure type was already found, and if not,
176  * add it.
177  */
178 short *add_struct(int namecode);
179
180 /**
181  * Remove comments from this buffer. Assumes that the buffer refers to
182  * ascii-code text.
183  */
184 int preprocess_include(char *maindata, int len);
185
186 /**
187  * Scan this file for serializable types.
188  */ 
189 int convert_include(char *filename);
190
191 /**
192  * Determine how many bytes are needed for an array.
193  */ 
194 int arraysize(char *astr, int len);
195
196 /**
197  * Determine how many bytes are needed for each struct.
198  */ 
199 static int calculate_structlens(int);
200
201 /**
202  * Construct the DNA.c file
203  */ 
204 void dna_write(FILE *file, void *pntr, int size);
205
206 /**
207  * Report all structures found so far, and print their lenghts.
208  */
209 void printStructLenghts(void);
210
211
212
213 /* ************************************************************************** */
214 /* Implementation                                                             */
215 /* ************************************************************************** */
216
217 /* ************************* MAKEN DNA ********************** */
218
219 int add_type(char *str, int len)
220 {
221         int nr;
222         char *cp;
223         
224         if(str[0]==0) return -1;
225         
226         /* search through type array */
227         for(nr=0; nr<nr_types; nr++) {
228                 if(strcmp(str, types[nr])==0) {
229                         if (len) {
230                                 typelens[nr]= len;
231                                 alphalens[nr] = len;
232                         }
233                         return nr;
234                 }
235         }
236         
237         /* append new type */
238         if(nr_types==0) cp= typedata;
239         else {
240                 cp= types[nr_types-1]+strlen(types[nr_types-1])+1;
241         }
242         strcpy(cp, str);
243         types[nr_types]= cp;
244         typelens[nr_types]= len;
245         alphalens[nr_types]= len;
246         
247         if(nr_types>=maxnr) {
248                 printf("too many types\n");
249                 return nr_types-1;;
250         }
251         nr_types++;
252         
253         return nr_types-1;
254 }
255
256
257 /**
258  *
259  * Because of the weird way of tokenizing, we have to 'cast' function
260  * pointers to ... (*f)(), whatever the original signature. In fact,
261  * we add name and type at the same time... There are two special
262  * cases, unfortunately. These are explicitly checked.
263  *
264  * */
265 int add_name(char *str)
266 {
267         int nr, i, j, k;
268         char *cp;
269         char buf[255]; /* stupid limit, change it :) */
270         char *name;
271
272         additional_slen_offset = 0;
273         
274         if((str[0]==0) /*  || (str[1]==0) */) return -1;
275
276         if (str[0] == '(' && str[1] == '*') {
277                 /* we handle function pointer and special array cases here, e.g.
278                    void (*function)(...) and float (*array)[..]. the array case
279                    name is still converted to (array*)() though because it is that
280                    way in old dna too, and works correct with elementsize() */
281                 int isfuncptr = (strchr(str+1, '(')) != NULL;
282
283                 if (debugSDNA > 3) printf("\t\t\t\t*** Function pointer or multidim array pointer found\n");
284                 /* functionpointer: transform the type (sometimes) */
285                 i = 0;
286                 j = 0;
287
288                 while (str[i] != ')') {
289                         buf[i] = str[i];
290                         i++;
291                 }
292                 
293                 /* Another number we need is the extra slen offset. This extra
294                  * offset is the overshoot after a space. If there is no
295                  * space, no overshoot should be calculated. */
296                 j = i; /* j at first closing brace */
297
298                 if (debugSDNA > 3) printf("first brace after offset %d\n", i);
299
300                 j++; /* j beyond closing brace ? */
301                 while ((str[j] != 0) && (str[j] != ')' )) {
302                         if (debugSDNA > 3) printf("seen %c ( %d) \n", str[j], str[j]);
303                         j++;
304                 }
305                 if (debugSDNA > 3) printf("seen %c ( %d) \n", str[j], str[j]); 
306                 if (debugSDNA > 3) printf("special after offset %d\n", j); 
307                                 
308                 if (!isfuncptr) {
309                         /* multidimensional array pointer case */
310                         if(str[j] == 0) {
311                                 if (debugSDNA > 3) printf("offsetting for multidim array pointer\n");
312                         }
313                         else
314                                 printf("Error during tokening multidim array pointer\n");
315                 }
316                 else if (str[j] == 0 ) {
317                         if (debugSDNA > 3) printf("offsetting for space\n"); 
318                         /* get additional offset */
319                         k = 0;
320                         while (str[j] != ')') {
321                                 j++;
322                                 k++;
323                         }
324                         if (debugSDNA > 3) printf("extra offset %d\n", k);
325                         additional_slen_offset = k;
326                 } else if (str[j] == ')' ) {
327                         if (debugSDNA > 3) printf("offsetting for brace\n");
328                         ; /* don't get extra offset */
329                 } else {
330                         printf("Error during tokening function pointer argument list\n");
331                 }
332                                 
333                 /*
334                  * Put )(void) at the end? Maybe )(). Should check this with
335                  * old sdna. Actually, sometimes )(), sometimes )(void...)
336                  * Alas.. such is the nature of braindamage :(
337                  *
338                  * Sorted it out: always do )(), except for headdraw and
339                  * windraw, part of ScrArea. This is important, because some
340                  * linkers will treat different fp's differently when called
341                  * !!! This has to do with interference in byte-alignment and
342                  * the way args are pushed on the stack.
343                  *
344                  * */
345                 buf[i] = 0;
346                 if (debugSDNA > 3) printf("Name before chomping: %s\n", buf); 
347                 if ( (strncmp(buf,"(*headdraw", 10) == 0)
348                         || (strncmp(buf,"(*windraw", 9) == 0) ) {
349                         buf[i] = ')';
350                         buf[i+1] = '(';
351                         buf[i+2] = 'v'; 
352                         buf[i+3] = 'o';
353                         buf[i+4] = 'i';
354                         buf[i+5] = 'd';
355                         buf[i+6] = ')';
356                         buf[i+7] = 0;
357                 } else {
358                         buf[i] = ')';
359                         buf[i+1] = '(';
360                         buf[i+2] = ')';
361                         buf[i+3] = 0;
362                 }
363                 /* now precede with buf*/
364                 if (debugSDNA > 3)  printf("\t\t\t\t\tProposing fp name %s\n", buf);
365                 name = buf;
366         } else {
367                 /* normal field: old code */
368                 name = str;
369         }
370         
371         /* search name array */
372         for(nr=0; nr<nr_names; nr++) {
373                 if(strcmp(name, names[nr])==0) {
374                         return nr;
375                 }
376         }
377         
378         /* append new type */
379         if(nr_names==0) cp= namedata;
380         else {
381                 cp= names[nr_names-1]+strlen(names[nr_names-1])+1;
382         }
383         strcpy(cp, name);
384         names[nr_names]= cp;
385         
386         if(nr_names>=maxnr) {
387                 printf("too many names\n");
388                 return nr_names-1;
389         }
390         nr_names++;
391         
392         return nr_names-1;
393 }
394
395 short *add_struct(int namecode)
396 {
397         int len;
398         short *sp;
399
400         if(nr_structs==0) {
401                 structs[0]= structdata;
402         }
403         else {
404                 sp= structs[nr_structs-1];
405                 len= sp[1];
406                 structs[nr_structs]= sp+ 2*len+2;
407         }
408         
409         sp= structs[nr_structs];
410         sp[0]= namecode;
411         
412         if(nr_structs>=maxnr) {
413                 printf("too many structs\n");
414                 return sp;
415         }
416         nr_structs++;
417         
418         return sp;
419 }
420
421 int preprocess_include(char *maindata, int len)
422 {
423         int a, newlen, comment = 0;
424         char *cp, *temp, *md;
425         
426         temp= MEM_mallocN(len, "preprocess_include");
427         memcpy(temp, maindata, len);
428         
429         // remove all c++ comments
430         /* replace all enters/tabs/etc with spaces */
431         cp= temp;
432         a= len;
433         comment = 0;
434         while(a--) {
435                 if(cp[0]=='/' && cp[1]=='/') {
436                         comment = 1;
437                 } else if (*cp<32) {
438                         comment = 0;
439                 }
440                 if (comment || *cp<32 || *cp>128 ) *cp= 32;
441                 cp++;
442         }
443         
444
445         /* data from temp copy to maindata, remove comments and double spaces */
446         cp= temp;
447         md= maindata;
448         newlen= 0;
449         comment= 0;
450         a= len;
451         while(a--) {
452                 
453                 if(cp[0]=='/' && cp[1]=='*') {
454                         comment= 1;
455                         cp[0]=cp[1]= 32;
456                 }
457                 if(cp[0]=='*' && cp[1]=='/') {
458                         comment= 0;
459                         cp[0]=cp[1]= 32;
460                 }
461
462                 /* do not copy when: */
463                 if(comment);
464                 else if( cp[0]==' ' && cp[1]==' ' );
465                 else if( cp[-1]=='*' && cp[0]==' ' );   /* pointers with a space */
466                 else {
467                         md[0]= cp[0];
468                         md++;
469                         newlen++;
470                 }
471                 cp++;
472         }
473         
474         MEM_freeN(temp);
475         return newlen;
476 }
477
478 static void *read_file_data(char *filename, int *len_r)
479 {
480 #ifdef WIN32
481         FILE *fp= fopen(filename, "rb");
482 #else
483         FILE *fp= fopen(filename, "r");
484 #endif
485         void *data;
486
487         if (!fp) {
488                 *len_r= -1;
489                 return NULL;
490         }
491
492         fseek(fp, 0L, SEEK_END);
493         *len_r= ftell(fp);
494         fseek(fp, 0L, SEEK_SET);
495
496         data= MEM_mallocN(*len_r, "read_file_data");
497         if (!data) {
498                 *len_r= -1;
499                 fclose(fp);
500                 return NULL;
501         }
502
503         if (fread(data, *len_r, 1, fp)!=1) {
504                 *len_r= -1;
505                 MEM_freeN(data);
506                 fclose(fp);
507                 return NULL;
508         }
509         
510         fclose(fp);
511         return data;
512 }
513
514 int convert_include(char *filename)
515 {
516         /* read include file, skip structs with a '#' before it.
517            store all data in temporal arrays.
518         */
519         int filelen, count, overslaan, slen, type, name, strct;
520         short *structpoin, *sp;
521         char *maindata, *mainend, *md, *md1;
522         
523         md= maindata= read_file_data(filename, &filelen);
524         if (filelen==-1) {
525                 printf("Can't read file %s\n", filename);
526                 return 1;
527         }
528
529         filelen= preprocess_include(maindata, filelen);
530         mainend= maindata+filelen-1;
531
532         /* we look for '{' and then back to 'struct' */
533         count= 0;
534         overslaan= 0;
535         while(count<filelen) {
536                 
537                 /* code for skipping a struct: two hashes on 2 lines. (preprocess added a space) */
538                 if(md[0]=='#' && md[1]==' ' && md[2]=='#') {
539                         overslaan= 1;
540                 }
541                 
542                 if(md[0]=='{') {
543                         md[0]= 0;
544                         if(overslaan) {
545                                 overslaan= 0;
546                         }
547                         else {
548                                 if(md[-1]==' ') md[-1]= 0;
549                                 md1= md-2;
550                                 while( *md1!=32) md1--;         /* to beginning of word */
551                                 md1++;
552                                 
553                                 /* we've got a struct name when... */
554                                 if( strncmp(md1-7, "struct", 6)==0 ) {
555
556                                         
557                                         strct= add_type(md1, 0);
558                                         structpoin= add_struct(strct);
559                                         sp= structpoin+2;
560
561                                         if (debugSDNA > 1) printf("\t|\t|-- detected struct %s\n", types[strct]);
562
563                                         /* first lets make it all nice strings */
564                                         md1= md+1;
565                                         while(*md1 != '}') {
566                                                 if(md1>mainend) break;
567                                                 
568                                                 if(*md1==',' || *md1==' ') *md1= 0;
569                                                 md1++;
570                                         }
571                                         
572                                         /* read types and names until first character that is not '}' */
573                                         md1= md+1;
574                                         while( *md1 != '}' ) {
575                                                 if(md1>mainend) break;
576                                                 
577                                                 /* skip when it says 'struct' or 'unsigned' or 'const' */
578                                                 if(*md1) {
579                                                         if( strncmp(md1, "struct", 6)==0 ) md1+= 7;
580                                                         if( strncmp(md1, "unsigned", 8)==0 ) md1+= 9;
581                                                         if( strncmp(md1, "const", 5)==0 ) md1+= 6;
582                                                         
583                                                         /* we've got a type! */
584                                                         type= add_type(md1, 0);
585
586                                                         if (debugSDNA > 1) printf("\t|\t|\tfound type %s (", md1);
587
588                                                         md1+= strlen(md1);
589
590                                                         
591                                                         /* read until ';' */
592                                                         while( *md1 != ';' ) {
593                                                                 if(md1>mainend) break;
594                                                                 
595                                                                 if(*md1) {
596                                                                         /* We've got a name. slen needs
597                                                                          * correction for function
598                                                                          * pointers! */
599                                                                         slen= (int) strlen(md1);
600                                                                         if( md1[slen-1]==';' ) {
601                                                                                 md1[slen-1]= 0;
602
603
604                                                                                 name= add_name(md1);
605                                                                                 slen += additional_slen_offset;
606                                                                                 sp[0]= type;
607                                                                                 sp[1]= name;
608
609                                                                                 if ((debugSDNA>1) && (names[name] != 0 )) printf("%s |", names[name]);
610
611                                                                                 structpoin[1]++;
612                                                                                 sp+= 2;
613                                                                                                                                                                         
614                                                                                 md1+= slen;
615                                                                                 break;
616                                                                         }
617                                                                         
618
619                                                                         name= add_name(md1);
620                                                                         slen += additional_slen_offset;
621
622                                                                         sp[0]= type;
623                                                                         sp[1]= name;
624                                                                         if ((debugSDNA > 1) && (names[name] != 0 )) printf("%s ||", names[name]);
625
626                                                                         structpoin[1]++;
627                                                                         sp+= 2;
628                                                                         
629                                                                         md1+= slen;
630                                                                 }
631                                                                 md1++;
632                                                         }
633
634                                                         if (debugSDNA > 1) printf(")\n");
635
636                                                 }
637                                                 md1++;
638                                         }
639                                 }
640                         }
641                 }
642                 count++;
643                 md++;
644         }
645         
646         MEM_freeN(maindata);
647
648         return 0;
649 }
650
651 int arraysize(char *astr, int len)
652 {
653         int a, mul=1;
654         char str[100], *cp=0;
655
656         memcpy(str, astr, len+1);
657         
658         for(a=0; a<len; a++) {
659                 if( str[a]== '[' ) {
660                         cp= &(str[a+1]);
661                 }
662                 else if( str[a]==']' && cp) {
663                         str[a]= 0;
664                         mul*= atoi(cp);
665                 }
666         }
667         
668         return mul;
669 }
670
671 static int calculate_structlens(int firststruct)
672 {
673         int a, b, len, alphalen, unknown= nr_structs, lastunknown, structtype, type, mul, namelen;
674         short *sp, *structpoin;
675         char *cp;
676         int has_pointer, dna_error = 0;
677                 
678         while(unknown) {
679                 lastunknown= unknown;
680                 unknown= 0;
681                 
682                 /* check all structs... */
683                 for(a=0; a<nr_structs; a++) {
684                         structpoin= structs[a];
685                         structtype= structpoin[0];
686
687                         /* when length is not known... */
688                         if(typelens[structtype]==0) {
689                                 
690                                 sp= structpoin+2;
691                                 len= 0;
692                                 alphalen = 0;
693                                 has_pointer = 0;
694                                 
695                                 /* check all elements in struct */
696                                 for(b=0; b<structpoin[1]; b++, sp+=2) {
697                                         type= sp[0];
698                                         cp= names[sp[1]];
699
700                                         namelen= (int) strlen(cp);
701                                         /* is it a pointer or function pointer? */
702                                         if(cp[0]=='*' || cp[1]=='*') {
703                                                 has_pointer = 1;
704                                                 /* has the name an extra length? (array) */
705                                                 mul= 1;
706                                                 if( cp[namelen-1]==']') mul= arraysize(cp, namelen);
707                                                 
708                                                 /* 4-8 aligned/ */
709                                                 if(sizeof(void *) == 4) {
710                                                         if (len % 4) {
711                                                                 printf("Align pointer error in struct (len4): %s %s\n", types[structtype], cp);
712                                                                 dna_error = 1;
713                                                         }
714                                                 } else {
715                                                         if (len % 8) {
716                                                                 printf("Align pointer error in struct (len8): %s %s\n", types[structtype], cp);
717                                                                 dna_error = 1;
718                                                         }
719                                                 }
720
721                                                 if (alphalen % 8) {
722                                                         printf("Align pointer error in struct (alphalen8): %s %s\n", types[structtype],cp);
723                                                         dna_error = 1;
724                                                 }
725
726                                                 len += sizeof(void *) * mul;
727                                                 alphalen += 8 * mul;
728
729                                         } else if(cp[0]=='[') {
730                                                 /* parsing can cause names "var" and "[3]" to be found for "float var [3]" ... */
731                                                 printf("Parse error in struct, invalid member name: %s %s\n", types[structtype], cp);
732                                                 dna_error = 1;
733                                         } else if( typelens[type] ) {
734                                                 /* has the name an extra length? (array) */
735                                                 mul= 1;
736                                                 if( cp[namelen-1]==']') mul= arraysize(cp, namelen);
737                                                 
738                                                 /* struct alignment */
739                                                 if(type >= firststruct) {
740                                                         if(sizeof(void *)==8 && (len % 8) ) {
741                                                                 printf("Align struct error: %s %s\n", types[structtype],cp);
742                                                                 dna_error = 1;
743                                                         }
744                                                 }
745                                                 
746                                                 /* 2-4 aligned/ */
747                                                 if(typelens[type]>3 && (len % 4) ) {
748                                                         printf("Align 4 error in struct: %s %s (add %d padding bytes)\n", types[structtype], cp, len%4);
749                                                         dna_error = 1;
750                                                 }
751                                                 else if(typelens[type]==2 && (len % 2) ) {
752                                                         printf("Align 2 error in struct: %s %s (add %d padding bytes)\n", types[structtype], cp, len%2);
753                                                         dna_error = 1;
754                                                 }
755
756                                                 len += mul*typelens[type];
757                                                 alphalen += mul * alphalens[type];
758                                                 
759                                         } else {
760                                                 len= 0;
761                                                 alphalen = 0;
762                                                 break;
763                                         }
764                                 }
765                                 
766                                 if (len==0) {
767                                         unknown++;
768                                 } else {
769                                         typelens[structtype]= len;
770                                         alphalens[structtype]= alphalen;
771                                         // two ways to detect if a struct contains a pointer:
772                                         // has_pointer is set or alphalen != len
773                                         if (has_pointer || alphalen != len) {
774                                                 if (alphalen % 8) {
775                                                         printf("Sizeerror 8 in struct: %s (add %d bytes)\n", types[structtype], alphalen%8);
776                                                         dna_error = 1;
777                                                 }
778                                         }
779                                         
780                                         if(len % 4) {
781                                                 printf("Sizeerror 4 in struct: %s (add %d bytes)\n", types[structtype], len%4);
782                                                 dna_error = 1;
783                                         }
784                                         
785                                 }
786                         }
787                 }
788                 
789                 if(unknown==lastunknown) break;
790         }
791         
792         if(unknown) {
793                 printf("ERROR: still %d structs unknown\n", unknown);
794
795                 if (debugSDNA) {
796                         printf("*** Known structs : \n");
797                         
798                         for(a=0; a<nr_structs; a++) {
799                                 structpoin= structs[a];
800                                 structtype= structpoin[0];
801                                 
802                                 /* length unknown */
803                                 if(typelens[structtype]!=0) {
804                                         printf("  %s\n", types[structtype]);
805                                 }
806                         }
807                 }
808
809                         
810                 printf("*** Unknown structs : \n");
811                         
812                 for(a=0; a<nr_structs; a++) {
813                         structpoin= structs[a];
814                         structtype= structpoin[0];
815
816                         /* length unkown yet */
817                         if(typelens[structtype]==0) {
818                                 printf("  %s\n", types[structtype]);
819                         }
820                 }
821         }
822
823         return(dna_error);
824 }
825
826 #define MAX_DNA_LINE_LENGTH 20
827
828 void dna_write(FILE *file, void *pntr, int size)
829 {
830         static int linelength = 0;
831         int i;
832         char *data;
833
834         data = (char *) pntr;
835         
836         for (i = 0 ; i < size ; i++)
837         {
838                 fprintf(file, "%d,", data[i]);
839                 linelength++;
840                 if (linelength >= MAX_DNA_LINE_LENGTH) {
841                         fprintf(file, "\n");
842                         linelength = 0;
843                 }
844         }
845 }
846
847 void printStructLenghts(void)
848 {
849         int a, unknown= nr_structs, lastunknown, structtype;
850         short *structpoin;
851         printf("\n\n*** All detected structs:\n");
852
853         while(unknown) {
854                 lastunknown= unknown;
855                 unknown= 0;
856                 
857                 /* check all structs... */
858                 for(a=0; a<nr_structs; a++) {
859                         structpoin= structs[a];
860                         structtype= structpoin[0];
861                         printf("\t%s\t:%d\n", types[structtype], typelens[structtype]);
862                 }
863         }
864
865         printf("*** End of list\n");
866
867 }
868
869
870 int make_structDNA(char *baseDirectory, FILE *file)
871 {
872         int len, i;
873         short *sp;
874         /* str contains filenames. Since we now include paths, I stretched       */
875         /* it a bit. Hope this is enough :) -nzc-                                */
876         char str[SDNA_MAX_FILENAME_LENGTH], *cp;
877         int firststruct;
878         
879         if (debugSDNA > -1) {
880                 fflush(stdout);
881                 printf("Running makesdna at debug level %d\n", debugSDNA);
882                 printf("\tProgram version: %s\n", DNA_VERSION_DATE);
883         }
884                 
885         /* the longest known struct is 50k, so we assume 100k is sufficent! */
886         namedata= MEM_callocN(maxdata, "namedata");
887         typedata= MEM_callocN(maxdata, "typedata");
888         structdata= MEM_callocN(maxdata, "structdata");
889         
890         /* a maximum of 5000 variables, must be sufficient? */
891         names= MEM_callocN(sizeof(char *)*maxnr, "names");
892         types= MEM_callocN(sizeof(char *)*maxnr, "types");
893         typelens= MEM_callocN(sizeof(short)*maxnr, "typelens");
894         alphalens= MEM_callocN(sizeof(short)*maxnr, "alphalens");
895         structs= MEM_callocN(sizeof(short)*maxnr, "structs");
896
897         /* insertion of all known types */
898         /* watch it: uint is not allowed! use in structs an unsigned int */
899         add_type("char", 1);    /* 0 */
900         add_type("uchar", 1);   /* 1 */
901         add_type("short", 2);   /* 2 */
902         add_type("ushort", 2);  /* 3 */
903         add_type("int", 4);             /* 4 */
904         add_type("long", 4);    /* 5 */         /* should it be 8 on 64 bits? */
905         add_type("ulong", 4);   /* 6 */
906         add_type("float", 4);   /* 7 */
907         add_type("double", 8);  /* 8 */
908         add_type("void", 0);    /* 9 */
909
910         // the defines above shouldn't be output in the padding file...
911         firststruct = nr_types;
912         
913         /* add all include files defined in the global array                     */
914         /* Since the internal file+path name buffer has limited length, I do a   */
915         /* little test first...                                                  */
916         /* Mind the breaking condition here!                                     */
917         if (debugSDNA) printf("\tStart of header scan:\n"); 
918         for (i = 0; strlen(includefiles[i]); i++) {
919                 sprintf(str, "%s%s", baseDirectory, includefiles[i]);
920                   if (debugSDNA) printf("\t|-- Converting %s\n", str); 
921                 if (convert_include(str)) {
922                         return (1);
923                 }
924         }
925         if (debugSDNA) printf("\tFinished scanning %d headers.\n", i); 
926
927         if (calculate_structlens(firststruct)) {
928                 // error
929                 return(1);
930         }
931
932         /* FOR DEBUG */
933         if (debugSDNA > 1)
934         {
935                 int a,b;
936 /*              short *elem; */
937                 short num_types;
938
939                 printf("nr_names %d nr_types %d nr_structs %d\n", nr_names, nr_types, nr_structs);
940                 for(a=0; a<nr_names; a++) { 
941                         printf(" %s \n", names[a]);
942                 }
943                 printf("\n");
944                 
945                 sp= typelens;
946                 for(a=0; a<nr_types; a++, sp++) { 
947                         printf(" %s %d\n", types[a], *sp);
948                 }
949                 printf("\n");
950                 
951                 for(a=0; a<nr_structs; a++) {
952                         sp= structs[a];
953                         printf(" struct %s elems: %d size: %d\n", types[sp[0]], sp[1],typelens[sp[0]]);
954                         num_types  = sp[1];
955                         sp+= 2;
956                         /* ? num_types was elem? */
957                         for(b=0; b< num_types; b++, sp+= 2) {
958                                 printf("   %s %s\n", types[sp[0]], names[sp[1]]);
959                         }
960                 }
961         }
962
963         /* file writing */
964
965         if (debugSDNA > -1) printf("Writing file ... ");
966                 
967         if(nr_names==0 || nr_structs==0);
968         else {
969                 strcpy(str, "SDNA");
970                 dna_write(file, str, 4);
971                 
972                 /* write names */
973                 strcpy(str, "NAME");
974                 dna_write(file, str, 4);
975                 len= nr_names;
976                 dna_write(file, &len, 4);
977                 
978                 /* calculate size of datablock with strings */
979                 cp= names[nr_names-1];
980                 cp+= strlen(names[nr_names-1]) + 1;                     /* +1: null-terminator */
981                 len= (intptr_t) (cp - (char*) names[0]);
982                 len= (len+3) & ~3;
983                 dna_write(file, names[0], len);
984                 
985                 /* write TYPES */
986                 strcpy(str, "TYPE");
987                 dna_write(file, str, 4);
988                 len= nr_types;
989                 dna_write(file, &len, 4);
990         
991                 /* calculate datablock size */
992                 cp= types[nr_types-1];
993                 cp+= strlen(types[nr_types-1]) + 1;             /* +1: null-terminator */
994                 len= (intptr_t) (cp - (char*) types[0]);
995                 len= (len+3) & ~3;
996                 
997                 dna_write(file, types[0], len);
998                 
999                 /* WRITE TYPELENGTHS */
1000                 strcpy(str, "TLEN");
1001                 dna_write(file, str, 4);
1002                 
1003                 len= 2*nr_types;
1004                 if(nr_types & 1) len+= 2;
1005                 dna_write(file, typelens, len);
1006                 
1007                 /* WRITE STRUCTS */
1008                 strcpy(str, "STRC");
1009                 dna_write(file, str, 4);
1010                 len= nr_structs;
1011                 dna_write(file, &len, 4);
1012         
1013                 /* calc datablock size */
1014                 sp= structs[nr_structs-1];
1015                 sp+= 2+ 2*( sp[1] );
1016                 len= (intptr_t) ((char*) sp - (char*) structs[0]);
1017                 len= (len+3) & ~3;
1018                 
1019                 dna_write(file, structs[0], len);
1020         
1021                 /* a simple dna padding test */
1022                 if (0) {
1023                         FILE *fp;
1024                         int a;
1025                         
1026                         fp= fopen("padding.c", "w");
1027                         if(fp==NULL);
1028                         else {
1029
1030                                 // add all include files defined in the global array
1031                                 for (i = 0; strlen(includefiles[i]); i++) {
1032                                         fprintf(fp, "#include \"%s%s\"\n", baseDirectory, includefiles[i]);
1033                                 }
1034
1035                                 fprintf(fp, "main(){\n");
1036                                 sp = typelens;
1037                                 sp += firststruct;
1038                                 for(a=firststruct; a<nr_types; a++, sp++) { 
1039                                         if(*sp) {
1040                                                 fprintf(fp, "\tif(sizeof(struct %s) - %d) printf(\"ALIGN ERROR:", types[a], *sp);
1041                                                 fprintf(fp, "%%d %s %d ", types[a], *sp);
1042                                                 fprintf(fp, "\\n\",  sizeof(struct %s) - %d);\n", types[a], *sp);
1043                                         }
1044                                 }
1045                                 fprintf(fp, "}\n");
1046                                 fclose(fp);
1047                         }
1048                 }
1049                 /*      end end padding test */
1050         }
1051         
1052         
1053         MEM_freeN(namedata);
1054         MEM_freeN(typedata);
1055         MEM_freeN(structdata);
1056         MEM_freeN(names);
1057         MEM_freeN(types);
1058         MEM_freeN(typelens);
1059         MEM_freeN(structs);
1060
1061         if (debugSDNA > -1) printf("done.\n");
1062         
1063         return(0);
1064 }
1065
1066 /* ************************* END MAKE DNA ********************** */
1067
1068 static void make_bad_file(char *file, int line)
1069 {
1070         FILE *fp= fopen(file, "w");
1071         fprintf(fp, "#error \"Error! can't make correct DNA.c file from %s:%d, STUPID!\"\n", __FILE__, line);
1072         fclose(fp);
1073 }
1074
1075 #ifndef BASE_HEADER
1076 #define BASE_HEADER "../"
1077 #endif
1078
1079 int main(int argc, char ** argv)
1080 {
1081         FILE *file;
1082         int return_status = 0;
1083
1084         if (argc!=2 && argc!=3) {
1085                 printf("Usage: %s outfile.c [base directory]\n", argv[0]);
1086                 return_status = 1;
1087         } else {
1088                 file = fopen(argv[1], "w");
1089                 if (!file) {
1090                         printf ("Unable to open file: %s\n", argv[1]);
1091                         return_status = 1;
1092                 } else {
1093                         char baseDirectory[256];
1094
1095                         if (argc==3) {
1096                                 strcpy(baseDirectory, argv[2]);
1097                         } else {
1098                                 strcpy(baseDirectory, BASE_HEADER);
1099                         }
1100
1101                         fprintf (file, "unsigned char DNAstr[]= {\n");
1102                         if (make_structDNA(baseDirectory, file)) {
1103                                 // error
1104                                 fclose(file);
1105                                 make_bad_file(argv[1], __LINE__);
1106                                 return_status = 1;
1107                         } else {
1108                                 fprintf(file, "};\n");
1109                                 fprintf(file, "int DNAlen= sizeof(DNAstr);\n");
1110         
1111                                 fclose(file);
1112                         }
1113                 }
1114         }
1115
1116                 
1117         return(return_status);
1118 }
1119
1120 // include files for automatic dependancies
1121 #include "DNA_listBase.h"
1122 #include "DNA_vec_types.h"
1123 #include "DNA_ID.h"
1124 #include "DNA_ipo_types.h"
1125 #include "DNA_key_types.h"
1126 #include "DNA_text_types.h"
1127 #include "DNA_packedFile_types.h"
1128 #include "DNA_camera_types.h"
1129 #include "DNA_image_types.h"
1130 #include "DNA_texture_types.h"
1131 #include "DNA_lamp_types.h"
1132 #include "DNA_wave_types.h"
1133 #include "DNA_material_types.h"
1134 #include "DNA_vfont_types.h"
1135 #include "DNA_meta_types.h"
1136 #include "DNA_curve_types.h"
1137 #include "DNA_mesh_types.h"
1138 #include "DNA_meshdata_types.h"
1139 #include "DNA_modifier_types.h"
1140 #include "DNA_lattice_types.h"  
1141 #include "DNA_object_types.h"
1142 #include "DNA_object_force.h"
1143 #include "DNA_object_fluidsim.h"
1144 #include "DNA_world_types.h"
1145 #include "DNA_scene_types.h"
1146 #include "DNA_view3d_types.h"
1147 #include "DNA_view2d_types.h"   
1148 #include "DNA_space_types.h"
1149 #include "DNA_userdef_types.h"
1150 #include "DNA_screen_types.h"
1151 #include "DNA_sdna_types.h"
1152 #include "DNA_fileglobal_types.h"
1153 #include "DNA_sequence_types.h"
1154 #include "DNA_effect_types.h"
1155 #include "DNA_outliner_types.h"
1156 #include "DNA_property_types.h"
1157 #include "DNA_sensor_types.h"
1158 #include "DNA_controller_types.h"
1159 #include "DNA_actuator_types.h"
1160 #include "DNA_sound_types.h"
1161 #include "DNA_group_types.h"
1162 #include "DNA_armature_types.h"
1163 #include "DNA_action_types.h"
1164 #include "DNA_constraint_types.h"
1165 #include "DNA_nla_types.h"
1166 #include "DNA_node_types.h"
1167 #include "DNA_color_types.h"
1168 #include "DNA_brush_types.h"
1169 #include "DNA_customdata_types.h"
1170 #include "DNA_particle_types.h"
1171 #include "DNA_cloth_types.h"
1172 #include "DNA_gpencil_types.h"
1173 #include "DNA_windowmanager_types.h"
1174 #include "DNA_anim_types.h"
1175 #include "DNA_boid_types.h"
1176 #include "DNA_smoke_types.h"
1177 #include "DNA_freestyle_types.h"
1178 /* end of list */