useful fn for list

"fred" <[email protected]>
Newsgroups gmane.comp.mobile.kannel.devel
Message-ID <[email protected]>
I find this usefull, apologies not in patch form, besides I think it'll be dismissed anyway...


list.c

/*
 * enumerate thru the items of the list, calling the user supplied function 
 * with each item, plus optionally some environment structure
 * if user fn returns 0, the enumeration can prematurely abort
 * 
 * eg
 * int prntmsgs(void *env, void * _msg);
 * ...
 * list_enumerate(list,0,prntmsgs);
 */
void list_enumerate(List *list, void * env, int (*fn)(void *env,void *item) )
{
    long i;
    void *item;

    lock(list);
    item = NULL;
    for (i = 0; i < list->len; ++i) 
    {
        item = GET(list, i);

        if (fn(env,item)) 
            break;
        
    }
    if (i == list->len) 
    {
        item = NULL;
    }
    unlock(list);

    return item;
}




/* how to use ...*


bb_box.c

#if DEBUG

/*
 * helper fn for user use in gdb
 */

int prntmsgs(void *env, void * _msg)
{
    Msg *msg = _msg;
    char *type = "";
    switch(msg->sms.sms_type) 
    {
    case mo:
     type = "mo";break;
    case mt_reply:
     type = "mt_reply";break;
    case report_mo:
     type = "report_mo";break;
    case report_mt:
     type = "report_mt";break;
    default:
     type = "unknown";
    }

    debug("bb.boxc", 0,"sms: sender <%s> receiver <%s> smsc <%s> id=%d sms_type=%s dlrmask=%d tid=%d",
    octstr_get_cstr(msg->sms.sender),
    octstr_get_cstr(msg->sms.receiver),
    octstr_get_cstr(msg->sms.smsc_id),
    msg->sms.id,
    type,
    msg->sms.dlr_mask,
    msg->sms.tid
    );
    return 0;
}

void listprint(int who)
{
    List *list;
    int len;
    int i;

    if(who==1)
 list = incoming_sms;
    if(who==2)
 list = outgoing_sms;

    len = list_len(list);

    list_enumerate(list,0,prntmsgs);
}

#endif



say your in gdb, do

call listprint(1)
call listprint(2);

*** very useful when your debugging and just want to see whats in the list at anytime !! ***
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.