-
Notifications
You must be signed in to change notification settings - Fork 15
/
oh-my-cap.pm
109 lines (88 loc) · 2.62 KB
/
oh-my-cap.pm
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
#!/usr/bin/env perl
use strict;
use warnings;
sub choose_config() {
# For cap
my ($dir) = @_;
my %pars;
my $conf_file;
# 1st: project-wide configure file
$conf_file = "$ENV{OH_MY_CAP}/event.conf";
%pars = config_parser($conf_file, %pars) if -e $conf_file;
# 2nd: configure file in current directory
$conf_file = "./event.conf";
%pars = config_parser($conf_file, %pars) if -e $conf_file;
# 3rd: event-based configure file
$conf_file = "$dir/event.conf";
%pars = config_parser($conf_file, %pars) if -e $conf_file;
# parse arguments of DEPTH
$pars{"DEPTH"} = join " ", setup_values(split m/\s+/, $pars{"DEPTH"});
# setup arguments for cap
$pars{'cap_args'} = setup_cap_args(%pars);
return %pars;
}
sub read_config() {
# For fk
my ($conf_file) = @_;
my %pars;
$conf_file = "$conf_file";
%pars = config_parser($conf_file, %pars);
# parse arguments of DIST, DEPTH
foreach ("DIST", "DEPTH") {
$pars{$_} = join " ", setup_values(split m/\s+/, $pars{$_});
}
return %pars;
}
sub setup_values() {# parse arguments of DIST FKDEPTH CAPDEPTH
my @out;
foreach (@_) {
if ($_ =~ m/\//g) {
my ($start, $end, $delta) = split m/\//;
for (my $value = $start; $value <= $end; $value = $value + $delta) {
push @out, $value;
}
} else {
push @out, $_;
}
}
@out = sort { $a <=> $b } @out;
return @out;
}
sub setup_cap_args() {
my (%pars) = @_;
my $cap_args;
my @args = keys %pars;
foreach my $args (@args) {
next unless ($args =~ "-");
$cap_args .= "$args$pars{$args} ";
}
return $cap_args;
}
sub config_parser() {
my ($config_file, %pars) = @_;
open(IN," < $config_file") or die "can not open configure file $config_file\n";
my @lines = <IN>;
close(IN);
foreach my $line (@lines) {
$line = substr $line, 0, (pos $line) - 1 if ($line =~ m/#/g);
chomp($line);
if ($line =~ m/:/g) {
my ($key, $value) = split ":", $line;
next unless (defined($key) and defined($value));
$key = trim($key);
$value = trim($value);
$pars{$key} = $value;
}else{
my ($a) = split m/\s+/, $line;
next unless (defined($a));
unless (defined($pars{"MODEL"})) {
$pars{"MODEL"} = $line;
} else {
$pars{"MODEL"} = join "\n", ($pars{"MODEL"}, $line);
}
}
}
return %pars;
}
sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };
1;