// HLA Demonstration program. // // This short program demonstrates two things: // // (1) How to call the Windows "FindFirstFile", // "FindNextFile", and "FindClose" routines // to process filenames, possibly containing // wildcard characters. // // (2) The use of HLA iterators to process // iterative objects (e.g., a file name list). // // Randall Hyde // 10/7/99 program FindFirstDemo; #include( "stdio.hhf" ); #include( "conv.hhf" ); #include( "memory.hhf" ); #include( "win32.hhf" ); // External declarations for Windows API calls: procedure FindFirstFile( var WFD:win.Win32FindData; FileName:string ); returns( "eax" ); // File Handle. external( "_FindFirstFileA@8" ); procedure FindNextFile( var WFD:win.Win32FindData; handle:dword ); returns( "al" ); // Boolean result, true=got a file. external( "_FindNextFileA@8" ); procedure FindClose( handle:dword ); external( "_FindClose@4" ); // These macros preserve registers that might get tweaked // by Windows API calls. macro pusha2; push( ebx ); push( ecx ); push( edx ); push( esi ); push( edi ); endmacro; macro popa2; pop( edi ); pop( esi ); pop( edx ); pop( ecx ); pop( ebx ); endmacro; // The following iterator returns a string (in EAX) that corresponds // to a filename. The parameter passed to this iterator is a filename, // one that typically contains wildcard characters (i.e., "*" and "?" ). // This iterator returns a list of filenames that match the, possibly // ambiguous, filename passed as a parameter. This iterator fails // when there are no more matching filenames. iterator FileInList( FileList:string ); nodisplay; var handle: dword; FileData: win.Win32FindData; begin FileInList; // Find the first matching file (if one exists). // Save the handle for use by FindNextFile. pusha2; FindFirstFile( FileData, FileList ); popa2; mov( eax, handle ); // If we matched at least one filename, return // the corresponding string and call the // FindNextFile routine to match any additional // filenames. if( eax <> win.INVALID_HANDLE_VALUE ) then repeat // FindFirstFile & FindNextFile return // zero-terminated strings. Convert these // to HLA compatible strings and return // the converted string: a_cStrToStr( FileData.FileName ); // Return the converted string to the FOREACH // loop and then free the storage associated // with the string. push( eax ); // Save, so we can free string on return yield(); // Return string to FOREACH loop. pop( eax ); // Free the storage used by the string. strfree( eax ); // Get the next filename in the list: pusha2; FindNextFile( FileData, handle ); popa2; until( al = false ); // When we've processed all the filenames in the list, // call FindClose to free the handle and other resources. pusha2; FindClose( handle ); popa2; endif; end FileInList; var FindData: win.Win32FindData; begin FindFirstDemo; // Simple foreach loop that demonstrates the FileInList iterator // by printing out all the filenames in the current directory. stdout.put( "FindFirst Demo:", nl, nl ); foreach FileInList( "*.*" ) do stdout.put( "file: ", (type string eax), nl ); endfor; end FindFirstDemo;