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 vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  21. require Exporter;
  22. @ISA = qw(Exporter);
  23. @EXPORT = qw(
  24. append_file edit_file read_file replace_file
  25. normalize_set is_subset
  26. );
  27. @EXPORT_OK = qw();
  28. %EXPORT_TAGS = ();
  29. ########################################################################
  30. # _compare_files
  31. sub _compare_files($$) {
  32. my $file1 = shift;
  33. my $file2 = shift;
  34. local $/ = undef;
  35. return -1 if !open(IN, "< $file1");
  36. my $s1 = <IN>;
  37. close(IN);
  38. return 1 if !open(IN, "< $file2");
  39. my $s2 = <IN>;
  40. close(IN);
  41. return $s1 cmp $s2;
  42. }
  43. ########################################################################
  44. # append_file
  45. sub append_file($$@) {
  46. my $filename = shift;
  47. my $function = shift;
  48. open(OUT, ">> $filename") || die "Can't open file '$filename'";
  49. my $result = &$function(\*OUT, @_);
  50. close(OUT);
  51. return $result;
  52. }
  53. ########################################################################
  54. # edit_file
  55. sub edit_file($$@) {
  56. my $filename = shift;
  57. my $function = shift;
  58. open(IN, "< $filename") || die "Can't open file '$filename'";
  59. open(OUT, "> $filename.tmp") || die "Can't open file '$filename.tmp'";
  60. my $result = &$function(\*IN, \*OUT, @_);
  61. close(IN);
  62. close(OUT);
  63. if($result) {
  64. unlink("$filename");
  65. rename("$filename.tmp", "$filename");
  66. } else {
  67. unlink("$filename.tmp");
  68. }
  69. return $result;
  70. }
  71. ########################################################################
  72. # read_file
  73. sub read_file($$@) {
  74. my $filename = shift;
  75. my $function = shift;
  76. open(IN, "< $filename") || die "Can't open file '$filename'";
  77. my $result = &$function(\*IN, @_);
  78. close(IN);
  79. return $result;
  80. }
  81. ########################################################################
  82. # replace_file
  83. sub replace_file($$@) {
  84. my $filename = shift;
  85. my $function = shift;
  86. open(OUT, "> $filename.tmp") || die "Can't open file '$filename.tmp'";
  87. my $result = &$function(\*OUT, @_);
  88. close(OUT);
  89. if($result && _compare_files($filename, "$filename.tmp")) {
  90. unlink("$filename");
  91. rename("$filename.tmp", $filename);
  92. } else {
  93. unlink("$filename.tmp");
  94. }
  95. return $result;
  96. }
  97. ########################################################################
  98. # normalize_set
  99. sub normalize_set($) {
  100. local $_ = shift;
  101. if(!defined($_)) {
  102. return undef;
  103. }
  104. my %hash = ();
  105. foreach my $key (split(/\s*&\s*/)) {
  106. $hash{$key}++;
  107. }
  108. return join(" & ", sort(keys(%hash)));
  109. }
  110. ########################################################################
  111. # is_subset
  112. sub is_subset($$) {
  113. my $subset = shift;
  114. my $set = shift;
  115. foreach my $subitem (split(/ & /, $subset)) {
  116. my $match = 0;
  117. foreach my $item (split(/ & /, $set)) {
  118. if($subitem eq $item) {
  119. $match = 1;
  120. last;
  121. }
  122. }
  123. if(!$match) {
  124. return 0;
  125. }
  126. }
  127. return 1;
  128. }
  129. 1;