Search SC3 Code Script
Sunday, January 9, 2005, 01:25 PM
This is a simple perl script which can be run to search through the text of the Supercollider 3 Help files, or through your own collection of Supercollider .rtf files, looking for a specified character string. This is helpful, say if you'd like to know which Help files deal with, say, "EnvGen", or "SynthDef". It's also helpful if you want to find that bit of code that you wrote 6 months ago that had something to do with a "reverb" unit, or something like that.
Yes, the OS X "find" function should be able to do this, but I've found that it sometimes misses files for some reason.
This script is meant to be run from the command line.
#!/usr/bin/perl
###############################################
# searches rtf files in a given dir for specific text:
# Arguments:
# 1. directory to search
# 2. Text to search for
# example:
# ./search_sc_code.pl sc_code/new_sc_code BasicMIDISocket
#
# Requires RTF::TEXT::Converter and RTF::Parser
#
# Tom Gersic
################################################
use RTF::TEXT::Converter;
my $mrString;
my $object = RTF::TEXT::Converter->new(output => \$mrString);
my $dir = shift;
my $searchString = quotemeta(shift);
print $searchString;
print "listing files:\n";
process_directory($dir,$searchString);
##################################
#process directory function
##################################
sub process_directory
{
my ($path,$searchString) = @_;
# get all of the names from the directory, excluding "." and ".."
local (*DIR);
opendir (DIR, $path) :: die "can't open directory $path: $!";
my @names = grep (!/^\.\.?$/, readdir DIR);
closedir DIR;
# the sort is optional
for (sort @names)
{
my $temp = "$path/$_";
if (-d $temp)
{
&process_directory ($temp,$searchString);
}
else
{
&process_file ($temp,$searchString);
}
}
}
##################################
#process file function
##################################
sub process_file
{
my ($path,$searchString) = @_;
if($path=~/$\.rtf/)
{
open RTF_FILE, $path;
$mrString='';
$object->parse_stream( \*RTF_FILE );
if($mrString =~ /($searchString)/)
{
print $path,"\n";
}
close RTF_FILE;
}
}