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