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