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

📄 platform.cpp

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

 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/Platform.cpp,v $
 * $Date: 2003/05/13 18:42:14 $
 *
 Description:
	This file provides the implementation of the abstract platform interface
	for a specific operating system. Most methods are static.
	
	Custom Memory Management
	========================
	To enable/disable custom memory management, search to the line
	with pattern 'Custom', set value of #if to 0 to disable, 1 to enable

\*___________________________________________________________________________*/
//$SourceTop:$

#include "OSConfig.h"

/*
** System includes
*/
#if defined(CONFIG_OS_POSIX)
#	include <sys/types.h>
#	include <sys/stat.h>
#	include <fcntl.h>
#	include "POSIXOSE.h"

#elif defined(CONFIG_OS_WIN32)
#	include <windows.h>
#	include "Win32OSE.h"

#elif defined(CONFIG_OS_WIN16)
#	include <windows.h>
#	include <io.h>
#	include <process.h>
#	include <dir.h>
#	include "Win16OSE.h"
#	define PATH_MAX MAXPATH

#endif

/*
** C library includes
*/
#include <ctype.h>
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>

/*
** Local includes
*/
#include "PICompat.h"
#include "Platform.h"
#include "PlatDefs.h"
#include "PIStrStr.h"
#if defined(CONFIG_POSIXISH)
#	include "POSIXFSE.h"
#else
#	include "Win32FSE.h"
#endif

/* --- static data members --- */
static Allocator *___pTheGlobalAllocator = 0;	/* global allocator */
static FileSysType *pFSType=0;			/* derived file system type for OS */
static Semaphore *pUnsafeBlockMutex=0;	/* guard unsafe system/library calls */
#if defined(CONFIG_MULTITHREADED)
static int ___tLastErrorKey;			/* for thread specific last error */
#else
static int ___iLastError;		   		/* for last error code */
#endif
static time_t tLastTime = 0;
static int iLastCount = 0;

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
	Most methods are static, so the constructor has little to do.
\*___________________________________________________________________________*/
Platform::Platform()
{
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, private
 Description:
	Sets up the platform description string.
\*___________________________________________________________________________*/
static PIString *pInternalPlatformDescription=0;
static void Internal_InitDescription()
{
	PIOStrStream os;
	os << PLATFORM_OS << " (" << PLATFORM_MACHINE << ")" << ends;
	pInternalPlatformDescription = PI_NEW( PIString( os.str() ) );
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, private
 Description:
	Cleans up the platform description string.
\*___________________________________________________________________________*/
static void Internal_CleanupDescription()
{
	PI_DELETE( pInternalPlatformDescription );
	pInternalPlatformDescription = 0;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
	Returns a string which is some concatenation of the operating system
	and hardware machine names.
\*___________________________________________________________________________*/
const char *Platform::GetDescription()
{
	assert( pInternalPlatformDescription );
	if ( !pInternalPlatformDescription ) 
		{ return 0; };
	return *pInternalPlatformDescription;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Return 0 on success, else error code.
\*___________________________________________________________________________*/
int Platform::BeforeUnsafeBlock()
{
	assert( pUnsafeBlockMutex );
	assert( pUnsafeBlockMutex->IsOK() );
	return pUnsafeBlockMutex->Lock();
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Return 0 on success, else error code.
\*___________________________________________________________________________*/
int Platform::AfterUnsafeBlock()
{
	assert( pUnsafeBlockMutex );
	assert( pUnsafeBlockMutex->IsOK() );
	return pUnsafeBlockMutex->UnLock();
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*___________________________________________________________________________*/
bool Platform::GetTimeString( PIString &sResult)
{
	time_t timer;
	if ( time( &timer ))
		{
#if defined(CONFIG_MULTITHREADED) && !defined(CONFIG_MT_USER)
		PIMUSTBETRUE( !BeforeUnsafeBlock() );
#endif
		sResult = ctime( &timer );
#if defined(CONFIG_MULTITHREADED) && !defined(CONFIG_MT_USER)
		PIMUSTBETRUE( !AfterUnsafeBlock() );
#endif
		return true;
		}
	else
		{
		return false;
		};
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*___________________________________________________________________________*/
bool Platform::GetPrettyTimeString( PIString &sResult )
{
	time_t t;
	struct tm *local;
	enum { LEN_DATA=1023 };
	char szDataBuf[LEN_DATA+1];

	if ( time( &t ))
		{
		local = localtime(&t);
		strftime(szDataBuf, LEN_DATA,
			// Mon Nov 29 01:35 GMT
			"%a %b %d %H:%M %Z",
			local);
		szDataBuf[LEN_DATA]='\0';
		sResult = szDataBuf;
		return true;
		}
	else
		{
		return false;
		};
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Allocate memory. This function is used to interface operating system
	functionality for allocating large chunks of memory. 
\*___________________________________________________________________________*/
void *Platform::AllocMemory( size_t tSize )
{
	return malloc( tSize );
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Free memory allocated using AllocMemory.
\*___________________________________________________________________________*/
bool Platform::FreeMemory( void *pV )
{
	free( pV );
	return true;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis: 	static, public
 Description:
\*___________________________________________________________________________*/
Allocator *Platform::GetGlobalAllocator()
{
	return ___pTheGlobalAllocator;
}

/*___________________________________________________________________________*\
 *
 Description:
	The following hook functions can be re-assigned to implement
	custom memory management and debug logging.
\*___________________________________________________________________________*/
/* --- these are the hook functions to re-assign --- */
extern void *(* hook_new)( size_t tSize );
extern void (* hook_delete)( void * );
#if !defined(NDEBUG)
extern void (* hook_PIBeforeNew)(int, const char*);
extern void (* hook_PIBeforeDelete)(int, const char*);
extern void (* hook_PIMark)(int, const char*);
#endif

/* --- save the old values in these variables --- */
static void *(* save_hook_new)( size_t tSize )=0;
static void (* save_hook_delete)( void * )=0;
#if !defined(NDEBUG)
static void (* save_hook_PIBeforeNew)(int, const char*)=0;
static void (* save_hook_PIBeforeDelete)(int, const char*)=0;
static void (* save_hook_PIMark)(int, const char*)=0;
#endif

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:	Internal
\*___________________________________________________________________________*/
void (* fnPlatform)(void) = 0;
void Internal_DoPlatform()
{
	pUnsafeBlockMutex = Platform::AllocLocalMutex();
	assert( pUnsafeBlockMutex );
	assert( pUnsafeBlockMutex->IsOK() );
#if defined(CONFIG_OS_POSIX)
	pFSType = PI_NEW( POSIXFileSysType );
#elif defined(CONFIG_OS_WIN32)
	pFSType = PI_NEW( Win32FATFileSysType );
#else /* CONFIG_OS_WIN16) */
	pFSType = PI_NEW( Win16FATFileSysType );
#endif
	Internal_InitDescription();
	assert( fnPlatform );

	/* --- Setup last error functionality --- */
#if defined(CONFIG_MULTITHREADED)
	___tLastErrorKey = Platform::AllocThreadKey();
#endif
	if ( fnPlatform )
		{ (fnPlatform)(); };
#if defined(CONFIG_MULTITHREADED)
	Platform::DeleteThreadKey( ___tLastErrorKey );
#endif

	Internal_CleanupDescription();
	PI_DELETE( pFSType );
	PI_DELETE( pUnsafeBlockMutex );
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*___________________________________________________________________________*/
const char *pProgramArgv0 = 0;	/* Used by GNU DLD */
void Platform::Enter( const char *pArgv0, void (* fn)(void) )
{
	/* --- allocate the global allocator --- */
	assert( !___pTheGlobalAllocator );
	___pTheGlobalAllocator = PI_NEW( Allocator() );

	/* --- set the hook functions --- */
	save_hook_new = hook_new;
	save_hook_delete = hook_delete;
#if !defined(NDEBUG)
	save_hook_PIBeforeNew = hook_PIBeforeNew;
	save_hook_PIBeforeDelete = hook_PIBeforeDelete;
	save_hook_PIMark = hook_PIMark;
#endif
	
#if 0	/* Do Custom memory managment ?, 1=yes, 0=no */
	hook_new = PICustomNew;
	hook_delete = PICustomDelete;
#if !defined(NDEBUG)
	hook_PIBeforeNew = PICustomBeforeNew;
	hook_PIBeforeDelete = PICustomBeforeDelete;
	hook_PIMark = PICustomMark;
#endif
#endif

	pProgramArgv0 = pArgv0;
	fnPlatform = fn;
	Internal_EnterThreads( Internal_DoPlatform );

	/* --- reset the hook functions --- */
	hook_new = save_hook_new;
	hook_delete = save_hook_delete;
#if !defined(NDEBUG)
	hook_PIBeforeNew = save_hook_PIBeforeNew;
	hook_PIBeforeDelete = save_hook_PIBeforeDelete;
	hook_PIMark = save_hook_PIMark;
#endif

	/* --- de-allocate the global allocator --- */
	assert( ___pTheGlobalAllocator );
	PI_DELETE( ___pTheGlobalAllocator );
	___pTheGlobalAllocator = 0;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
void *Platform::PICustomNew( size_t tS )
{
	assert( Platform::GetCurrentAllocator() );
	return Platform::GetCurrentAllocator()->Allocate( tS );
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
void Platform::PICustomDelete( void *pData )
{
//  if ( !Platform::GetCurrentAllocator() ) { free( pData ); return; };   // L
#if defined(NDEBUG)
    /* ---
    This for in case of delete 0 which is valid and needs to be handled.
    Don't return for development builds because we still want to write
    something to the logfiles
    --- */
    if ( !pData ) return;
#endif
    assert( GetCurrentAllocator() );
	GetCurrentAllocator()->DeAllocate( pData );
}

#if !defined(NDEBUG)
/*
** These functions are used in development builds only
*/
/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
void Platform::PICustomBeforeNew( int iLine, const char *pFileName )
{
    if ( !GetCurrentAllocator() )
		{ return; };
    assert( GetCurrentAllocator() );
    GetCurrentAllocator()->BeforeAllocate( iLine, pFileName );
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
void Platform::PICustomBeforeDelete( int iLine, const char *pFileName )
{
    if ( !GetCurrentAllocator() )
		{ return; };
	 assert( GetCurrentAllocator() );
    GetCurrentAllocator()->BeforeDeAllocate( iLine, pFileName );
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
void Platform::PICustomMark( int iLine, const char *pFileName )
{
    if ( !GetCurrentAllocator() )
		{ return; };
    assert( GetCurrentAllocator() );
    GetCurrentAllocator()->Mark( iLine, pFileName );
}
#endif

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
DLL *Platform::AllocDLL( const char *pPath )
{ 
	assert( pPath );
#if defined(CONFIG_OS_POSIX)
	return PI_NEW( UnixDLL( pPath ) );
#elif defined(CONFIG_OS_WIN32)
	return PI_NEW( Win32DLL( pPath ) );
#else
	return 0;
#endif
}
void *Platform::AllocDLL( void *pPath ) 	// MSVC4.0 hack
{ return Platform::AllocDLL( (const char *)pPath ); }

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:      static, public:

⌨️ 快捷键说明

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