File iteration class

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

File iteration class

Post by cha0s »

I wrote this to make using DIR a bit simpler.

file_iter.bi:

Code: Select all

'' ruben.coder@gmail.com

namespace chi
	
	'' never use this in multiple threads or this on one thread
	'' and DIR() on another, etc...
	
	type FILE_ITER
		
		declare constructor( byref path as string )
		
		declare operator for( )
		declare operator step( )
		declare operator next( byref end_cond as FILE_ITER ) as integer
		
		declare function filename( ) as string
		
		as string p_pathname, p_latest
		
	end type
	
end namespace

#inclib "file_iter"
file_iter.bas:

Code: Select all

'' ruben.coder@gmail.com

#include "file_iter.bi"

namespace chi
	
	constructor FILE_ITER( byref path as string )
		
		p_pathname = path
		
	end constructor
	
	operator FILE_ITER.for( )
		
		p_latest = dir(p_pathname & "/*.*")
		
	end operator
	
	operator FILE_ITER.step( )
		
		p_latest = dir("")
		
	end operator
	
	operator FILE_ITER.next( byref end_cond as FILE_ITER ) as integer
		
		return p_latest <> ""
		
	end operator
	
	function FILE_ITER.filename( ) as string
		
		return p_latest
		
	end function
	
end namespace
the test:

Code: Select all

#include "file_iter.bi"

using chi

'' change it to any directory
for i as FILE_ITER = "d:\media\music\cha0s" to ""
	? i.filename( )
next
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

Neat!
I guess I have a long way to go with OOP before code snippets like this fail to surprise me.
Post Reply