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