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