-
Notifications
You must be signed in to change notification settings - Fork 0
/
h_FILT.pl
109 lines (108 loc) · 3.19 KB
/
h_FILT.pl
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/local/bin/perl
use strict; use warnings;
################################
# FILT - metadata user interface
# feat. ningu irc.freenode.net
# ARGS & FRIENDS ################
my %master;
my @commands = qw(name path size encode);
die "no source directory" unless @ARGV;
my ($data_dir) = @ARGV;
die "no dir $data_dir" unless -d $data_dir;
$data_dir =~ s%/\z%%;
# POPULATE HASHES ################
foreach my $comm (@commands)
{ read_file(uc substr($comm, 0, 3), $comm); }
my @masterkeyset = keys %{$master{$commands[0]}};
my @keyset = @masterkeyset;
# PROMPT ##########################
while (1) {
prmpt();
my $input = <STDIN>; chomp $input;
print "\nwork'n on $input\n";
my ($comm, $string) = split(' ', $input, 2);
# RESET ############################
if ($comm eq 'reset')
{ @keyset = @masterkeyset; }
# LOAD #############################
elsif ($comm eq 'load') {
my $load_keyset = $string;
open(my $loadfh, '<', $load_keyset);
@newkeyset = readline $loadfh; chomp @newkeyset;
}
# PRINT ############################
elsif ($comm eq 'print') {
my $pfh = crfile($string);
foreach my $key (@keyset)
{ print $pfh "$key\n"; }
close $pfh;
}
# COUNT ##############################
elsif ($comm eq 'count')
{ my $cnt = @keyset; print "CURRENT: $cnt\n"; }
# VALUE ##############################
elsif ($comm eq 'value') {
my %descript = %{$master{$string}};
foreach my $key (@keyset)
{ print "$descript{$key}\n"; }
}
# GREP ###############################
elsif ($master{$comm})
{ layer_s($comm, $string); }
# POPULATE ############################
elsif ($comm eq 'pop') {
my $target_size = $string;
my %pop = %{$master{"size"}};
my $pfh = crfile($target_size);
my $ofh = crfile("leftover_$target_size");
my $cur_size = 0; my @leftokeys = @keyset;
foreach my $key (@keyset) {
my $iter_amt = $pop{$key};
$cur_size += $iter_amt;
if ($cur_size < $target_size) {
my $index = 0;
$index++ until $keyset[$index] eq $key;
splice(@keyset, $index, 1);
print $pfh "$key:$iter_amt:$cur_size\n";
}
else
{ print $ofh "$key:$iter_amt:$cur_size\n"; }
}
close $pfh; close $ofh;
}
# DEFAULT ##############################
else { print "unknown command $comm\n"; }
} # SUBS ###############################
sub read_file {
my ($filename, $cmd) = @_;
my $path = "$data_dir/$filename";
open(my $fh, '<', $path) or die "Couldn't open $filename at sub read_file\n";
my @lines = readline $fh; chomp @lines; close $fh;
my %sub_hash;
foreach my $i (@lines) {
my @key_value = split(" ", $i, 2);
$sub_hash{$key_value[0]} = $key_value[1];
}
$master{$cmd} = \%sub_hash;
}
sub layer_s {
my ($cmd, $string) = @_;
my $sub_hash = $master{$cmd};
@keyset = grep { $sub_hash->{$_} =~ /$string/i } @keyset;
foreach my $key (@keyset)
{ print "$key\n"; }
}
sub crfile {
my ($fname) = @_;
my $sub_path = "$data_dir/$fname";
if (-e $sub_path)
{ print "$sub_path already exists"; exit; }
open(my $sfh, '>>', $sub_path) or die "cant open $sub_path";
print "listing save to $sub_path\n";
return $sfh;
}
sub prmpt {
print "usage: type \$string || reset || print \$filename\n";
print " load \$file || count || value \$type || pop \$amt\n";
print "MKRX SYSTEMS RDY: ";
}