Serialize.pm 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # ex:ts=8 sw=4:
  2. # $OpenBSD: Serialize.pm,v 1.1 2013/11/16 16:39:28 espie Exp $
  3. #
  4. # Copyright (c) 2013 Marc Espie <espie@openbsd.org>
  5. #
  6. # Permission to use, copy, modify, and distribute this software for any
  7. # purpose with or without fee is hereby granted, provided that the above
  8. # copyright notice and this permission notice appear in all copies.
  9. #
  10. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. use strict;
  18. use warnings;
  19. # use to read/write some log files in a sensible fashion
  20. package DPB::Serialize;
  21. sub read
  22. {
  23. my ($class, $line) = @_;
  24. chomp $line;
  25. my @list = $class->list;
  26. my $r = {};
  27. my @e;
  28. ($r->{pkgpath}, @e) = split(/\s+/, $line);
  29. if ($r->{pkgpath} =~ m/^(.*)\((.*)\)$/) {
  30. ($r->{pkgpath}, $r->{pkgname}) = ($1, $2);
  31. }
  32. while (@e > 0) {
  33. my $v = shift @e;
  34. if ($v =~ m/^(\w+)\=(.*)$/) {
  35. $r->{$1} = $2;
  36. } else {
  37. my $k = shift @list or return;
  38. $r->{$k} = $v;
  39. }
  40. }
  41. return $r;
  42. }
  43. sub write
  44. {
  45. my ($class, $r) = @_;
  46. my @r = ();
  47. if ($r->{pkgname}) {
  48. push(@r, "$r->{pkgpath}($r->{pkgname})");
  49. } else {
  50. push(@r, $r->{pkgpath});
  51. }
  52. for my $k ($class->list) {
  53. if (defined $r->{$k}) {
  54. push(@r, "$k=$r->{$k}");
  55. }
  56. }
  57. return join(' ', @r);
  58. }
  59. package DPB::Serialize::Build;
  60. our @ISA = qw(DPB::Serialize);
  61. sub list
  62. {
  63. return (qw(host time size ts));
  64. }
  65. package DPB::Serialize::Size;
  66. our @ISA = qw(DPB::Serialize);
  67. sub list
  68. {
  69. return (qw(size ts));
  70. }
  71. 1;