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