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