[interchange-cvs] interchange - racke modified code/UserTag/find.tag

interchange-core@icdevgroup.org interchange-core@icdevgroup.org
Sun Feb 23 19:37:01 2003


User:      racke
Date:      2003-02-24 00:36:26 GMT
Modified:  code/UserTag find.tag
Log:
added search criteria name and type
added documentation

Revision  Changes    Path
1.2       +67 -7     interchange/code/UserTag/find.tag


rev 1.2, prev_rev 1.1
Index: find.tag
===================================================================
RCS file: /var/cvs/interchange/code/UserTag/find.tag,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- find.tag	18 Feb 2003 15:45:44 -0000	1.1
+++ find.tag	24 Feb 2003 00:36:25 -0000	1.2
@@ -1,10 +1,17 @@
-UserTag find Order type
 UserTag find addAttr
+UserTag find PosNumber 0
 UserTag find hasEndTag
 UserTag find Routine <<EOR
+
+my %typemap = (b => '-b _', c => '-c _', d => '-d _', dir => '-d _',
+	p => '-p _', pipe => '-p _',	
+	f => '-f _', file => '-f _', 
+	l => '-l _', symlink => '-l _', 
+	s => '-S _', S => '-S _', socket => '-S _');
+					
 sub {
-	my ($type, $opt, $list) = @_;
-	my (@paths, @stack, @files, $file, $entry);
+	my ($opt, $list) = @_;
+	my (@paths, @stack, @files, $file, $entry, @statinfo, $nmatch);
 
 	# take array reference or string as list for the paths to scour for files
 	if (ref($list)) {
@@ -27,6 +34,18 @@
 	} else {
 		@stack = @paths;
 	}
+	
+	if ($opt->{name}) {
+		$nmatch = $opt->{name};
+		$nmatch =~ s/\./\\./g;
+		$nmatch =~ s/\*/.*/g;
+		$nmatch =~ s/\?/./g;		
+		$nmatch =~
+			s[({(?:.+?,)+.+?})]
+			 [ local $_ = $1; tr/{,}/(|)/; $_ ]eg;
+		$nmatch =~ s/\s+/|/g;
+		$nmatch = qr($nmatch);
+	}
 
 	# now build a list of all files matching the given criteria
 	while (@stack) {
@@ -41,14 +60,55 @@
 				push (@stack, "$file/$entry");
 			}
 			closedir(DIR);
-			push (@files, $file);
-		} elsif (-e $file) {
-			push (@files, $file);
-		} else {
+		} elsif (! -e $file) {
 			::logError("No such file or directory: %s", $file);
+			next;
 		}
+
+		if ($opt->{type}) {
+			# make stat call ahead so info is accessible via _ filehandle
+			unless (@statinfo = stat($file)) {
+				::logError("Stat call failed on file %s: %s", $file, $!);
+				next;
+			}
+		}	
+
+		# match file against our specifications
+		if ($nmatch) {
+			next unless $file =~ /$nmatch/;
+		}
+
+		if ($opt->{type}) {
+			next unless eval "$typemap{$opt->{type}}";
+		}
+
+		push (@files, $file);
 	}
 
 	wantarray ? @files : join(' ', @files);
 }
 EOR
+UserTag find Documentation <<EOD
+
+=pod
+
+This tag produces a list of files which match the given criteria.
+
+	[find name="*.html"]pages[/find]
+
+Parameters for this tag are:
+
+=over 4
+
+=item name I<PATTERN>
+
+Excludes any files not matching I<PATTERN>.
+
+=item type I<TYPE>
+
+Excludes any files not of type I<TYPE>.
+
+=back
+
+=cut
+EOD