diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-12-23 03:18:43 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-12-23 03:18:43 +0400 |
commit | cb61a71ff66d2f29a60ac0407d9936c8c99c6a33 (patch) | |
tree | 65e9e45ee624eca591fef907af3226e550676ecd /source | |
parent | e68f0ff3c0d5f580aaf0e4ec2b278be3c0875ba5 (diff) |
DNA genfile - de-duplicate some checks and replace magic type numbers with an enum
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/makesdna/DNA_genfile.h | 16 | ||||
-rw-r--r-- | source/blender/makesdna/intern/dna_genfile.c | 140 | ||||
-rw-r--r-- | source/blender/makesdna/intern/makesdna.c | 21 |
3 files changed, 106 insertions, 71 deletions
diff --git a/source/blender/makesdna/DNA_genfile.h b/source/blender/makesdna/DNA_genfile.h index 4f8b2343a9f..1ca669836ac 100644 --- a/source/blender/makesdna/DNA_genfile.h +++ b/source/blender/makesdna/DNA_genfile.h @@ -38,6 +38,19 @@ struct SDNA; extern unsigned char DNAstr[]; /* DNA.c */ extern int DNAlen; +typedef enum eSDNA_Type { + SDNA_TYPE_CHAR = 0, + SDNA_TYPE_UCHAR = 1, + SDNA_TYPE_SHORT = 2, + SDNA_TYPE_USHORT = 3, + SDNA_TYPE_INT = 4, + SDNA_TYPE_LONG = 5, + SDNA_TYPE_ULONG = 6, + SDNA_TYPE_FLOAT = 7, + SDNA_TYPE_DOUBLE = 8 + /* ,SDNA_TYPE_VOID = 9 */ /* nothing uses yet */ +} eSDNA_Type; + struct SDNA *DNA_sdna_from_data(void *data, int datalen, int do_endian_swap); void DNA_sdna_free(struct SDNA *sdna); @@ -49,6 +62,7 @@ void *DNA_struct_reconstruct(struct SDNA *newsdna, struct SDNA *oldsdna, char *c int DNA_elem_array_size(const char *astr, int len); int DNA_elem_offset(struct SDNA *sdna, const char *stype, const char *vartype, const char *name); -#endif +int DNA_elem_type_size(const eSDNA_Type elem_nr); +#endif diff --git a/source/blender/makesdna/intern/dna_genfile.c b/source/blender/makesdna/intern/dna_genfile.c index 0dba78ff97e..88b6fa65370 100644 --- a/source/blender/makesdna/intern/dna_genfile.c +++ b/source/blender/makesdna/intern/dna_genfile.c @@ -655,90 +655,80 @@ char *DNA_struct_get_compareflags(SDNA *sdna, SDNA *newsdna) return compflags; } -static void cast_elem(char *ctype, char *otype, const char *name, char *curdata, char *olddata) +static eSDNA_Type sdna_type_nr(const char *dna_type) +{ + if ((strcmp(dna_type, "char")==0) || (strcmp(dna_type, "const char")==0)) return SDNA_TYPE_CHAR; + else if((strcmp(dna_type, "uchar")==0) || (strcmp(dna_type, "unsigned char")==0)) return SDNA_TYPE_UCHAR; + else if( strcmp(dna_type, "short")==0) return SDNA_TYPE_SHORT; + else if((strcmp(dna_type, "ushort")==0)||(strcmp(dna_type, "unsigned short")==0)) return SDNA_TYPE_USHORT; + else if( strcmp(dna_type, "int")==0) return SDNA_TYPE_INT; + else if( strcmp(dna_type, "long")==0) return SDNA_TYPE_LONG; + else if((strcmp(dna_type, "ulong")==0)||(strcmp(dna_type, "unsigned long")==0)) return SDNA_TYPE_ULONG; + else if( strcmp(dna_type, "float")==0) return SDNA_TYPE_FLOAT; + else if( strcmp(dna_type, "double")==0) return SDNA_TYPE_DOUBLE; + else return -1; /* invalid! */ +} + +static void cast_elem(const char *ctype, const char *otype, const char *name, char *curdata, char *olddata) { double val = 0.0; - int arrlen, curlen=1, oldlen=1, ctypenr, otypenr; - + int arrlen, curlen=1, oldlen=1; + + eSDNA_Type ctypenr, otypenr; + arrlen= DNA_elem_array_size(name, strlen(name)); - - /* define otypenr */ - if(strcmp(otype, "char")==0 || (strcmp(otype, "const char")==0)) otypenr= 0; - else if((strcmp(otype, "uchar")==0) || (strcmp(otype, "unsigned char")==0)) otypenr= 1; - else if(strcmp(otype, "short")==0) otypenr= 2; - else if((strcmp(otype, "ushort")==0)||(strcmp(otype, "unsigned short")==0)) otypenr= 3; - else if(strcmp(otype, "int")==0) otypenr= 4; - else if(strcmp(otype, "long")==0) otypenr= 5; - else if((strcmp(otype, "ulong")==0)||(strcmp(otype, "unsigned long")==0)) otypenr= 6; - else if(strcmp(otype, "float")==0) otypenr= 7; - else if(strcmp(otype, "double")==0) otypenr= 8; - else return; - - /* define ctypenr */ - if(strcmp(ctype, "char")==0) ctypenr= 0; - else if(strcmp(ctype, "const char")==0) ctypenr= 0; - else if((strcmp(ctype, "uchar")==0)||(strcmp(ctype, "unsigned char")==0)) ctypenr= 1; - else if(strcmp(ctype, "short")==0) ctypenr= 2; - else if((strcmp(ctype, "ushort")==0)||(strcmp(ctype, "unsigned short")==0)) ctypenr= 3; - else if(strcmp(ctype, "int")==0) ctypenr= 4; - else if(strcmp(ctype, "long")==0) ctypenr= 5; - else if((strcmp(ctype, "ulong")==0)||(strcmp(ctype, "unsigned long")==0)) ctypenr= 6; - else if(strcmp(ctype, "float")==0) ctypenr= 7; - else if(strcmp(ctype, "double")==0) ctypenr= 8; - else return; + + if ( (otypenr= sdna_type_nr(otype)) == -1 || + (ctypenr= sdna_type_nr(ctype)) == -1 ) + { + return; + } /* define lengths */ - if(otypenr < 2) oldlen= 1; - else if(otypenr < 4) oldlen= 2; - else if(otypenr < 8) oldlen= 4; - else oldlen= 8; - - if(ctypenr < 2) curlen= 1; - else if(ctypenr < 4) curlen= 2; - else if(ctypenr < 8) curlen= 4; - else curlen= 8; - + oldlen= DNA_elem_type_size(otypenr); + curlen= DNA_elem_type_size(ctypenr); + while(arrlen>0) { switch(otypenr) { - case 0: + case SDNA_TYPE_CHAR: val= *olddata; break; - case 1: + case SDNA_TYPE_UCHAR: val= *( (unsigned char *)olddata); break; - case 2: + case SDNA_TYPE_SHORT: val= *( (short *)olddata); break; - case 3: + case SDNA_TYPE_USHORT: val= *( (unsigned short *)olddata); break; - case 4: + case SDNA_TYPE_INT: val= *( (int *)olddata); break; - case 5: + case SDNA_TYPE_LONG: val= *( (int *)olddata); break; - case 6: + case SDNA_TYPE_ULONG: val= *( (unsigned int *)olddata); break; - case 7: + case SDNA_TYPE_FLOAT: val= *( (float *)olddata); break; - case 8: + case SDNA_TYPE_DOUBLE: val= *( (double *)olddata); break; } switch(ctypenr) { - case 0: + case SDNA_TYPE_CHAR: *curdata= val; break; - case 1: + case SDNA_TYPE_UCHAR: *( (unsigned char *)curdata)= val; break; - case 2: + case SDNA_TYPE_SHORT: *( (short *)curdata)= val; break; - case 3: + case SDNA_TYPE_USHORT: *( (unsigned short *)curdata)= val; break; - case 4: + case SDNA_TYPE_INT: *( (int *)curdata)= val; break; - case 5: + case SDNA_TYPE_LONG: *( (int *)curdata)= val; break; - case 6: + case SDNA_TYPE_ULONG: *( (unsigned int *)curdata)= val; break; - case 7: + case SDNA_TYPE_FLOAT: if(otypenr<2) val/= 255; *( (float *)curdata)= val; break; - case 8: + case SDNA_TYPE_DOUBLE: if(otypenr<2) val/= 255; *( (double *)curdata)= val; break; } @@ -838,7 +828,8 @@ static char *find_elem(SDNA *sdna, const char *type, const char *name, short *ol return NULL; } -static void reconstruct_elem(SDNA *newsdna, SDNA *oldsdna, char *type, const char *name, char *curdata, short *old, char *olddata) +static void reconstruct_elem(SDNA *newsdna, SDNA *oldsdna, + char *type, const char *name, char *curdata, short *old, char *olddata) { /* rules: test for NAME: - name equal: @@ -912,7 +903,8 @@ static void reconstruct_elem(SDNA *newsdna, SDNA *oldsdna, char *type, const cha } } -static void reconstruct_struct(SDNA *newsdna, SDNA *oldsdna, char *compflags, int oldSDNAnr, char *data, int curSDNAnr, char *cur) +static void reconstruct_struct(SDNA *newsdna, SDNA *oldsdna, + char *compflags, int oldSDNAnr, char *data, int curSDNAnr, char *cur) { /* Recursive! * Per element from cur_struct, read data from old_struct. @@ -1054,7 +1046,9 @@ void DNA_struct_switch_endian(SDNA *oldsdna, int oldSDNAnr, char *data) } else { - if( spc[0]==2 || spc[0]==3 ) { /* short-ushort */ + if ( spc[0]==SDNA_TYPE_SHORT || + spc[0]==SDNA_TYPE_USHORT ) + { /* exception: variable called blocktype/ipowin: derived from ID_ */ skip= 0; @@ -1076,7 +1070,11 @@ void DNA_struct_switch_endian(SDNA *oldsdna, int oldSDNAnr, char *data) } } } - else if(spc[0]>3 && spc[0]<8) { /* int-long-ulong-float */ + else if ( (spc[0]==SDNA_TYPE_INT || + spc[0]==SDNA_TYPE_LONG || + spc[0]==SDNA_TYPE_ULONG || + spc[0]==SDNA_TYPE_FLOAT)) + { mul= DNA_elem_array_size(name, strlen(name)); cpo= cur; @@ -1138,3 +1136,25 @@ int DNA_elem_offset(SDNA *sdna, const char *stype, const char *vartype, const ch return (int)((intptr_t)cp); } +int DNA_elem_type_size(const eSDNA_Type elem_nr) +{ + /* should containt all enum types */ + switch (elem_nr) { + case SDNA_TYPE_CHAR: + case SDNA_TYPE_UCHAR: + return 1; + case SDNA_TYPE_SHORT: + case SDNA_TYPE_USHORT: + return 2; + case SDNA_TYPE_INT: + case SDNA_TYPE_LONG: + case SDNA_TYPE_ULONG: + case SDNA_TYPE_FLOAT: + return 4; + case SDNA_TYPE_DOUBLE: + return 8; + } + + /* weak */ + return 8; +} diff --git a/source/blender/makesdna/intern/makesdna.c b/source/blender/makesdna/intern/makesdna.c index 11ab9b1efcf..e3f62fa0f4d 100644 --- a/source/blender/makesdna/intern/makesdna.c +++ b/source/blender/makesdna/intern/makesdna.c @@ -928,16 +928,17 @@ static int make_structDNA(char *baseDirectory, FILE *file) /* insertion of all known types */ /* watch it: uint is not allowed! use in structs an unsigned int */ - add_type("char", 1); /* 0 */ - add_type("uchar", 1); /* 1 */ - add_type("short", 2); /* 2 */ - add_type("ushort", 2); /* 3 */ - add_type("int", 4); /* 4 */ - add_type("long", 4); /* 5 */ /* should it be 8 on 64 bits? */ - add_type("ulong", 4); /* 6 */ - add_type("float", 4); /* 7 */ - add_type("double", 8); /* 8 */ - add_type("void", 0); /* 9 */ + /* watch it: sizes must match DNA_elem_type_size() */ + add_type("char", 1); /* SDNA_TYPE_CHAR */ + add_type("uchar", 1); /* SDNA_TYPE_UCHAR */ + add_type("short", 2); /* SDNA_TYPE_SHORT */ + add_type("ushort", 2); /* SDNA_TYPE_USHORT */ + add_type("int", 4); /* SDNA_TYPE_INT */ + add_type("long", 4); /* SDNA_TYPE_LONG */ /* should it be 8 on 64 bits? */ + add_type("ulong", 4); /* SDNA_TYPE_ULONG */ + add_type("float", 4); /* SDNA_TYPE_FLOAT */ + add_type("double", 8); /* SDNA_TYPE_DOUBLE */ + add_type("void", 0); /* SDNA_TYPE_VOID */ // the defines above shouldn't be output in the padding file... firststruct = nr_types; |