鹰之歌

既然你崇拜鹰,就要像鹰一样翱翔天际,俯瞰大地。哪怕会摔的粉身碎骨。

导航

uemf.c文件注释

/*

 * uemf.c -- GoAhead Micro Embedded Management Framework

 *

 * Copyright (c) GoAhead Software Inc., 1995-2010. All Rights Reserved.

 *

 * See the file "license.txt" for usage and redistribution license requirements

 *

 */

/********************************** Description *******************************/

/*

 *    Micro embedded management framework

 */

//uemf的含义是micro embedded management

/*********************************** Includes *********************************/

#include  "uemf.h"

/********************************** Local Data ********************************/

int emfInst;                                               /* Application instance handle */

emfInst是应用程序实例句柄,通过emfInstSet和emfInstGet函数来分别设置和获取emfInst变量。

/****************************** Forward Declarations **************************/

extern void defaultErrorHandler(int etype, char_t *buf);

static void (*errorHandler)(int etype, char_t *msg) = defaultErrorHandler;

//errorHandle是一个函数指针,其指向的函数接受两个参数,返回void。在此文件中将该指//针指向defaultErrorHandler函数,使其作为默认的错误处理函数

extern void     defaultTraceHandler(int level, char_t *buf);

static void (*traceHandler)(int level, char_t *buf) = defaultTraceHandler;

//defaultTraceHandler也是一个函数指针,初始化为defaultTranceHandler

/************************************* Code ***********************************/

/*

 *    Error message that doesn't need user attention. Customize this code

 *    to direct error messages to wherever the developer wishes

//不需要用户关心的错误消息。开发者可以把错误消息重定向到其他地方

 */

错误输出函数

void error(E_ARGS_DEC, int etype, char_t *fmt, ...)

{

       va_list    args;

       char_t            *fmtBuf, *buf;

       va_start(args, fmt);

       fmtValloc(&fmtBuf, E_MAX_ERROR, fmt, args);

       if (etype == E_LOG) {

              fmtAlloc(&buf, E_MAX_ERROR, T("%s\n"), fmtBuf);

/*#ifdef DEV*/

       } else if (etype == E_ASSERT) {

              fmtAlloc(&buf, E_MAX_ERROR,

                     T("Assertion %s, failed at %s %d\n"), fmtBuf, E_ARGS);

/*#endif*/

       } else if (etype == E_USER) {

              fmtAlloc(&buf, E_MAX_ERROR, T("%s\n"), fmtBuf);

       }

   /*

    * bugfix -- if etype is not E_LOG, E_ASSERT, or E_USER, the call to

    * bfreeSafe(B_L, buf) below will fail, because 'buf' is randomly

    * initialized. To be nice, we format a message saying that this is an

    * unknown message type, and in doing so give buf a valid value. Thanks

    * to Simon Byholm.

    */

   else {

      fmtAlloc(&buf, E_MAX_ERROR, T("Unknown error"));

   }

       va_end(args);

       bfree(B_L, fmtBuf);

       if (errorHandler) {

              errorHandler(etype, buf);

       }

       bfreeSafe(B_L, buf);

}

/******************************************************************************/

/*

 *    Replace the default error handler. Return pointer to old error handler.

 */

void (*errorSetHandler(void (*function)(int etype, char_t *msg))) \

       (int etype, char_t *msg)

函数原型解析:errorSetHandler是一个函数,这个函数的参数是一个函数指针,返回值也是一个函数指针。且两个函数指针是同一类型的,都是指向参数为etype,msg返回值为void的函数。

errorSetHandler的作用是改变错误处理函数,并返回改变前的函数指针。

{

       void (*oldHandler)(int etype, char_t *buf);

       oldHandler = errorHandler;

       errorHandler = function;

       return oldHandler;

}

/******************************************************************************/

/*

 *    Trace log. Customize this function to log trace output

 */

void trace(int level, char_t *fmt, ...)

{

       va_list    args;

       char_t            *buf;

       va_start(args, fmt);

       fmtValloc(&buf, VALUE_MAX_STRING, fmt, args);

       if (traceHandler) {

              traceHandler(level, buf);

       }

       bfreeSafe(B_L, buf);

       va_end(args);

}

/******************************************************************************/

/*

 *    Trace log. Customize this function to log trace output

 */

void traceRaw(char_t *buf)

{

       if (traceHandler) {

              traceHandler(0, buf);

       }

}

/******************************************************************************/

/*

 *    Replace the default trace handler. Return a pointer to the old handler.

 */

功能与errorSetHandler相似。都是一个函数,参数是一个函数指针,返回值也是一个函数指针。

void (*traceSetHandler(void (*function)(int level, char_t *buf)))

       (int level, char *buf)

{

       void (*oldHandler)(int level, char_t *buf);

       oldHandler = traceHandler;

       if (function) {

              traceHandler = function;

       }

       return oldHandler;

}

/******************************************************************************/

/*

 *    Save the instance handle

保存实例句柄

 */

void emfInstSet(int inst)

{

       emfInst = inst;

}

/******************************************************************************/

/*

 *    Get the instance handle

获取实例句柄

 */

int emfInstGet()

{

       return emfInst;

}

/******************************************************************************/

/*

 *    Convert a string to lower case

 */

将一个字符串中的大写字母转化为小写字母

char_t *strlower(char_t *string)

{

       char_t     *s;

       a_assert(string);

       if (string == NULL) {

              return NULL;

       }

       s = string;

       while (*s) {

              if (gisupper(*s)) {

                     *s = (char_t) gtolower(*s);

              }

              s++;

       }

       *s = '\0';

       return string;

}

/******************************************************************************/

/*

 *    Convert a string to upper case

 */

将一个字符串中的小写字母转换为大写字母

char_t *strupper(char_t *string)

{

       char_t     *s;

       a_assert(string);

       if (string == NULL) {

              return NULL;

       }

       s = string;

       while (*s) {

              if (gislower(*s)) {

                     *s = (char_t) gtoupper(*s);

              }

              s++;

       }

       *s = '\0';

       return string;

}

/******************************************************************************/

/*

 *    Convert integer to ascii string. Allow a NULL string in which case we

 *    allocate a dynamic buffer.

 */

将一个数字转换为ascii码格式的字符串存储起来,如果string是NULL,则调用balloc分配一块缓冲区。

char_t *stritoa(int n, char_t *string, int width)

{

       char_t     *cp, *lim, *s;

       char_t     buf[16];                                     /* Just temp to hold number */

       int          next, minus;

       a_assert(string && width > 0);

       if (string == NULL) {

              if (width == 0) {

                     width = 10;

              }

              if ((string = balloc(B_L, width + 1)) == NULL) {

                     return NULL;

              }

       }

       if (n < 0) {

              minus = 1;

              n = -n;

              width--;

       } else {

              minus = 0;

       }

       cp = buf;

       lim = &buf[width - 1];

       while (n > 9 && cp < lim) {

              next = n;

              n /= 10;

              *cp++ = (char_t) (next - n * 10 + '0');

       }

       if (cp < lim) {

              *cp++ = (char_t) (n + '0');

       }

       s = string;

       if (minus) {

              *s++ = '-';

       }

       while (cp > buf) {

              *s++ = *--cp;

       }

       *s++ = '\0';

       return string;

}

/******************************************************************************/

/*

 *    Default error and trace  

 */

/******************************************************************************/

/*

 *  Default error handler.  The developer should insert code to handle

 *  error messages in the desired manner.

 */

缺省的错误处理函数

void defaultErrorHandler(int etype, char_t *msg)

{

#if 0

将错误信息打印到标准输出上,但是此行被屏蔽了。即默认状态下缺省错误处理函数是不做任何事情的

write(1, msg, gstrlen(msg));

#endif

}

/******************************************************************************/

/*

 *  Trace log. Customize this function to log trace output

 */

void defaultTraceHandler(int level, char_t *buf)

{

/*

 *  The following code would write all trace regardless of level

 *  to stdout.

 */

将信息打印到标准输出,但是同defaultErrorHandler一样,缺省状态下是不做任何事情的。如果需要开启这个功能,需要将下面的#if 0改掉

#if 0

    if (buf) {

        write(1, buf, gstrlen(buf));

    }

#endif

}

/******************************************************************************/

下面的函数都无关紧要,很简单。

/*

 *    Stubs

 */

char_t *basicGetProduct()

{

       return T("uemf");

}

char_t *basicGetAddress()

{

       return T("localhost");

}

int errorOpen(char_t *pname)

{

       return 0;

}

void errorClose()

{

}

/******************************************************************************/

posted on 2011-06-03 20:39  鹰之歌  阅读(620)  评论(1编辑  收藏  举报