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