/[suikacvs]/markup/html/html5/spec-ja/common.pl
Suika

Diff of /markup/html/html5/spec-ja/common.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by wakaba, Sun Aug 10 06:15:00 2008 UTC revision 1.11 by wakaba, Sun Oct 26 08:44:19 2008 UTC
# Line 2  use strict; Line 2  use strict;
2    
3  my $data_suffix = q[.dat];  my $data_suffix = q[.dat];
4  my $data_dir_name = q[data/];  my $data_dir_name = q[data/];
5    my $data2_dir_name = q[data2/];
6    my $data2_suffix = q[.dat];
7    my $lock_suffix = q[.lock];
8    my $fallback_file_name = $data2_dir_name . 'fallback' . $data2_suffix;
9    my $patch_file_name = $data2_dir_name . 'modified.txt';
10    
11    our $UseCVS //= 1;
12    
13  sub normalize ($) {  sub normalize ($) {
14    my $s = shift;    my $s = shift;
# Line 11  sub normalize ($) { Line 18  sub normalize ($) {
18    return $s;    return $s;
19  } # normalize  } # normalize
20    
21    sub get_hash ($) {
22      require Digest::MD5;
23      return Digest::MD5::md5_hex (normalize ($_[0]));
24    } # get_hash
25    
26  sub create_pattern1 ($) {  sub create_pattern1 ($) {
27    my $s = quotemeta shift;    my $s = quotemeta shift;
28    $s =~ s/\\\*/(.+)/g;    $s =~ s/\\\*/(.+)/g;
# Line 55  sub for_each_data_file ($) { Line 67  sub for_each_data_file ($) {
67    }    }
68  } # for_each_data_file  } # for_each_data_file
69    
70    sub read_data_file ($) {
71      my $file_name = shift;
72      if (-f $file_name) {
73        warn "Loading $file_name...\n";
74        return do $file_name;
75      } else {
76        warn "File $file_name not found\n";
77        return {};
78      }
79    } # read_data_file
80    
81    sub write_data_file ($$) {
82      my ($file_name, $data) = @_;
83    
84      require Data::Dumper;
85      local $Data::Dumper::Sortkeys = 1;
86    
87      my $had_file = -f $file_name;
88      open my $file, '>:encoding(utf8)', $file_name or die "$0: $file_name: $!";
89      print $file Data::Dumper::Dumper ($data);
90      close $file;
91      unless ($had_file) {
92        system_ ('cvs', 'add', $file_name) if $UseCVS;
93      }
94    } # write_data_file
95    
96    sub hash_to_file_name ($) {
97      return $data2_dir_name . substr ($_[0], 0, 2) . $data2_suffix;
98    } # hash_to_file_name
99    
100    my $Entry = {};
101    my $ModifiedHash = {};
102    
103    sub get_entry ($) {
104      my $hash = shift;
105      
106      my $file_name = hash_to_file_name ($hash);
107      unless ($Entry->{$file_name}) {
108        $Entry->{$file_name} = read_data_file ($file_name);
109      }
110      
111      if ($Entry->{$file_name}->{exact}->{$hash}) {
112        return (0, $Entry->{$file_name}->{exact}->{$hash});
113      } elsif ($Entry->{$file_name}->{pattern}->{$hash}) {
114        return (1, $Entry->{$file_name}->{pattern}->{$hash});
115      } else {
116        return (undef, undef);
117      }
118    } # get_entry
119    
120    sub set_entry ($$$) {
121      my ($hash, $is_pattern, $value) = @_;
122      
123      my $file_name = hash_to_file_name ($hash);
124      unless ($Entry->{$file_name}) {
125        $Entry->{$file_name} = read_data_file ($file_name);    
126      }
127    
128      unless ($value) {
129        delete $Entry->{$file_name}->{exact}->{$hash};
130        delete $Entry->{$file_name}->{pattern}->{$hash};
131      } elsif ($is_pattern) {
132        delete $Entry->{$file_name}->{exact}->{$hash};
133        $Entry->{$file_name}->{pattern}->{$hash} = $value;
134      } else {
135        $Entry->{$file_name}->{exact}->{$hash} = $value;
136        delete $Entry->{$file_name}->{pattern}->{$hash};
137      }
138      $Entry->{$file_name}->{modified} = 1;
139      $ModifiedHash->{$hash} = 1;
140    } # set_entry
141    
142    use Fcntl ':flock';
143    my $Lock;
144    
145    sub lock_entry ($) {
146      if ($Lock) {
147        die "$0: lock_entry: Another entry is locked";
148      }
149    
150      my $hash = shift;
151      my $file_name = hash_to_file_name ($hash) . $lock_suffix;
152      open $Lock, '>', $file_name or die "$0: $file_name: $!";
153      flock $Lock, LOCK_EX;
154    } # lock_entry
155    
156    sub commit_entries ($) {
157      for my $file_name (keys %{$Entry}) {
158        if ($Entry->{$file_name}->{modified}) {
159          delete $Entry->{$file_name}->{modified};
160          write_data_file ($file_name => $Entry->{$file_name});
161        }
162      }
163    
164      open my $file, '>>', $patch_file_name or die "$0: $patch_file_name: $!";
165      for (keys %$ModifiedHash) {
166        print $file "$_\n";
167      }
168      close $file;
169    
170      my $msg = shift // $0;
171      system_ ('cvs', 'commit', -m => $msg, $data2_dir_name) if $UseCVS;
172    } # commit_entries
173    
174    sub get_all_entries () {
175      opendir my $dir, $data2_dir_name or die "$0: $data2_dir_name: $!";
176      for (readdir $dir) {
177        next unless /\Q$data2_suffix\E$/;
178        my $file_name = $data2_dir_name . $_;
179        next if $Entry->{$file_name};
180    
181        $Entry->{$file_name} = read_data_file ($file_name);
182      }
183    
184      return $Entry;
185    } # get_all_entries
186    
187    my $FallbackEntry;
188    sub get_fallback_entry ($) {
189      my $hash = shift;
190      unless (defined $FallbackEntry) {
191        $FallbackEntry = read_data_file ($fallback_file_name);
192      }
193      return $FallbackEntry->{$hash} // {};
194    } # get_fallback_entry
195    
196    sub get_entry_or_fallback_entry ($) {
197      my $hash = shift;
198    
199      my ($is_pattern, $entry) = get_entry ($hash);
200      unless (defined $entry->{en}) {
201        $entry = get_fallback_entry ($hash);
202      }
203      $entry->{tags} ||= [] if defined $entry->{en};
204      $entry->{isPattern} = 1 if $is_pattern;
205    
206      return $entry;
207    } # get_entry_or_fallback_entry
208    
209    sub set_fallback_entry ($$) {
210      my ($hash, $value) = @_;
211      unless (defined $FallbackEntry) {
212        $FallbackEntry = read_data_file ($fallback_file_name);
213      }
214      $FallbackEntry->{$hash} = $value;
215    } # set_fallback_entry
216    
217    sub clear_fallback_entries () {
218      $FallbackEntry = {};
219    } # clear_fallback_entries
220    
221    sub save_fallback_entries () {
222      write_data_file ($fallback_file_name => $FallbackEntry)
223          if defined $FallbackEntry;
224    } # save_fallback_entries
225    
226    sub get_modified_hashes () {
227      open my $file, '<', $patch_file_name or die "$0: $patch_file_name: $!";
228      return map {tr/\x0D\x0A//d; $_} <$file>;
229    } # get_modified_hashes
230    
231    sub clear_modified_hashes () {
232      open my $file, '>', $patch_file_name;
233      close $file;
234    } # clear_modified_hashes
235    
236    sub htescape ($) {
237      my $s = shift;
238      $s =~ s/&/&amp;/g;
239      $s =~ s/</&lt;/g;
240      $s =~ s/"/&quot;/g;
241      return $s;
242    } # htescape
243    
244    sub system_ (@) {
245      (system join (' ', map {quotemeta $_} @_) . " > /dev/null") == 0
246          or die "$0: $?";
247    } # system_
248    
249  1;  1;
250    

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.11

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24