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

📄 platform.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
 Description:
\*___________________________________________________________________________*/
void Platform::UserYieldThread()
{
#if defined(CONFIG_MULTITHREADED) && defined(CONFIG_MT_USER)
	Platform::YieldThread();
#endif
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:      static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::GetLastError()
{
#if defined(CONFIG_MULTITHREADED)
	int iValue;
	return GetMainThread()->GetData( ___tLastErrorKey, (void **)&iValue ) ?
		iValue : 0;
#else
	return ___iLastError;
#endif
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:      static, public:
 Description:
\*___________________________________________________________________________*/
void Platform::SetLastError( int iErrorCode )
{
#if defined(CONFIG_MULTITHREADED)
	GetMainThread()->SetData( ___tLastErrorKey, (void *)iErrorCode );
#else
	___iLastError = iErrorCode;
#endif
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
FileSystemEntity *Platform::GetFile( const PIString &sPath )
{
	assert( pFSType );
	FileSystemEntity *pFSE = 0;
#if defined(CONFIG_POSIXISH)
	pFSE = PI_NEW( POSIXFileSystemEntity( *pFSType, sPath ) );
#elif defined(CONFIG_OS_WIN32)
	pFSE = PI_NEW( Win32FileSystemEntity( *pFSType, sPath ) );
#else
	PIERROR( PIAPI_NOTSUPPORTED );
	return 0;
#endif
	if ( !pFSE )
		{ PIERROR( PIAPI_EXHAUSTED ); }
	else
		{ PICLEAR; };
	return pFSE;
}
void *Platform::GetFile( void *pV )	/* hack for MSVC4.0, link */
{ return Platform::GetFile( *((const PIString *)pV) ); };

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
MappedFile *Platform::AllocMappedFile( FileSystemEntity &tFSE )
{
#if defined(CONFIG_OS_POSIX)
	return PI_NEW( POSIXMappedFile( tFSE ) );
#elif defined(CONFIG_OS_WIN32)
	return PI_NEW( Win32MappedFile( tFSE ) );
#else
	(void)tFSE;
	PIERROR( PIAPI_NOTSUPPORTED );
	return 0;
#endif
}
void *Platform::AllocMappedFile( char &c )
{ return Platform::AllocMappedFile( (FileSystemEntity &)c ); };

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
	Fork a new process, as per POSIX.
\*___________________________________________________________________________*/
unsigned long Platform::Fork()
{
#if defined(CONFIG_OS_POSIX)
	int i = fork();
	if ( i==-1 ) { return 1; };
	return i;
#else
	return -1;
#endif
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
unsigned long Platform::GetPID()
{
#if defined(CONFIG_OS_POSIX)
	return (unsigned long)::getpid();
#elif defined(CONFIG_OS_WIN32)
	return (unsigned long)::GetCurrentProcessId();
#else
	return (unsigned long)-1;
#endif
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::GetUniqueId( const char *szBuf, unsigned int iLen )
{
	enum { BUF_SIZE=64 };
	if (iLen < BUF_SIZE) return BUF_SIZE;
	if (!szBuf) return PIAPI_EINVAL;

	/* ---
	Generates a new id that is a sequence of hexadecimal digits guaranteed
	to be unique for the lifetime of this object.
	--- */
#if defined(CONFIG_MULTITHREADED)
	PIMUSTBETRUE( !BeforeUnsafeBlock() );
#endif
	time_t tDummy;
	time_t tCurrentTime = time( &tDummy );

	if ( tCurrentTime==tLastTime )
		iLastCount++;
	else
		tLastTime=tCurrentTime;
		
	sprintf(
		(char *)szBuf,
		"%lx%lx",
		(long)(tCurrentTime & 0x00FFFFFF),
		(long)iLastCount);
#if defined(CONFIG_MULTITHREADED)
		PIMUSTBETRUE( !AfterUnsafeBlock() );
#endif
	return PIAPI_COMPLETED;
}

#if 0
/*__________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::GetErrno()
{
	return errno;
}
#endif

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
	This function is called early on in the process initialization, it
	perform some actions to make the process be a daemon and to avoid
	unwanted interactions from happening.
\*___________________________________________________________________________*/
bool Platform::DaemonInit()
{
#if defined(CONFIG_OS_POSIX)
	/*
	** Call fork() and then have the parent exit(), this makes us
	** independant of any shell that spawned us, it also makes sure the
	** process is not a process group leader
	*/
	switch( fork() )
		{
		case -1:
			/* error, exit */
			return false;
		case 0:
			/* the child, continue */
			break;
		default:
			/* the parent, exit */
			exit(0);
		};

	/*
	** Do the following actions
	** 	1) Become a session leader
	** 	2) Change the working directory
	** 	3) Clear out the file mode creation mask
	*/
//	setsid( 0 );	?? this doesn't seem to work so well
	setsid( );
	chdir( "/" );
	umask( 0 );

	return true;
#else
	return false;
#endif
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
	Allocate a timer.
\*___________________________________________________________________________*/
Timer *Platform::AllocTimer()
{
#if defined(CONFIG_OS_POSIX)
	return PI_NEW( UnixTimer() );
#elif defined(CONFIG_OS_WIN32)
	return PI_NEW( Win32Timer() );
#else
	return 0;
#endif
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::Open( const char *pPath, const char *pFlags )
{
	if ( !pPath ) 
		{
		assert( 0 );
		PIERROR( PIAPI_EINVAL );
		return PIAPI_ERROR;
		};
	if ( !pFlags )
		{ pFlags = "w"; };

#if defined(CONFIG_OS_POSIX)
	int iFlags = 0;
#elif defined(CONFIG_OS_WIN32)
	bool bRead = false;
	DWORD dwFlags = 0;
#endif
	for(; *pFlags; pFlags++ )
		{
		switch( toupper( *pFlags ) )
			{
#if defined(CONFIG_OS_POSIX)
			case 'A': iFlags |= O_CREAT | O_WRONLY | O_APPEND; break;
			case 'R': iFlags |= O_RDONLY; break;
			case 'W': iFlags |= O_CREAT | O_WRONLY | O_TRUNC; break;
			case 'N': iFlags |= O_EXCL; break;
#elif defined(CONFIG_OS_WIN32)
			/* --- note that these flags are not OR'ed --- */
			case 'A': dwFlags = OPEN_ALWAYS; break;
			case 'R': dwFlags = OPEN_EXISTING; bRead = true; break;
			case 'W': dwFlags = CREATE_ALWAYS; break;
			case 'N': dwFlags = CREATE_NEW; break;
#endif
			default:
				/* --- invalid flag --- */
				assert( 0 );
				PIERROR( PIAPI_EINVAL );
				return PIAPI_ERROR;
			};
		};
#if defined(CONFIG_OS_POSIX)
	int iFd = -1;
	for(;;)
		{
		iFd = ::open( pPath, iFlags, 0644  );	
		if ( iFd==-1 && errno==EINTR )
			{ continue; };
		break;
		};
	if ( iFd==-1 )
		{
		PIOSERR;
		return PIAPI_ERROR;
		};
	return iFd;
#elif defined(CONFIG_OS_WIN32)
	HANDLE hFile = INVALID_HANDLE_VALUE;
	if ( !bRead )
		{
		hFile = ::CreateFile(
			pPath,
			GENERIC_WRITE,
			FILE_SHARE_READ | FILE_SHARE_WRITE,
			NULL,
			dwFlags,
			FILE_ATTRIBUTE_NORMAL,
			NULL );
		}	
	else
		{
		hFile = ::CreateFile(
			pPath,
			GENERIC_READ,
			FILE_SHARE_READ | FILE_SHARE_WRITE,
			NULL,
			dwFlags,
			FILE_ATTRIBUTE_NORMAL,
			NULL );
		};
	if ( hFile==INVALID_HANDLE_VALUE )
		{
		PIOSERR;
		return PIAPI_ERROR;
		}
	else
		{
		if ( !bRead && ( dwFlags & OPEN_ALWAYS ) )
			{
			/*
			** Append mode was specified, move file pointer to the
			** end of the file
			*/
			DWORD dwWord = ::SetFilePointer( hFile, 0, NULL, FILE_END );
			assert( dwWord>=0 );
			if ( dwWord<0 )
				{
				PIOSERR;
				::CloseHandle( hFile );
				return PIAPI_ERROR;
				};
			};
		};

	return (int)hFile;
	
#endif
	PIERROR( PIAPI_NOTSUPPORTED );
	return PIAPI_ERROR;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::Close( int iFd )
{
#if defined(CONFIG_POSIXISH)
	int iRet = -1;
	for(;;)
		{
		iRet = ::close( iFd );
		/*
		** Restart on interupt for UNIX System V
		*/
		if ( iRet==-1 && errno==EINTR )
			{ continue; };
		break;
		};
	if ( iRet==-1 )
#elif defined(CONFIG_OS_WIN32)
	if ( !::CloseHandle( (HANDLE)iFd ) )
#endif
		{
		PIOSERR;
		return PIAPI_ERROR;
		};
	return PIAPI_COMPLETED;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::Unlink( const char *pPath )
{
	if ( !pPath ) 
		{
		assert( 0 );
		PIERROR( PIAPI_EINVAL );
		return PIAPI_ERROR;
		};

#if defined(CONFIG_POSIXISH)
	int iRet = -1;
	for(;;)
		{
		iRet = ::unlink( pPath );
		/*
		** Restart on interupt for UNIX System V
		*/
		if ( iRet==-1 && errno==EINTR )
			{ continue; };
		break;
		};
	if ( iRet==-1 )
#elif defined(CONFIG_OS_WIN32)
	if ( !::DeleteFile( pPath ) )
#endif
		{
		PIOSERR;
		return PIAPI_ERROR;
		};
	return PIAPI_COMPLETED;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::RetrieveCurrentDirectory( class PIString &sCurrentDirectory )
{
#if defined(CONFIG_POSIXISH)
	char szBuf[PATH_MAX+1];
	if ( ::getcwd( szBuf, PATH_MAX ) )
		{
		szBuf[PATH_MAX] = '\0';
		sCurrentDirectory = szBuf;
		}
	else
		{
		PIOSERR;
		return PIAPI_ERROR;
		};
#elif defined(CONFIG_OS_WIN32)
	char szBuf[MAX_PATH+1];
	int iLen=::GetCurrentDirectory( MAX_PATH, szBuf );
	if ( iLen>0 )
		{
		szBuf[MAX_PATH] = '\0';
		sCurrentDirectory = szBuf;
		}
	else
		{
		PIOSERR;
		return PIAPI_ERROR;
		};
#endif
	return PIAPI_COMPLETED;
}

char Platform::getDirectorySeparator()
{
	return pFSType ? pFSType->DirectorySeperatorCharacter() : '0';                            
};


/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::GetProcess()
{
#if defined(CONFIG_OS_POSIX)
	return (int)::getpid();
#elif defined(CONFIG_OS_WIN32)
	return (int)::GetCurrentProcess();
#else
	return (int)-1;
#endif
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:		static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::GetProcessId()
{
#if defined(CONFIG_OS_POSIX)
	return (int)::getpid();
#elif defined(CONFIG_OS_WIN32)
	return (int)::GetCurrentProcessId();
#else
	return (int)-1;
#endif
}


⌨️ 快捷键说明

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