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

📄 piiobuf.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:

	/* ---
	Otherwise read in some more
	--- */
	switch( GrowData() )
		{
		case 0:		/* --- no more data --- */
			return 0;
		case -1:	/* --- IO error --- */
			return -1;

		default:
			/* got more data */
			;
		};

	/* --- recurse to return the data block --- */
	return Read( pszBuffer, iMaxLen );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Returns the next line from the IO Object. If pszBuf is not
	NULL then the line is written into it, up to iMaxLen bytes.
	Returns the length of the line read.

	Will non-terminate line.
\*____________________________________________________________________________*/
int PIIOBuffer::GetLine( char *pszBuf, int iMaxLen )
{
	assert( pszBuf && iMaxLen );
	if ( !pszBuf || !iMaxLen )
		{ return 0; };
	enum { NORMAL, CR_SEEN, LF_SEEN, CRLF_SEEN } eState = NORMAL;
	int i;
	int iRet = 0;
	int iBlockStart = 0;		/* --- start of last block appended by
										GrowData() --- */
	int iLineLen = 0;			/* --- length of line --- */

do_again:
	for(;;)
		{
		/* ---
		Scan this block for EOL 
		--- */
		for( i=iBlockStart; i<iBufferLen; i++)
			{
			switch( eState )
				{
				case NORMAL:
					if ( szInBuf[i]==HTTP_CR )
						{ eState = CR_SEEN; }
					else if ( szInBuf[i]==HTTP_LF )
						{
						eState = LF_SEEN;
						i++; goto done;
						};
					break;

				case CR_SEEN:
					if ( szInBuf[i]==HTTP_LF )
						{
						eState = CRLF_SEEN;
						i++; goto done;
						};
					i++; goto done;
					break;

				default:
					assert( 0 );
				};
			};

		/* i is now pointing at the char after the last valid char */

		/* ---
		This block has been exhausted without reading till EOL, try to
		read in more data
		--- */
		switch( GrowData() )
			{
			case 0:
				/* no more data */
				goto done;
				break;
			case -1:
				/* buffer full or IO error */
				goto done;
				break;
			default:
				/* got more data */
				;
			};
		};
done:
	if ( !i )
		{ goto do_return; };
	iLineLen = i;
	if ( pszBuf )
		{
		if ( iMaxLen < i )
			{ i = iMaxLen; };
		assert( i>=1 );
		if ( eState == CRLF_SEEN )
			{
			if ( i>=2 )
				{	
				i -= 2;
				strncpy( pszBuf, szInBuf, i );
				}
			else
				{ *szInBuf = '\0'; i=0; };
			}
		else if ( eState == LF_SEEN )
			{
			if ( i>=1 )
				{
				i -= 1;
				strncpy( pszBuf, szInBuf, i );
				}
			else
				{ *szInBuf = '\0'; i=0; };
			}
		else
			{
			strncpy( pszBuf, szInBuf, i );
			};
		assert( i<=iMaxLen );
		}

	/* ---
	Shrink the buffer for the next GetLine()
	--- */
	if ( iLineLen<iBufferLen )
		{
		memmove( szInBuf, &( szInBuf[iLineLen] ), iBufferLen-iLineLen );
		}
	else
		{		
		assert( iLineLen<=iBufferLen );
		};
	iBufferLen -= iLineLen;

	/* ---
	Increase total bytes read
	--- */
	iRet += i;

	/* ---
	If the block has been exhausted without reading till EOL, try to
	read in more data
	--- */
	if (( eState == NORMAL ) && ( iMaxLen > 1 ))
		{
		// adjust buffer and limit
		pszBuf += i;
		iMaxLen -= i;

		switch( GrowData() )
			{
			case 0:
				/* no more data */
				goto do_return;
				break;
			case -1:
				/* buffer full or IO error */
				goto do_return;
				break;
			default:
				/* got more data */
				goto do_again;
			};
		};

do_return:
	pszBuf[i] = '\0';
	return iRet;
}

	/* -------------- +++++++++++++++++++++++++++++++ ------------------ *\
	
						C Functions

	\* -------------- +++++++++++++++++++++++++++++++ ------------------ */
/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PIIOBuffer *PIIOBuffer_new( PIObject *pIOObject )
{
	if ( !pIOObject ) 
		{ return 0; };
	return PI_NEW( PIIOBuffer( pIOObject ) );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int PIIOBuffer_delete( PIIOBuffer *pPIIOBuffer )
{
	if ( !pPIIOBuffer )
		{ return PIAPI_ERROR; };
	PI_DELETE( pPIIOBuffer );
	return PIAPI_COMPLETED;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int PIIOBuffer_getLine( PIIOBuffer *pIOBuf, char *pszBuf, int iBufLen )
{
	if ( !pIOBuf )
		{ return -1; };
	return pIOBuf->GetLine( pszBuf, iBufLen );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int PIIOBuffer_pollBeforeRead( PIIOBuffer *pIOBuf )
{
	if ( !pIOBuf )
		{ return -1; };
	return pIOBuf->PollBeforeRead();
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
const char *PIIOBuffer_read( PIIOBuffer *pIOBuf, int *piRead )
{
	if ( !pIOBuf )
		{ return 0; };
	return pIOBuf->Read( piRead );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int PIIOBuffer_readToBuffer( PIIOBuffer *pIOBuf, char *pszBuf, int iMaxLen )
{
	if ( !pIOBuf )
		{ return -1; };
	return pIOBuf->Read( pszBuf, iMaxLen );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int PIIOBuffer_write( PIIOBuffer *pIOBuf, const char *pszBuf, int iLen,
	int iFlags )
{
	if ( !pIOBuf )
		{ return -1; };
	return pIOBuf->Write( pszBuf, iLen, iFlags );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int PIIOBuffer_writeLn( PIIOBuffer *pIOBuf, const char *pszBuf, int iLen,
	int iFlags )
{
	if ( !pIOBuf )
		{ return -1; };
	return pIOBuf->WriteLn( pszBuf, iLen, iFlags );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int PIIOBuffer_flush( PIIOBuffer *pIOBuf )
{
	if ( !pIOBuf )
		{ return 0; };
	return pIOBuf->Flush();
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int PIIOBuffer_getOutputBuffer( PIIOBuffer *pIOBuf, char **ppBuffer )
{
	if ( !pIOBuf )
		{ return -1; };
	return pIOBuf->GetOutputBuffer( ppBuffer );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int PIIOBuffer_advanceBufferPointer( PIIOBuffer *pIOBuf, int iCount )
{
	if ( !pIOBuf )
		{ return -1; };
	return pIOBuf->AdvanceBufferPointer( iCount );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int PIIOBuffer_getBytesSent( PIIOBuffer *pIOBuf )
{
	if ( !pIOBuf )
		{ return -1; };
	return pIOBuf->GetBytesSent();
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void PIIOBuffer_resetBytesSent( PIIOBuffer *pIOBuf )
{
	if ( !pIOBuf )
		{ return; };
	pIOBuf->ResetBytesSent();
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void *PIIOBuffer_getUserData( PIIOBuffer *pIOBuf )
{
	if ( !pIOBuf )
		{ return 0; };
    return pIOBuf->GetUserData();
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void PIIOBuffer_setUserData( PIIOBuffer *pIOBuf, void *pUserData )
{
	if ( !pIOBuf )
		{ return; };
    pIOBuf->SetUserData( pUserData );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIIOBuffer_getBufferLen( PIIOBuffer *pIOBuf )
{
	if ( !pIOBuf )
		{ return 0; }
	 return pIOBuf->getBufferLen();
}

⌨️ 快捷键说明

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