source: src/gph/gph @ 5f4d9c3

Last change on this file since 5f4d9c3 was 5f4d9c3, checked in by Maciej Prill <mprill@…>, 12 years ago

Rewritten the build system, added lem UTF-8 version.

  • Property mode set to 100755
File size: 3.2 KB
Line 
1#!/usr/bin/perl
2
3#package:       UAM Text Tools
4#component:     gph
5#version:       1.0
6#author:        Tomasz Obrebski
7
8use strict;
9use Getopt::Long;
10use File::HomeDir;
11
12
13
14
15
16my $systemconfigfile='/usr/local/etc/utt/gph.conf';
17my $userconfigfile=home()."/.utt/gph.conf";
18
19Getopt::Long::Configure('no_ignore_case_always');
20
21my $help=0;
22my $inputfile=0;
23my $outputfile=0;
24my @process=();
25my $reset;
26my $interactive=0;
27
28#read configuration files###########################
29my $file;
30my @process_conf=();
31foreach $file ($systemconfigfile, $userconfigfile){
32  if(open(CONFIG, $file)){
33        while (<CONFIG>) {
34                chomp;
35                s/#.*//;
36                s/^\s+//;
37                s/\s+$//;
38                next unless length;
39                my ($name, $value) = split(/\s*=\s*/, $_, 2);
40                if(($name eq "inputfile")or($name eq "f")){
41                        $inputfile=$value;
42                }
43                elsif(($name eq "outputfile")or($name eq "o")){
44                        $outputfile=$value;
45                }
46                elsif(($name eq "process")or($name eq "p")){
47                        push @process_conf, $value;
48                }
49                elsif(($name eq "reset")or($name eq "r")){
50                        $reset=$value;
51                }
52                elsif(($name eq "interactive")or($name eq "i")){
53                        $interactive=1;
54                }
55                elsif(($name eq "help")or($name eq "h")){
56                        $help=1;
57                }
58
59        }
60        close CONFIG;
61  }
62}
63#########################################################
64
65
66
67GetOptions("process|p=s" => \@process,
68           "inputfile|f=s" => \$inputfile,
69           "outputfile|o=s" => \$outputfile,
70           "help|h" => \$help,
71           "reset|r=s" => \$reset,
72           "interactive|i" => \$interactive);
73
74@process = @process_conf if @process<1;
75
76if($help)
77{
78    print <<'END'
79Usage: gph [OPTIONS]
80
81Options:
82   --process=TYPE -p TYPE    Process segments of type TYPE.
83   --reset=TYPE -r TYPE      Start new graph at tags of type TYPE.
84   --inputfile=FILE -f FILE  Input file.
85   --outputfile=FILE -o FILE Output file.
86   --interactive -i          Toggle interactive mode (default=off).
87END
88;
89    exit 0;
90}
91
92
93$|=1 if $interactive;
94
95
96if(!$inputfile or $inputfile eq "-") {
97        *INPUT = *STDIN;
98}
99else {
100        open(INPUT, "$inputfile") or die("Can't open input file: $inputfile!");
101}
102
103if(!$outputfile or $outputfile eq "-") {
104        *OUTPUT = *STDOUT;
105}
106else {
107        open(OUTPUT, "$outputfile") or die("Can't open output file: $outputfile!");
108}
109
110my @prev;
111my $n=0;
112
113while(<INPUT>)
114{
115    chomp;
116    my $do=0;
117
118    my @line = split /\s+/;
119
120    if($line[2] eq $reset)
121    {
122        $n=0;
123        @prev = ();
124    }
125
126    for my $p (@process)
127    {
128        $do=1 if $line[2] eq $p;
129    }
130
131        my $gph = '';
132    if($do)
133    {
134        my @preds = ();
135        shift @prev while @prev+0 && $prev[0]->[1] + $prev[0]->[2] < $line[0];
136        for my $p (@prev)
137        {
138            push(@preds, $p->[0]) if $p->[1] + $p->[2] == $line[0];
139        }
140        push @prev, [$n, $line[0], $line[1]];
141       
142        $gph=' gph:'.$n.':'.join(',',@preds);
143
144        $n++;
145    }
146    else
147    {
148        for my $p (@prev)
149        {
150            if($p->[1]+$p->[2] == $line[0])
151            {
152                $p->[2] += $line[1];
153            }
154        }
155
156        $gph='';
157
158    }
159
160    print OUTPUT $_.$gph."\n";   
161}
162
Note: See TracBrowser for help on using the repository browser.