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