Cleanup: reduce variable scope

This commit is contained in:
Jacques Lucke 2020-09-17 14:10:32 +02:00
parent 393b5a231f
commit 82bd1287dd
1 changed files with 77 additions and 112 deletions

View File

@ -186,9 +186,8 @@ static bool ispointer(const char *name)
*/
int DNA_elem_size_nr(const SDNA *sdna, short type, short name)
{
int len;
const char *cp = sdna->names[name];
len = 0;
int len = 0;
/* is it a pointer or function pointer? */
if (ispointer(cp)) {
@ -334,10 +333,9 @@ BLI_INLINE const char *pad_up_4(const char *ptr)
*/
static bool init_structDNA(SDNA *sdna, bool do_endian_swap, const char **r_error_message)
{
int *data, gravity_fix = -1;
short *sp;
int gravity_fix = -1;
data = (int *)sdna->data;
int *data = (int *)sdna->data;
/* Clear pointers in case of error. */
sdna->names = NULL;
@ -431,6 +429,7 @@ static bool init_structDNA(SDNA *sdna, bool do_endian_swap, const char **r_error
/* Type lengths array ('TLEN') */
data = (int *)cp;
short *sp;
if (*data == MAKE_ID('T', 'L', 'E', 'N')) {
data++;
sp = (short *)data;
@ -623,22 +622,18 @@ void DNA_sdna_current_free(void)
*/
static void recurs_test_compflags(const SDNA *sdna, char *compflags, int structnr)
{
int a, b, typenr, elems;
const short *sp;
const char *cp;
/* check all structs, test if it's inside another struct */
sp = sdna->structs[structnr];
typenr = sp[0];
const short *sp = sdna->structs[structnr];
const int typenr = sp[0];
for (a = 0; a < sdna->structs_len; a++) {
for (int a = 0; a < sdna->structs_len; a++) {
if ((a != structnr) && (compflags[a] == SDNA_CMP_EQUAL)) {
sp = sdna->structs[a];
elems = sp[1];
const int elems = sp[1];
sp += 2;
for (b = 0; b < elems; b++, sp += 2) {
for (int b = 0; b < elems; b++, sp += 2) {
if (sp[0] == typenr) {
cp = sdna->names[sp[1]];
const char *cp = sdna->names[sp[1]];
if (!ispointer(cp)) {
compflags[a] = SDNA_CMP_NOT_EQUAL;
recurs_test_compflags(sdna, compflags, a);
@ -655,25 +650,20 @@ static void recurs_test_compflags(const SDNA *sdna, char *compflags, int structn
*/
const char *DNA_struct_get_compareflags(const SDNA *oldsdna, const SDNA *newsdna)
{
int a, b;
const short *sp_old, *sp_new;
const char *str1, *str2;
char *compflags;
if (oldsdna->structs_len == 0) {
printf("error: file without SDNA\n");
return NULL;
}
compflags = MEM_callocN(oldsdna->structs_len, "compflags");
char *compflags = MEM_callocN(oldsdna->structs_len, "compflags");
/* we check all structs in 'oldsdna' and compare them with
* the structs in 'newsdna'
*/
unsigned int newsdna_index_last = 0;
for (a = 0; a < oldsdna->structs_len; a++) {
sp_old = oldsdna->structs[a];
for (int a = 0; a < oldsdna->structs_len; a++) {
const short *sp_old = oldsdna->structs[a];
/* search for type in cur */
int sp_new_index = DNA_struct_find_nr_ex(
@ -683,7 +673,7 @@ const char *DNA_struct_get_compareflags(const SDNA *oldsdna, const SDNA *newsdna
newsdna_index_last++;
if (sp_new_index != -1) {
sp_new = newsdna->structs[sp_new_index];
const short *sp_new = newsdna->structs[sp_new_index];
/* initial assumption */
compflags[a] = SDNA_CMP_NOT_EQUAL;
@ -692,12 +682,12 @@ const char *DNA_struct_get_compareflags(const SDNA *oldsdna, const SDNA *newsdna
if (newsdna->types_size[sp_new[0]] == oldsdna->types_size[sp_old[0]]) {
/* same length, same amount of elems, now per type and name */
b = sp_old[1];
int b = sp_old[1];
sp_old += 2;
sp_new += 2;
while (b > 0) {
str1 = newsdna->types[sp_new[0]];
str2 = oldsdna->types[sp_old[0]];
const char *str1 = newsdna->types[sp_new[0]];
const char *str2 = oldsdna->types[sp_old[0]];
if (!STREQ(str1, str2)) {
break;
}
@ -736,7 +726,7 @@ const char *DNA_struct_get_compareflags(const SDNA *oldsdna, const SDNA *newsdna
/* Because structs can be inside structs, we recursively
* set flags when a struct is altered
*/
for (a = 0; a < oldsdna->structs_len; a++) {
for (int a = 0; a < oldsdna->structs_len; a++) {
if (compflags[a] == SDNA_CMP_NOT_EQUAL) {
recurs_test_compflags(oldsdna, compflags, a);
}
@ -913,15 +903,13 @@ static void cast_elem(
static void cast_pointer(
int curlen, int oldlen, int name_array_len, char *curdata, const char *olddata)
{
int64_t lval;
while (name_array_len > 0) {
if (curlen == oldlen) {
memcpy(curdata, olddata, curlen);
}
else if (curlen == 4 && oldlen == 8) {
lval = *((int64_t *)olddata);
int64_t lval = *((int64_t *)olddata);
/* WARNING: 32-bit Blender trying to load file saved by 64-bit Blender,
* pointers may lose uniqueness on truncation! (Hopefully this wont
@ -982,15 +970,12 @@ static bool elem_exists_impl(
const char *name,
const short *old)
{
int a, elemcount;
const char *otype, *oname;
/* in old is the old struct */
elemcount = old[1];
const int elemcount = old[1];
old += 2;
for (a = 0; a < elemcount; a++, old += 2) {
otype = types[old[0]];
oname = names[old[1]];
for (int a = 0; a < elemcount; a++, old += 2) {
const char *otype = types[old[0]];
const char *oname = names[old[1]];
if (elem_streq(name, oname)) { /* name equal */
return STREQ(type, otype); /* type equal */
@ -1053,20 +1038,17 @@ static const char *find_elem(const SDNA *sdna,
const char *olddata,
const short **sppo)
{
int a, elemcount, len;
const char *otype, *oname;
/* without arraypart, so names can differ: return old namenr and type */
/* in old is the old struct */
elemcount = old[1];
const int elemcount = old[1];
old += 2;
for (a = 0; a < elemcount; a++, old += 2) {
for (int a = 0; a < elemcount; a++, old += 2) {
otype = sdna->types[old[0]];
oname = sdna->names[old[1]];
const char *otype = sdna->types[old[0]];
const char *oname = sdna->names[old[1]];
len = DNA_elem_size_nr(sdna, old[0], old[1]);
const int len = DNA_elem_size_nr(sdna, old[0], old[1]);
if (elem_streq(name, oname)) { /* name equal */
if (STREQ(type, otype)) { /* type equal */
@ -1113,13 +1095,11 @@ static void reconstruct_elem(const SDNA *newsdna,
* (nzc 2-4-2001 I want the 'unsigned' bit to be parsed as well. Where
* can I force this?)
*/
int a, elemcount, len, countpos, mul;
const char *otype, *oname, *cp;
/* is 'name' an array? */
const char *name = newsdna->names[new_name_nr];
cp = name;
countpos = 0;
const char *cp = name;
int countpos = 0;
while (*cp && *cp != '[') {
cp++;
countpos++;
@ -1129,13 +1109,13 @@ static void reconstruct_elem(const SDNA *newsdna,
}
/* in old is the old struct */
elemcount = old[1];
const int elemcount = old[1];
old += 2;
for (a = 0; a < elemcount; a++, old += 2) {
for (int a = 0; a < elemcount; a++, old += 2) {
const int old_name_nr = old[1];
otype = oldsdna->types[old[0]];
oname = oldsdna->names[old[1]];
len = DNA_elem_size_nr(oldsdna, old[0], old[1]);
const char *otype = oldsdna->types[old[0]];
const char *oname = oldsdna->names[old[1]];
const int len = DNA_elem_size_nr(oldsdna, old[0], old[1]);
if (STREQ(name, oname)) { /* name equal */
@ -1167,8 +1147,8 @@ static void reconstruct_elem(const SDNA *newsdna,
newsdna->pointer_size, oldsdna->pointer_size, min_name_array_len, curdata, olddata);
}
else if (STREQ(type, otype)) { /* type equal */
/* size of single old array element */
mul = len / old_name_array_len;
/* size of single old array element */
int mul = len / old_name_array_len;
/* smaller of sizes of old and new arrays */
mul *= min_name_array_len;
@ -1215,15 +1195,6 @@ static void reconstruct_struct(const SDNA *newsdna,
* Per element from cur_struct, read data from old_struct.
* If element is a struct, call recursive.
*/
int a, elemcount, elen, eleno, mul, mulo, firststructtypenr;
const short *spo, *spc, *sppo;
const char *type;
const char *cpo;
char *cpc;
const char *name;
unsigned int oldsdna_index_last = UINT_MAX;
unsigned int cursdna_index_last = UINT_MAX;
if (oldSDNAnr == -1) {
return;
@ -1234,27 +1205,27 @@ static void reconstruct_struct(const SDNA *newsdna,
if (compflags[oldSDNAnr] == SDNA_CMP_EQUAL) {
/* if recursive: test for equal */
spo = oldsdna->structs[oldSDNAnr];
elen = oldsdna->types_size[spo[0]];
const short *spo = oldsdna->structs[oldSDNAnr];
const int elen = oldsdna->types_size[spo[0]];
memcpy(cur, data, elen);
return;
}
firststructtypenr = *(newsdna->structs[0]);
const int firststructtypenr = *(newsdna->structs[0]);
spo = oldsdna->structs[oldSDNAnr];
spc = newsdna->structs[curSDNAnr];
const short *spo = oldsdna->structs[oldSDNAnr];
const short *spc = newsdna->structs[curSDNAnr];
elemcount = spc[1];
const int elemcount = spc[1];
spc += 2;
cpc = cur;
for (a = 0; a < elemcount; a++, spc += 2) { /* convert each field */
type = newsdna->types[spc[0]];
name = newsdna->names[spc[1]];
char *cpc = cur;
for (int a = 0; a < elemcount; a++, spc += 2) { /* convert each field */
const char *type = newsdna->types[spc[0]];
const char *name = newsdna->names[spc[1]];
elen = DNA_elem_size_nr(newsdna, spc[0], spc[1]);
int elen = DNA_elem_size_nr(newsdna, spc[0], spc[1]);
/* Skip pad bytes which must start with '_pad', see makesdna.c 'is_name_legal'.
* for exact rules. Note that if we fail to skip a pad byte it's harmless,
@ -1266,17 +1237,20 @@ static void reconstruct_struct(const SDNA *newsdna,
/* struct field type */
/* where does the old struct data start (and is there an old one?) */
cpo = (char *)find_elem(oldsdna, type, name, spo, data, &sppo);
const short *sppo;
const char *cpo = find_elem(oldsdna, type, name, spo, data, &sppo);
if (cpo) {
unsigned int oldsdna_index_last = UINT_MAX;
unsigned int cursdna_index_last = UINT_MAX;
oldSDNAnr = DNA_struct_find_nr_ex(oldsdna, type, &oldsdna_index_last);
curSDNAnr = DNA_struct_find_nr_ex(newsdna, type, &cursdna_index_last);
/* array! */
mul = newsdna->names_array_len[spc[1]];
mulo = oldsdna->names_array_len[sppo[1]];
int mul = newsdna->names_array_len[spc[1]];
int mulo = oldsdna->names_array_len[sppo[1]];
eleno = DNA_elem_size_nr(oldsdna, sppo[0], sppo[1]);
int eleno = DNA_elem_size_nr(oldsdna, sppo[0], sppo[1]);
elen /= mul;
eleno /= mulo;
@ -1317,31 +1291,26 @@ void DNA_struct_switch_endian(const SDNA *oldsdna, int oldSDNAnr, char *data)
/* Recursive!
* If element is a struct, call recursive.
*/
int a, mul, elemcount, elen, elena, firststructtypenr;
const short *spo, *spc;
char *cur;
const char *type, *name;
unsigned int oldsdna_index_last = UINT_MAX;
if (oldSDNAnr == -1) {
return;
}
firststructtypenr = *(oldsdna->structs[0]);
const int firststructtypenr = *(oldsdna->structs[0]);
spo = spc = oldsdna->structs[oldSDNAnr];
const short *spo = oldsdna->structs[oldSDNAnr];
const short *spc = spo;
elemcount = spo[1];
const int elemcount = spo[1];
spc += 2;
cur = data;
char *cur = data;
for (a = 0; a < elemcount; a++, spc += 2) {
type = oldsdna->types[spc[0]];
name = oldsdna->names[spc[1]];
for (int a = 0; a < elemcount; a++, spc += 2) {
const char *type = oldsdna->types[spc[0]];
const char *name = oldsdna->names[spc[1]];
const int old_name_array_len = oldsdna->names_array_len[spc[1]];
/* DNA_elem_size_nr = including arraysize */
elen = DNA_elem_size_nr(oldsdna, spc[0], spc[1]);
const int elen = DNA_elem_size_nr(oldsdna, spc[0], spc[1]);
/* test: is type a struct? */
if (spc[0] >= firststructtypenr && !ispointer(name)) {
@ -1349,10 +1318,11 @@ void DNA_struct_switch_endian(const SDNA *oldsdna, int oldSDNAnr, char *data)
/* where does the old data start (is there one?) */
char *cpo = (char *)find_elem(oldsdna, type, name, spo, data, NULL);
if (cpo) {
unsigned int oldsdna_index_last = UINT_MAX;
oldSDNAnr = DNA_struct_find_nr_ex(oldsdna, type, &oldsdna_index_last);
mul = old_name_array_len;
elena = elen / mul;
int mul = old_name_array_len;
const int elena = elen / mul;
while (mul--) {
DNA_struct_switch_endian(oldsdna, oldSDNAnr, cpo);
@ -1416,31 +1386,26 @@ void *DNA_struct_reconstruct(const SDNA *newsdna,
int blocks,
const void *data)
{
int a, curSDNAnr, curlen = 0, oldlen;
const short *spo, *spc;
char *cur, *cpc;
const char *cpo;
const char *type;
/* oldSDNAnr == structnr, we're looking for the corresponding 'cur' number */
spo = oldsdna->structs[oldSDNAnr];
type = oldsdna->types[spo[0]];
oldlen = oldsdna->types_size[spo[0]];
curSDNAnr = DNA_struct_find_nr(newsdna, type);
const short *spo = oldsdna->structs[oldSDNAnr];
const char *type = oldsdna->types[spo[0]];
const int oldlen = oldsdna->types_size[spo[0]];
const int curSDNAnr = DNA_struct_find_nr(newsdna, type);
/* init data and alloc */
int curlen = 0;
if (curSDNAnr != -1) {
spc = newsdna->structs[curSDNAnr];
const short *spc = newsdna->structs[curSDNAnr];
curlen = newsdna->types_size[spc[0]];
}
if (curlen == 0) {
return NULL;
}
cur = MEM_callocN(blocks * curlen, "reconstruct");
cpc = cur;
cpo = data;
for (a = 0; a < blocks; a++) {
char *cur = MEM_callocN(blocks * curlen, "reconstruct");
char *cpc = cur;
const char *cpo = data;
for (int a = 0; a < blocks; a++) {
reconstruct_struct(newsdna, oldsdna, compflags, oldSDNAnr, cpo, curSDNAnr, cpc);
cpc += curlen;
cpo += oldlen;