root/juggler/trunk/release/scripts/SourceList.pm
| Revision 20974, 8.8 kB (checked in by patrick, 1 year ago) | |
|---|---|
| |
| Line | |
|---|---|
| 1 | # ************** <auto-copyright.pl BEGIN do not edit this line> ************** |
| 2 | # |
| 3 | # VR Juggler is (C) Copyright 1998-2008 by Iowa State University |
| 4 | # |
| 5 | # Original Authors: |
| 6 | # Allen Bierbaum, Christopher Just, |
| 7 | # Patrick Hartling, Kevin Meinert, |
| 8 | # Carolina Cruz-Neira, Albert Baker |
| 9 | # |
| 10 | # This library is free software; you can redistribute it and/or |
| 11 | # modify it under the terms of the GNU Library General Public |
| 12 | # License as published by the Free Software Foundation; either |
| 13 | # version 2 of the License, or (at your option) any later version. |
| 14 | # |
| 15 | # This library is distributed in the hope that it will be useful, |
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | # Library General Public License for more details. |
| 19 | # |
| 20 | # You should have received a copy of the GNU Library General Public |
| 21 | # License along with this library; if not, write to the |
| 22 | # Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 | # Boston, MA 02111-1307, USA. |
| 24 | # |
| 25 | # *************** <auto-copyright.pl END do not edit this line> *************** |
| 26 | |
| 27 | package SourceList; |
| 28 | |
| 29 | require 5.004; |
| 30 | |
| 31 | use strict qw(vars); |
| 32 | |
| 33 | # ----------------------------------------------------------------------------- |
| 34 | # Constructor. |
| 35 | # ----------------------------------------------------------------------------- |
| 36 | sub new($;@) |
| 37 | { |
| 38 | my $class = shift; |
| 39 | |
| 40 | # Make the anonymous array reference that will contain the list of files. |
| 41 | # If a list of files was given as the optional arguments to the |
| 42 | # constructor, assign that list to the new array reference. |
| 43 | # XXX: I suspect that this could all be done in a single assignment to |
| 44 | # \@_, but I am wary of using a reference to something that I didn't |
| 45 | # create myself. |
| 46 | my $files = []; |
| 47 | @{$files} = @_ if @_; |
| 48 | |
| 49 | # The structure used for this class is a little complicated. It uses |
| 50 | # the following data members: |
| 51 | # |
| 52 | # all_files - An array reference. |
| 53 | # suffixes - A hash with suffixes as keys but with no associated |
| 54 | # values. A hash is used to prevent duplication of |
| 55 | # suffixes. |
| 56 | # dirs - A hash whose keys are the directories containing source |
| 57 | # files. The values associated with the keys are in turn |
| 58 | # hashes whose keys are suffixes. The values associated |
| 59 | # with those keys are the actual files in an aray |
| 60 | # reference. Conceptually, it looks like this: |
| 61 | # |
| 62 | # dirs => { |
| 63 | # dir1 => { |
| 64 | # .c => [ file1.c, file2.c ], |
| 65 | # .cxx => [ file3.cxx, file4.cxx ], |
| 66 | # : |
| 67 | # : |
| 68 | # }, |
| 69 | # dir2 => { |
| 70 | # .c++ => [ file5.c++, file6.c++ ], |
| 71 | # .cpp => [ file7.cpp, file8.cpp ], |
| 72 | # : |
| 73 | # : |
| 74 | # }, |
| 75 | # : |
| 76 | # : |
| 77 | # } |
| 78 | return bless { |
| 79 | 'all_files' => $files, # Full list of all known source files |
| 80 | 'suffixes' => {}, # List of suffixes used by those files |
| 81 | 'dirs' => {} # Complete categorization of source files |
| 82 | }, $class; |
| 83 | } |
| 84 | |
| 85 | # ----------------------------------------------------------------------------- |
| 86 | # Destructor. |
| 87 | # ----------------------------------------------------------------------------- |
| 88 | sub DESTROY($) |
| 89 | { |
| 90 | my $this = shift; |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | # ----------------------------------------------------------------------------- |
| 95 | # Read all the source files from the given directory. This populates this |
| 96 | # object with the actual source list. |
| 97 | # ----------------------------------------------------------------------------- |
| 98 | sub readSources($$) |
| 99 | { |
| 100 | my $this = shift; |
| 101 | my $dir = shift; |
| 102 | my @nosrcs = @_; |
| 103 | |
| 104 | my $status; |
| 105 | |
| 106 | if ( opendir(SRCDIR, "$dir") ) |
| 107 | { |
| 108 | my @all_files = readdir(SRCDIR); |
| 109 | closedir(SRCDIR); |
| 110 | |
| 111 | ${$this->{'dirs'}}{"$dir"} = {}; |
| 112 | |
| 113 | my $file; |
| 114 | foreach $file ( @all_files ) |
| 115 | { |
| 116 | # Only match C/C++ source files. |
| 117 | if ( -f "$dir/$file" && $file =~ /(\.(c\+\+|cpp|cxx|cc|c))$/i ) |
| 118 | { |
| 119 | my $ext = "$1"; |
| 120 | |
| 121 | # Compare the current file name against the list of files to |
| 122 | # be excluded. |
| 123 | my $exclude = 0; |
| 124 | foreach ( @nosrcs ) |
| 125 | { |
| 126 | if ( "$dir/$file" eq "$_" ) |
| 127 | { |
| 128 | $exclude = 1; |
| 129 | last; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | # Skip this file if is to be excluded. |
| 134 | next if $exclude; |
| 135 | |
| 136 | # Add this suffix to the main suffix hash. |
| 137 | ${$this->{'suffixes'}}{"$ext"} = ''; |
| 138 | |
| 139 | # Create the anonymous array for the files if this extension |
| 140 | # has not already been registered. |
| 141 | unless ( exists(${${$this->{'dirs'}}{"$dir"}}{"$ext"}) ) |
| 142 | { |
| 143 | ${${$this->{'dirs'}}{"$dir"}}{"$ext"} = []; |
| 144 | } |
| 145 | |
| 146 | # Add this file to the list of files for the the current file |
| 147 | # extension in this directory, and add it to the main list of |
| 148 | # all source files. |
| 149 | push(@{${$this->{'dirs'}{"$dir"}}{"$ext"}}, "$file"); |
| 150 | push(@{$this->{'all_files'}}, "$file"); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | $status = 1; |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | warn "WARNING: Could not open directory $dir: $!\n"; |
| 159 | $status = 0; |
| 160 | } |
| 161 | |
| 162 | return $status; |
| 163 | } |
| 164 | |
| 165 | # ----------------------------------------------------------------------------- |
| 166 | # Deterime if the given directory is known to this source file list. |
| 167 | # |
| 168 | # Returns: |
| 169 | # 0 - The directory is not in this source list. |
| 170 | # 1 - The directory is in this source list. |
| 171 | # ----------------------------------------------------------------------------- |
| 172 | sub hasDirectory($$) |
| 173 | { |
| 174 | my $this = shift; |
| 175 | |
| 176 | return exists(${$this->{'dirs'}}{"$_[0]"}); |
| 177 | } |
| 178 | |
| 179 | sub hasSuffix($$$) |
| 180 | { |
| 181 | my $this = shift; |
| 182 | my $dir = shift; |
| 183 | my $ext = shift; |
| 184 | |
| 185 | return exists(${${$this->{'dirs'}}{"$dir"}}{"$ext"}); |
| 186 | } |
| 187 | |
| 188 | # ----------------------------------------------------------------------------- |
| 189 | # Get all the files registered with this object. |
| 190 | # ----------------------------------------------------------------------------- |
| 191 | sub getAllFiles($) |
| 192 | { |
| 193 | my $this = shift; |
| 194 | return @{$this->{'all_files'}}; |
| 195 | } |
| 196 | |
| 197 | # ----------------------------------------------------------------------------- |
| 198 | # Get all the directories registered with this object. |
| 199 | # ----------------------------------------------------------------------------- |
| 200 | sub getDirectories($) |
| 201 | { |
| 202 | my $this = shift; |
| 203 | return keys(%{$this->{'dirs'}}); |
| 204 | } |
| 205 | |
| 206 | # ----------------------------------------------------------------------------- |
| 207 | # Get all the suffixes registered with this object or all the suffixes in use |
| 208 | # within a given directory. |
| 209 | # ----------------------------------------------------------------------------- |
| 210 | sub getSuffixes($;$) |
| 211 | { |
| 212 | my $this = shift; |
| 213 | my $dir = shift || ''; |
| 214 | |
| 215 | my @suffixes = (); |
| 216 | |
| 217 | # If a specific directory is requested, return only its suffixes. |
| 218 | if ( $dir ) |
| 219 | { |
| 220 | @suffixes = keys(%{${$this->{'dirs'}}{"$dir"}}); |
| 221 | } |
| 222 | # Otherwise, return all the known suffixes. |
| 223 | else |
| 224 | { |
| 225 | @suffixes = keys(%{$this->{'suffixes'}}); |
| 226 | } |
| 227 | |
| 228 | return @suffixes; |
| 229 | } |
| 230 | |
| 231 | # ----------------------------------------------------------------------------- |
| 232 | # Get all the files in the given directory with the given suffix. |
| 233 | # ----------------------------------------------------------------------------- |
| 234 | sub getFiles($$$) |
| 235 | { |
| 236 | my $this = shift; |
| 237 | my $dir = shift; |
| 238 | my $suffix = shift; |
| 239 | |
| 240 | return @{${${$this->{'dirs'}}{"$dir"}}{"$suffix"}}; |
| 241 | } |
| 242 | |
| 243 | # ----------------------------------------------------------------------------- |
| 244 | # Find a file in this source list. Its containing directory is returned to |
| 245 | # the caller, or undef is returned if the file is not in this list. |
| 246 | # ----------------------------------------------------------------------------- |
| 247 | sub findFile($$) |
| 248 | { |
| 249 | my $this = shift; |
| 250 | my $file = shift; |
| 251 | |
| 252 | my($dir, $suffix); |
| 253 | foreach $dir ( $this->getDirectories() ) |
| 254 | { |
| 255 | foreach $suffix ( $this->getSuffixes("$dir") ) |
| 256 | { |
| 257 | if ( $file =~ /$suffix$/i ) |
| 258 | { |
| 259 | foreach ( $this->getFiles("$dir", "$suffix") ) |
| 260 | { |
| 261 | return $dir if "$file" eq "$_"; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return undef; |
| 268 | } |
| 269 | |
| 270 | # ----------------------------------------------------------------------------- |
| 271 | # Insert a file into this source list within the given directory. |
| 272 | # ----------------------------------------------------------------------------- |
| 273 | sub insertFile($$$) |
| 274 | { |
| 275 | my $this = shift; |
| 276 | my $file = shift; |
| 277 | my $dir = shift; |
| 278 | |
| 279 | $file =~ /(\.(c\+\+|cpp|cxx|cc|c))$/i; |
| 280 | my $ext = "$1"; |
| 281 | |
| 282 | push(@{${${$this->{'dirs'}}{"$dir"}}{"$ext"}}, "$file"); |
| 283 | } |
| 284 | |
| 285 | 1; |
Note: See TracBrowser for help on using the browser.
