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