summaryrefslogtreecommitdiff
path: root/ACE/TAO/orbsvcs/examples/Notify/MC/TkMonitor/modules/GeometryStore.pm
blob: ca25280b1b75a0d5058afb354ed562752f7a207d (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
package GeometryStore;

# ******************************************************************
# Description : Store and retrieve geometry
# Author      : Chad Elliott
# Create Date : 1/5/2006
# ******************************************************************

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

use strict;
use Errno qw(ENOLCK);
use FileHandle;
use Fcntl qw(:DEFAULT :flock);

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

our($VERSION) = '1.0.2';
my($file) = (defined $ENV{HOME} ? $ENV{HOME} : '' ) . '/.geometry';

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

sub store {
  my($name) = shift;
  my($geom) = shift;
  my($fh)   = new FileHandle();

  if (open($fh, "+<$file")) {
    my($locked) = flock($fh, LOCK_EX | LOCK_NB);
    ## If we were able to lock the file or locking is not
    ## supported by the OS, then we will go ahead and write
    if ($locked || $! == ENOLCK) {
      my(@lines) = ();
      my($re)    = $name;
      $re =~ s/([\+\-\\\$\[\]\(\)\.])/\\$1/g;
      while(<$fh>) {
        if ($_ !~ /^$re:/) {
          push(@lines, $_);
        }
      }

      seek($fh, 0, 0);
      truncate($fh, 0);
      foreach my $line (@lines) {
        print $fh $line;
      }
      print $fh "$name: $geom\n";
      close($fh);
    }
  }
}


sub retrieve {
  my($name) = shift;
  my($fh)   = new FileHandle();
  if (open($fh, $file)) {
    $name =~ s/([\+\-\\\$\[\]\(\)\.])/\\$1/g;
    while(<$fh>) {
      if (/^\s*$name:\s+(\d+x\d+)(\+[\-]?\d+\+[\-]?\d+)/) {
        return $1, $2;
      }
    }
    close($fh);
  }
  return '', '';
}


1;