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