barfatchen barfatchen - 1 month ago 8
C Question

C how to identify function exist or not

I have the following code in c :

char tag34[32]={0} ,tag16104[32]={0} ,tag43[16]={0} ,tag37[32]={0} ;
char tag35[8]={0} , tag150[8]={0} , tag11[32]={0} , tag41[32]={0};
char tag48[32]={0} , tag207[32]={0} , tag55[32]={0} ;
char tag167[32]={0} , tag200[32]={0} , tag44[32]={0} ;
char tag151[16]={0} , tag32[16]={0} , tag31[16]={0} ;
char tag379[32]={0}, tag372[16]={0} , tag58[128]={0} ;
char tag16710[32]={0},tag16724[32]={0} ;

for(int idx=0;idx<inum;idx++){
if( strlen(ArrLeftVar[idx]) < 1 )
break ;
if( strcmp(ArrLeftVar[idx],"34") == 0 ){
strncpy( tag34,ArrRightVal[idx],sizeof(tag34) - 1 ) ;
}else if( strcmp(ArrLeftVar[idx],"150") == 0 ){
strncpy( tag150,ArrRightVal[idx],sizeof(tag150) - 1 ) ;
}else if( strcmp(ArrLeftVar[idx],"11") == 0 ){
strncpy( tag11,ArrRightVal[idx],sizeof(tag11) - 1 ) ;
}else if( strcmp(ArrLeftVar[idx],"41") == 0 ){
strncpy( tag41,ArrRightVal[idx],sizeof(tag41) - 1 ) ;
}else if( strcmp(ArrLeftVar[idx],"43") == 0 ){
strncpy( tag43,ArrRightVal[idx],sizeof(tag43) - 1 ) ;
}else if( strcmp(ArrLeftVar[idx],"48") == 0 ){
strncpy( tag48,ArrRightVal[idx],sizeof(tag48) - 1 ) ;
}else if( strcmp(ArrLeftVar[idx],"207") == 0 ){
strncpy( tag207,ArrRightVal[idx],sizeof(tag207) - 1 ) ;
}else if( strcmp(ArrLeftVar[idx],"55") == 0 ){
strncpy( tag55,ArrRightVal[idx],sizeof(tag55) - 1 ) ;
}else if( strcmp(ArrLeftVar[idx],"167") == 0 ){
strncpy( tag167,ArrRightVal[idx],sizeof(tag167) - 1 ) ;
}else if( strcmp(ArrLeftVar[idx],"200") == 0 ){
strncpy( tag200,ArrRightVal[idx],sizeof(tag200) - 1 ) ;
}else if( strcmp(ArrLeftVar[idx],"44") == 0 ){
strncpy( tag44,ArrRightVal[idx],sizeof(tag44) - 1 ) ;
} .......
......
}//for


It works but very lousy , I like to know how to implement a
c macro so that I wont need so many if ...else.... ?!

The psudocode would be :

for(int idx=0;idx<inum;idx++){
if( strlen(ArrLeftVar[idx]) < 1 )
break ;
int len = atoi( ArrLeftVar[idx] ) ;
if( (funcptr = Exist(Func_#len)) != NULL )
funcptr( ArrRightVal[idx] ) ;
}//for


then I have Func_34(char* ptr) , Func_150(char* ptr), ....
that make code cleaner .

How can I implement such template or macro , any hints ?!

Answer

You could define a struct like this:

struct tags {
    const char *name;
    int maxlen;
    char value[128];
};

struct tags tagList[] = {
    { "34", 32, 0 },
    { "43", 16, 0 },
    { "11", 32, 0 },
    ...
};

Then you could write your loop like this:

for(int idx=0;idx<inum;idx++){
    if( strlen(ArrLeftVar[idx]) < 1 )
        break ;
    for (int tagIdx=0; tagIdx < (sizeof(tagList) / sizeof(tagList[0]); tagIdx++) {
        if( strcmp(ArrLeftVar[idx],tagList[tagIdx].name) == 0 ){ 
            strncpy(tagList[tagIdx].value,
                    ArrRightVal[idx],
                    tagList[tagIdx].maxlen - 1 );
            tagList[tagIdx].value[tagList[tagIdx].maxlen - 1] = '\0';
            break;
        }
    }
}

If at any time you need to add/remove tags, you only have to change the tag list array instead of a large if/else block.

Comments