Merge with 2.5 -r 21003:21788.
[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_scriptlink_types.h",
80         "DNA_text_types.h",
81         "DNA_packedFile_types.h",
82         "DNA_camera_types.h",
83         "DNA_image_types.h",
84         "DNA_texture_types.h",
85         "DNA_lamp_types.h",
86         "DNA_wave_types.h",
87         "DNA_material_types.h",
88         "DNA_vfont_types.h",
89         // if you add files here, please add them at the end
90         // of makesdna.c (this file) as well
91         "DNA_meta_types.h",
92         "DNA_curve_types.h",
93         "DNA_mesh_types.h",
94         "DNA_meshdata_types.h",
95         "DNA_modifier_types.h",
96         "DNA_lattice_types.h",  
97         "DNA_object_types.h",
98         "DNA_object_force.h",
99         "DNA_object_fluidsim.h",
100         "DNA_world_types.h",
101         "DNA_scene_types.h",
102         "DNA_view3d_types.h",
103         "DNA_view2d_types.h",   
104         "DNA_space_types.h",
105         "DNA_userdef_types.h",
106         "DNA_screen_types.h",
107         "DNA_sdna_types.h",
108         // if you add files here, please add them at the end
109         // of makesdna.c (this file) as well
110         "DNA_fileglobal_types.h",
111         "DNA_sequence_types.h",
112         "DNA_effect_types.h",
113         "DNA_outliner_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         "DNA_node_types.h",
125         "DNA_color_types.h",
126         "DNA_brush_types.h",
127         "DNA_customdata_types.h",
128         "DNA_particle_types.h",
129         "DNA_cloth_types.h",
130         "DNA_gpencil_types.h",
131         // if you add files here, please add them at the end
132         // of makesdna.c (this file) as well
133         "DNA_windowmanager_types.h",
134         "DNA_anim_types.h",
135         "DNA_boid_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                 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 on 2 lines. (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(md1>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(md1>mainend) break;
562                                                 
563                                                 /* skip when it says 'struct' or 'unsigned' or 'const' */
564                                                 if(*md1) {
565                                                         if( strncmp(md1, "struct", 6)==0 ) md1+= 7;
566                                                         if( strncmp(md1, "unsigned", 8)==0 ) md1+= 9;
567                                                         if( strncmp(md1, "const", 5)==0 ) md1+= 6;
568                                                         
569                                                         /* we've got a type! */
570                                                         type= add_type(md1, 0);
571
572                                                         if (debugSDNA > 1) printf("\t|\t|\tfound type %s (", md1);
573
574                                                         md1+= strlen(md1);
575
576                                                         
577                                                         /* read until ';' */
578                                                         while( *md1 != ';' ) {
579                                                                 if(md1>mainend) break;
580                                                                 
581                                                                 if(*md1) {
582                                                                         /* We've got a name. slen needs
583                                                                          * correction for function
584                                                                          * pointers! */
585                                                                         slen= (int) strlen(md1);
586                                                                         if( md1[slen-1]==';' ) {
587                                                                                 md1[slen-1]= 0;
588
589
590                                                                                 name= add_name(md1);
591                                                                                 slen += additional_slen_offset;
592                                                                                 sp[0]= type;
593                                                                                 sp[1]= name;
594
595                                                                                 if ((debugSDNA>1) && (names[name] != 0 )) printf("%s |", names[name]);
596
597                                                                                 structpoin[1]++;
598                                                                                 sp+= 2;
599                                                                                                                                                                         
600                                                                                 md1+= slen;
601                                                                                 break;
602                                                                         }
603                                                                         
604
605                                                                         name= add_name(md1);
606                                                                         slen += additional_slen_offset;
607
608                                                                         sp[0]= type;
609                                                                         sp[1]= name;
610                                                                         if ((debugSDNA > 1) && (names[name] != 0 )) printf("%s ||", names[name]);
611
612                                                                         structpoin[1]++;
613                                                                         sp+= 2;
614                                                                         
615                                                                         md1+= slen;
616                                                                 }
617                                                                 md1++;
618                                                         }
619
620                                                         if (debugSDNA > 1) printf(")\n");
621
622                                                 }
623                                                 md1++;
624                                         }
625                                 }
626                         }
627                 }
628                 count++;
629                 md++;
630         }
631         
632         MEM_freeN(maindata);
633
634         return 0;
635 }
636
637 int arraysize(char *astr, int len)
638 {
639         int a, mul=1;
640         char str[100], *cp=0;
641
642         memcpy(str, astr, len+1);
643         
644         for(a=0; a<len; a++) {
645                 if( str[a]== '[' ) {
646                         cp= &(str[a+1]);
647                 }
648                 else if( str[a]==']' && cp) {
649                         str[a]= 0;
650                         mul*= atoi(cp);
651                 }
652         }
653         
654         return mul;
655 }
656
657 static int calculate_structlens(int firststruct)
658 {
659         int a, b, len, alphalen, unknown= nr_structs, lastunknown, structtype, type, mul, namelen;
660         short *sp, *structpoin;
661         char *cp;
662         int has_pointer, dna_error = 0;
663                 
664         while(unknown) {
665                 lastunknown= unknown;
666                 unknown= 0;
667                 
668                 /* check all structs... */
669                 for(a=0; a<nr_structs; a++) {
670                         structpoin= structs[a];
671                         structtype= structpoin[0];
672
673                         /* when length is not known... */
674                         if(typelens[structtype]==0) {
675                                 
676                                 sp= structpoin+2;
677                                 len= 0;
678                                 alphalen = 0;
679                                 has_pointer = 0;
680                                 
681                                 /* check all elements in struct */
682                                 for(b=0; b<structpoin[1]; b++, sp+=2) {
683                                         type= sp[0];
684                                         cp= names[sp[1]];
685                                         
686                                         namelen= (int) strlen(cp);
687                                         /* is it a pointer or function pointer? */
688                                         if(cp[0]=='*' || cp[1]=='*') {
689                                                 has_pointer = 1;
690                                                 /* has the name an extra length? (array) */
691                                                 mul= 1;
692                                                 if( cp[namelen-1]==']') mul= arraysize(cp, namelen);
693                                                 
694                                                 /* 4-8 aligned/ */
695                                                 if(sizeof(void *) == 4) {
696                                                         if (len % 4) {
697                                                                 printf("Align pointer error in struct (len4): %s %s\n", types[structtype], cp);
698                                                                 dna_error = 1;
699                                                         }
700                                                 } else {
701                                                         if (len % 8) {
702                                                                 printf("Align pointer error in struct (len8): %s %s\n", types[structtype], cp);
703                                                                 dna_error = 1;
704                                                         }
705                                                 }
706
707                                                 if (alphalen % 8) {
708                                                         printf("Align pointer error in struct (alphalen8): %s %s\n", types[structtype],cp);
709                                                         dna_error = 1;
710                                                 }
711
712                                                 len += sizeof(void *) * mul;
713                                                 alphalen += 8 * mul;
714
715                                         } else if( typelens[type] ) {
716                                                 /* has the name an extra length? (array) */
717                                                 mul= 1;
718                                                 if( cp[namelen-1]==']') mul= arraysize(cp, namelen);
719                                                 
720                                                 /* struct alignment */
721                                                 if(type >= firststruct) {
722                                                         if(sizeof(void *)==8 && (len % 8) ) {
723                                                                 printf("Align struct error: %s %s\n", types[structtype],cp);
724                                                                 dna_error = 1;
725                                                         }
726                                                 }
727                                                 
728                                                 /* 2-4 aligned/ */
729                                                 if(typelens[type]>3 && (len % 4) ) {
730                                                         printf("Align 4 error in struct: %s %s (add %d padding bytes)\n", types[structtype], cp, len%4);
731                                                         dna_error = 1;
732                                                 }
733                                                 else if(typelens[type]==2 && (len % 2) ) {
734                                                         printf("Align 2 error in struct: %s %s (add %d padding bytes)\n", types[structtype], cp, len%2);
735                                                         dna_error = 1;
736                                                 }
737
738                                                 len += mul*typelens[type];
739                                                 alphalen += mul * alphalens[type];
740                                                 
741                                         } else {
742                                                 len= 0;
743                                                 alphalen = 0;
744                                                 break;
745                                         }
746                                 }
747                                 
748                                 if (len==0) {
749                                         unknown++;
750                                 } else {
751                                         typelens[structtype]= len;
752                                         alphalens[structtype]= alphalen;
753                                         // two ways to detect if a struct contains a pointer:
754                                         // has_pointer is set or alphalen != len
755                                         if (has_pointer || alphalen != len) {
756                                                 if (alphalen % 8) {
757                                                         printf("Sizeerror 8 in struct: %s (add %d bytes)\n", types[structtype], alphalen%8);
758                                                         dna_error = 1;
759                                                 }
760                                         }
761                                         
762                                         if(len % 4) {
763                                                 printf("Sizeerror 4 in struct: %s (add %d bytes)\n", types[structtype], len%4);
764                                                 dna_error = 1;
765                                         }
766                                         
767                                 }
768                         }
769                 }
770                 
771                 if(unknown==lastunknown) break;
772         }
773         
774         if(unknown) {
775                 printf("ERROR: still %d structs unknown\n", unknown);
776
777                 if (debugSDNA) {
778                         printf("*** Known structs : \n");
779                         
780                         for(a=0; a<nr_structs; a++) {
781                                 structpoin= structs[a];
782                                 structtype= structpoin[0];
783                                 
784                                 /* length unknown */
785                                 if(typelens[structtype]!=0) {
786                                         printf("  %s\n", types[structtype]);
787                                 }
788                         }
789                 }
790
791                         
792                 printf("*** Unknown structs : \n");
793                         
794                 for(a=0; a<nr_structs; a++) {
795                         structpoin= structs[a];
796                         structtype= structpoin[0];
797
798                         /* length unkown yet */
799                         if(typelens[structtype]==0) {
800                                 printf("  %s\n", types[structtype]);
801                         }
802                 }
803         }
804
805         return(dna_error);
806 }
807
808 #define MAX_DNA_LINE_LENGTH 20
809
810 void dna_write(FILE *file, void *pntr, int size)
811 {
812         static int linelength = 0;
813         int i;
814         char *data;
815
816         data = (char *) pntr;
817         
818         for (i = 0 ; i < size ; i++)
819         {
820                 fprintf(file, "%d,", data[i]);
821                 linelength++;
822                 if (linelength >= MAX_DNA_LINE_LENGTH) {
823                         fprintf(file, "\n");
824                         linelength = 0;
825                 }
826         }
827 }
828
829 void printStructLenghts(void)
830 {
831         int a, unknown= nr_structs, lastunknown, structtype;
832         short *structpoin;
833         printf("\n\n*** All detected structs:\n");
834
835         while(unknown) {
836                 lastunknown= unknown;
837                 unknown= 0;
838                 
839                 /* check all structs... */
840                 for(a=0; a<nr_structs; a++) {
841                         structpoin= structs[a];
842                         structtype= structpoin[0];
843                         printf("\t%s\t:%d\n", types[structtype], typelens[structtype]);
844                 }
845         }
846
847         printf("*** End of list\n");
848
849 }
850
851
852 int make_structDNA(char *baseDirectory, FILE *file)
853 {
854         int len, i;
855         short *sp;
856         /* str contains filenames. Since we now include paths, I stretched       */
857         /* it a bit. Hope this is enough :) -nzc-                                */
858         char str[SDNA_MAX_FILENAME_LENGTH], *cp;
859         int firststruct;
860         
861         if (debugSDNA > -1) {
862                 fflush(stdout);
863                 printf("Running makesdna at debug level %d\n", debugSDNA);
864                 printf("\tProgram version: %s\n", DNA_VERSION_DATE);
865         }
866                 
867         /* the longest known struct is 50k, so we assume 100k is sufficent! */
868         namedata= MEM_callocN(maxdata, "namedata");
869         typedata= MEM_callocN(maxdata, "typedata");
870         structdata= MEM_callocN(maxdata, "structdata");
871         
872         /* a maximum of 5000 variables, must be sufficient? */
873         names= MEM_callocN(sizeof(char *)*maxnr, "names");
874         types= MEM_callocN(sizeof(char *)*maxnr, "types");
875         typelens= MEM_callocN(sizeof(short)*maxnr, "typelens");
876         alphalens= MEM_callocN(sizeof(short)*maxnr, "alphalens");
877         structs= MEM_callocN(sizeof(short)*maxnr, "structs");
878
879         /* insertion of all known types */
880         /* watch it: uint is not allowed! use in structs an unsigned int */
881         add_type("char", 1);    /* 0 */
882         add_type("uchar", 1);   /* 1 */
883         add_type("short", 2);   /* 2 */
884         add_type("ushort", 2);  /* 3 */
885         add_type("int", 4);             /* 4 */
886         add_type("long", 4);    /* 5 */         /* should it be 8 on 64 bits? */
887         add_type("ulong", 4);   /* 6 */
888         add_type("float", 4);   /* 7 */
889         add_type("double", 8);  /* 8 */
890         add_type("void", 0);    /* 9 */
891
892         // the defines above shouldn't be output in the padding file...
893         firststruct = nr_types;
894         
895         /* add all include files defined in the global array                     */
896         /* Since the internal file+path name buffer has limited length, I do a   */
897         /* little test first...                                                  */
898         /* Mind the breaking condition here!                                     */
899         if (debugSDNA) printf("\tStart of header scan:\n"); 
900         for (i = 0; strlen(includefiles[i]); i++) {
901                 sprintf(str, "%s%s", baseDirectory, includefiles[i]);
902                 if (debugSDNA) printf("\t|-- Converting %s\n", str); 
903                 if (convert_include(str)) {
904                         return (1);
905                 }
906         }
907         if (debugSDNA) printf("\tFinished scanning %d headers.\n", i); 
908
909         if (calculate_structlens(firststruct)) {
910                 // error
911                 return(1);
912         }
913
914         /* FOR DEBUG */
915         if (debugSDNA > 1)
916         {
917                 int a,b;
918 /*              short *elem; */
919                 short num_types;
920
921                 printf("nr_names %d nr_types %d nr_structs %d\n", nr_names, nr_types, nr_structs);
922                 for(a=0; a<nr_names; a++) { 
923                         printf(" %s \n", names[a]);
924                 }
925                 printf("\n");
926                 
927                 sp= typelens;
928                 for(a=0; a<nr_types; a++, sp++) { 
929                         printf(" %s %d\n", types[a], *sp);
930                 }
931                 printf("\n");
932                 
933                 for(a=0; a<nr_structs; a++) {
934                         sp= structs[a];
935                         printf(" struct %s elems: %d size: %d\n", types[sp[0]], sp[1],typelens[sp[0]]);
936                         num_types  = sp[1];
937                         sp+= 2;
938                         /* ? num_types was elem? */
939                         for(b=0; b< num_types; b++, sp+= 2) {
940                                 printf("   %s %s\n", types[sp[0]], names[sp[1]]);
941                         }
942                 }
943         }
944
945         /* file writing */
946
947         if (debugSDNA > -1) printf("Writing file ... ");
948                 
949         if(nr_names==0 || nr_structs==0);
950         else {
951                 strcpy(str, "SDNA");
952                 dna_write(file, str, 4);
953                 
954                 /* write names */
955                 strcpy(str, "NAME");
956                 dna_write(file, str, 4);
957                 len= nr_names;
958                 dna_write(file, &len, 4);
959                 
960                 /* calculate size of datablock with strings */
961                 cp= names[nr_names-1];
962                 cp+= strlen(names[nr_names-1]) + 1;                     /* +1: null-terminator */
963                 len= (intptr_t) (cp - (char*) names[0]);
964                 len= (len+3) & ~3;
965                 dna_write(file, names[0], len);
966                 
967                 /* write TYPES */
968                 strcpy(str, "TYPE");
969                 dna_write(file, str, 4);
970                 len= nr_types;
971                 dna_write(file, &len, 4);
972         
973                 /* calculate datablock size */
974                 cp= types[nr_types-1];
975                 cp+= strlen(types[nr_types-1]) + 1;             /* +1: null-terminator */
976                 len= (intptr_t) (cp - (char*) types[0]);
977                 len= (len+3) & ~3;
978                 
979                 dna_write(file, types[0], len);
980                 
981                 /* WRITE TYPELENGTHS */
982                 strcpy(str, "TLEN");
983                 dna_write(file, str, 4);
984                 
985                 len= 2*nr_types;
986                 if(nr_types & 1) len+= 2;
987                 dna_write(file, typelens, len);
988                 
989                 /* WRITE STRUCTS */
990                 strcpy(str, "STRC");
991                 dna_write(file, str, 4);
992                 len= nr_structs;
993                 dna_write(file, &len, 4);
994         
995                 /* calc datablock size */
996                 sp= structs[nr_structs-1];
997                 sp+= 2+ 2*( sp[1] );
998                 len= (intptr_t) ((char*) sp - (char*) structs[0]);
999                 len= (len+3) & ~3;
1000                 
1001                 dna_write(file, structs[0], len);
1002         
1003                 /* a simple dna padding test */
1004                 if (0) {
1005                         FILE *fp;
1006                         int a;
1007                         
1008                         fp= fopen("padding.c", "w");
1009                         if(fp==NULL);
1010                         else {
1011
1012                                 // add all include files defined in the global array
1013                                 for (i = 0; strlen(includefiles[i]); i++) {
1014                                         fprintf(fp, "#include \"%s%s\"\n", baseDirectory, includefiles[i]);
1015                                 }
1016
1017                                 fprintf(fp, "main(){\n");
1018                                 sp = typelens;
1019                                 sp += firststruct;
1020                                 for(a=firststruct; a<nr_types; a++, sp++) { 
1021                                         if(*sp) {
1022                                                 fprintf(fp, "\tif(sizeof(struct %s) - %d) printf(\"ALIGN ERROR:", types[a], *sp);
1023                                                 fprintf(fp, "%%d %s %d ", types[a], *sp);
1024                                                 fprintf(fp, "\\n\",  sizeof(struct %s) - %d);\n", types[a], *sp);
1025                                         }
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 #ifndef BASE_HEADER
1058 #define BASE_HEADER "../"
1059 #endif
1060
1061 int main(int argc, char ** argv)
1062 {
1063         FILE *file;
1064         int return_status = 0;
1065
1066         if (argc!=2 && argc!=3) {
1067                 printf("Usage: %s outfile.c [base directory]\n", argv[0]);
1068                 return_status = 1;
1069         } else {
1070                 file = fopen(argv[1], "w");
1071                 if (!file) {
1072                         printf ("Unable to open file: %s\n", argv[1]);
1073                         return_status = 1;
1074                 } else {
1075                         char baseDirectory[256];
1076
1077                         if (argc==3) {
1078                                 strcpy(baseDirectory, argv[2]);
1079                         } else {
1080                                 strcpy(baseDirectory, BASE_HEADER);
1081                         }
1082
1083                         fprintf (file, "unsigned char DNAstr[]= {\n");
1084                         if (make_structDNA(baseDirectory, file)) {
1085                                 // error
1086                                 fclose(file);
1087                                 make_bad_file(argv[1]);
1088                                 return_status = 1;
1089                         } else {
1090                                 fprintf(file, "};\n");
1091                                 fprintf(file, "int DNAlen= sizeof(DNAstr);\n");
1092         
1093                                 fclose(file);
1094                         }
1095                 }
1096         }
1097
1098                 
1099         return(return_status);
1100 }
1101
1102 // include files for automatic dependancies
1103 #include "DNA_listBase.h"
1104 #include "DNA_vec_types.h"
1105 #include "DNA_ID.h"
1106 #include "DNA_ipo_types.h"
1107 #include "DNA_key_types.h"
1108 #include "DNA_scriptlink_types.h"
1109 #include "DNA_text_types.h"
1110 #include "DNA_packedFile_types.h"
1111 #include "DNA_camera_types.h"
1112 #include "DNA_image_types.h"
1113 #include "DNA_texture_types.h"
1114 #include "DNA_lamp_types.h"
1115 #include "DNA_wave_types.h"
1116 #include "DNA_material_types.h"
1117 #include "DNA_vfont_types.h"
1118 #include "DNA_meta_types.h"
1119 #include "DNA_curve_types.h"
1120 #include "DNA_mesh_types.h"
1121 #include "DNA_meshdata_types.h"
1122 #include "DNA_modifier_types.h"
1123 #include "DNA_lattice_types.h"  
1124 #include "DNA_object_types.h"
1125 #include "DNA_object_force.h"
1126 #include "DNA_object_fluidsim.h"
1127 #include "DNA_world_types.h"
1128 #include "DNA_scene_types.h"
1129 #include "DNA_view3d_types.h"
1130 #include "DNA_view2d_types.h"   
1131 #include "DNA_space_types.h"
1132 #include "DNA_userdef_types.h"
1133 #include "DNA_screen_types.h"
1134 #include "DNA_sdna_types.h"
1135 #include "DNA_fileglobal_types.h"
1136 #include "DNA_sequence_types.h"
1137 #include "DNA_effect_types.h"
1138 #include "DNA_outliner_types.h"
1139 #include "DNA_property_types.h"
1140 #include "DNA_sensor_types.h"
1141 #include "DNA_controller_types.h"
1142 #include "DNA_actuator_types.h"
1143 #include "DNA_sound_types.h"
1144 #include "DNA_group_types.h"
1145 #include "DNA_armature_types.h"
1146 #include "DNA_action_types.h"
1147 #include "DNA_constraint_types.h"
1148 #include "DNA_nla_types.h"
1149 #include "DNA_node_types.h"
1150 #include "DNA_color_types.h"
1151 #include "DNA_brush_types.h"
1152 #include "DNA_customdata_types.h"
1153 #include "DNA_particle_types.h"
1154 #include "DNA_cloth_types.h"
1155 #include "DNA_gpencil_types.h"
1156 #include "DNA_windowmanager_types.h"
1157 #include "DNA_anim_types.h"
1158 #include "DNA_boid_types.h"
1159 /* end of list */