posixose.cpp

来自「mini http server,可以集成嵌入到程序中,实现简单的web功能」· C++ 代码 · 共 286 行

CPP
286
字号
/*____________________________________________________________________________*\
 *

 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/POSIXOSE.cpp,v $
 * $Date: 2003/05/13 18:42:14 $
 *
 Description:
	This file provides the implementation of some of the abstract
	platform objects for unix operating systems.
\*____________________________________________________________________________*/
//$SourceTop:$

#include "OSConfig.h"

#if defined(CONFIG_OS_POSIX)	/* only for POSIX OS's */

#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>

#include "POSIXOSE.h"
#include "Platform.h"
#include "errno.h"
#include "PlatDefs.h"

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:		public 
 Description:
\*____________________________________________________________________________*/
const char *POSIXFileSysType::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:
\*____________________________________________________________________________*/
#if defined(CONFIG_DLL_GNU_DLD)
	extern char *pProgramArgv0;		/* defined in Main.cpp */
	bool UnixDLL::bHaveInitializedDld = false;	
#endif
UnixDLL::UnixDLL( const char *pPath )
:	pDlHandle( 0 ), sPath( pPath ), sLastErrorMsg( "No error" )
{
	assert( pPath );
#if defined(CONFIG_DLL_DL)
	pDlHandle = dlopen( pPath, RTLD_NOW );
	if ( !pDlHandle )
		{
		sLastErrorMsg = dlerror();
		};
#elif defined(CONFIG_DLL_GNU_DLD)
	assert( pProgramArgv0 );
	if ( !bHaveInitializedDld )
		{
		int iError = dld_init( pProgramArgv0 );
		if ( iError )
			{ sLastErrorMsg = dld_strerror( iError ); }
		else	
			{ bHaveInitializedDld = true; };
		};
	if ( bHaveInitializedDld )
		{
		int iError = dld_link( pPath );
		if ( iError )
			{ sLastErrorMsg = dld_strerror( iError ); }
		else
			{ pDlHandle = (void *)~0; };
		};
#else
	sLastErrorMsg = "This operating system does not support \
dynamic loading of libraries";
#endif
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
UnixDLL::~UnixDLL()
{
	if ( pDlHandle )
#if defined(CONFIG_DLL_DL)
		{
		if ( dlclose( pDlHandle ) ) { PIOSERR; };
		};
#elif defined(CONFIG_DLL_GNU_DLD)
		{ dld_unlink_by_file( sPath, 0 ); };
#else
		;
#endif
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:	
 Description:
\*____________________________________________________________________________*/
void *UnixDLL::GetAddress( const char *pSymbolName )
{
	assert( pDlHandle && pSymbolName );
	if ( !pDlHandle || !pSymbolName ) 
		{ return 0; };
	void *pAddr = 0;
#if defined(CONFIG_DLL_DL)
	pAddr = dlsym( pDlHandle, pSymbolName );
	if ( !pAddr )
		{
		sLastErrorMsg = dlerror();
		};
#elif defined(CONFIG_DLL_GNU_DLD)
	pAddr = dld_get_func( pSymbolName );
	if ( !pAddr )
		{ 
		PIOStrStream os;
		os << "Unknown symbol '" << pSymbolName << "'" << ends;
		sLastErrorMsg = os.str();
		};
	if ( pAddr && !dld_function_executable_p( pSymbolName ) )
		{ sLastErrorMsg = "There are undefined symbols"; pAddr = 0; };
#endif
	return pAddr;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:		public 
 Description:
\*____________________________________________________________________________*/
POSIXMappedFile::POSIXMappedFile( FileSystemEntity &tFSE )
:	iFileDes( -1 ),
	tLength( 0 ),
	tAddress( 0 )
{
	Setup( tFSE.GetPath(), tFSE.GetFileSize() );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:		public 
 Description:
\*____________________________________________________________________________*/
POSIXMappedFile::~POSIXMappedFile()
{
	if ( tAddress )
		{
		if ( munmap( tAddress, tLength ) )
			{ PIOSERR; };
		};

	if ( iFileDes!=-1 )
		{
		if ( close( iFileDes ) == -1 )
			{ PIOSERR; };
		iFileDes = -1;
		};
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:		private:
 Description:
\*____________________________________________________________________________*/
void POSIXMappedFile::Setup( const char *pFileName, size_t tTheLength )
{
	assert( pFileName );
	if ( !pFileName ) { PIERROR( PIAPI_EINVAL ); return; };
	iFileDes = open( pFileName, O_RDONLY );	
	if ( iFileDes == -1 ) { PIOSERR; return; };
	tAddress = (caddr_t)mmap( 0, tTheLength, PROT_READ,
		MAP_SHARED, iFileDes, 0 );
	if ( tAddress==((caddr_t)-1) ) { PIOSERR; tAddress = 0; return; };
	tLength = tTheLength;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:		public 
 Description:
\*____________________________________________________________________________*/
UnixTimer::UnixTimer()
:	bOK( true )
{
	Reset();
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:		public 
 Description:
\*____________________________________________________________________________*/
bool UnixTimer::Reset()
{
	if ( !bOK ) return false;
	bOK = ::gettimeofday( &tv, 0 )==0;
	if ( !bOK )
		{ PIOSERR; };
	return bOK;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:		public 
 Description:
\*____________________________________________________________________________*/
bool UnixTimer::Elapsed( long *plSeconds, long *plMicroSeconds )
{
	if ( !bOK ) return false;
	struct timeval tv_tmp;
	if ( ::gettimeofday( &tv_tmp, 0 ) )
		{
		bOK = false;
		PIOSERR;
		return false;
		};
	long lSeconds = tv_tmp.tv_sec - tv.tv_sec;
	long lMicroSeconds = tv_tmp.tv_usec - tv.tv_usec;
	if ( lMicroSeconds<0 )
		{
		lMicroSeconds += 1000000;
		lSeconds -= 1;
		};
	if ( plSeconds ) { *plSeconds = lSeconds; };
	if ( plMicroSeconds ) { *plMicroSeconds = lMicroSeconds; };
	return true;
}

#endif	/* #if defined(CONFIG_OS_POSIX) */

⌨️ 快捷键说明

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