1 package FileIO::FileIO; 2 # 3 # $RCSfile: FileIO.pm,v $ 4 # $Date: 2015/02/28 20:48:43 $ 5 # $Revision: 1.27 $ 6 # 7 # Author: Manish Sud <msud@san.rr.com> 8 # 9 # Copyright (C) 2015 Manish Sud. All rights reserved. 10 # 11 # This file is part of MayaChemTools. 12 # 13 # MayaChemTools is free software; you can redistribute it and/or modify it under 14 # the terms of the GNU Lesser General Public License as published by the Free 15 # Software Foundation; either version 3 of the License, or (at your option) any 16 # later version. 17 # 18 # MayaChemTools is distributed in the hope that it will be useful, but without 19 # any warranty; without even the implied warranty of merchantability of fitness 20 # for a particular purpose. See the GNU Lesser General Public License for more 21 # details. 22 # 23 # You should have received a copy of the GNU Lesser General Public License 24 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or 25 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330, 26 # Boston, MA, 02111-1307, USA. 27 # 28 29 use strict; 30 use Carp; 31 use Exporter; 32 use FileHandle; 33 use ObjectProperty; 34 35 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); 36 37 @ISA = qw(ObjectProperty Exporter); 38 @EXPORT = qw(); 39 @EXPORT_OK = qw(); 40 41 %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); 42 43 # Setup class variables... 44 my($ClassName); 45 _InitializeClass(); 46 47 # Class constructor... 48 sub new { 49 my($Class, %NamesAndValues) = @_; 50 51 # Initialize object... 52 my $This = {}; 53 bless $This, ref($Class) || $Class; 54 $This->_InitializeFileIO(); 55 56 $This->_InitializeFileIOProperties(%NamesAndValues); 57 58 return $This; 59 } 60 61 # Initialize object data... 62 # 63 sub _InitializeFileIO { 64 my($This) = @_; 65 66 # File name... 67 $This->{Name} = ''; 68 69 # Read, write or append... 70 $This->{Mode} = 'Read'; 71 72 # Open/close status... 73 $This->{Status} = 0; 74 75 # File handle returned by file open... 76 $This->{FileHandle} = ''; 77 } 78 79 # Initialize class ... 80 sub _InitializeClass { 81 #Class name... 82 $ClassName = __PACKAGE__; 83 84 } 85 86 # Initialize object properties.... 87 sub _InitializeFileIOProperties { 88 my($This, %NamesAndValues) = @_; 89 90 my($Name, $Value, $MethodName); 91 while (($Name, $Value) = each %NamesAndValues) { 92 $MethodName = "Set${Name}"; 93 $This->$MethodName($Value); 94 } 95 96 return $This; 97 } 98 99 # Close any open file... 100 sub DESTROY { 101 my($This) = @_; 102 103 $This->Close(); 104 105 return $This; 106 } 107 108 # Set file name and make sure it's not already set... 109 # 110 sub SetName { 111 my($This, $Name) = @_; 112 113 if ($This->{Name}) { 114 croak "Error: ${ClassName}->SetName: Can't set file name to $Name: $This->{Name}..."; 115 } 116 117 $This->{Name} = $Name; 118 119 return $This; 120 } 121 122 # Open file using specified mode... 123 # 124 sub Open { 125 my($This, $Mode) = @_; 126 127 if ($This->{Status}) { 128 croak "Error: ${ClassName}->Open: Can't open file $This->{Name}: It's already open..."; 129 } 130 131 if (defined $Mode) { 132 # Set mode... 133 $This->SetMode($Mode); 134 } 135 136 # Get name and mode... 137 my($Name); 138 $Name = $This->{Name}; 139 $Mode = $This->_GetOpenMode(); 140 141 # Open the file using specified mode and store FileHandle... 142 my($FileHandle); 143 $FileHandle = new FileHandle("${Mode}${Name}"); 144 if (!defined $FileHandle) { 145 croak "Error: ${ClassName}->Open: Can't open $Name: $! ..."; 146 } 147 $This->{FileHandle} = $FileHandle; 148 $This->{Status} = 1; 149 150 return $This; 151 } 152 153 # Close an open file... 154 sub Close { 155 my($This) = @_; 156 157 if ($This->{Status}) { 158 $This->{FileHandle}->close(); 159 } 160 $This->{Status} = 0; 161 162 return $This; 163 } 164 165 # Supported Mode values are: Read, Write, Append, <, >, >>, r, w, a 166 # 167 sub SetMode { 168 my($This, $SpecifiedMode) = @_; 169 my($Mode); 170 171 if (!defined $SpecifiedMode) { 172 $SpecifiedMode = 'Read'; 173 } 174 175 MODE: { 176 if ($SpecifiedMode =~ /^(Read|<|r)$/i) { $Mode = 'Read'; last MODE; } 177 if ($SpecifiedMode =~ /^(Write|>|w)$/i) { $Mode = 'Write'; last MODE; } 178 if ($SpecifiedMode =~ /^(Append|>>|a)$/i) { $Mode = 'Append'; last MODE; } 179 croak "Error: ${ClassName}->SetMode: Specified mode value, $SpecifiedMode, is not valid: Supported values: Read, Write, Append, <, >, >>, r, w, a..."; 180 } 181 $This->{Mode} = $Mode; 182 183 return $This; 184 } 185 186 # Get mode values to be used for file open function: <, >, >> 187 # 188 sub _GetOpenMode { 189 my($This) = @_; 190 my($Mode); 191 192 MODE: { 193 if ($This->{Mode} =~ /^(Read|<|r)$/i) { $Mode = '<'; last MODE; } 194 if ($This->{Mode} =~ /^(Write|>|w)$/i) { $Mode = '>'; last MODE; } 195 if ($This->{Mode} =~ /^(Append|>>|a)$/i) { $Mode = '>>'; last MODE; } 196 $Mode = ''; 197 } 198 return $Mode; 199 } 200