summaryrefslogtreecommitdiff
path: root/combine_dsw.pl
blob: e7324893f6c0110cd0eb42d0378a175ad16fee64 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env perl
eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}'
    & eval 'exec perl -w -S $0 $argv:q'
    if 0;

# ******************************************************************
#      Author: Chad Elliott
#        Date: 4/8/2004
# Description: Combined multiple dsw's into a single dsw
# ******************************************************************

# ******************************************************************
# Pragma Section
# ******************************************************************

use strict;
use FileHandle;
use File::Basename;

# ******************************************************************
# Data Section
# ******************************************************************

my $version = '1.3';

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

sub usageAndExit {
  my $str = shift;
  print STDERR "$str\n" if (defined $str);
  print STDERR "Combine DSW v$version\n",
               "Usage: ", basename($0),
               " [-u] <output file> <input files...>\n\n",
               "-u  Each input file will be removed after successful ",
               "combination\n\n",
               "NOTE: This script will work for vcw's too.\n\n",
               "Combine multiple dsw's into a single dsw.  You can use ",
               "MPC to generate\n",
               "dynamic projects and then generate static projects using ",
               "the -static,\n",
               "-name_modifier and -apply_project options together.  You ",
               "can then run this\n",
               "script to combine the workspaces into one.\n";
  exit(0);
}

# ******************************************************************
# Main Section
# ******************************************************************

my $output;
my $unlink;
my @input;

for(my $i = 0; $i <= $#ARGV; $i++) {
  my $arg = $ARGV[$i];
  if ($arg =~ /^-/) {
    if ($arg eq '-u') {
      $unlink = 1;
    }
    else {
      usageAndExit("Unknown option: $arg");
    }
  }
  else {
    if (!defined $output) {
      $output = $arg;
    }
    else {
      push(@input, $arg);
    }
  }
}

## Print the usage if there is no output file provided or there isn't at
## least two input files.
usageAndExit() if (!defined $output ||
                   !defined $input[0] || !defined $input[1]);

my $tmp = "$output.$$.tmp";
my $oh  = new FileHandle();

if (open($oh, ">$tmp")) {
  my $msident;
  for(my $i = 0; $i <= $#input; ++$i) {
    ## We only want to take the global settings from the last input file.
    my $input  = $input[$i];
    my $global = ($i == $#input);

    ## Read in the input file and write out the parts that are common
    ## between multiple workspace files (but only on the first input
    ## file).  After that, write out the project information from each
    ## input file.
    my $fh = new FileHandle();
    if (open($fh, $input)) {
      my $in_global = 0;
      while(<$fh>) {
        if (/Microsoft\s+(Developer\s+Studio|eMbedded\s+Visual)/) {
          ## We only want to print out the identifier from the first
          ## input file.
          if (!$msident) {
            $msident = 1;
            print $oh $_;
          }
        }
        else {
          if (/^Global:/) {
            $in_global = 1;
          }
          elsif ($in_global && /^[#]{79,}/) {
            $in_global = 0;
            $_ = '';
          }

          ## Only print out the line if it's not part of the global
          ## settings (unless this is the last input file).
          print $oh $_ if (!$in_global || ($global && $in_global));
        }
      }
      close($fh);
    }
    else {
      print STDERR "ERROR: Unable to open '$input' for reading\n";
      exit(2);
    }
  }
  close($oh);

  ## If the user wants us to remove the input files after successfully
  ## combining them, then do so here.
  unlink(@input) if ($unlink);

  ## We have written the new workspace to a temporary file to allow an
  ## input file to be the same as an output file.  Once we're done, we
  ## need to move it to it's correct output name.
  unlink($output);
  rename($tmp, $output);
}
else {
  print STDERR "ERROR: Unable to open '$tmp' for writing\n";
  exit(1);
}