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