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