summaryrefslogtreecommitdiff
path: root/bin/ChangeLogEditor/ChangeLogEdit.pm
blob: 1bbfee507b11673dcb2f6c97d8289ff3d02110d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package ChangeLogEdit;

# ************************************************************
# Description   : Edit the existing ChangeLog.
# Author        : Chad Elliott
# Create Date   : 9/10/2002
# ************************************************************

# ************************************************************
# Pragmas
# ************************************************************

use strict;
use FileHandle;
use File::Copy;

use ChangeLogEntry;

# ************************************************************
# Subroutine Section
# ************************************************************

sub new {
  my($class) = shift;
  my($name)  = shift;
  my($email) = shift;
  my($self)  = bless {'name'  => $name,
                      'email' => $email,
                     }, $class;
  return $self;
}


sub edit {
  my($self)    = shift;
  my($ofile)   = shift;
  my(@dirs)    = @_;
  my($tfile)   = "$ofile.$<.$$";
  my($status)  = 0;
  my($error)   = '';
  my($rh)      = new FileHandle();
  my($unknown) = undef;

  if (open($rh, $ofile)) {
    my($creator) = new ChangeLogEntry($self->{'name'},
                                      $self->{'email'});
    my($entry) = '';
    ($entry, $unknown, $error) = $creator->create(@dirs);
    if (defined $entry) {
      if ($entry =~ /^ERROR:/) {
        $error = $entry;
      }
      else {
        my($oh) = new FileHandle();
        if (open($oh, ">$tfile")) {
          $status = print $oh $entry;
          if ($status) {
            while(<$rh>) {
              my($line) = $_;
              $line =~ s/\s+$//;
              if ($line =~ /\t/) {
                $line = $self->convertTabs($line);
              }
              $status = print $oh "$line\n";
              if ($status == 0) {
                $error = "Unable to copy $ofile";
                last;
              }
            }
          }
          else {
            $error = 'Unable to print the first entry';
          }
          close($oh);
        }
        else {
          $error = "Unable to open $tfile for writing";
        }
        close($rh);

        if ($status) {
          $status = 0;
          if (unlink($ofile)) {
            if (rename($tfile, $ofile)) {
              $status = 1;
            }
            else {
              $error = "Unable to rename $tfile to $ofile";
            }
          }
          else {
            $error = "Unable to remove $ofile";
          }
        }
      }
    }
    else {
      $error = (defined $error ? "There is a revision control system " .
                                 "problem:\n$error" :
                                 'There are no modified/removed files.');
    }
  }
  else {
    $error = "Unable to open $ofile for reading";
  }

  return $status, $error, $unknown;
}


sub convertTabs {
  my($self) = shift;
  my($line) = shift;
  while($line =~ /\t/) {
    my($spaces) = 8 - (index($line, "\t") % 8);
    $line =~ s/\t/sprintf("%${spaces}s", ' ')/e;
  }
  return $line;
}


1;