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