#!/usr/bin/perl -w
# [EMAIL PROTECTED] 2004/6/15

my $path='/';
my $pwd='/';
my $base;
my $depth=0;
my $name;
my $nick;
my $tab;
my $find_args;
my $dir;

my @files;
# Initailise the leaf array.  Here we say / currently contains 0 leaves.
my @leaf; $leaf[0]=0;
my @types = qw(ac 3ds ase dfx flt md2 obj vrml vrml1 atg);

$find_args=' -follow -type f -name "*.'.pop(@types).'"';
foreach $i (@types) {
  $find_args.=" -or -name \"*.$i\"";

$base=$ENV{PWD} unless ($base=shift());
$base=~/^--?h/ and &Help();
chdir($base) or die "could not chdir to $base";
@files = `find * $find_args`;
@files = sort @files;
foreach $i (@files) {

print '<?xml version="1.0"?>'."\n";
print "\n";
print "<PropertyList>\n";
print "  <name type=\"string\">Library</name>\n";

foreach $path (@files) {

  ($pwd, $depth, @leaf) = &GotoRightDir($path, $pwd, $depth, @leaf);

  # Now assume $path eq $pwd
  $tab = &Tab($depth);
  print "$tab<model n=\"$leaf[$depth]\">\n";
  print "$tab  <name type=\"string\">${nick}</name>\n";
  print "$tab  <path type=\"string\">${base}${path}/${name}</path>\n";
  print "$tab</model>\n";

# Clean up
&GotoRightDir($path, $pwd, $depth, @leaf);
print "</PropertyList>\n";

sub GotoRightDir {
  # Ascend or descecnd directories until $path eq $pwd
  # Make appropriate STDOUT along the way

  my ($path, $pwd, $depth, @leaf) = @_;
  my $branch;

  # Just in case
  $path='/' unless $path;

  while ("$path" ne "$pwd") {
    if ($path=~/^$pwd/) {
      # Descend to the next dir
      # branch is the next directory in $path after $pwd
      # / is a special case

      # $depth is still set to the parent's level
      $tab = &Tab($depth);
      print "${tab}<folder n=\"$leaf[$depth]\">\n";
      print "$tab  <name type=\"string\">$branch</name>\n";

      # Keep track of parent's number of leaves
      # This is a new dir, and has no leaves yet
    } else {
      # Ascend toward /
      # Set $depth to parent's level

      $tab = &Tab($depth);
      print "${tab}</folder>\n";
  return $pwd, $depth, @leaf;

sub Tab {
  # Generate an indentation based on the parent's level
  my $depth = shift;
  my $tab='';

  for ($i=0; $i<=$depth; $i++) {
    $tab.='  ';
  return $tab;

sub Help {
  print "usage: $0 [-h] [path]\n\n";
  print "Builds an fgsd xml object library and outputs it to STDOUT,\n";
  print "If a path is given the library will be built with that as the root,\n";
  print "otherwise the pwd will be used as the root.\n";
  print "[path] may contain symlinks to other directories.\n";
  exit 0;

Flightgear-devel mailing list

Reply via email to