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