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