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