* merge r22734 from trunk
[blender-staging.git] / source / blender / makesdna / intern / makesdna.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  * Struct muncher for making SDNA
30  *
31  * Originally by Ton, some mods by Frank, and some cleaning and
32  * extension by Nzc.
33  *
34  * Makesdna creates a .c file with a long string of numbers that
35  * encode the Blender file format. It is fast, because it is basically
36  * a binary dump. There are some details to mind when reconstructing
37  * the file (endianness and byte-alignment).
38  *
39  * This little program scans all structs that need to be serialized,
40  * and determined the names and types of all members. It calculates
41  * how much memory (on disk or in ram) is needed to store that struct,
42  * and the offsets for reaching a particular one.
43  *
44  * There is a facility to get verbose output from sdna. Search for
45  * debugSDNA. This int can be set to 0 (no output) to some int. Higher
46  * numbers give more output.
47  * */
48
49 #define DNA_VERSION_DATE "$Id$"
50
51 #include <string.h>
52 #include <stdlib.h>
53 #include <stdio.h>
54
55 #include "MEM_guardedalloc.h"
56 #include "DNA_sdna_types.h"
57
58 #include "BLO_sys_types.h" // for intptr_t support
59
60 #ifdef HAVE_CONFIG_H
61 #include <config.h>
62 #endif
63
64 #define SDNA_MAX_FILENAME_LENGTH 255
65
66
67 /* Included the path relative from /source/blender/ here, so we can move     */
68 /* headers around with more freedom.                                         */
69 char *includefiles[] = {
70
71         // if you add files here, please add them at the end
72         // of makesdna.c (this file) as well
73
74         "DNA_listBase.h",
75         "DNA_vec_types.h",
76         "DNA_ID.h",
77         "DNA_ipo_types.h",
78         "DNA_key_types.h",
79         "DNA_text_types.h",
80         "DNA_packedFile_types.h",
81         "DNA_camera_types.h",
82         "DNA_image_types.h",
83         "DNA_texture_types.h",
84         "DNA_lamp_types.h",
85         "DNA_wave_types.h",
86         "DNA_material_types.h",
87         "DNA_vfont_types.h",
88         // if you add files here, please add them at the end
89         // of makesdna.c (this file) as well
90         "DNA_meta_types.h",
91         "DNA_curve_types.h",
92         "DNA_mesh_types.h",
93         "DNA_meshdata_types.h",
94         "DNA_modifier_types.h",
95         "DNA_lattice_types.h",  
96         "DNA_object_types.h",
97         "DNA_object_force.h",
98         "DNA_object_fluidsim.h",
99         "DNA_world_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_outliner_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         "DNA_windowmanager_types.h",
133         "DNA_anim_types.h",
134         "DNA_boid_types.h",
135         "DNA_smoke_types.h",
136
137         // empty string to indicate end of includefiles
138         ""
139 };
140
141 int maxdata= 500000, maxnr= 50000;
142 int nr_names=0;
143 int nr_types=0;
144 int nr_structs=0;
145 char **names, *namedata;                /* at adress names[a] is string a */
146 char **types, *typedata;                /* at adress types[a] is string a */
147 short *typelens;                                /* at typelens[a] is de length of type a */
148 short *alphalens;                           /* contains sizes as they are calculated on the DEC Alpha (64 bits) */
149 short **structs, *structdata;   /* at sp= structs[a] is the first adress of a struct definition
150                                                                    sp[0] is type number
151                                                                    sp[1] is amount of elements
152                                                                    sp[2] sp[3] is typenr,  namenr (etc) */
153 /*
154  * debugSDNA:
155  *  - 0 = no output, except errors
156  *  - 1 = detail actions
157  *  - 2 = full trace, tell which names and types were found
158  *  - 4 = full trace, plus all gritty details
159  */
160 int debugSDNA = 0;
161 int additional_slen_offset;
162
163 /* ************************************************************************** */
164 /* Functions                                                                  */
165 /* ************************************************************************** */
166
167 /**
168  * Add type <str> to struct indexed by <len>, if it was not yet found.
169  */
170 int add_type(char *str, int len);
171
172 /**
173  * Add variable <str> to 
174  */
175 int add_name(char *str);
176
177 /**
178  * Search whether this structure type was already found, and if not,
179  * add it.
180  */
181 short *add_struct(int namecode);
182
183 /**
184  * Remove comments from this buffer. Assumes that the buffer refers to
185  * ascii-code text.
186  */
187 int preprocess_include(char *maindata, int len);
188
189 /**
190  * Scan this file for serializable types.
191  */ 
192 int convert_include(char *filename);
193
194 /**
195  * Determine how many bytes are needed for an array.
196  */ 
197 int arraysize(char *astr, int len);
198
199 /**
200  * Determine how many bytes are needed for each struct.
201  */ 
202 static int calculate_structlens(int);
203
204 /**
205  * Construct the DNA.c file
206  */ 
207 void dna_write(FILE *file, void *pntr, int size);
208
209 /**
210  * Report all structures found so far, and print their lenghts.
211  */
212 void printStructLenghts(void);
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                 fclose(fp);
489                 return NULL;
490         }
491
492         if (fread(data, *len_r, 1, fp)!=1) {
493                 *len_r= -1;
494                 MEM_freeN(data);
495                 fclose(fp);
496                 return NULL;
497         }
498         
499         fclose(fp);
500         return data;
501 }
502
503 int convert_include(char *filename)
504 {
505         /* read include file, skip structs with a '#' before it.
506            store all data in temporal arrays.
507         */
508         int filelen, count, overslaan, slen, type, name, strct;
509         short *structpoin, *sp;
510         char *maindata, *mainend, *md, *md1;
511         
512         md= maindata= read_file_data(filename, &filelen);
513         if (filelen==-1) {
514                 printf("Can't read file %s\n", filename);
515                 return 1;
516         }
517
518         filelen= preprocess_include(maindata, filelen);
519         mainend= maindata+filelen-1;
520
521         /* we look for '{' and then back to 'struct' */
522         count= 0;
523         overslaan= 0;
524         while(count<filelen) {
525                 
526                 /* code for skipping a struct: two hashes on 2 lines. (preprocess added a space) */
527                 if(md[0]=='#' && md[1]==' ' && md[2]=='#') {
528                         overslaan= 1;
529                 }
530                 
531                 if(md[0]=='{') {
532                         md[0]= 0;
533                         if(overslaan) {
534                                 overslaan= 0;
535                         }
536                         else {
537                                 if(md[-1]==' ') md[-1]= 0;
538                                 md1= md-2;
539                                 while( *md1!=32) md1--;         /* to beginning of word */
540                                 md1++;
541                                 
542                                 /* we've got a struct name when... */
543                                 if( strncmp(md1-7, "struct", 6)==0 ) {
544
545                                         
546                                         strct= add_type(md1, 0);
547                                         structpoin= add_struct(strct);
548                                         sp= structpoin+2;
549
550                                         if (debugSDNA > 1) printf("\t|\t|-- detected struct %s\n", types[strct]);
551
552                                         /* first lets make it all nice strings */
553                                         md1= md+1;
554                                         while(*md1 != '}') {
555                                                 if(md1>mainend) break;
556                                                 
557                                                 if(*md1==',' || *md1==' ') *md1= 0;
558                                                 md1++;
559                                         }
560                                         
561                                         /* read types and names until first character that is not '}' */
562                                         md1= md+1;
563                                         while( *md1 != '}' ) {
564                                                 if(md1>mainend) break;
565                                                 
566                                                 /* skip when it says 'struct' or 'unsigned' or 'const' */
567                                                 if(*md1) {
568                                                         if( strncmp(md1, "struct", 6)==0 ) md1+= 7;
569                                                         if( strncmp(md1, "unsigned", 8)==0 ) md1+= 9;
570                                                         if( strncmp(md1, "const", 5)==0 ) md1+= 6;
571                                                         
572                                                         /* we've got a type! */
573                                                         type= add_type(md1, 0);
574
575                                                         if (debugSDNA > 1) printf("\t|\t|\tfound type %s (", md1);
576
577                                                         md1+= strlen(md1);
578
579                                                         
580                                                         /* read until ';' */
581                                                         while( *md1 != ';' ) {
582                                                                 if(md1>mainend) break;
583                                                                 
584                                                                 if(*md1) {
585                                                                         /* We've got a name. slen needs
586                                                                          * correction for function
587                                                                          * pointers! */
588                                                                         slen= (int) strlen(md1);
589                                                                         if( md1[slen-1]==';' ) {
590                                                                                 md1[slen-1]= 0;
591
592
593                                                                                 name= add_name(md1);
594                                                                                 slen += additional_slen_offset;
595                                                                                 sp[0]= type;
596                                                                                 sp[1]= name;
597
598                                                                                 if ((debugSDNA>1) && (names[name] != 0 )) printf("%s |", names[name]);
599
600                                                                                 structpoin[1]++;
601                                                                                 sp+= 2;
602                                                                                                                                                                         
603                                                                                 md1+= slen;
604                                                                                 break;
605                                                                         }
606                                                                         
607
608                                                                         name= add_name(md1);
609                                                                         slen += additional_slen_offset;
610
611                                                                         sp[0]= type;
612                                                                         sp[1]= name;
613                                                                         if ((debugSDNA > 1) && (names[name] != 0 )) printf("%s ||", names[name]);
614
615                                                                         structpoin[1]++;
616                                                                         sp+= 2;
617                                                                         
618                                                                         md1+= slen;
619                                                                 }
620                                                                 md1++;
621                                                         }
622
623                                                         if (debugSDNA > 1) printf(")\n");
624
625                                                 }
626                                                 md1++;
627                                         }
628                                 }
629                         }
630                 }
631                 count++;
632                 md++;
633         }
634         
635         MEM_freeN(maindata);
636
637         return 0;
638 }
639
640 int arraysize(char *astr, int len)
641 {
642         int a, mul=1;
643         char str[100], *cp=0;
644
645         memcpy(str, astr, len+1);
646         
647         for(a=0; a<len; a++) {
648                 if( str[a]== '[' ) {
649                         cp= &(str[a+1]);
650                 }
651                 else if( str[a]==']' && cp) {
652                         str[a]= 0;
653                         mul*= atoi(cp);
654                 }
655         }
656         
657         return mul;
658 }
659
660 static int calculate_structlens(int firststruct)
661 {
662         int a, b, len, alphalen, unknown= nr_structs, lastunknown, structtype, type, mul, namelen;
663         short *sp, *structpoin;
664         char *cp;
665         int has_pointer, dna_error = 0;
666                 
667         while(unknown) {
668                 lastunknown= unknown;
669                 unknown= 0;
670                 
671                 /* check all structs... */
672                 for(a=0; a<nr_structs; a++) {
673                         structpoin= structs[a];
674                         structtype= structpoin[0];
675
676                         /* when length is not known... */
677                         if(typelens[structtype]==0) {
678                                 
679                                 sp= structpoin+2;
680                                 len= 0;
681                                 alphalen = 0;
682                                 has_pointer = 0;
683                                 
684                                 /* check all elements in struct */
685                                 for(b=0; b<structpoin[1]; b++, sp+=2) {
686                                         type= sp[0];
687                                         cp= names[sp[1]];
688                                         
689                                         namelen= (int) strlen(cp);
690                                         /* is it a pointer or function pointer? */
691                                         if(cp[0]=='*' || cp[1]=='*') {
692                                                 has_pointer = 1;
693                                                 /* has the name an extra length? (array) */
694                                                 mul= 1;
695                                                 if( cp[namelen-1]==']') mul= arraysize(cp, namelen);
696                                                 
697                                                 /* 4-8 aligned/ */
698                                                 if(sizeof(void *) == 4) {
699                                                         if (len % 4) {
700                                                                 printf("Align pointer error in struct (len4): %s %s\n", types[structtype], cp);
701                                                                 dna_error = 1;
702                                                         }
703                                                 } else {
704                                                         if (len % 8) {
705                                                                 printf("Align pointer error in struct (len8): %s %s\n", types[structtype], cp);
706                                                                 dna_error = 1;
707                                                         }
708                                                 }
709
710                                                 if (alphalen % 8) {
711                                                         printf("Align pointer error in struct (alphalen8): %s %s\n", types[structtype],cp);
712                                                         dna_error = 1;
713                                                 }
714
715                                                 len += sizeof(void *) * mul;
716                                                 alphalen += 8 * mul;
717
718                                         } else if( typelens[type] ) {
719                                                 /* has the name an extra length? (array) */
720                                                 mul= 1;
721                                                 if( cp[namelen-1]==']') mul= arraysize(cp, namelen);
722                                                 
723                                                 /* struct alignment */
724                                                 if(type >= firststruct) {
725                                                         if(sizeof(void *)==8 && (len % 8) ) {
726                                                                 printf("Align struct error: %s %s\n", types[structtype],cp);
727                                                                 dna_error = 1;
728                                                         }
729                                                 }
730                                                 
731                                                 /* 2-4 aligned/ */
732                                                 if(typelens[type]>3 && (len % 4) ) {
733                                                         printf("Align 4 error in struct: %s %s (add %d padding bytes)\n", types[structtype], cp, len%4);
734                                                         dna_error = 1;
735                                                 }
736                                                 else if(typelens[type]==2 && (len % 2) ) {
737                                                         printf("Align 2 error in struct: %s %s (add %d padding bytes)\n", types[structtype], cp, len%2);
738                                                         dna_error = 1;
739                                                 }
740
741                                                 len += mul*typelens[type];
742                                                 alphalen += mul * alphalens[type];
743                                                 
744                                         } else {
745                                                 len= 0;
746                                                 alphalen = 0;
747                                                 break;
748                                         }
749                                 }
750                                 
751                                 if (len==0) {
752                                         unknown++;
753                                 } else {
754                                         typelens[structtype]= len;
755                                         alphalens[structtype]= alphalen;
756                                         // two ways to detect if a struct contains a pointer:
757                                         // has_pointer is set or alphalen != len
758                                         if (has_pointer || alphalen != len) {
759                                                 if (alphalen % 8) {
760                                                         printf("Sizeerror 8 in struct: %s (add %d bytes)\n", types[structtype], alphalen%8);
761                                                         dna_error = 1;
762                                                 }
763                                         }
764                                         
765                                         if(len % 4) {
766                                                 printf("Sizeerror 4 in struct: %s (add %d bytes)\n", types[structtype], len%4);
767                                                 dna_error = 1;
768                                         }
769                                         
770                                 }
771                         }
772                 }
773                 
774                 if(unknown==lastunknown) break;
775         }
776         
777         if(unknown) {
778                 printf("ERROR: still %d structs unknown\n", unknown);
779
780                 if (debugSDNA) {
781                         printf("*** Known structs : \n");
782                         
783                         for(a=0; a<nr_structs; a++) {
784                                 structpoin= structs[a];
785                                 structtype= structpoin[0];
786                                 
787                                 /* length unknown */
788                                 if(typelens[structtype]!=0) {
789                                         printf("  %s\n", types[structtype]);
790                                 }
791                         }
792                 }
793
794                         
795                 printf("*** Unknown structs : \n");
796                         
797                 for(a=0; a<nr_structs; a++) {
798                         structpoin= structs[a];
799                         structtype= structpoin[0];
800
801                         /* length unkown yet */
802                         if(typelens[structtype]==0) {
803                                 printf("  %s\n", types[structtype]);
804                         }
805                 }
806         }
807
808         return(dna_error);
809 }
810
811 #define MAX_DNA_LINE_LENGTH 20
812
813 void dna_write(FILE *file, void *pntr, int size)
814 {
815         static int linelength = 0;
816         int i;
817         char *data;
818
819         data = (char *) pntr;
820         
821         for (i = 0 ; i < size ; i++)
822         {
823                 fprintf(file, "%d,", data[i]);
824                 linelength++;
825                 if (linelength >= MAX_DNA_LINE_LENGTH) {
826                         fprintf(file, "\n");
827                         linelength = 0;
828                 }
829         }
830 }
831
832 void printStructLenghts(void)
833 {
834         int a, unknown= nr_structs, lastunknown, structtype;
835         short *structpoin;
836         printf("\n\n*** All detected structs:\n");
837
838         while(unknown) {
839                 lastunknown= unknown;
840                 unknown= 0;
841                 
842                 /* check all structs... */
843                 for(a=0; a<nr_structs; a++) {
844                         structpoin= structs[a];
845                         structtype= structpoin[0];
846                         printf("\t%s\t:%d\n", types[structtype], typelens[structtype]);
847                 }
848         }
849
850         printf("*** End of list\n");
851
852 }
853
854
855 int make_structDNA(char *baseDirectory, FILE *file)
856 {
857         int len, i;
858         short *sp;
859         /* str contains filenames. Since we now include paths, I stretched       */
860         /* it a bit. Hope this is enough :) -nzc-                                */
861         char str[SDNA_MAX_FILENAME_LENGTH], *cp;
862         int firststruct;
863         
864         if (debugSDNA > -1) {
865                 fflush(stdout);
866                 printf("Running makesdna at debug level %d\n", debugSDNA);
867                 printf("\tProgram version: %s\n", DNA_VERSION_DATE);
868         }
869                 
870         /* the longest known struct is 50k, so we assume 100k is sufficent! */
871         namedata= MEM_callocN(maxdata, "namedata");
872         typedata= MEM_callocN(maxdata, "typedata");
873         structdata= MEM_callocN(maxdata, "structdata");
874         
875         /* a maximum of 5000 variables, must be sufficient? */
876         names= MEM_callocN(sizeof(char *)*maxnr, "names");
877         types= MEM_callocN(sizeof(char *)*maxnr, "types");
878         typelens= MEM_callocN(sizeof(short)*maxnr, "typelens");
879         alphalens= MEM_callocN(sizeof(short)*maxnr, "alphalens");
880         structs= MEM_callocN(sizeof(short)*maxnr, "structs");
881
882         /* insertion of all known types */
883         /* watch it: uint is not allowed! use in structs an unsigned int */
884         add_type("char", 1);    /* 0 */
885         add_type("uchar", 1);   /* 1 */
886         add_type("short", 2);   /* 2 */
887         add_type("ushort", 2);  /* 3 */
888         add_type("int", 4);             /* 4 */
889         add_type("long", 4);    /* 5 */         /* should it be 8 on 64 bits? */
890         add_type("ulong", 4);   /* 6 */
891         add_type("float", 4);   /* 7 */
892         add_type("double", 8);  /* 8 */
893         add_type("void", 0);    /* 9 */
894
895         // the defines above shouldn't be output in the padding file...
896         firststruct = nr_types;
897         
898         /* add all include files defined in the global array                     */
899         /* Since the internal file+path name buffer has limited length, I do a   */
900         /* little test first...                                                  */
901         /* Mind the breaking condition here!                                     */
902         if (debugSDNA) printf("\tStart of header scan:\n"); 
903         for (i = 0; strlen(includefiles[i]); i++) {
904                 sprintf(str, "%s%s", baseDirectory, includefiles[i]);
905                 if (debugSDNA) printf("\t|-- Converting %s\n", str); 
906                 if (convert_include(str)) {
907                         return (1);
908                 }
909         }
910         if (debugSDNA) printf("\tFinished scanning %d headers.\n", i); 
911
912         if (calculate_structlens(firststruct)) {
913                 // error
914                 return(1);
915         }
916
917         /* FOR DEBUG */
918         if (debugSDNA > 1)
919         {
920                 int a,b;
921 /*              short *elem; */
922                 short num_types;
923
924                 printf("nr_names %d nr_types %d nr_structs %d\n", nr_names, nr_types, nr_structs);
925                 for(a=0; a<nr_names; a++) { 
926                         printf(" %s \n", names[a]);
927                 }
928                 printf("\n");
929                 
930                 sp= typelens;
931                 for(a=0; a<nr_types; a++, sp++) { 
932                         printf(" %s %d\n", types[a], *sp);
933                 }
934                 printf("\n");
935                 
936                 for(a=0; a<nr_structs; a++) {
937                         sp= structs[a];
938                         printf(" struct %s elems: %d size: %d\n", types[sp[0]], sp[1],typelens[sp[0]]);
939                         num_types  = sp[1];
940                         sp+= 2;
941                         /* ? num_types was elem? */
942                         for(b=0; b< num_types; b++, sp+= 2) {
943                                 printf("   %s %s\n", types[sp[0]], names[sp[1]]);
944                         }
945                 }
946         }
947
948         /* file writing */
949
950         if (debugSDNA > -1) printf("Writing file ... ");
951                 
952         if(nr_names==0 || nr_structs==0);
953         else {
954                 strcpy(str, "SDNA");
955                 dna_write(file, str, 4);
956                 
957                 /* write names */
958                 strcpy(str, "NAME");
959                 dna_write(file, str, 4);
960                 len= nr_names;
961                 dna_write(file, &len, 4);
962                 
963                 /* calculate size of datablock with strings */
964                 cp= names[nr_names-1];
965                 cp+= strlen(names[nr_names-1]) + 1;                     /* +1: null-terminator */
966                 len= (intptr_t) (cp - (char*) names[0]);
967                 len= (len+3) & ~3;
968                 dna_write(file, names[0], len);
969                 
970                 /* write TYPES */
971                 strcpy(str, "TYPE");
972                 dna_write(file, str, 4);
973                 len= nr_types;
974                 dna_write(file, &len, 4);
975         
976                 /* calculate datablock size */
977                 cp= types[nr_types-1];
978                 cp+= strlen(types[nr_types-1]) + 1;             /* +1: null-terminator */
979                 len= (intptr_t) (cp - (char*) types[0]);
980                 len= (len+3) & ~3;
981                 
982                 dna_write(file, types[0], len);
983                 
984                 /* WRITE TYPELENGTHS */
985                 strcpy(str, "TLEN");
986                 dna_write(file, str, 4);
987                 
988                 len= 2*nr_types;
989                 if(nr_types & 1) len+= 2;
990                 dna_write(file, typelens, len);
991                 
992                 /* WRITE STRUCTS */
993                 strcpy(str, "STRC");
994                 dna_write(file, str, 4);
995                 len= nr_structs;
996                 dna_write(file, &len, 4);
997         
998                 /* calc datablock size */
999                 sp= structs[nr_structs-1];
1000                 sp+= 2+ 2*( sp[1] );
1001                 len= (intptr_t) ((char*) sp - (char*) structs[0]);
1002                 len= (len+3) & ~3;
1003                 
1004                 dna_write(file, structs[0], len);
1005         
1006                 /* a simple dna padding test */
1007                 if (0) {
1008                         FILE *fp;
1009                         int a;
1010                         
1011                         fp= fopen("padding.c", "w");
1012                         if(fp==NULL);
1013                         else {
1014
1015                                 // add all include files defined in the global array
1016                                 for (i = 0; strlen(includefiles[i]); i++) {
1017                                         fprintf(fp, "#include \"%s%s\"\n", baseDirectory, includefiles[i]);
1018                                 }
1019
1020                                 fprintf(fp, "main(){\n");
1021                                 sp = typelens;
1022                                 sp += firststruct;
1023                                 for(a=firststruct; a<nr_types; a++, sp++) { 
1024                                         if(*sp) {
1025                                                 fprintf(fp, "\tif(sizeof(struct %s) - %d) printf(\"ALIGN ERROR:", types[a], *sp);
1026                                                 fprintf(fp, "%%d %s %d ", types[a], *sp);
1027                                                 fprintf(fp, "\\n\",  sizeof(struct %s) - %d);\n", types[a], *sp);
1028                                         }
1029                                 }
1030                                 fprintf(fp, "}\n");
1031                                 fclose(fp);
1032                         }
1033                 }
1034                 /*      end end padding test */
1035         }
1036         
1037         
1038         MEM_freeN(namedata);
1039         MEM_freeN(typedata);
1040         MEM_freeN(structdata);
1041         MEM_freeN(names);
1042         MEM_freeN(types);
1043         MEM_freeN(typelens);
1044         MEM_freeN(structs);
1045
1046         if (debugSDNA > -1) printf("done.\n");
1047         
1048         return(0);
1049 }
1050
1051 /* ************************* END MAKE DNA ********************** */
1052
1053 static void make_bad_file(char *file)
1054 {
1055         FILE *fp= fopen(file, "w");
1056         fprintf(fp, "ERROR! Cannot make correct DNA.c file, STUPID!\n");
1057         fclose(fp);
1058 }
1059
1060 #ifndef BASE_HEADER
1061 #define BASE_HEADER "../"
1062 #endif
1063
1064 int main(int argc, char ** argv)
1065 {
1066         FILE *file;
1067         int return_status = 0;
1068
1069         if (argc!=2 && argc!=3) {
1070                 printf("Usage: %s outfile.c [base directory]\n", argv[0]);
1071                 return_status = 1;
1072         } else {
1073                 file = fopen(argv[1], "w");
1074                 if (!file) {
1075                         printf ("Unable to open file: %s\n", argv[1]);
1076                         return_status = 1;
1077                 } else {
1078                         char baseDirectory[256];
1079
1080                         if (argc==3) {
1081                                 strcpy(baseDirectory, argv[2]);
1082                         } else {
1083                                 strcpy(baseDirectory, BASE_HEADER);
1084                         }
1085
1086                         fprintf (file, "unsigned char DNAstr[]= {\n");
1087                         if (make_structDNA(baseDirectory, file)) {
1088                                 // error
1089                                 fclose(file);
1090                                 make_bad_file(argv[1]);
1091                                 return_status = 1;
1092                         } else {
1093                                 fprintf(file, "};\n");
1094                                 fprintf(file, "int DNAlen= sizeof(DNAstr);\n");
1095         
1096                                 fclose(file);
1097                         }
1098                 }
1099         }
1100
1101                 
1102         return(return_status);
1103 }
1104
1105 // include files for automatic dependancies
1106 #include "DNA_listBase.h"
1107 #include "DNA_vec_types.h"
1108 #include "DNA_ID.h"
1109 #include "DNA_ipo_types.h"
1110 #include "DNA_key_types.h"
1111 #include "DNA_text_types.h"
1112 #include "DNA_packedFile_types.h"
1113 #include "DNA_camera_types.h"
1114 #include "DNA_image_types.h"
1115 #include "DNA_texture_types.h"
1116 #include "DNA_lamp_types.h"
1117 #include "DNA_wave_types.h"
1118 #include "DNA_material_types.h"
1119 #include "DNA_vfont_types.h"
1120 #include "DNA_meta_types.h"
1121 #include "DNA_curve_types.h"
1122 #include "DNA_mesh_types.h"
1123 #include "DNA_meshdata_types.h"
1124 #include "DNA_modifier_types.h"
1125 #include "DNA_lattice_types.h"  
1126 #include "DNA_object_types.h"
1127 #include "DNA_object_force.h"
1128 #include "DNA_object_fluidsim.h"
1129 #include "DNA_world_types.h"
1130 #include "DNA_scene_types.h"
1131 #include "DNA_view3d_types.h"
1132 #include "DNA_view2d_types.h"   
1133 #include "DNA_space_types.h"
1134 #include "DNA_userdef_types.h"
1135 #include "DNA_screen_types.h"
1136 #include "DNA_sdna_types.h"
1137 #include "DNA_fileglobal_types.h"
1138 #include "DNA_sequence_types.h"
1139 #include "DNA_effect_types.h"
1140 #include "DNA_outliner_types.h"
1141 #include "DNA_property_types.h"
1142 #include "DNA_sensor_types.h"
1143 #include "DNA_controller_types.h"
1144 #include "DNA_actuator_types.h"
1145 #include "DNA_sound_types.h"
1146 #include "DNA_group_types.h"
1147 #include "DNA_armature_types.h"
1148 #include "DNA_action_types.h"
1149 #include "DNA_constraint_types.h"
1150 #include "DNA_nla_types.h"
1151 #include "DNA_node_types.h"
1152 #include "DNA_color_types.h"
1153 #include "DNA_brush_types.h"
1154 #include "DNA_customdata_types.h"
1155 #include "DNA_particle_types.h"
1156 #include "DNA_cloth_types.h"
1157 #include "DNA_gpencil_types.h"
1158 #include "DNA_windowmanager_types.h"
1159 #include "DNA_anim_types.h"
1160 #include "DNA_boid_types.h"
1161 #include "DNA_smoke_types.h"
1162 /* end of list */