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