comparison lib/FileIO/FileIO.pm @ 0:4816e4a8ae95 draft default tip

Uploaded
author deepakjadmin
date Wed, 20 Jan 2016 09:23:18 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4816e4a8ae95
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
201 1;
202
203 __END__
204
205 =head1 NAME
206
207 FileIO
208
209 =head1 SYNOPSIS
210
211 use FileIO::FileIO;
212
213 use FileIO::FileIO qw(:all);
214
215 =head1 DESCRIPTION
216
217 B<FIleIO> class provides following methods:
218
219 new, Close, Open, SetMode
220
221 B<FleIO> class serves as a base class for all classes involved in file IO. It is derived from
222 B<ObjectProperty> base class which provides methods not explicitly defined in B<Atom> or
223 B<ObjectProperty> class using Perl's AUTOLOAD functionality. These methods
224 are generated on-the-fly for a specified object property:
225
226 Set<PropertyName>(<PropertyValue>);
227 $PropertyValue = Get<PropertyName>();
228 Delete<PropertyName>();
229
230 =head2 METHODS
231
232 =over 4
233
234 =item B<new>
235
236 $NewFileIO = new FileIO(%NamesAndValues);
237
238 Using specified I<FileIO> property names and values hash, B<new> method creates a new object
239 and returns a reference to a newly created B<FileIO> object. By default, the following properties are
240 initialized:
241
242 Name = '';
243 Mode = 'Read';
244 Status = 0;
245 FileHandle = '';
246
247 =item B<Close>
248
249 $FileIO->Close();
250
251 Close open file and returns I<FileIO>.
252
253 =item B<Open>
254
255 $FileIO->Open();
256
257 Opens the file using file I<Name> and I<Mode> and returns I<FileIO>.
258
259 =item B<SetMode>
260
261 $FileIO->SetMode($Mode);
262
263 Sets up file I<Mode> and returns I<FileIO> Default I<Mode> value: I<Read>.
264 Supported I<Mode> values:
265
266 Read, Write, Append, <, >, >>, r, w, a
267
268 =item B<SetName>
269
270 $FileIO->SetName($Name);
271
272 Sets up file I<Name> and returns I<FileIO>.
273
274 =back
275
276 =head1 AUTHOR
277
278 Manish Sud <msud@san.rr.com>
279
280 =head1 SEE ALSO
281
282 MoleculeFileIO.pm, MDLMolFileIO.pm, SDFileIO.pm
283
284 =head1 COPYRIGHT
285
286 Copyright (C) 2015 Manish Sud. All rights reserved.
287
288 This file is part of MayaChemTools.
289
290 MayaChemTools is free software; you can redistribute it and/or modify it under
291 the terms of the GNU Lesser General Public License as published by the Free
292 Software Foundation; either version 3 of the License, or (at your option)
293 any later version.
294
295 =cut