| 1 | package LCC::Backend::textfile; | ||
| 2 | |||
| 3 | # Make sure we do everything by the book | ||
| 4 | # Set modules to inherit from | ||
| 5 | # Set version information | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | @LCC::Backend::textfile::ISA = qw(LCC::Backend); | ||
| 9 | $LCC::Backend::textfile::VERSION = '0.02'; | ||
| 10 | |||
| 11 | # Default filename | ||
| 12 | |||
| 13 | my $default_file = 'LCC.textfile'; | ||
| 14 | |||
| 15 | # Return true value for use | ||
| 16 | |||
| 17 | 1; | ||
| 18 | |||
| 19 | #------------------------------------------------------------------------ | ||
| 20 | |||
| 21 | # The following methods are class methods | ||
| 22 | |||
| 23 | #------------------------------------------------------------------------ | ||
| 24 | |||
| 25 | # IN: 1 instantiated object | ||
| 26 | # 2 (optional) flag, specifying whether to force partial update | ||
| 27 | |||
| 28 | sub partial { | ||
| 29 | |||
| 30 | # Obtain the object | ||
| 31 | # Obtain the source filename | ||
| 32 | # Return now if the file does not exist | ||
| 33 | |||
| 34 | my $self = shift; | ||
| 35 | my $source = $self->{'source'} || $default_file; | ||
| 36 | return $self unless -e $source; | ||
| 37 | |||
| 38 | # Initialize the handle | ||
| 39 | # If we're reading from a gzipped file | ||
| 40 | # Attempt to open the file through a gzip pipe | ||
| 41 | # Else (just an ordinary file) | ||
| 42 | # Attempt to open file for reading normally | ||
| 43 | # Add error if failed | ||
| 44 | |||
| 45 | my $handle; | ||
| 46 | if ($source =~ m#\.gz$#) { | ||
| 47 | $handle = IO::File->new( "gzip --stdout $source |" ); | ||
| 48 | } else { | ||
| 49 | $handle = IO::File->new( $source,'<' ); | ||
| 50 | } | ||
| 51 | $self->_add_error( "Could not open file '$source' for reading: $!" ) | ||
| 52 | unless $handle; | ||
| 53 | |||
| 54 | # Initialize the reference to the old hash | ||
| 55 | # While there are lines to be read | ||
| 56 | # Split on the first "real" null byte | ||
| 57 | # Add the id to the hash with its value | ||
| 58 | # Check whether UNS required full action | ||
| 59 | |||
| 60 | my $old = $self->{'old'} ||= {}; | ||
| 61 | while (<$handle>) { | ||
| 62 | my ($key,$list) = m#^(.[^\0]+)\0(.*)$#; | ||
| 63 | $key =~ m#^\0# ? ($self->{$key} = $list) : ($old->{$key} = $list); | ||
| 64 | } | ||
| 65 | $self->_check_uns_complete unless shift || ''; | ||
| 66 | } #partial | ||
| 67 | |||
| 68 | #------------------------------------------------------------------------ | ||
| 69 | |||
| 70 | # The following methods change the object | ||
| 71 | |||
| 72 | #------------------------------------------------------------------------ | ||
| 73 | |||
| 74 | # IN: 1 instantiated object | ||
| 75 | |||
| 76 | sub update { | ||
| 77 | |||
| 78 | # Obtain the object | ||
| 79 | # Add error if unclear what kind of update was done | ||
| 80 | # Return now if there is nothing to do | ||
| 81 | |||
| 82 | my $self = shift; | ||
| 83 | $self->_add_error( "Unclear whether 'complete' or 'partial' update" ) | ||
| 84 | unless exists( $self->{'old'} ); | ||
| 85 | return unless exists $self->{'new'}; | ||
| 86 | |||
| 87 | # Create local copy of reference to old hash | ||
| 88 | # Create local copy of reference to new hash | ||
| 89 | # For all of the key => value pairs in the new hash | ||
| 90 | # Save/Overwrite the new value in the old hash | ||
| 91 | |||
| 92 | my $old = $self->{'old'}; | ||
| 93 | my $new = $self->{'new'}; | ||
| 94 | while (my ($key,$value) = each( %{$new} )) { | ||
| 95 | $old->{$key} = $value; | ||
| 96 | } | ||
| 97 | |||
| 98 | # Initialize the handle | ||
| 99 | # Obtain the name of the source file | ||
| 100 | # If we're to store to a gzipped file | ||
| 101 | # Open a pipe to write to a gzipped file | ||
| 102 | # Else (a normal file) | ||
| 103 | # Create ordinary file to write to | ||
| 104 | # Add error if failed | ||
| 105 | |||
| 106 | my $handle; | ||
| 107 | my $source = $self->{'source'} || $default_file; | ||
| 108 | if ($source =~ m#\.gz$#) { | ||
| 109 | $handle = IO::File->new( "| gzip --best - >$source.new" ); | ||
| 110 | } else { | ||
| 111 | $handle = IO::File->new( "$source.new",'>' ); | ||
| 112 | } | ||
| 113 | $self->_add_error( "Could not open file '$source.new' for writing: $!" ) | ||
| 114 | unless $handle; | ||
| 115 | |||
| 116 | # For all of the special fields that need to be saved | ||
| 117 | # Write a line to the file | ||
| 118 | |||
| 119 | foreach ($self->_additional_fields) { | ||
| 120 | print $handle "\0$_\0$self->{$_}\n" if exists $self->{$_}; | ||
| 121 | } | ||
| 122 | |||
| 123 | # While there are keys to be handled from the now old hash | ||
| 124 | # Write a line to the file | ||
| 125 | |||
| 126 | while (my ($id,$value) = each %{$old}) { | ||
| 127 | print $handle "$id\0$value\n"; | ||
| 128 | } | ||
| 129 | |||
| 130 | # Forget about any changes made | ||
| 131 | # Move the current file to "old" file | ||
| 132 | # Move the "new" file to current file | ||
| 133 | |||
| 134 | delete( $self->{'new'} ); | ||
| 135 | rename( $source,"$source.old" ); | ||
| 136 | rename( "$source.new",$source ); | ||
| 137 | } #update | ||
| 138 | |||
| 139 | #------------------------------------------------------------------------ | ||
| 140 | |||
| 141 | # The following subroutines deal with standard Perl features | ||
| 142 | |||
| 143 | #------------------------------------------------------------------------ | ||
| 144 | |||
| 145 | __END__ |