/[suikacvs]/perl/lib/Data/Count.pm
Suika

Contents of /perl/lib/Data/Count.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations) (download)
Fri Aug 15 08:15:52 2003 UTC (21 years, 2 months ago) by wakaba
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +0 -0 lines
FILE REMOVED
Historical

1 wakaba 1.1
2     =pod
3    
4     Data::Count;
5     Data file for Counter.
6    
7     Copyright wakaba 2001, GNU GPL2.
8     Change:
9    
10     2001-08-07 wakaba <wakaba@suika.fam.cx>
11    
12     * (_init(), namelist(), list()): New.
13    
14     2001-06-05 wakaba
15     - New File. Taken from Suika::CGI::Counter.
16    
17     =cut
18    
19     package Data::Count;
20     $VERSION = '1.01';
21    
22     sub open {
23     my $class = shift;
24     my $file = shift;
25     my $name = shift || 'default'; $name =~ tr/\x0d\x0a\x1f//d;
26     my $self = bless {file => $file, name => $name, error => \&error, count => {}}, $class;
27     $self->_init();
28     $self;
29     }
30     sub error {}
31    
32     sub _init {
33     my $self = shift;
34     open COUNT, $self->{file} or &{$self->{error}}('open', file => $self->{file});
35     while (<COUNT>) {
36     if (/^([^\x1F\x0D\x0A]+)\x1f(\d+)/) {
37     $self->{count}->{$1} = $2;
38     }
39     }
40     close COUNT;
41     $self;
42     }
43    
44     sub namelist {
45     my $self = shift;
46     keys %{$self->{count}};
47     }
48    
49     sub list {
50     my $self = shift;
51     %{$self->{count}};
52     }
53    
54     sub get {
55     my $self = shift;
56     my ($id) = shift || $self->{name}; $id =~ tr/\x0d\x0a\x1f//d;
57     return $self->{count}->{$id} if $self->{count}->{$id};
58     my ($ret);
59     open COUNT, $self->{file} or &{$self->{error}}('open', file => $self->{file});
60     while (<COUNT>) {
61     if (/^${id}\x1f(\d+)/) {
62     $ret = $1;
63     last;
64     }
65     }
66     close COUNT;
67     $self->{count}->{$id} = $ret; $ret;
68     }
69    
70     sub up {
71     my $self = shift; my ($f, @COUNT) = 0;
72     my ($id) = shift || $self->{name}; $id =~ tr/\x0d\x0a\x1f//d;
73     if (open COUNT, $self->{file}) {
74     @COUNT = <COUNT>;
75     close COUNT}
76    
77     my $ret;
78     open COUNT, '> '.$self->{file}
79     or &{$self->{error}}('write', file => $self->{file});
80     for (@COUNT) {
81     if (/^\Q${id}\E\x1f(\d+)/) {
82     $_ = $id."\x1f".($1+1)."\n";
83     $ret = $1+1; $f = 1;
84     }
85     print COUNT $_;
86     }
87     unless ($f) {
88     print COUNT $id."\x1f1\n";
89     $ret = 1;
90     }
91     close COUNT;
92     $self->{count}->{$id} = $ret; $self;
93     }
94    
95     1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24