MayaChemTools

   1 package FileIO;
   2 #
   3 # $RCSfile: FileIO.pm,v $
   4 # $Date: 2008/04/25 00:00:55 $
   5 # $Revision: 1.11 $
   6 #
   7 # Author: Manish Sud <msud@san.rr.com>
   8 #
   9 # Copyright (C) 2004-2008 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 use 5.006;
  29 use strict;
  30 use Carp;
  31 use Exporter;
  32 use FileHandle;
  33 use ObjectProperty;
  34 
  35 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  36 
  37 $VERSION = '1.00';
  38 @ISA = qw(ObjectProperty Exporter);
  39 @EXPORT = qw();
  40 @EXPORT_OK = qw();
  41 
  42 %EXPORT_TAGS = (all  => [@EXPORT, @EXPORT_OK]);
  43 
  44 # Setup class variables...
  45 my($ClassName);
  46 _InitializeClass();
  47 
  48 # Class constructor...
  49 sub new {
  50   my($Class, %NamesAndValues) = @_;
  51 
  52   # Initialize object...
  53   my $This = {};
  54   bless $This, ref($Class) || $Class;
  55   $This->_InitializeFileIO();
  56 
  57   $This->_InitializeFileIOProperties(%NamesAndValues);
  58 
  59   return $This;
  60 }
  61 
  62 # Initialize object data...
  63 #
  64 sub _InitializeFileIO {
  65   my($This) = @_;
  66 
  67   # File name...
  68   $This->{Name} = '';
  69 
  70   # Read, write or append...
  71   $This->{Mode} = 'Read';
  72 
  73   # Open/close status...
  74   $This->{Status} = 0;
  75 
  76   # File handle returned by file open...
  77   $This->{FileHandle} = '';
  78 }
  79 
  80 # Initialize class ...
  81 sub _InitializeClass {
  82   #Class name...
  83   $ClassName = __PACKAGE__;
  84 
  85 }
  86 
  87 # Initialize object properties....
  88 sub _InitializeFileIOProperties {
  89   my($This, %NamesAndValues) = @_;
  90 
  91   my($Name, $Value, $MethodName);
  92   while (($Name, $Value) = each  %NamesAndValues) {
  93     $MethodName = "Set${Name}";
  94     $This->$MethodName($Value);
  95   }
  96 
  97   return $This;
  98 }
  99 
 100 # Close any open file...
 101 sub DESTROY {
 102   my($This) = @_;
 103 
 104   $This->Close();
 105 
 106   return $This;
 107 }
 108 
 109 # Set file name and make sure it's not already set...
 110 #
 111 sub SetName {
 112   my($This, $Name) = @_;
 113 
 114   if ($This->{Name}) {
 115     croak "Error: ${ClassName}->SetName: Can't set file name to $Name:  $This->{Name}...";
 116   }
 117 
 118   $This->{Name} = $Name;
 119 
 120   return $This;
 121 }
 122 
 123 # Open file using specified mode...
 124 #
 125 sub Open {
 126   my($This, $Mode) = @_;
 127 
 128   if ($This->{Status}) {
 129     croak "Error: ${ClassName}->Open: Can't open file $This->{Name}: It's already open...";
 130   }
 131 
 132   if (defined $Mode) {
 133     # Set mode...
 134     $This->SetMode($Mode);
 135   }
 136 
 137   # Get name and mode...
 138   my($Name);
 139   $Name = $This->{Name};
 140   $Mode = $This->_GetOpenMode();
 141 
 142   # Open the file using specified mode and store FileHandle...
 143   my($FileHandle);
 144   $FileHandle = new FileHandle("${Mode}${Name}");
 145   if (!defined $FileHandle) {
 146     croak "Error: ${ClassName}->Open: Can't open $Name:  $! ...";
 147   }
 148   $This->{FileHandle} = $FileHandle;
 149   $This->{Status} = 1;
 150 
 151   return $This;
 152 }
 153 
 154 # Close an open file...
 155 sub Close {
 156   my($This) = @_;
 157 
 158   if ($This->{Status}) {
 159     $This->{FileHandle}->close();
 160   }
 161   $This->{Status} = 0;
 162 
 163   return $This;
 164 }
 165 
 166 # Supported Mode values are: Read, Write, Append, <, >, >>, r, w, a
 167 #
 168 sub SetMode {
 169   my($This, $SpecifiedMode) = @_;
 170   my($Mode);
 171 
 172   if (!defined $SpecifiedMode) {
 173     $SpecifiedMode = 'Read';
 174   }
 175 
 176   MODE: {
 177     if ($SpecifiedMode =~ /^(Read|<|r)$/i) { $Mode = 'Read'; last MODE; }
 178     if ($SpecifiedMode =~ /^(Write|>|w)$/i) { $Mode = 'Write'; last MODE; }
 179     if ($SpecifiedMode =~ /^(Append|>>|a)$/i) { $Mode = 'Append'; last MODE; }
 180     croak "Error: ${ClassName}->SetMode: Specified mode value, $SpecifiedMode, is not valid: Supported values: Read, Write, Append, <, >, >>, r, w, a...";
 181   }
 182   $This->{Mode} = $Mode;
 183 
 184   return $This;
 185 }
 186 
 187 # Get mode values to be used for file open function: <, >, >>
 188 #
 189 sub _GetOpenMode {
 190   my($This) = @_;
 191   my($Mode);
 192 
 193   MODE: {
 194     if ($This->{Mode} =~ /^(Read|<|r)$/i) { $Mode = '<'; last MODE; }
 195     if ($This->{Mode} =~ /^(Write|>|w)$/i) { $Mode = '>'; last MODE; }
 196     if ($This->{Mode} =~ /^(Append|>>|a)$/i) { $Mode = '>>'; last MODE; }
 197     $Mode = '';
 198   }
 199   return $Mode;
 200 }
 201