Index: darwinports.tcl
===================================================================
RCS file: /Volumes/src/cvs/od/proj/darwinports/base/src/darwinports1.0/darwinports.tcl,v
retrieving revision 1.195
diff -r1.195 darwinports.tcl
49a50,51
> 
> 	array set portindex_cache [list]
1093c1095,1096
< proc dportsearch {pattern {case_sensitive yes} {matchstyle regexp} {field name}} {
---
> 
> proc dportsearch_old {pattern {case_sensitive yes} {matchstyle regexp} {field name}} {
1161a1165,1306
> 	}
> 
> 	return $matches
> }
> 
> 
> proc _load_portindex_cache {} {
> 	global darwinports::portindex_cache darwinports::sources
> 
> 	# if we've already loaded the portindex files, we don't need to run again
> 	if {[array size darwinports::portindex_cache] != 0} {
> 		return
> 	}
> 
> 	set found 0
> 	foreach source $sources {
> 		# if we have a remote source, don't cache
> 		if {[darwinports::getprotocol $source] == "dports"} {
> 			continue
> 		}
> 
> 		ui_debug "Caching $source"
> 		
> 		if {[catch {set fd [open [darwinports::getindex $source] r]} result]} {
> 			ui_warn "Can't open index file for source: $source"
> 			continue
> 		} 
> 
> 		incr found 1
> 
> 		switch -regexp -- [darwinports::getprotocol ${source}] {
> 			{^rsync$} {
> 				# Rsync files are local
> 				set source_url "file://[darwinports::getsourcepath $source]"
> 			}
> 			default {
> 				set source_url $source
> 			}
> 		}
> 		
> 		while {[gets $fd line] >= 0} {
> 			set name [lindex $line 0]
> 			gets $fd line
> 							
> 			array set portinfo $line
> 		
> 			if {[info exists portinfo(portarchive)]} {
> 				set porturl ${source_url}/$portinfo(portarchive)
> 			} elseif {[info exists portinfo(portdir)]} {
> 				set porturl ${source_url}/$portinfo(portdir)
> 			}
> 			if {[info exists porturl]} {
> 				lappend line porturl $porturl
> 			}
> 			set darwinports::portindex_cache($name) $line
> 		}
> 		close $fd
> 	}
> 
> 	if {!$found} {
> 		return -code error "No index(es) found! Have you synced your source indexes?"
> 	}
> 
> 	return
> }
> 
> # look up ports via all of the remote sources
> proc dportsearch_remote {pattern} {
> 	global darwinports::portdbpath darwinports::sources
> 	set matches [list]
> 	
> 	foreach source $sources {
> 		if {[darwinports::getprotocol $source] == "dports"} {
> 			array set attrs [list name $pattern]
> 			set res [darwinports::index::search $darwinports::portdbpath $source [array get attrs]]
> 			eval lappend matches $res
> 		}
> 	}
> 
> 	return $matches
> }
> 
> # match ports with case sensitivity on field name
> proc dportsearch_exact {name} {
> 	# load all the portindex caches
> 	set x [_load_portindex_cache]
> 
> 	global darwinports::portindex_cache
> 
> 	# if the port doesn't exist in the cache, return nothing
> 	if {[info exists darwinports::portindex_cache($name)]} {
> 		return [list $name $darwinports::portindex_cache($name)]
> 	} else {
> 		return [list]
> 	}
> }
> 
> proc dportsearch {pattern {case_sensitive yes} {matchstyle regexp} {field name}} {
> 	global darwinports::portdbpath darwinports::sources darwinports::portindex_cache
> 	
> 	set matches [dportsearch_remote $pattern]
> 
> 	# load all the portindex caches
> 	set x [_load_portindex_cache]
> 
> 	if {$field == "name" && $case_sensitive == "yes" && $matchstyle == "exact"} {
> 		# have an optimized path if we can lookup the name exactly
> 		set matches [concat $matches [dportsearch_exact $pattern]]
> 	} else {
> 		# do an expensive search to match against other fields or without case sensitivity
> 		set easy [expr { $field == "name" }]
> 
> 		# step through each item in the cache
> 		foreach {name line} [array get darwinports::portindex_cache] {
> 			if {$easy} {
> 				set target $name
> 			} else {
> 				array set portinfo $line
> 				if {![info exists portinfo($field)]} continue
> 				set target $portinfo($field)
> 			}
> 					
> 			switch $matchstyle {
> 				exact	{ set matchres [expr 0 == ( {$case_sensitive == "yes"} ? [string compare $pattern $target] : [string compare -nocase $pattern $target] )] }
> 				glob	{ set matchres [expr {$case_sensitive == "yes"} ? [string match $pattern $target] : [string match -nocase $pattern $target]] }
> 				regexp	-
> 				default	{ set matchres [expr {$case_sensitive == "yes"} ? [regexp -- $pattern $target] : [regexp -nocase -- $pattern $target]] }
> 			}
> 					
> 			if {$matchres == 1} {
> 				if {$easy} {
> 					array set portinfo $line
> 				}
> 				if {[info exists porturl]} {
> 					ui_debug "Found port in $porturl"
> 				} else {
> 					ui_debug "Found port info: $line"
> 				}
> 				lappend matches $name
> 				lappend matches $line
> 			}
> 		}
