util.pm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #
  2. # Copyright 1999, 2000, 2001 Patrik Stridvall
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2.1 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public
  15. # License along with this library; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  17. #
  18. package util;
  19. use strict;
  20. use warnings 'all';
  21. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  22. require Exporter;
  23. @ISA = qw(Exporter);
  24. @EXPORT = qw(
  25. append_file edit_file read_file replace_file
  26. normalize_set is_subset
  27. );
  28. @EXPORT_OK = qw();
  29. %EXPORT_TAGS = ();
  30. ########################################################################
  31. # _compare_files
  32. sub _compare_files($$) {
  33. my $file1 = shift;
  34. my $file2 = shift;
  35. local $/ = undef;
  36. return -1 if !open(IN, "< $file1");
  37. my $s1 = <IN>;
  38. close(IN);
  39. return 1 if !open(IN, "< $file2");
  40. my $s2 = <IN>;
  41. close(IN);
  42. return $s1 cmp $s2;
  43. }
  44. ########################################################################
  45. # append_file
  46. sub append_file($$@) {
  47. my $filename = shift;
  48. my $function = shift;
  49. open(OUT, ">> $filename") || die "Can't open file '$filename'";
  50. my $result = &$function(\*OUT, @_);
  51. close(OUT);
  52. return $result;
  53. }
  54. ########################################################################
  55. # edit_file
  56. sub edit_file($$@) {
  57. my $filename = shift;
  58. my $function = shift;
  59. open(IN, "< $filename") || die "Can't open file '$filename'";
  60. open(OUT, "> $filename.tmp") || die "Can't open file '$filename.tmp'";
  61. my $result = &$function(\*IN, \*OUT, @_);
  62. close(IN);
  63. close(OUT);
  64. if($result) {
  65. unlink("$filename");
  66. rename("$filename.tmp", "$filename");
  67. } else {
  68. unlink("$filename.tmp");
  69. }
  70. return $result;
  71. }
  72. ########################################################################
  73. # read_file
  74. sub read_file($$@) {
  75. my $filename = shift;
  76. my $function = shift;
  77. open(IN, "< $filename") || die "Can't open file '$filename'";
  78. my $result = &$function(\*IN, @_);
  79. close(IN);
  80. return $result;
  81. }
  82. ########################################################################
  83. # replace_file
  84. sub replace_file($$@) {
  85. my $filename = shift;
  86. my $function = shift;
  87. open(OUT, "> $filename.tmp") || die "Can't open file '$filename.tmp'";
  88. my $result = &$function(\*OUT, @_);
  89. close(OUT);
  90. if($result && _compare_files($filename, "$filename.tmp")) {
  91. unlink("$filename");
  92. rename("$filename.tmp", $filename);
  93. } else {
  94. unlink("$filename.tmp");
  95. }
  96. return $result;
  97. }
  98. ########################################################################
  99. # normalize_set
  100. sub normalize_set($) {
  101. local $_ = shift;
  102. if(!defined($_)) {
  103. return undef;
  104. }
  105. my %hash = ();
  106. foreach my $key (split(/\s*&\s*/)) {
  107. $hash{$key}++;
  108. }
  109. return join(" & ", sort(keys(%hash)));
  110. }
  111. ########################################################################
  112. # is_subset
  113. sub is_subset($$) {
  114. my $subset = shift;
  115. my $set = shift;
  116. foreach my $subitem (split(/ & /, $subset)) {
  117. my $match = 0;
  118. foreach my $item (split(/ & /, $set)) {
  119. if($subitem eq $item) {
  120. $match = 1;
  121. last;
  122. }
  123. }
  124. if(!$match) {
  125. return 0;
  126. }
  127. }
  128. return 1;
  129. }
  130. 1;