logo       

QOTW15e - building a data structure for the patch file?: msg#00147

Subject: QOTW15e - building a data structure for the patch file?
Mark Jason Dominus writes:
[...]
> I thought last week's intro quiz was unusually cool, and I was
> disappointed that so few people seemed to have done it.

So did I - ;-), but didn't feel up to tackling it.

> Michael Toren's sample solution to the 'expert' quiz shows that it
> doesn't have to be difficult to write a useful implementation of
> 'patch', and I was hoping that the space of possible versions would
> have been more thoroughly explored.  Alas.

Having been guilted (just kidding) into actually trying something, I tackled 
the expert (patch) quiz this evening. I did first look at Michael Toren's 
to get me started.

What I've worked on so far is trying to get the diff file into a suitable 
data structure. I want to process the file being patched one line at a 
time, so I thought it would be easier to first load the patchfile into a 
useable data structure. What I've got so far...

# get BEGIN and END and COMMAND
open PATCH, $patchfile or die "Couldn't read $patchfile: $!\n";

my ($begin, $end, $command, @line);
while (<PATCH>) {
   if (/^(\d+),?(\d+)?([adc]).*/) {
      ($begin, $end, $command) = ($1, $2, $3);
      $end ||= $begin;
      for ($begin .. $end) {
         push @{ $line[$_] }, $command;
      }
   }

   # get new lines to add
   if (/^>\s(.*)/) {
      push @{ $line[$begin] },  $1 ;
   }
}

>From this:
1,2d0
< The Way that can be told of is not the eternal Way;
< The name that can be named is not the eternal name.
4c2,3
< The Named is the mother of all things.
---
> The named is the mother of all things.
>
11a11,13
> They both may be called deep and profound.
> Deeper and more profound,
> The door of all subtleties!

Data::Dumper gives me:
$VAR1 = [
          undef,
          [
            'd'
          ],
          [
            'd'
          ],
          ${\$VAR1->[0]},
          [
            'c',
            'The named is the mother of all things.',
            ''
          ],
          ${\$VAR1->[0]},
          ${\$VAR1->[0]},
          ${\$VAR1->[0]},
          ${\$VAR1->[0]},
          ${\$VAR1->[0]},
          ${\$VAR1->[0]},
          [
            'a',
            'They both may be called deep and profound.',
            'Deeper and more profound,',
            'The door of all subtleties!'
          ]
        ];

So $line[line_number]->[0] gives me the command for each line number and if 
there are new lines to add they follow in the same array.

This seems like a useful start to me, but I should probably make this 
'two-sided' so that a reverse patch would also be possible. 
Advice/comments/suggestions welcome. 

-K
-- 
Kevin Pfeiffer <pfeiffer-Is5wZoSgCRJn68oJJulU0Q@xxxxxxxxxxxxxxxx> - 
www.iu-bremen.de



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
linux.arklinux....    user-groups.lin...    kde.usability/2...    ietf.ipp/2002-0...    mail.spam.spamc...    os.netbsd.devel...    audio.cd-record...    text.unicode.de...    php.documentati...    games.fps.halfl...    window-managers...    suse.oracle.gen...    bug-tracking.gn...    video.dvdrip.us...    xfree86.cvs/200...    java.netbeans.m...    network.argus/2...    culture.sf.kill...    debian.ports.al...    freebsd.questio...    qplus.devel/200...    handhelds.palm....   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe