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

📄 win32ose.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/Platform/Win32OSE.cpp,v $
 * $Date: 2003/05/13 18:42:14 $
 *
 Description:
	This file provides the implementation of some of the abstract
	platform objects for the Win32 os interface.

\*____________________________________________________________________________*/
//$SourceTop:$

#include "OSConfig.h"

#if defined(CONFIG_OS_WIN32)

#include "Win32OSE.h"
#include "Platform.h"
#include "errno.h"
#include "Utils.h"
#include "PlatDefs.h"

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
const char *Win32FATFileSysType::GetExtension( const char *pName ) const
{
	assert( pName );
	int i = strlen( pName ) -1;
	for(; i>=0; i--)
		{
		if ( pName[i]=='.' )
			{ return &( pName[i+1] ); };
		};
	return "";
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:	
 Description:
\*____________________________________________________________________________*/
Win32DLL::Win32DLL( const char *pPath )
:	hInstance( 0 ), sPath( pPath ), sLastErrorMsg( "No error" )
{
	assert( pPath );
/*
** Disable use of search path in loading DLL's
*/
/*	hInstance = LoadLibrary( pPath ); */

	hInstance = LoadLibraryEx( pPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH );
	if ( !hInstance )
		{
		CaptureWin32Error();
		};
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
Win32DLL::~Win32DLL()
{
	if ( hInstance )
		{
		if ( FreeLibrary( hInstance )!=TRUE )
			{ PIOSERR; };
		};
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:	
 Description:
\*____________________________________________________________________________*/
void *Win32DLL::GetAddress( const char *pSymbolName )
{
	assert( hInstance && pSymbolName );
	if ( !hInstance || !pSymbolName ) 
		{ return 0; };
	void *pAddr = GetProcAddress( hInstance, pSymbolName );
	if ( !pAddr )
		{
		CaptureWin32Error();
		};
	return pAddr;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:		public 
 Description:
\*____________________________________________________________________________*/
void Win32DLL::CaptureWin32Error()
{
	DWORD dwError = ::GetLastError();
	PIRETERR( dwError );
	PIString sTmp;
	pi_ltoa( dwError, 10, sTmp );
	sLastErrorMsg = "Win32 error code: ";
	sLastErrorMsg.Concatenate( sTmp );	
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:		public 
 Description:
\*____________________________________________________________________________*/
Win32MappedFile::Win32MappedFile( FileSystemEntity &tFSE )
:	hFile( INVALID_HANDLE_VALUE ),
	hMapping( 0 ),
	lpAddress( 0 ),
	dwLength( 0 )
{
	Setup( tFSE.GetPath(), tFSE.GetFileSize() );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:		public 
 Description:
\*____________________________________________________________________________*/
Win32MappedFile::~Win32MappedFile()
{
	if ( lpAddress )
		{ DoUnlock(); };

	if ( hMapping != 0 )
		{
		if ( ::CloseHandle( hMapping ) != TRUE )
			{ PIOSERR; };
		};

	if ( hFile != INVALID_HANDLE_VALUE )
		{
		if ( ::CloseHandle( hFile ) != TRUE )
			{ PIOSERR; };
		};
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:		private:
 Description:
\*____________________________________________________________________________*/
void Win32MappedFile::Setup( const char *pFileName, DWORD dwTheLength )
{
	assert( pFileName );
	if ( !pFileName ) { PIERROR( PIAPI_EINVAL ); return; };

	/* ---
	Create a file handle
	--- */
	hFile = CreateFile(
		pFileName,
		GENERIC_READ,
		FILE_SHARE_READ | FILE_SHARE_WRITE,
		0,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		0 );
	if ( hFile == INVALID_HANDLE_VALUE )
		{ PIOSERR; return; };

	/* ---
	Create a file mapping
	--- */
	hMapping = CreateFileMapping(
		hFile,
		0,
		PAGE_READONLY,
		0,
		0,
		0 );
	if ( hMapping == 0 )
		{ PIOSERR; return ; };

	dwLength = dwTheLength;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:		private
 Description:
\*____________________________________________________________________________*/
void *Win32MappedFile::DoLock( int *piLength )
{
	if ( hMapping == 0 )
		{ PIERROR( PIAPI_EINVAL ); return 0; };

	if ( lpAddress )
		{ return lpAddress; };

	/* ---
	Map the file into memory
	--- */
	lpAddress = MapViewOfFile(
		hMapping,
		FILE_MAP_READ,
		0,
		0,
		0 /* Entire file */ );
	if ( !lpAddress )
		{ PIOSERR; return 0; };
	
	if ( piLength )
		{ *piLength = dwLength;	};
	return lpAddress;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:		private
 Description:
\*____________________________________________________________________________*/
bool Win32MappedFile::DoUnlock()
{
	if ( !lpAddress )
		{ return false; };

	if ( UnmapViewOfFile( lpAddress )==0 )
		{ PIOSERR; return false; };	

	lpAddress = 0;
	return true;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:		public 
 Description:
\*____________________________________________________________________________*/
Win32Timer::Win32Timer()
{
	Reset();
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:		public 
 Description:
\*____________________________________________________________________________*/
bool Win32Timer::Reset()
{
	dwTime = ::GetTickCount();
	return true;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:		public 
 Description:
\*____________________________________________________________________________*/
bool Win32Timer::Elapsed( long *plSeconds, long *plMicroSeconds )
{
	DWORD dwElapsed = ::GetTickCount() - dwTime;
	if ( plSeconds )
		{ *plSeconds = dwElapsed / 1000; };
	if ( plMicroSeconds )
		{ *plMicroSeconds = (dwElapsed % 1000) * 1000; };
	return true;
}

#endif	/* if defined(CONFIG_OS_WIN32) */

⌨️ 快捷键说明

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