To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.cad.devOpen lugnet.cad.dev in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 CAD / Development / 212
211  |  213
Subject: 
MPD<->DAT conversion in Perl
Newsgroups: 
lugnet.cad.dev
Date: 
Sun, 21 Feb 1999 09:24:28 GMT
Viewed: 
957 times
  
I want to make sure I understand this MPD file format.  I wrote two Perl
programs, one for slurping up a nested tree of DAT's and producing an MPD,
and one for splitting up an MPD back into DAT's.  I'm not sure how useful
these will be in future lugnet.cad.dat enhancements, but it was a good
exercise.

--Todd



#!/usr/local/bin/perl5
#--------------------------------------------------------------------------
#
#  Slurp up multiple .dat files into a single .mpd file.  Usage:
#
#     mpdslurp dat [dat ...] > mpd
#
#  Any files not found in the current directory are ignored and assumed to
#  be present elsewhere (for example in a parts library).
#

use strict; $^W = 1;

my %slurp;
slurp(@ARGV);
foreach (sort keys %slurp) { print "0 FILE $_\n", $slurp{$_}; }

sub slurp
{
   local $/;
   foreach my $dat (@_)
   {
      next if exists $slurp{$dat};
      open DAT, "<$dat" or next;
      $slurp{$dat} = <DAT>;
      close DAT;
      slurp($slurp{$dat} =~ m/^\s*1\s.*\s(\S+\.dat)\s*$/gim);
   }
}



#!/usr/local/bin/perl5
#--------------------------------------------------------------------------
#
#  Split an .mpd file back into .dat files.  Usage:
#
#     mpdgarp < mpd
#

use strict;
$^W = 1;

my $dat;
while (defined($_ = <STDIN>))
{
   if (m/^\s*0\s+FILE\s+(.*?)\s*$/I)
   {
      close DAT if $dat;
      $dat = $1;
      die "Illegal characters in filename $dat"
         if $dat =~ m/[^a-zA-Z0-9_\-\.]/;
      open DAT, ">$dat";
   }
   else
   {
      print DAT $_ if $dat;
   }
}
close DAT if $dat;



Message has 1 Reply:
  Re: MPD<->DAT conversion in Perl
 
(...) Are you sure this handles all the legal LDraw file names? I think both tildes (~) and hashed (#) are legal as well. Sorry if this comment is out of place, as I'm not too familiar with Perl. Other than that, your programs look useful. Fredrik (26 years ago, 21-Feb-99, to lugnet.cad.dev)

3 Messages in This Thread:

Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR