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

📄 win32fse.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/Win32FSE.cpp,v $
 * $Date: 2003/05/31 09:12:32 $
 *
 Description:
\*____________________________________________________________________________*/
//$SourceTop:$

#include "OSConfig.h"

#if defined(CONFIG_OS_WIN32)	/* must be Win32 OS */

#include <assert.h>
#include "Win32FSE.h"
#include "picompat.h"
#include "PlatDefs.h"

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
Win32FileSystemEntity::Win32FileSystemEntity(
		const Win32FileSystemEntity &tFSE )
:	FileSystemEntity( tFSE ),
	bPopulated( tFSE.bPopulated ),
	hFileInDirectory( tFSE.hFileInDirectory )
{
	memcpy( &tData, &(tFSE.tData), sizeof( WIN32_FIND_DATA ) );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
Win32FileSystemEntity::Win32FileSystemEntity(
	const FileSysType &tTheFSType,
	const PIString &sFullPath )
:	FileSystemEntity( tTheFSType, sFullPath ),
	hFileInDirectory( INVALID_HANDLE_VALUE ),
	bPopulated( false )
{
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis: 	private
 Description:
\*____________________________________________________________________________*/
Win32FileSystemEntity::Win32FileSystemEntity(
		const FileSysType &tTheFSType,
		const char *pPath,
		HANDLE hFind,
		WIN32_FIND_DATA &tFind
		)
:	FileSystemEntity( pPath, tTheFSType ),		/* protected constructor */
	hFileInDirectory( hFind ),
	bPopulated( true )
{
	NewFileInSamePathRoot( tFind.cFileName );
	memcpy( &tData, &tFind, sizeof( WIN32_FIND_DATA ) );
	bPopulated = true;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
Win32FileSystemEntity::~Win32FileSystemEntity()
{
	if ( hFileInDirectory!=INVALID_HANDLE_VALUE )
		{ FindClose( hFileInDirectory ); };
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
bool Win32FileSystemEntity::Populate()
{ 
	assert( !bPopulated );
	HANDLE hFind=FindFirstFile( GetPath(), &tData );
	if ( hFind==INVALID_HANDLE_VALUE )
		{
		PIOSERR;
		return false;
		}
	else
		{
		FindClose( hFind );
		bPopulated=true;
		return true;
		};
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
bool Win32FileSystemEntity::Exists()
{
	if ( !bPopulated && !Populate() ) return false;
	/* --- a tricky test for (invalid) names ending on '.' --- */
	const char *pPath = GetPath();
	if ( pPath && strlen(pPath) && (
		pPath[strlen(pPath)-1] == '.' ||
		strchr(pPath, '*') )) return false;
	return true;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
bool Win32FileSystemEntity::IsRegularFile()
{
	if ( !bPopulated && !Populate() ) return false;
	return	(
		!(tData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && 
	   	!(tData.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM))
		   	);
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
bool Win32FileSystemEntity::IsDirectory()
{
	if ( !bPopulated && !Populate() ) return false;
	return 	tData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;

	/*
	** A directory is a directory, whether its hidden or not..., so
	** remove this logic
	**
	return 	(
		(tData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && 
	   	!(tData.dwFileAttributes&(FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM))
		   	);
	*/
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
bool Win32FileSystemEntity::IsLink()
{
	if ( !bPopulated && !Populate() ) return false;
	return false;		// the FAT filesystem does not support links
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Note that this only looks at file attributes, not security permissions
	of current process/thread on file.
\*____________________________________________________________________________*/
bool Win32FileSystemEntity::IsReadable()
{
	if ( !bPopulated && !Populate() ) return false;
	return	 !(tData.dwFileAttributes & (
				FILE_ATTRIBUTE_HIDDEN |
				FILE_ATTRIBUTE_SYSTEM |
				FILE_ATTRIBUTE_OFFLINE 
		   		) );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Note that this only looks at file attributes, not security permissions
	of current process/thread on file.
\*____________________________________________________________________________*/
bool Win32FileSystemEntity::IsWritable()
{
	if ( !bPopulated && !Populate() ) return false;
	return	 !(tData.dwFileAttributes & (
				FILE_ATTRIBUTE_HIDDEN |
				FILE_ATTRIBUTE_SYSTEM |
				FILE_ATTRIBUTE_OFFLINE |
				FILE_ATTRIBUTE_READONLY
		   		) );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Note that this only looks at file attributes, not security permissions
	of current process/thread on file.
\*____________________________________________________________________________*/
bool Win32FileSystemEntity::IsExecutable()
{
	if ( !bPopulated && !Populate() ) return false;
	return IsReadable();
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
unsigned long Win32FileSystemEntity::GetFileSize()
{
	if ( !bPopulated && !Populate() ) return false;
	/*
	** Note that this ignores the upper high dword of the filesize quadword.
	*/
	return (unsigned long)tData.nFileSizeLow;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
const int _aDaysInYear[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273,
	304, 334 /* , 365 */ };
int InternalGetEpochTime( FILETIME &tF )
{
	SYSTEMTIME tS;
	FileTimeToSystemTime( &tF, &tS );
	int iTmpYear = tS.wYear - 1900;
	if ( iTmpYear<0 ) { return -1; };
	if ( iTmpYear>138 ) { return -1; };
	int iTmpDays = _aDaysInYear[tS.wMonth-1] + tS.wDay;
	if ( !(iTmpYear%4) && (tS.wMonth>2) )
		{ iTmpDays++; };
	iTmpYear -= 70;
	int iTmp = ((iTmpYear-3)/4) + ((iTmpYear)*365) + iTmpDays;
	iTmp = (iTmp * 24) + tS.wHour;
	iTmp = (iTmp * 60) + tS.wMinute;
	iTmp = (iTmp * 60) + tS.wSecond;

	return iTmp;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
time_t Win32FileSystemEntity::GetLastModified()
{
	if ( !bPopulated && !Populate() ) return false;
	return InternalGetEpochTime( tData.ftLastWriteTime );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
FileSystemEntity *Win32FileSystemEntity::FirstFileInDirectory( int /*iFlags*/ )
{
	char szFindSuffix[]="\\*.*";
	PIString sTmp( GetPath() );
	sTmp.Concatenate( szFindSuffix );
	WIN32_FIND_DATA tTmp;
	HANDLE hFind=FindFirstFile( sTmp, &tTmp );
	if ( hFind==INVALID_HANDLE_VALUE )
		{
		assert(( GetLastError()==ERROR_NO_MORE_FILES )
		    || ( GetLastError()==ERROR_INVALID_NAME ));
		PIOSERR;
		return 0;
		}
	else
		{
		return PI_NEW( Win32FileSystemEntity(
			GetFSType(),	
			GetPath(),
			hFind,			/* find handle */
			tTmp			/* WIN32_FIND_DATA */
			) );
		};
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
bool Win32FileSystemEntity::NextSiblingFile()
{
	if ( hFileInDirectory==INVALID_HANDLE_VALUE )
		{
		assert( 0/* this is a bad thing to do */ );
		return false;
		}
	if ( FindNextFile( hFileInDirectory, &tData ) )
		{
		NewFileInSamePathRoot( tData.cFileName );		
		return true;
		}
	else
		{
		assert( GetLastError()==ERROR_NO_MORE_FILES );
		FindClose( hFileInDirectory );
		hFileInDirectory=INVALID_HANDLE_VALUE;
		return false;
		};
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
const char *Win32FileSystemEntity::GetAlternateName()
{
	if ( !bPopulated && !Populate() )
		{ return GetName(); };
	const char *pAltName=tData.cAlternateFileName;
	if ( pAltName && *pAltName )
		{ return pAltName; }
	else
		{ return GetName(); };
}

#endif	/* CONFIG_OS_WIN32 */

⌨️ 快捷键说明

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