#include ONCE "window9.bi"

#include "datetime.bi"

extern "C"
	
	type _stat
		
		st_dev as dev_t
		
		#ifndef __FB_64BIT__
		
		pad1 as long
		
		#endif
		
		st_ino as ino_t
		
		st_mode as mode_t
		
		st_nlink as nlink_t
		
		st_uid as uid_t
		
		st_gid as gid_t
		
		st_rdev as dev_t
		
		#ifndef __FB_64BIT__
		
		pad2 as long
		
		#endif
		
		st_size as off_t
		
		st_atime as time_t
		
		st_atime_usec as time_t
		
		st_mtime as time_t
		
		st_mtime_usec as time_t
		
		st_ctime as time_t
		
		st_ctime_usec as time_t
		
		st_blksize as uinteger 'blksize_t
		
		st_blocks as blkcnt_t
		
	end type
	
	declare function stat (byval as zstring ptr, byval as _stat ptr) as long 
	
end extern		


type TWIN9DIR
	
	sInitDir as zstring*2000
	
	sCurentDir as zstring*256
	
	iOutAttr as Uinteger 
	
	tBuf as _stat ptr
	
End Type

function ExamineDirectory(sDirectoryName as string , sPattern as string) as integer export
	
	dim as Uinteger iAttribMask , iOutAttr
	
	dim as string sDir , sDirplusmask
	
	dim as _stat ptr tempBuf
	
	if len(sDirectoryName) andalso len(sPattern) then
		
		dim wdir as TWIN9DIR ptr = new TWIN9DIR
		
		if trim(sPattern) = "*.*" then
			
			iAttribMask = FILE_ATTRIBUTE_ARCHIVE or FILE_ATTRIBUTE_READONLY Or FILE_ATTRIBUTE_HIDDEN Or FILE_ATTRIBUTE_SYSTEM Or FILE_ATTRIBUTE_DIRECTORY
			
		else
			
			iAttribMask = FILE_ATTRIBUTE_ARCHIVE or FILE_ATTRIBUTE_READONLY Or FILE_ATTRIBUTE_HIDDEN Or FILE_ATTRIBUTE_SYSTEM
			
		EndIf
		
		
		if right(sDirectoryName , 1) <> "/" then 
			
			sDirectoryName &= "/"
			
		EndIf
	
		wdir->sInitDir = sDirectoryName	
		
		sDirplusmask = sDirectoryName & sPattern
		
		sDir = Dir(sDirplusmask , iAttribMask, iOutAttr)
		
		wdir->sCurentDir = sDir
		
		wdir->iOutAttr = iOutAttr
		
		tempBuf = callocate (sizeof(_stat)+100) 

		stat(strptr(wdir->sInitDir), tempBuf)

		wdir->tBuf = tempBuf

		return cast(integer,wdir)
		
	endif
	
End Function

function DirectoryEntryAttributes (hDir as Integer) as Uinteger export
	
	return (cast(TWIN9DIR ptr, hDir))->iOutAttr
	
End Function

function NextDirectoryEntry(hDir as Integer) as integer export

	dim as Uinteger iOutattr
	
	dim as string sDir

	sDir = Dir(iOutattr)
	
	(cast(TWIN9DIR ptr, hDir))->sCurentDir = sDir

	(cast(TWIN9DIR ptr, hDir))->iOutAttr = iOutattr

	stat((cast(TWIN9DIR ptr, hDir))->sInitDir & (cast(TWIN9DIR ptr, hDir))->sCurentDir, ((cast(TWIN9DIR ptr, hDir))->tBuf))

	if (cast(TWIN9DIR ptr, hDir))->sCurentDir <> "" then 
		
		return 1
		
	EndIf

End Function

sub FinishDirectory(hDir as Integer) export
	
	deallocate (cast(TWIN9DIR ptr, hDir)->tBuf)
	
	delete (cast(TWIN9DIR ptr, hDir))
	
End Sub

function DirectoryEntryName(hDir as Integer) as string export
	
	return (cast(TWIN9DIR ptr, hDir))->sCurentDir
	
End Function

function DirectoryEntrySize(hDir as Integer) as Ulongint export
	
	return (cast(TWIN9DIR ptr, hDir))->tBuf->st_size
	
End Function

function DirectoryEntryDate(hDir as Integer , iFlag as long) as string export
	
	dim tinf as tm ptr
	
	dim dSerial as double
	
	dim as TWIN9DIR ptr wDir = cast(TWIN9DIR ptr, hDir)
	
	select case iFlag
		
		Case 1
		
			tinf = localtime(@(wDir->tBuf->st_atime))
		
		case 2
		
			tinf = localtime(@(wDir->tBuf->st_mtime))
		
		case 3
		
			tinf = localtime(@(wDir->tBuf->st_ctime))
		
	End Select
	
	if tinf then
		
		dSerial = DateSerial(1900 + tinf->tm_year, 1 + tinf->tm_mon, tinf->tm_mday) _
		+ TimeSerial(tinf->tm_hour, tinf->tm_min, tinf->tm_sec)
		
		dim as integer _
		_
		iDay = day(dSerial) ,_
		iMonth = month(dSerial) , _
		iYear = year(dSerial),_
		iHour = hour(dSerial),_
		iMinute = minute(dSerial),_
		iSecond = second(dSerial)
		
		dim as string sDay , sMonth , sHour , sMinute , sSecond
		
		If iDay < 10 Then
			
			sDay = "0" & iDay
			
		Else
			
			sDay = Str(iDay)
			
		EndIf
		
		If iMonth < 10 Then
			
			sMonth = "0" & iMonth
			
		Else
			
			sMonth = Str(iMonth)
			
		EndIf
		
		If iHour < 10 Then
			
			sHour = "0" & iHour
			
		Else
			
			sHour = Str(iHour)
			
		EndIf
		
		If iMinute < 10 Then
			
			sMinute = "0" & iMinute
			
		Else
			
			sMinute = Str(iMinute)
			
		EndIf
		
		If iSecond < 10 Then
			
			sSecond = "0" & iSecond
			
		Else
			
			sSecond = Str(iSecond)
			
		EndIf
		
		Return sDay & "." & sMonth & "." & iYear & " " & sHour & ":" & sMinute & ":" & sSecond
		
		
	EndIf
	
End Function