/************************************************************************/
/* Cache Msg Queue Operations */
/************************************************************************/
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct MsgNode
{
char* data;
struct MsgNode *next;
};
struct MsgQueue
{
struct MsgNode *front;
struct MsgNode *rear;
};
//////////////////////////////////////////////////////////////////////////
//1.初始化链队
void initQueue(struct MsgQueue *pHq)
{
pHq->front = pHq->rear = NULL;
return;
}
//////////////////////////////////////////////////////////////////////////
// 2.向链队中插入一个元素
void enQueue( struct MsgQueue *pHq, char* pMsg )
{
struct MsgNode *newP;
newP = (MsgNode *)malloc(sizeof(struct MsgNode));
if(newP == NULL)
{
return;
}
newP->data = pMsg;
newP->next = NULL;
if(pHq->rear == NULL)
{
pHq->front = pHq->rear = newP;
}
else
{
pHq->rear = pHq->rear->next = newP;
}
return;
}
//////////////////////////////////////////////////////////////////////////
// 3.从队列中删除一个元素
char* outQueue(struct MsgQueue *pHq)
{
struct MsgNode *p;
char* temp;
if(pHq->front == NULL)
{
return NULL;
}
temp = pHq->front->data;
p = pHq->front;
pHq->front = p->next;
if(pHq->front == NULL)
{
pHq->rear = NULL;
}
free(p);
return temp;
}
//////////////////////////////////////////////////////////////////////////
// 4.读取队首元素
char* peekQueue(struct MsgQueue *pHq)
{
if(pHq->front == NULL)
{
return NULL;
}
return pHq->front->data;
}
//////////////////////////////////////////////////////////////////////////
// 5.检查链队是否为空,若为空则返回1, 否则返回0
int emptyQueue(struct MsgQueue *pHq)
{
if(pHq->front == NULL)
{
return 1;
}
else
{
return 0;
}
}
//////////////////////////////////////////////////////////////////////////
// 6.清除链队中的所有元素
void clearQueue(struct MsgQueue *pHq)
{
struct MsgNode *p = pHq->front;
while(p != NULL)
{
pHq->front = pHq->front->next;
free(p);
p = pHq->front;
}
pHq->rear = NULL;
return;
}
//////////////////////////////////////////////////////////////////////////
//Instert Cache Msg Into Queue.
int InstertCacheMsgToQueue( struct MsgQueue *pMsgQue, char* pMsg )
{
if ( !pMsg || !pMsgQue )
{
return 0; // pMsg or pMsgQue is null.
}
enQueue( pMsgQue, pMsg);
return 1;
}
//////////////////////////////////////////////////////////////////////////
//Pop the queue msgs and Write them into CacheFile
int PopQueueMsgAndWriteCacheFlie( struct MsgQueue *pMsgQue, char* pCacheFilePath )
{
if ( !pMsgQue || !pCacheFilePath )
{
return 0;
}
while( !emptyQueue( pMsgQue ) )
{
char* l_svMsg = outQueue( pMsgQue );
if ( l_svMsg )
{
//TODO: Write File into Cache
FILE *fp;
fp = fopen( pCacheFilePath, "ab");
if (fp)
{
fwrite( l_svMsg, sizeof( char ), strlen(l_svMsg),fp);
}
fclose(fp);
printf("/n Node->data: %s", l_svMsg);
free(l_svMsg);
}
}
return 1;
}
void TestFunction()
{
printf("/n-----------Come in to Test -----------------/n");
char *str1;
str1 =(char*) malloc(100);
sprintf(str1, "hello 1/r/n");
char *str2;
str2=(char*) malloc(100);
sprintf(str2, "hello 2/r/n");
char *str3;
str3 =(char*) malloc(100);
sprintf(str3, "hello 3/r/n");
char *str4;
str4 =(char*) malloc(100);
sprintf(str4, "hello 4/r/n");
struct MsgQueue q;
initQueue(&q);
InstertCacheMsgToQueue(&q, str1);
InstertCacheMsgToQueue(&q, str2);
InstertCacheMsgToQueue(&q, str3);
InstertCacheMsgToQueue(&q, str4);
PopQueueMsgAndWriteCacheFlie(&q, "C://howhaha");
printf("/n-----------End to Test -----------------/n");
printf(str1);
}
int _tmain(int argc, _TCHAR* argv[])
{
TestFunction();
return 0;
}