package GMS::MemberFile; require 5.6.0; use strict; require GMS; require Tie::Hash; use Carp; our $VERSION = "0.75"; our @ISA = ("GMS","Exporter","Tie::StdHash"); our @EXPORT = qw(); our @EXPORT_OK = qw(); our $GMS = new GMS(settings => 1); #USE: tie(%HASH,"GMS::MemberFile",{UID=>123,NAME=>'John'}); #Accepted Arguments #UID=>UID #NAME=>NAME #GENFILE=>1/0 #READONLY => 1/0 #Under normal circumstances, the only required field is UID... when generating #a file, however, NAME and GENFILE are required, and if UID is specified #the tie will fail. #Using READONLY on, you cannot change values, readonly is ignored if genfile is active sub TIEHASH { my ($self,%DETAIL) = @_; my ($this,$filedata,$ini,$uid,$tohash, $name); unless(exists $DETAIL{UID} xor $DETAIL{GENFILE}){ croak "Missing UID in TIE attempt or UID provided while constructing new file, UID = $DETAIL{UID}, GENFILE = $DETAIL{GENFILE}"; } #my $MEMBERDIRL = $GMS->getsetting("FILE","MEMBERDIRL"); my $MEMBERDIRL = 'TestMember\\'; if($DETAIL{GENFILE}){ $DETAIL{READONLY} = 0; croak "Missing MemberName in memberfile generation attempt" unless($DETAIL{'MEMBER'}); #$uid = $GMS->getsetting("OTHER","UIDCOUNT") + 1; $uid = 1; $ini = new IniFile("$MEMBERDIRL$DETAIL{'MEMBER'}.gmf"); croak "Error, UID retrieved from settings already exists" if($ini->exists([$uid])); $ini->put([$uid,"UID",$uid], -add => 1); $ini->put([$uid,"DATEJOIN", time], -add => 1); $ini->save(); #$GMS->changesetting("OTHER","UIDCOUNT",$uid); #print "Saved GenFile as $MEMBERDIRL$DETAIL{'MEMBER'}.gmf"; }else{ $uid = $DETAIL{'UID'}; if(exists $DETAIL{MEMBER} && defined $DETAIL{MEMBER} && -e "$MEMBERDIRL$DETAIL{MEMBER}.gmf"){ $ini = new IniFile("$MEMBERDIRL$DETAIL{MEMBER}.gmf"); croak "Unable to locate UID '$uid' in $MEMBERDIRL$DETAIL{'MEMBER'}.gmf" unless($ini->exists([$uid])); }else{ my $mlist = new IniFile($MEMBERDIRL."memberlist.gms"); croak "Unable to locate UID '$uid' in $MEMBERDIRL"."memberlist.gms" unless($name = $ini->get(['UID',$uid], -mapping => 'single')); $ini = new IniFile("$MEMBERDIRL$name.gmf"); croak "Unable to locate UID '$uid' in $MEMBERDIRL$DETAIL{'MEMBER'}.gmf" unless($ini->exists([$uid])); } } $tohash = $ini->get([$uid]); #print "$tohash"; croak "Unknown Error retrieving file data: $!" unless(ref($tohash)); $filedata = {_ini => $ini, _uid => $uid, _read => $DETAIL{'READONLY'}, _keylist => {} }; #print " Generated filedata hash\n"; my %temparray = getdatakey($ini,$uid); $filedata->{_keylist} = \%temparray; #print "\n"."returning"; return bless $filedata, $self; } sub FETCH { my($self,$key) = @_; my $ini = $self->{_ini}; my $uid = $self->{_uid}; croak "Unable to locate $key" unless($ini->exists([$uid,$key])); return $ini->get([$uid,$key], -mapping => 'single'); } sub STORE { my($self,$key,$change) = @_; if(!$self->{_read}){ my $ini = $self->{_ini}; croak "INI Missing" unless(ref $ini); my $uid = $self->{_uid}; $ini->delete([$uid,$key]); $ini->put([$uid,$key,$change], -add => 1); my %temp = getdatakey($ini,$uid); $self->{_keylist} = \%temp; } print "Store $key, $change"; } sub DELETE { my($self,$key) = @_; if(!$self->{_read}){ my $ini = $self->{_ini}; my $uid = $self->{_uid}; $ini->delete([$uid,$key]); my %temp = getdatakey($ini,$uid); $self->{_keylist} = \%temp; } } sub FIRSTKEY { my($self,$key) = @_; my $temp = keys(%{ $self->{_keylist} }); return scalar each %{ $self->{_keylist} }; } sub NEXTKEY { my $self = shift; return scalar each %{ $self->{_keylist} }; } sub DESTROY { print "Attempting Save"; my $self = shift; my $ini = $self->{_ini}; print "Ini Is: ".ref $ini; if(!$self->{_read}){ print "Stage 2\n"; #Fails here my $ini = $self->{_ini}; print "Stage 3: ini = $ini !!!\n"; print ref $ini; croak "\n\nError retrieving INI interface" unless(ref $ini); print "Stage 4\n"; $ini->save() || croak "Failed Save! $!"; print "Saved!\n"; }else{ print "Skiping due to read only"; } } #Send it the INI and the UID, it should be able to determine the current list of keys sub getdatakey { return; my($ini,$uid) = @_; my (%temp,$key); my %testhash = %{ $ini->get([$uid]) }; foreach $key (keys %testhash){ $temp{$key} = 1; #print "Processing '$key'"; } #print "Finished Getdatakey\n"; return %temp; } 1; #### #!/usr/bin/perl use GMS::MemberFile; %member; tie(%member,"GMS::MemberFile", MEMBER=>'Iron', GENFILE => 0, UID=> 1); print "The UID for the new member is $member{UID}\n"; print "Did Ini Exit already?";