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