⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 events.c

📁 掌握如何用C来实现各种算法
💻 C
字号:
/*****************************************************************************
*                                                                            *
*  ------------------------------- events.c -------------------------------  *
*                                                                            *
*****************************************************************************/

#include <stdlib.h>
#include <string.h>

#include "event.h"
#include "events.h"
#include "queue.h"

/*****************************************************************************
*                                                                            *
*  ---------------------------- receive_event -----------------------------  *
*                                                                            *
*****************************************************************************/

int receive_event(Queue *events, const Event *event) {

Event              *new_event;

/*****************************************************************************
*                                                                            *
*  Allocate space for the event.                                             *
*                                                                            *
*****************************************************************************/

if ((new_event = (Event *)malloc(sizeof(Event))) == NULL)
   return -1;

/*****************************************************************************
*                                                                            *
*  Make a copy of the event and enqueue it.                                  *
*                                                                            *
*****************************************************************************/

memcpy(new_event, event, sizeof(Event));

if (queue_enqueue(events, new_event) != 0)
   return -1;

return 0;

}

/*****************************************************************************
*                                                                            *
*  ---------------------------- process_event -----------------------------  *
*                                                                            *
*****************************************************************************/

int process_event(Queue *events, int (*dispatch)(Event *event)) {

Event              *event;

if (queue_size(events) == 0)

   /**************************************************************************
   *                                                                         *
   *  Return that there are no events to dispatch.                           *
   *                                                                         *
   **************************************************************************/

   return -1;

else {

   if (queue_dequeue(events, (void **)&event) != 0)

      /***********************************************************************
      *                                                                      *
      *  Return that an event could not be retrieved.                        *
      *                                                                      *
      ***********************************************************************/

      return -1;

   else {

      /***********************************************************************
      *                                                                      *
      *  Call a user-defined function to dispatch the event.                 *
      *                                                                      *
      ***********************************************************************/

      dispatch(event);
      free(event);

   }

}

return 0;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -