Skip to content

Instantly share code, notes, and snippets.

@arakov
Created May 22, 2015 07:27
Show Gist options
  • Save arakov/6be668b516cbb367fd42 to your computer and use it in GitHub Desktop.
Save arakov/6be668b516cbb367fd42 to your computer and use it in GitHub Desktop.
ELENA: File Searching
#define system.
#define system'text.
#define extensions'text.
#class FileEnumerator
{
#field(type:bytearray)theStruct.
#field theHandle.
#field theSearchPath.
#field theOpen.
#constructor new &path:aPath
[
theStruct := ByteArray new &length:592.
theSearchPath := aPath wide.
theOpen := false.
]
#method next
[
($nil == theHandle)
? [
#var(type:handle)aHandle := system'external'KERNEL32 FindFirstFileW
&wide:(theSearchPath wide)
&bytearray:theStruct.
(aHandle == -1)
! [ theOpen := true. ].
theHandle := Handle new &handle:aHandle.
]
! [
#var(type:int)aRetVal := system'external'KERNEL32 FindNextFileW
&handle:(theHandle handle)
&bytearray:theStruct.
(aRetVal != 0)
! [ theOpen := false. ].
].
^ theOpen.
]
#method get
[
theOpen
? [
#var(type:int)aFileLength := 0.
#var(type:bytearray,size:520)aNameBuffer.
theStruct read &index:44 &length:520 &bytearray:aNameBuffer.
// define length
aFileLength := system'external'KERNEL32 lstrlenW &bytearray:aNameBuffer.
^ UTF16Encoder::encoderOp toWide &bytearray:aNameBuffer &length:(aFileLength * 2).
]
! [ ^ $nil. ].
]
#method free
[
system'external'KERNEL32 FindClose &handle:(theHandle handle).
]
}
#symbol Program =
[
#var en := FileEnumerator new &path:"c:\*.*".
#loop (en next)?
[
console writeLine:(en get).
].
en free.
].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment