2010年11月25日 星期四

[好code分享] 利用掛function pointer 對同一堆data structure做不同的操作

int ecmpIprtNextHopIterate(
    void    (*exefunc)(ecmpRtNhEntry_t* ,ecmpNhEntry_t *,void*),
    void * data
){
    ecmpRtNhEntry_t* ecmpRtNhEntry_p = NULL;
    ecmpNhEntry_t* ecmpNhEntry_p = NULL;
    int32 bits,hashVal;
    for ( bits = IPRT_ECMP_NETMASK-1; bits >= 0; bits--)
    {
        for(hashVal = HASHMOD-1; hashVal >= 0; hashVal--){
            TAILQ_FOREACH(ecmpRtNhEntry_p, &ecmpRtNhTable[bits][hashVal],link) 
            {
                TAILQ_FOREACH(ecmpNhEntry_p, &(ecmpRtNhEntry_p->nhList),link){
                    if(exefunc){
                        exefunc(ecmpRtNhEntry_p, ecmpNhEntry_p,data);
                    }
                }
            }
        }
    }
    return ZYNOS_E_NONE;
}

要一直對相同的一堆data structure做不同的處理時用的, 感覺還不錯

印很多變數 檢查null的方式

if (str && buf && hw_addr){
    IPRT_ECMP_LOG(ECMP_DB_INFO,
        "update nexthop =%s hw addr = %02x:%02x:%02x:%02x:%02x:%02x    flags = %s caller = 0x%x\n",
        buf, MACADDR_LIST(hw_addr), str, addr);
}

我喜歡這個 很乾淨

2010年11月2日 星期二

passwd

帳號: 公司mail

密碼: real name

2010年11月1日 星期一

Sample.c

/*
* $Id: $
*/
/****************************************************************************/
/*
*    Copyright <Company logo>
*/
/****************************************************************************/
/*
* $Log: $
*/
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "global.h"


#ifdef SAMPLE_SUPPORT

#if 1 /* macro */
/*************************************************************
*
* macro
*
*************************************************************/
#endif /* macro */

#if 1 /* prototype */
/**************************************************************
*
* prototype
*
**************************************************************/
#endif /* prototype */

#if 1 /* global variable */
/**************************************************************
*
* global variable
*
**************************************************************/
#endif /* global variable */

#if 1 /* local variable */
/**************************************************************
*
* local variable
*
**************************************************************/
#endif  /* local variable */

#if 1 /* local function */
/**************************************************************
*
* local function
*
**************************************************************/
#endif  /* local function */

#if 1 /* global function */
/**************************************************************
*
* global function
*
**************************************************************/
#endif /* global function */

#endif /* SAMPLE_SUPPORT */

Sample.h

/*
* $Id:  $
*/

/****************************************************************************/
/*
*    Copyright <Company logo>
*/
/****************************************************************************/

/*
* $Log:  $
*/

#ifndef _SAMPLE_H_
#define _SAMPLE_H_

#if 1  /* define */
/**************************************************************
*
* define
*
**************************************************************/
#endif /* define */

#if 1  /* enum */
/**************************************************************
*
* enum
*
**************************************************************/
#endif /* enum */

#if 1  /* type */
/**************************************************************
*
* type
*
**************************************************************/
#endif /* type */

#if 1  /* prototype */
/**************************************************************
*
* prototype
*
**************************************************************/
#endif /* prototype */

#endif /* !_SAMPLE_H_ */

Comment Templete

/*
* Function:
*      <function name>
* Purpose:
*      <purpose>
* Parameters:
*      <p1 name> - <parameter 1 description>
*      <p2 name> - <parameter 2 description>
*      <p3 name> - <parameter 3 description>
* Returns:
*     <error 1 enum>    - <ERROR 1 description>
*     <error 2 enum>    - <ERROR 2 description>
*     <error 3 enum>    - <ERROR 3 description>
*     <error 4 enum>    - <ERROR 4 description>
*     <error 5 enum>    - <ERROR 5 description>
* Notes:
*/

Auto fill function name

/*----------------------------------------------------
*/
/* This file try auto fill funciton name in the begin of debug 20090703 smo
*/
/* Key word: __VA_ARGS__         
*/
/*------------------------------------------------------
*/

#include <stdio.h>
#include <stdarg.h>

/*if no ##, there must be one argument at least. */
#define mldsnppxyFuncPrint(a,...) mldsnppxyDbg(__PRETTY_FUNCTION__,a,##__VA_ARGS__)

int mldsnppxyDbg(char* funcName,char* format,...)
{
    char buffer[256];
    printf("@%s: ",funcName);
    va_list args;
    va_start (args, format);
    vsprintf (buffer,format, args);
    printf (buffer);
    va_end (args);
    return 0;
}

int main()
{
    int testInt=3;
    char testStr[]="smo";
    mldsnppxyFuncPrint("int =%d, string= %s\n\n",testInt,testStr);   
    return 0;
}