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