Skip to content

Commit

Permalink
Handle conflicting VarDef specs in THaVarList::DefineVariables
Browse files Browse the repository at this point in the history
The original version would not detect a situation where size>1,
indicating a fixed-size array, and count!=0, indicating a
variable-size array. Admittedly, that's a user error, but the code
should still deal with it. The result was an ill-formed variable-size
array variable with an array name like "a[10]".

The fixed version prints a warning if size>0 and count!=0 and
assumes that the user wants to create a variable-size array.
Variable-size arrays should always be specified with size==0.

Additionally, the new version allows definition of std::vector
variables by giving a type like kIntV etc.

Closes #153
  • Loading branch information
hansenjo committed Mar 14, 2018
1 parent e795ce1 commit d52843a
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions src/THaVarList.C
Expand Up @@ -70,6 +70,7 @@ using namespace std;

static const Int_t kInitVarListCapacity = 100;
static const Int_t kVarListRehashLevel = 3;

//_____________________________________________________________________________
THaVarList::THaVarList() : THashList(kInitVarListCapacity, kVarListRehashLevel)
{
Expand Down Expand Up @@ -466,38 +467,33 @@ Int_t THaVarList::DefineVariables( const VarDef* list, const char* prefix,
return -1;
}

if( !prefix )
prefix = "";

const VarDef* item = list;
Int_t ndef = 0;
size_t nlen = 64;
char* name = new char[nlen];
TString name;

while( item->name ) {

const char* description = item->desc;
if( !description || !*description )
description = item->name;

size_t len = strlen( item->name ), slen = 0, plen = 0;

// size>1 means 1-d array
if( item->size>1 ) {
slen = static_cast<size_t>( TMath::Log10( item->size )) + 3;
}
if( prefix ) {
plen = strlen( prefix );
}
// Resize name string buffer if necessay
if( plen+len+slen+1 > nlen ) {
delete [] name;
name = new char[plen+len+slen+1];
nlen = plen+len+slen+1;
}
// Assemble the name string
if( plen )
strcpy( name, prefix );
strcpy( name+plen, item->name );
if( slen ) {
sprintf( name+plen+len, "[%d]", item->size );
name = prefix;
name.Append(item->name);
// size>1 means fixed-size 1-d array
bool fixed_array = (item->size > 1);
bool var_array = (item->count != 0) ||
(item->type >= kIntV && item->type <= kDoubleV);
if( item->size>0 && var_array ) {
Warning( errloc, "Variable %s: variable-size arrays must have size=0. "
"Ignoring size.", name.Data() );
} else if( fixed_array ) {
char dimstr[256];
sprintf( dimstr, "[%d]", item->size );
name.Append(dimstr);
}

// Define this variable, using the indicated type
Expand All @@ -509,7 +505,6 @@ Int_t THaVarList::DefineVariables( const VarDef* list, const char* prefix,
ndef++;
item++;
}
delete [] name;

return ndef;
}
Expand Down

0 comments on commit d52843a

Please sign in to comment.