cwCommonImpl.h/cpp idToLabel() now returns label associated with 'eolId' instead of nullptr when the requested id is not found.

Added idToLabelNull() which return nullptr when the requested id is not found.
This commit is contained in:
kevin 2024-05-06 15:41:18 -04:00
parent 94dac23cdf
commit 2395feb4a7
2 changed files with 25 additions and 10 deletions

View File

@ -13,16 +13,32 @@ namespace cw
nanosleep(ts,NULL);
}
const idLabelPair_t* _idToSlot( const idLabelPair_t* array, unsigned id, unsigned eolId )
{
const idLabelPair_t* p = array;
for(; p->id != eolId; ++p)
if( p->id == id )
break;
return p;
}
}
const char* cw::idToLabelNull( const idLabelPair_t* array, unsigned id, unsigned eolId )
{
const idLabelPair_t* p = _idToSlot(array,id,eolId);
return p->id == eolId ? nullptr : p->label;
}
const char* cw::idToLabel( const idLabelPair_t* array, unsigned id, unsigned eolId )
{
const idLabelPair_t* p = array;
for(; p->id != eolId; ++p)
if( p->id == id )
return p->label;
const idLabelPair_t* p = _idToSlot(array,id,eolId);
return nullptr;
return p->label;
}
unsigned cw::labelToId( const idLabelPair_t* array, const char* label, unsigned eolId )
@ -31,16 +47,12 @@ unsigned cw::labelToId( const idLabelPair_t* array, const char* label, unsigned
if( label != nullptr )
for(; p->id != eolId; ++p)
if( std::strcmp(label,p->label) == 0 )
if( p->label != nullptr && std::strcmp(label,p->label) == 0 )
return p->id;
return eolId;
}
void cw::sleepSec( unsigned secs )
{
struct timespec ts;

View File

@ -160,6 +160,9 @@ namespace cw
} idLabelPair_t;
// Return nullptr if id is not found.
const char* idToLabelNull( const idLabelPair_t* array, unsigned id, unsigned eolId );
// Returns label in 'eolId' slot if id is not found.
const char* idToLabel( const idLabelPair_t* array, unsigned id, unsigned eolId );
// Returns eolId if the id is not found.