Click here to Skip to main content
15,909,953 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Generalarray problem... Pin
DaveE9th8-Jul-03 23:11
DaveE9th8-Jul-03 23:11 
GeneralRe: array problem... Pin
Alan Dugdall9-Jul-03 0:19
Alan Dugdall9-Jul-03 0:19 
GeneralRe: array problem... Pin
David Crow9-Jul-03 3:24
David Crow9-Jul-03 3:24 
GeneralRe: array problem... Pin
DaveE9th9-Jul-03 3:51
DaveE9th9-Jul-03 3:51 
GeneralFind Window Handles of a process Pin
parths8-Jul-03 22:53
parths8-Jul-03 22:53 
GeneralRe: Find Window Handles of a process Pin
Magnus Westin8-Jul-03 23:06
Magnus Westin8-Jul-03 23:06 
GeneralRe: Find Window Handles of a process Pin
parths8-Jul-03 23:14
parths8-Jul-03 23:14 
QuestionI have written a pipe-like class. Would you tell me some advices on it? Pin
xiaochnegwx8-Jul-03 22:37
xiaochnegwx8-Jul-03 22:37 
This is a pipe-like class written in standard c.Read() and Write()can critically access the inner data section.When there is no data to be read,Read() will block and wait for Write() to write some data to the pipe.Only when
it reads datas does Read() return.
The inner data section are block structures using new operator to calloc units.

/*--------------------------------------------------------
* Pipe.h: interface for the CPipe class.
*
* 2003 DJ Express Studio All rights reserved
*------------------------------------------------------*/
#ifndef _PIPE_H_
#define _PIPE_H_

/* CRITICAL_SECTION was defined in winbase.h
* possibly not compatible with stdafx.h */

#include <Winbase.h>

#define BL_ERROR -1
#define BL_OK 0

struct Block
{
PBYTE pData;
int data_len;
Block* next;
Block* pre;
};

class CPipe
{
private:
CRITICAL_SECTION cs;
HANDLE hEvent;
Block* pBuffer;
Block* pHead;
Block* pTail;
int pointer;
int nBlocks;

public:
void CPipe::EmptyPipe();
int Write(BYTE* buf, int len, int* write);
int Read(BYTE* buf, int len, int* read);
bool IsEmpty();
CPipe()
{
pHead=NULL;
pTail=NULL;
nBlocks=0;
pointer=0;
InitializeCriticalSection(&cs);
hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
};
virtual ~CPipe()
{
CloseHandle(hEvent);
DeleteCriticalSection (&cs);
};
};

#endif

/*---------------------------------------------------
* Pipe.cpp: implementation of the CPipe class.
*
* 2003 DJ Express Studio All rights reserved
*-------------------------------------------------*/
#include "Pipe.h"

int CPipe::Write(BYTE* buf,int len,int* write)
{
Block* pTem;

if(!buf || len <=0 )
goto error;

pTem=NULL;
if(!(pTem=new Block))
goto error;

if(!(pTem->pData=new BYTE[len]))
goto error;
else
{
/* lock the pipe */
EnterCriticalSection(&cs);

pTem->data_len=len;
memcpy(pTem->pData,buf,len);

nBlocks++;
pTem->pre=NULL;
if(pHead==NULL)
pTail=pTem;
else pHead->pre=pTem;
pHead=pTem;

/* unlock the pipe */
LeaveCriticalSection(&cs);
}

*write=len;
SetEvent(hEvent);

return BL_OK;

error:
*write=0;
return BL_ERROR;
}

int CPipe::Read(BYTE* buf,int len,int* read)
{
int total;
Block* tem;

if(!buf || len <= 0 )
goto error;

if( nBlocks <= 0 )
{
WaitForSingleObject(hEvent,INFINITE);
ResetEvent(hEvent);
}

/* lock the pipe */
EnterCriticalSection(&cs);

if(len < pTail->data_len )
{
pointer = len;
memcpy(buf,pTail->pData,len);
*read = len;
}
else
{
total=0;
tem=NULL;
while(len-total)
{
if(len-total >= pTail->data_len )
{
memcpy(buf+total,pTail->pData,pTail->data_len);
total += pTail->data_len;
tem = pTail;
pTail=pTail->pre;
pointer = 0;
delete []tem->pData;
delete tem;
}
else
{
memcpy(buf+total,pTail->pData,len-total);
total = len;
pointer = len - total;
break;
}

if(--nBlocks <= 0)
{
pTail=NULL;
pHead=NULL;
break;
}
}
*read = total;
}

/* unlock the pipe */
LeaveCriticalSection(&cs);

return BL_OK;

error:
*read=0;
return BL_ERROR;
}

bool CPipe::IsEmpty()
{
return nBlocks > 0 ? false : true ;
}

void CPipe::EmptyPipe()
{
Block* tem;

if(nBlocks <= 0)
return;

/* lock the pipe */
EnterCriticalSection(&cs);

while(nBlocks--)
{
tem=pTail;
pTail=pTail->pre;
delete []tem->pData;
delete tem;
tem=NULL;
}
pHead=pTail=NULL;

/* unlock the pipe */
LeaveCriticalSection(&cs);
}
/*---------------------------------------------------------*/

GeneralSTRANGE!!!!!!!!!! Pin
vcseeker8-Jul-03 22:19
vcseeker8-Jul-03 22:19 
GeneralAnyone????????? Pin
vcseeker9-Jul-03 1:23
vcseeker9-Jul-03 1:23 
GeneralRe: Anyone????????? Pin
Rage9-Jul-03 2:04
professionalRage9-Jul-03 2:04 
QuestionWhich one is best for Wait in a thread? Pin
chen8-Jul-03 21:53
chen8-Jul-03 21:53 
AnswerRe: Which one is best for Wait in a thread? Pin
Magnus Westin8-Jul-03 23:01
Magnus Westin8-Jul-03 23:01 
AnswerRe: Which one is best for Wait in a thread? Pin
Mike Dimmick8-Jul-03 23:14
Mike Dimmick8-Jul-03 23:14 
GeneralRe: Which one is best for Wait in a thread? Pin
chen9-Jul-03 0:09
chen9-Jul-03 0:09 
GeneralRe: Which one is best for Wait in a thread? Pin
Magnus Westin9-Jul-03 7:14
Magnus Westin9-Jul-03 7:14 
GeneralCommand Line Options Pin
BoudewijnEctor8-Jul-03 21:44
BoudewijnEctor8-Jul-03 21:44 
GeneralRe: Command Line Options Pin
giorgos8-Jul-03 21:52
giorgos8-Jul-03 21:52 
GeneralRe: Command Line Options Pin
David Crow9-Jul-03 3:55
David Crow9-Jul-03 3:55 
GeneralMDI - PLEASE URGENT Pin
giorgos8-Jul-03 21:30
giorgos8-Jul-03 21:30 
GeneralRe: MDI - PLEASE URGENT Pin
Ryan Binns8-Jul-03 21:52
Ryan Binns8-Jul-03 21:52 
GeneralRe: MDI - PLEASE URGENT Pin
giorgos8-Jul-03 22:12
giorgos8-Jul-03 22:12 
GeneralRe: MDI - PLEASE URGENT Pin
Ryan Binns8-Jul-03 22:15
Ryan Binns8-Jul-03 22:15 
GeneralRe: MDI - PLEASE URGENT Pin
giorgos8-Jul-03 22:23
giorgos8-Jul-03 22:23 
GeneralRe: MDI - PLEASE URGENT Pin
Ryan Binns8-Jul-03 22:27
Ryan Binns8-Jul-03 22:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.