home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / FAQ / discus_admin_1357211388 / source / storage.pl < prev    next >
Text File  |  2009-11-06  |  5KB  |  199 lines

  1. # FILE: storage.pl
  2. # DESCRIPTION: Storing and retrieving arrays and hashes in flat text files
  3. #-------------------------------------------------------------------------------
  4. # DISCUS COPYRIGHT NOTICE
  5. #
  6. # Discus is copyright (c) 2002 by DiscusWare, LLC, all rights reserved.
  7. # The use of Discus is governed by the Discus License Agreement which is
  8. # available from the Discus WWW site at:
  9. #    http://www.discusware.com/discus/license
  10. #
  11. # Pursuant to the Discus License Agreement, this copyright notice may not be
  12. # removed or altered in any way.
  13. #-------------------------------------------------------------------------------
  14.  
  15. use strict;
  16. use vars qw($GLOBAL_OPTIONS $DCONF $PARAMS);
  17.  
  18. ###
  19. ### storage_encode_arguments
  20. ###
  21. ### Encodes subroutine arguments
  22. ###
  23.  
  24. sub storage_encode_arguments {
  25.     my @args = ();
  26.     my $internal_counter = 0;
  27.     foreach my $k (@_) {
  28.         if (ref $k eq "HASH") {
  29.             my ($u, $l, $ic) = storage_encode_hash($k, ++$internal_counter);
  30.             push @args, join(":", "ARG", "HASH($internal_counter)");
  31.             unshift @args, $u;
  32.             unshift @args, @{ $l };
  33.             $internal_counter = $ic;
  34.         } elsif (ref $k eq "ARRAY") {
  35.             my ($u, $l, $ic) = storage_encode_array($k, ++$internal_counter);
  36.             push @args, join(":", "ARG", "ARRAY($internal_counter)");
  37.             unshift @args, $u;
  38.             unshift @args, @{ $l };
  39.             $internal_counter = $ic;
  40.         } else {
  41.             my $u = join(":", "ARG", escape($k));
  42.             push @args, $u;
  43.         }
  44.     }
  45.     return @args;
  46. }
  47.  
  48. ###
  49. ### storage_unencode_arguments
  50. ###
  51. ### Unencodes an argument list
  52. ###
  53.  
  54. sub storage_unencode_arguments {
  55.     my ($file) = @_;    
  56.     my @args = ();
  57.     my @file = @{ $file };
  58.     my $hashes = {};
  59.     my $arrays = {};
  60.     foreach my $line (@file) {
  61.         chomp $line;
  62.         if ($line =~ /^ARRAY(\d+):/) {
  63.             $arrays->{$1} = $';
  64.         } elsif ($line =~ /^HASH(\d+):/) {
  65.             $hashes->{$1} = $';
  66.         } elsif ($line =~ /^ARG:/) {
  67.             my $aft = $';
  68.             if ($aft =~ /^HASH\((\d+)\)/) {
  69.                 push @args, storage_unencode_hash($1, $hashes, $arrays)
  70.             } elsif ($aft =~ /^ARRAY\((\d+)\)/) {
  71.                 push @args, storage_unencode_array($1, $hashes, $arrays);
  72.             } else {
  73.                 if ($aft eq "") {
  74.                     push @args, undef;
  75.                 } else {
  76.                     push @args, unescape($aft);
  77.                 }
  78.             }
  79.         }
  80.     }
  81.     return @args;    
  82. }
  83.  
  84. ###
  85. ### storage_unencode_hash
  86. ###
  87. ### Transforms an encoded hash into an unencoded hash
  88. ###
  89.  
  90. sub storage_unencode_hash {
  91.     my ($hashnum, $hashes, $arrays) = @_;
  92.     my @pairs = split(/&/, $hashes->{$hashnum});
  93.     my $hashout = {};
  94.     foreach my $pair (@pairs) {
  95.         my ($name, $value) = split(/=/, $pair);
  96.         $name = unescape($name);
  97.         if ($value =~ /^HASH\((\d+)\)/) {
  98.             $hashout->{$name} = storage_unencode_hash($1, $hashes, $arrays);
  99.         } elsif ($value =~ /^ARRAY\((\d+)\)/) {
  100.             $hashout->{$name} = storage_unencode_array($1, $hashes, $arrays);
  101.         } else {
  102.             $hashout->{$name} = unescape($value);
  103.         }
  104.     }
  105.     return $hashout;
  106. }
  107.  
  108. ###
  109. ### storage_unencode_array
  110. ###
  111. ### Transforms an encoded array into an unencoded array
  112. ###
  113.  
  114. sub storage_unencode_array {
  115.     my ($hashnum, $hashes, $arrays) = @_;
  116.     my @pairs = split(/&/, $arrays->{$hashnum});
  117.     my @aout = ();
  118.     foreach my $pair (@pairs) {
  119.         my ($name, $value) = split(/=/, $pair);
  120.         if ($value =~ /^HASH\((\d+)\)/) {
  121.             $aout[$name] = storage_unencode_hash($1, $hashes, $arrays);
  122.         } elsif ($value =~ /^ARRAY\((\d+)\)/) {
  123.             $aout[$name] = storage_unencode_array($1, $hashes, $arrays);
  124.         } else {
  125.             $aout[$name] = unescape($value);
  126.         }
  127.     }
  128.     return \@aout;
  129.  
  130. }
  131.  
  132. ###
  133. ### storage_encode_hash
  134. ###
  135. ### Transforms an unencoded hash into an encoded hash
  136. ###
  137.  
  138. sub storage_encode_hash {
  139.     my ($hash, $ctr) = @_;
  140.     my @k = keys(%{ $hash });
  141.     my @r = ();
  142.     my @o = ();
  143.     my $r = "HASH$ctr";
  144.     foreach my $key (@k) {
  145.         if (ref $hash->{$key} eq "HASH") {
  146.             my ($thathash, $otherhashes, $counter) = storage_encode_hash($hash->{$key}, ++$ctr);
  147.             $ctr = $counter;
  148.             push @r, join("=", escape($key), "HASH($ctr)");
  149.             push @o, $thathash;
  150.             push @o, @{ $otherhashes } if defined $otherhashes;
  151.         } elsif (ref $hash->{$key} eq "ARRAY") {
  152.             my ($thathash, $otherhashes, $counter) = storage_encode_array($hash->{$key}, ++$ctr);
  153.             $ctr = $counter;
  154.             push @r, join("=", escape($key), "ARRAY($ctr)");
  155.             push @o, $thathash;
  156.             push @o, @{ $otherhashes } if defined $otherhashes;
  157.         } else {
  158.             push @r, join("=", escape($key), escape($hash->{$key}));
  159.         }
  160.     }
  161.     return (join(":", $r, join("&", @r)), \@o, $ctr);
  162. }
  163.  
  164. ###
  165. ### storage_encode_array
  166. ###
  167. ### Transforms an unencoded array into an encoded array
  168. ###
  169.  
  170. sub storage_encode_array {
  171.     my ($array, $ctr) = @_;
  172.     my @k = @{$array};
  173.     my @r = ();
  174.     my @o = ();
  175.     my $r = "ARRAY$ctr";
  176.     my $count = 0;
  177.     foreach my $key (@k) {
  178.         if (ref $key eq "HASH") {
  179.             my ($thathash, $otherhashes, $counter) = storage_encode_hash($key, ++$ctr);
  180.             $ctr = $counter;
  181.             push @r, join("=", $count, "HASH($ctr)");
  182.             push @o, $thathash;
  183.             push @o, @{ $otherhashes } if defined $otherhashes;
  184.         } elsif (ref $key eq "ARRAY") {
  185.             my ($thathash, $otherhashes, $counter) = storage_encode_array($key, ++$ctr);
  186.             $ctr = $counter;
  187.             push @r, join("=", $count, "ARRAY($ctr)");
  188.             push @o, $thathash;
  189.             push @o, @{ $otherhashes } if defined $otherhashes;
  190.         } else {
  191.             push @r, join("=", $count, escape($key));
  192.         }
  193.         $count++;
  194.     }
  195.     return (join(":", $r, join("&", @r)), \@o, $ctr);
  196. }
  197.  
  198. 1;
  199.