include cleanup, no functional changes
[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., 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 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(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(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         temp= MEM_mallocN(len, "preprocess_include");
425         memcpy(temp, maindata, len);
426         
427         // remove all c++ comments
428         /* replace all enters/tabs/etc with spaces */
429         cp= temp;
430         a= len;
431         comment = 0;
432         while(a--) {
433                 if(cp[0]=='/' && cp[1]=='/') {
434                         comment = 1;
435                 } else if (*cp<32) {
436                         comment = 0;
437                 }
438                 if (comment || *cp<32 || *cp>128 ) *cp= 32;
439                 cp++;
440         }
441         
442
443         /* data from temp copy to maindata, remove comments and double spaces */
444         cp= temp;
445         md= maindata;
446         newlen= 0;
447         comment= 0;
448         a= len;
449         while(a--) {
450                 
451                 if(cp[0]=='/' && cp[1]=='*') {
452                         comment= 1;
453                         cp[0]=cp[1]= 32;
454                 }
455                 if(cp[0]=='*' && cp[1]=='/') {
456                         comment= 0;
457                         cp[0]=cp[1]= 32;
458                 }
459
460                 /* do not copy when: */
461                 if(comment);
462                 else if( cp[0]==' ' && cp[1]==' ' );
463                 else if( cp[-1]=='*' && cp[0]==' ' );   /* pointers with a space */
464                 else {
465                         md[0]= cp[0];
466                         md++;
467                         newlen++;
468                 }
469                 cp++;
470         }
471         
472         MEM_freeN(temp);
473         return newlen;
474 }
475
476 static void *read_file_data(char *filename, int *len_r)
477 {
478 #ifdef WIN32
479         FILE *fp= fopen(filename, "rb");
480 #else
481         FILE *fp= fopen(filename, "r");
482 #endif
483         void *data;
484
485         if (!fp) {
486                 *len_r= -1;
487                 return NULL;
488         }
489
490         fseek(fp, 0L, SEEK_END);
491         *len_r= ftell(fp);
492         fseek(fp, 0L, SEEK_SET);
493
494         data= MEM_mallocN(*len_r, "read_file_data");
495         if (!data) {
496                 *len_r= -1;
497                 fclose(fp);
498                 return NULL;
499         }
500
501         if (fread(data, *len_r, 1, fp)!=1) {
502                 *len_r= -1;
503                 MEM_freeN(data);
504                 fclose(fp);
505                 return NULL;
506         }
507         
508         fclose(fp);
509         return data;
510 }
511
512 int convert_include(char *filename)
513 {
514         /* read include file, skip structs with a '#' before it.
515            store all data in temporal arrays.
516         */
517         int filelen, count, overslaan, slen, type, name, strct;
518         short *structpoin, *sp;
519         char *maindata, *mainend, *md, *md1;
520         
521         md= maindata= read_file_data(filename, &filelen);
522         if (filelen==-1) {
523                 printf("Can't read file %s\n", filename);
524                 return 1;
525         }
526
527         filelen= preprocess_include(maindata, filelen);
528         mainend= maindata+filelen-1;
529
530         /* we look for '{' and then back to 'struct' */
531         count= 0;
532         overslaan= 0;
533         while(count<filelen) {
534                 
535                 /* code for skipping a struct: two hashes on 2 lines. (preprocess added a space) */
536                 if(md[0]=='#' && md[1]==' ' && md[2]=='#') {
537                         overslaan= 1;
538                 }
539                 
540                 if(md[0]=='{') {
541                         md[0]= 0;
542                         if(overslaan) {
543                                 overslaan= 0;
544                         }
545                         else {
546                                 if(md[-1]==' ') md[-1]= 0;
547                                 md1= md-2;
548                                 while( *md1!=32) md1--;         /* to beginning of word */
549                                 md1++;
550                                 
551                                 /* we've got a struct name when... */
552                                 if( strncmp(md1-7, "struct", 6)==0 ) {
553
554                                         
555                                         strct= add_type(md1, 0);
556                                         structpoin= add_struct(strct);
557                                         sp= structpoin+2;
558
559                                         if (debugSDNA > 1) printf("\t|\t|-- detected struct %s\n", types[strct]);
560
561                                         /* first lets make it all nice strings */
562                                         md1= md+1;
563                                         while(*md1 != '}') {
564                                                 if(md1>mainend) break;
565                                                 
566                                                 if(*md1==',' || *md1==' ') *md1= 0;
567                                                 md1++;
568                                         }
569                                         
570                                         /* read types and names until first character that is not '}' */
571                                         md1= md+1;
572                                         while( *md1 != '}' ) {
573                                                 if(md1>mainend) break;
574                                                 
575                                                 /* skip when it says 'struct' or 'unsigned' or 'const' */
576                                                 if(*md1) {
577                                                         if( strncmp(md1, "struct", 6)==0 ) md1+= 7;
578                                                         if( strncmp(md1, "unsigned", 8)==0 ) md1+= 9;
579                                                         if( strncmp(md1, "const", 5)==0 ) md1+= 6;
580                                                         
581                                                         /* we've got a type! */
582                                                         type= add_type(md1, 0);
583
584                                                         if (debugSDNA > 1) printf("\t|\t|\tfound type %s (", md1);
585
586                                                         md1+= strlen(md1);
587
588                                                         
589                                                         /* read until ';' */
590                                                         while( *md1 != ';' ) {
591                                                                 if(md1>mainend) break;
592                                                                 
593                                                                 if(*md1) {
594                                                                         /* We've got a name. slen needs
595                                                                          * correction for function
596                                                                          * pointers! */
597                                                                         slen= (int) strlen(md1);
598                                                                         if( md1[slen-1]==';' ) {
599                                                                                 md1[slen-1]= 0;
600
601
602                                                                                 name= add_name(md1);
603                                                                                 slen += additional_slen_offset;
604                                                                                 sp[0]= type;
605                                                                                 sp[1]= name;
606
607                                                                                 if ((debugSDNA>1) && (names[name] != 0 )) printf("%s |", names[name]);
608
609                                                                                 structpoin[1]++;
610                                                                                 sp+= 2;
611                                                                                                                                                                         
612                                                                                 md1+= slen;
613                                                                                 break;
614                                                                         }
615                                                                         
616
617                                                                         name= add_name(md1);
618                                                                         slen += additional_slen_offset;
619
620                                                                         sp[0]= type;
621                                                                         sp[1]= name;
622                                                                         if ((debugSDNA > 1) && (names[name] != 0 )) printf("%s ||", names[name]);
623
624                                                                         structpoin[1]++;
625                                                                         sp+= 2;
626                                                                         
627                                                                         md1+= slen;
628                                                                 }
629                                                                 md1++;
630                                                         }
631
632                                                         if (debugSDNA > 1) printf(")\n");
633
634                                                 }
635                                                 md1++;
636                                         }
637                                 }
638                         }
639                 }
640                 count++;
641                 md++;
642         }
643         
644         MEM_freeN(maindata);
645
646         return 0;
647 }
648
649 int arraysize(char *astr, int len)
650 {
651         int a, mul=1;
652         char str[100], *cp=0;
653
654         memcpy(str, astr, len+1);
655         
656         for(a=0; a<len; a++) {
657                 if( str[a]== '[' ) {
658                         cp= &(str[a+1]);
659                 }
660                 else if( str[a]==']' && cp) {
661                         str[a]= 0;
662                         mul*= atoi(cp);
663                 }
664         }
665         
666         return mul;
667 }
668
669 static int calculate_structlens(int firststruct)
670 {
671         int a, b, len, alphalen, unknown= nr_structs, lastunknown, structtype, type, mul, namelen;
672         short *sp, *structpoin;
673         char *cp;
674         int has_pointer, dna_error = 0;
675                 
676         while(unknown) {
677                 lastunknown= unknown;
678                 unknown= 0;
679                 
680                 /* check all structs... */
681                 for(a=0; a<nr_structs; a++) {
682                         structpoin= structs[a];
683                         structtype= structpoin[0];
684
685                         /* when length is not known... */
686                         if(typelens[structtype]==0) {
687                                 
688                                 sp= structpoin+2;
689                                 len= 0;
690                                 alphalen = 0;
691                                 has_pointer = 0;
692                                 
693                                 /* check all elements in struct */
694                                 for(b=0; b<structpoin[1]; b++, sp+=2) {
695                                         type= sp[0];
696                                         cp= names[sp[1]];
697
698                                         namelen= (int) strlen(cp);
699                                         /* is it a pointer or function pointer? */
700                                         if(cp[0]=='*' || cp[1]=='*') {
701                                                 has_pointer = 1;
702                                                 /* has the name an extra length? (array) */
703                                                 mul= 1;
704                                                 if( cp[namelen-1]==']') mul= arraysize(cp, namelen);
705                                                 
706                                                 /* 4-8 aligned/ */
707                                                 if(sizeof(void *) == 4) {
708                                                         if (len % 4) {
709                                                                 printf("Align pointer error in struct (len4): %s %s\n", types[structtype], cp);
710                                                                 dna_error = 1;
711                                                         }
712                                                 } else {
713                                                         if (len % 8) {
714                                                                 printf("Align pointer error in struct (len8): %s %s\n", types[structtype], cp);
715                                                                 dna_error = 1;
716                                                         }
717                                                 }
718
719                                                 if (alphalen % 8) {
720                                                         printf("Align pointer error in struct (alphalen8): %s %s\n", types[structtype],cp);
721                                                         dna_error = 1;
722                                                 }
723
724                                                 len += sizeof(void *) * mul;
725                                                 alphalen += 8 * mul;
726
727                                         } else if(cp[0]=='[') {
728                                                 /* parsing can cause names "var" and "[3]" to be found for "float var [3]" ... */
729                                                 printf("Parse error in struct, invalid member name: %s %s\n", types[structtype], cp);
730                                                 dna_error = 1;
731                                         } else if( typelens[type] ) {
732                                                 /* has the name an extra length? (array) */
733                                                 mul= 1;
734                                                 if( cp[namelen-1]==']') mul= arraysize(cp, namelen);
735                                                 
736                                                 /* struct alignment */
737                                                 if(type >= firststruct) {
738                                                         if(sizeof(void *)==8 && (len % 8) ) {
739                                                                 printf("Align struct error: %s %s\n", types[structtype],cp);
740                                                                 dna_error = 1;
741                                                         }
742                                                 }
743                                                 
744                                                 /* 2-4 aligned/ */
745                                                 if(typelens[type]>3 && (len % 4) ) {
746                                                         printf("Align 4 error in struct: %s %s (add %d padding bytes)\n", types[structtype], cp, len%4);
747                                                         dna_error = 1;
748                                                 }
749                                                 else if(typelens[type]==2 && (len % 2) ) {
750                                                         printf("Align 2 error in struct: %s %s (add %d padding bytes)\n", types[structtype], cp, len%2);
751                                                         dna_error = 1;
752                                                 }
753
754                                                 len += mul*typelens[type];
755                                                 alphalen += mul * alphalens[type];
756                                                 
757                                         } else {
758                                                 len= 0;
759                                                 alphalen = 0;
760                                                 break;
761                                         }
762                                 }
763                                 
764                                 if (len==0) {
765                                         unknown++;
766                                 } else {
767                                         typelens[structtype]= len;
768                                         alphalens[structtype]= alphalen;
769                                         // two ways to detect if a struct contains a pointer:
770                                         // has_pointer is set or alphalen != len
771                                         if (has_pointer || alphalen != len) {
772                                                 if (alphalen % 8) {
773                                                         printf("Sizeerror 8 in struct: %s (add %d bytes)\n", types[structtype], alphalen%8);
774                                                         dna_error = 1;
775                                                 }
776                                         }
777                                         
778                                         if(len % 4) {
779                                                 printf("Sizeerror 4 in struct: %s (add %d bytes)\n", types[structtype], len%4);
780                                                 dna_error = 1;
781                                         }
782                                         
783                                 }
784                         }
785                 }
786                 
787                 if(unknown==lastunknown) break;
788         }
789         
790         if(unknown) {
791                 printf("ERROR: still %d structs unknown\n", unknown);
792
793                 if (debugSDNA) {
794                         printf("*** Known structs : \n");
795                         
796                         for(a=0; a<nr_structs; a++) {
797                                 structpoin= structs[a];
798                                 structtype= structpoin[0];
799                                 
800                                 /* length unknown */
801                                 if(typelens[structtype]!=0) {
802                                         printf("  %s\n", types[structtype]);
803                                 }
804                         }
805                 }
806
807                         
808                 printf("*** Unknown structs : \n");
809                         
810                 for(a=0; a<nr_structs; a++) {
811                         structpoin= structs[a];
812                         structtype= structpoin[0];
813
814                         /* length unkown yet */
815                         if(typelens[structtype]==0) {
816                                 printf("  %s\n", types[structtype]);
817                         }
818                 }
819         }
820
821         return(dna_error);
822 }
823
824 #define MAX_DNA_LINE_LENGTH 20
825
826 void dna_write(FILE *file, void *pntr, int size)
827 {
828         static int linelength = 0;
829         int i;
830         char *data;
831
832         data = (char *) pntr;
833         
834         for (i = 0 ; i < size ; i++)
835         {
836                 fprintf(file, "%d,", data[i]);
837                 linelength++;
838                 if (linelength >= MAX_DNA_LINE_LENGTH) {
839                         fprintf(file, "\n");
840                         linelength = 0;
841                 }
842         }
843 }
844
845 void printStructLenghts(void)
846 {
847         int a, unknown= nr_structs, lastunknown, structtype;
848         short *structpoin;
849         printf("\n\n*** All detected structs:\n");
850
851         while(unknown) {
852                 lastunknown= unknown;
853                 unknown= 0;
854                 
855                 /* check all structs... */
856                 for(a=0; a<nr_structs; a++) {
857                         structpoin= structs[a];
858                         structtype= structpoin[0];
859                         printf("\t%s\t:%d\n", types[structtype], typelens[structtype]);
860                 }
861         }
862
863         printf("*** End of list\n");
864
865 }
866
867
868 int make_structDNA(char *baseDirectory, FILE *file)
869 {
870         int len, i;
871         short *sp;
872         /* str contains filenames. Since we now include paths, I stretched       */
873         /* it a bit. Hope this is enough :) -nzc-                                */
874         char str[SDNA_MAX_FILENAME_LENGTH], *cp;
875         int firststruct;
876         
877         if (debugSDNA > -1) {
878                 fflush(stdout);
879                 printf("Running makesdna at debug level %d\n", debugSDNA);
880                 printf("\tProgram version: %s\n", DNA_VERSION_DATE);
881         }
882                 
883         /* the longest known struct is 50k, so we assume 100k is sufficent! */
884         namedata= MEM_callocN(maxdata, "namedata");
885         typedata= MEM_callocN(maxdata, "typedata");
886         structdata= MEM_callocN(maxdata, "structdata");
887         
888         /* a maximum of 5000 variables, must be sufficient? */
889         names= MEM_callocN(sizeof(char *)*maxnr, "names");
890         types= MEM_callocN(sizeof(char *)*maxnr, "types");
891         typelens= MEM_callocN(sizeof(short)*maxnr, "typelens");
892         alphalens= MEM_callocN(sizeof(short)*maxnr, "alphalens");
893         structs= MEM_callocN(sizeof(short)*maxnr, "structs");
894
895         /* insertion of all known types */
896         /* watch it: uint is not allowed! use in structs an unsigned int */
897         add_type("char", 1);    /* 0 */
898         add_type("uchar", 1);   /* 1 */
899         add_type("short", 2);   /* 2 */
900         add_type("ushort", 2);  /* 3 */
901         add_type("int", 4);             /* 4 */
902         add_type("long", 4);    /* 5 */         /* should it be 8 on 64 bits? */
903         add_type("ulong", 4);   /* 6 */
904         add_type("float", 4);   /* 7 */
905         add_type("double", 8);  /* 8 */
906         add_type("void", 0);    /* 9 */
907
908         // the defines above shouldn't be output in the padding file...
909         firststruct = nr_types;
910         
911         /* add all include files defined in the global array                     */
912         /* Since the internal file+path name buffer has limited length, I do a   */
913         /* little test first...                                                  */
914         /* Mind the breaking condition here!                                     */
915         if (debugSDNA) printf("\tStart of header scan:\n"); 
916         for (i = 0; strlen(includefiles[i]); i++) {
917                 sprintf(str, "%s%s", baseDirectory, includefiles[i]);
918                   if (debugSDNA) printf("\t|-- Converting %s\n", str); 
919                 if (convert_include(str)) {
920                         return (1);
921                 }
922         }
923         if (debugSDNA) printf("\tFinished scanning %d headers.\n", i); 
924
925         if (calculate_structlens(firststruct)) {
926                 // error
927                 return(1);
928         }
929
930         /* FOR DEBUG */
931         if (debugSDNA > 1)
932         {
933                 int a,b;
934 /*              short *elem; */
935                 short num_types;
936
937                 printf("nr_names %d nr_types %d nr_structs %d\n", nr_names, nr_types, nr_structs);
938                 for(a=0; a<nr_names; a++) { 
939                         printf(" %s \n", names[a]);
940                 }
941                 printf("\n");
942                 
943                 sp= typelens;
944                 for(a=0; a<nr_types; a++, sp++) { 
945                         printf(" %s %d\n", types[a], *sp);
946                 }
947                 printf("\n");
948                 
949                 for(a=0; a<nr_structs; a++) {
950                         sp= structs[a];
951                         printf(" struct %s elems: %d size: %d\n", types[sp[0]], sp[1],typelens[sp[0]]);
952                         num_types  = sp[1];
953                         sp+= 2;
954                         /* ? num_types was elem? */
955                         for(b=0; b< num_types; b++, sp+= 2) {
956                                 printf("   %s %s\n", types[sp[0]], names[sp[1]]);
957                         }
958                 }
959         }
960
961         /* file writing */
962
963         if (debugSDNA > -1) printf("Writing file ... ");
964                 
965         if(nr_names==0 || nr_structs==0);
966         else {
967                 strcpy(str, "SDNA");
968                 dna_write(file, str, 4);
969                 
970                 /* write names */
971                 strcpy(str, "NAME");
972                 dna_write(file, str, 4);
973                 len= nr_names;
974                 dna_write(file, &len, 4);
975                 
976                 /* calculate size of datablock with strings */
977                 cp= names[nr_names-1];
978                 cp+= strlen(names[nr_names-1]) + 1;                     /* +1: null-terminator */
979                 len= (intptr_t) (cp - (char*) names[0]);
980                 len= (len+3) & ~3;
981                 dna_write(file, names[0], len);
982                 
983                 /* write TYPES */
984                 strcpy(str, "TYPE");
985                 dna_write(file, str, 4);
986                 len= nr_types;
987                 dna_write(file, &len, 4);
988         
989                 /* calculate datablock size */
990                 cp= types[nr_types-1];
991                 cp+= strlen(types[nr_types-1]) + 1;             /* +1: null-terminator */
992                 len= (intptr_t) (cp - (char*) types[0]);
993                 len= (len+3) & ~3;
994                 
995                 dna_write(file, types[0], len);
996                 
997                 /* WRITE TYPELENGTHS */
998                 strcpy(str, "TLEN");
999                 dna_write(file, str, 4);
1000                 
1001                 len= 2*nr_types;
1002                 if(nr_types & 1) len+= 2;
1003                 dna_write(file, typelens, len);
1004                 
1005                 /* WRITE STRUCTS */
1006                 strcpy(str, "STRC");
1007                 dna_write(file, str, 4);
1008                 len= nr_structs;
1009                 dna_write(file, &len, 4);
1010         
1011                 /* calc datablock size */
1012                 sp= structs[nr_structs-1];
1013                 sp+= 2+ 2*( sp[1] );
1014                 len= (intptr_t) ((char*) sp - (char*) structs[0]);
1015                 len= (len+3) & ~3;
1016                 
1017                 dna_write(file, structs[0], len);
1018         
1019                 /* a simple dna padding test */
1020                 if (0) {
1021                         FILE *fp;
1022                         int a;
1023                         
1024                         fp= fopen("padding.c", "w");
1025                         if(fp==NULL);
1026                         else {
1027
1028                                 // add all include files defined in the global array
1029                                 for (i = 0; strlen(includefiles[i]); i++) {
1030                                         fprintf(fp, "#include \"%s%s\"\n", baseDirectory, includefiles[i]);
1031                                 }
1032
1033                                 fprintf(fp, "main(){\n");
1034                                 sp = typelens;
1035                                 sp += firststruct;
1036                                 for(a=firststruct; a<nr_types; a++, sp++) { 
1037                                         if(*sp) {
1038                                                 fprintf(fp, "\tif(sizeof(struct %s) - %d) printf(\"ALIGN ERROR:", types[a], *sp);
1039                                                 fprintf(fp, "%%d %s %d ", types[a], *sp);
1040                                                 fprintf(fp, "\\n\",  sizeof(struct %s) - %d);\n", types[a], *sp);
1041                                         }
1042                                 }
1043                                 fprintf(fp, "}\n");
1044                                 fclose(fp);
1045                         }
1046                 }
1047                 /*      end end padding test */
1048         }
1049         
1050         
1051         MEM_freeN(namedata);
1052         MEM_freeN(typedata);
1053         MEM_freeN(structdata);
1054         MEM_freeN(names);
1055         MEM_freeN(types);
1056         MEM_freeN(typelens);
1057         MEM_freeN(structs);
1058
1059         if (debugSDNA > -1) printf("done.\n");
1060         
1061         return(0);
1062 }
1063
1064 /* ************************* END MAKE DNA ********************** */
1065
1066 static void make_bad_file(char *file, int line)
1067 {
1068         FILE *fp= fopen(file, "w");
1069         fprintf(fp, "#error \"Error! can't make correct DNA.c file from %s:%d, STUPID!\"\n", __FILE__, line);
1070         fclose(fp);
1071 }
1072
1073 #ifndef BASE_HEADER
1074 #define BASE_HEADER "../"
1075 #endif
1076
1077 int main(int argc, char ** argv)
1078 {
1079         FILE *file;
1080         int return_status = 0;
1081
1082         if (argc!=2 && argc!=3) {
1083                 printf("Usage: %s outfile.c [base directory]\n", argv[0]);
1084                 return_status = 1;
1085         } else {
1086                 file = fopen(argv[1], "w");
1087                 if (!file) {
1088                         printf ("Unable to open file: %s\n", argv[1]);
1089                         return_status = 1;
1090                 } else {
1091                         char baseDirectory[256];
1092
1093                         if (argc==3) {
1094                                 strcpy(baseDirectory, argv[2]);
1095                         } else {
1096                                 strcpy(baseDirectory, BASE_HEADER);
1097                         }
1098
1099                         fprintf (file, "unsigned char DNAstr[]= {\n");
1100                         if (make_structDNA(baseDirectory, file)) {
1101                                 // error
1102                                 fclose(file);
1103                                 make_bad_file(argv[1], __LINE__);
1104                                 return_status = 1;
1105                         } else {
1106                                 fprintf(file, "};\n");
1107                                 fprintf(file, "int DNAlen= sizeof(DNAstr);\n");
1108         
1109                                 fclose(file);
1110                         }
1111                 }
1112         }
1113
1114                 
1115         return(return_status);
1116 }
1117
1118 // include files for automatic dependancies
1119 #include "DNA_listBase.h"
1120 #include "DNA_vec_types.h"
1121 #include "DNA_ID.h"
1122 #include "DNA_ipo_types.h"
1123 #include "DNA_key_types.h"
1124 #include "DNA_text_types.h"
1125 #include "DNA_packedFile_types.h"
1126 #include "DNA_camera_types.h"
1127 #include "DNA_image_types.h"
1128 #include "DNA_texture_types.h"
1129 #include "DNA_lamp_types.h"
1130 #include "DNA_material_types.h"
1131 #include "DNA_vfont_types.h"
1132 #include "DNA_meta_types.h"
1133 #include "DNA_curve_types.h"
1134 #include "DNA_mesh_types.h"
1135 #include "DNA_meshdata_types.h"
1136 #include "DNA_modifier_types.h"
1137 #include "DNA_lattice_types.h"  
1138 #include "DNA_object_types.h"
1139 #include "DNA_object_force.h"
1140 #include "DNA_object_fluidsim.h"
1141 #include "DNA_world_types.h"
1142 #include "DNA_scene_types.h"
1143 #include "DNA_view3d_types.h"
1144 #include "DNA_view2d_types.h"   
1145 #include "DNA_space_types.h"
1146 #include "DNA_userdef_types.h"
1147 #include "DNA_screen_types.h"
1148 #include "DNA_sdna_types.h"
1149 #include "DNA_fileglobal_types.h"
1150 #include "DNA_sequence_types.h"
1151 #include "DNA_effect_types.h"
1152 #include "DNA_outliner_types.h"
1153 #include "DNA_property_types.h"
1154 #include "DNA_sensor_types.h"
1155 #include "DNA_controller_types.h"
1156 #include "DNA_actuator_types.h"
1157 #include "DNA_sound_types.h"
1158 #include "DNA_group_types.h"
1159 #include "DNA_armature_types.h"
1160 #include "DNA_action_types.h"
1161 #include "DNA_constraint_types.h"
1162 #include "DNA_nla_types.h"
1163 #include "DNA_node_types.h"
1164 #include "DNA_color_types.h"
1165 #include "DNA_brush_types.h"
1166 #include "DNA_customdata_types.h"
1167 #include "DNA_particle_types.h"
1168 #include "DNA_cloth_types.h"
1169 #include "DNA_gpencil_types.h"
1170 #include "DNA_windowmanager_types.h"
1171 #include "DNA_anim_types.h"
1172 #include "DNA_boid_types.h"
1173 #include "DNA_smoke_types.h"
1174 /* end of list */