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