#include Once "window9.bi"

Dim Shared As WIN32_FIND_DATA g_FileInformation

Function Examinedirectory(Byref DirectoryName As USTRING, Byref Pattern As USTRING) As Integer Export
	Dim As USTRING strPattern = DirectoryName + Pattern
	#ifdef UNICODE
		Return Cast(Integer,FindFirstFile(*strPattern, @g_FileInformation))
	#else	
		Return Cast(Integer,FindFirstFile(Strptr(strPattern), @g_FileInformation))
	#endif
End Function

Function Nextdirectoryentry(Byval HandleDirectory As Integer) As Integer Export
	Return FindNextFile(Cast(HANDLE,HandleDirectory), @g_FileInformation)
End Function

Function Directoryentryattributes(Byval HandleDirectory As Integer) As Uinteger Export
	Return g_FileInformation.dwFileAttributes
End Function

Sub Finishdirectory(Byval HandleDirectory As Integer) Export
	FindClose(Cast(HANDLE,HandleDirectory))
End Sub

Function Directoryentrysize(Byval HandleDirectory As Integer) As Ulongint Export
	Return Culngint( Culngint(g_FileInformation.nFileSizeLow) Or (Culngint(g_FileInformation.nFileSizeHigh) Shl 32) )
End Function

Function Directoryentrydate(Byval HandleDirectory As Integer , Byval flag As Long) As USTRING Export
	Dim As FILETIME fttime
	Dim As FILETIME ftlocal
	Dim As SYSTEMTIME SYS_TIME
	Dim As USTRING hour_,minute_,second_,month_,Day_
	Select Case flag
		Case 1
			fttime=g_FileInformation.ftCreationTime
		Case 2
			fttime=g_FileInformation.ftLastAccessTime
		Case 3
			fttime=g_FileInformation.ftLastWriteTime
	End Select
	
	FileTimeToLocalFileTime(@fttime,@ftlocal)
	FileTimeToSystemTime(@ftlocal,@SYS_TIME)
	If SYS_TIME.wday<10 Then
		Day_="0" & SYS_TIME.wday
	Else
		Day_=Str( SYS_TIME.wday)
	Endif
	If SYS_TIME.wmonth<10 Then
		month_="0" & SYS_TIME.wmonth
	Else
		month_=Str( SYS_TIME.wmonth)
	Endif
	If SYS_TIME.whour<10 Then
		hour_="0" & SYS_TIME.whour
	Else
		hour_=Str( SYS_TIME.whour)
	Endif
	If SYS_TIME.wminute<10 Then
		minute_="0" & SYS_TIME.wminute
	Else
		minute_=Str( SYS_TIME.wminute)
	Endif
	If SYS_TIME.wsecond<10 Then
		second_="0" & SYS_TIME.wsecond
	Else
		second_=Str(SYS_TIME.wsecond)
	Endif
	day_ = day_ & "." & month_ & "." & SYS_TIME.wyear & " " & hour_ & ":" & minute_ & ":" & second_
	Return day_
End Function

Function Directoryentryname(Byval  HandleDirectory As Integer) As USTRING Export
	Dim As USTRING sRet = g_FileInformation.cFileName
	Return sRet
End Function

Function w9isFile(sFileName As UString) As Long
	
	Dim As WIN32_FIND_DATA g_FileInformation
	
	Dim As Ustring sTemp = Rtrim(sFileName, Any "\/")
	
	#ifdef UNICODE
		Dim As Handle hFind = FindFirstFile(*sTemp, @g_FileInformation)
	#else
		Dim As Handle hFind = FindFirstFile(Strptr(sTemp), @g_FileInformation)
	#endif
	
	If hFind <> Cast(Any Ptr, -1) Then
		
		If g_FileInformation.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY  Then
		Else
			Function = 1
		Endif
		
		FindClose(hFind)
		
	Endif
	
End Function

Function w9isFileExists(sFileName As UString) As Long
	
	Dim As HANDLE h = Read_file(sFileName)
	
	If h <> Cast(Any Ptr, -1) Then
	   
	   Function = 1
	   
	   Close_file(h)
	   
	Endif
	
End Function

Function w9isDirExists(sFileName As UString) As Long
	
	Dim As WIN32_FIND_DATA g_FileInformation
	
	Dim As Ustring sTemp = Rtrim(sFileName, Any "\/")
	
	#ifdef UNICODE
		Dim As Handle hFind = FindFirstFile(*sTemp, @g_FileInformation)
	#else
		Dim As Handle hFind = FindFirstFile(Strptr(sTemp), @g_FileInformation)
	#endif
	
	If hFind <> Cast(Any Ptr, -1) Then
		
		If g_FileInformation.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY  Then				
			
			Function = 1
			
		Endif
		
		FindClose(hFind)
		
	Endif
	
End Function
