import weakref
class ThanFile:
"A file that reads lines sequentialy, stores line number, and cant do unread."
lang = -1 # Default language is Greek
opened = [] # Opened files
def __init__(self, fr):
"Hold the opened file and do housekeeping."
self.fr = fr
self.linesf = 0
self.isSavedf = .false.
self.isEOFf = .false.
self.dlineSavf = "" # In case someone call unerfile1 before reading anything
self.icodf = 0
# if self.lang == 0: self.optFile1(0, -1) #-----If language is not set, then set language to Greek
self.opened.append(weakref.ref(self))
def reFile1ee(self):
"""This sr reads one line form file unit iun, and
stops the program if end of file or an error occurs
"""
buf, ierr = self.reFile1()
if ierr < 0:
self.erFile1s(DLFILEND) # Incomplete file
elif ierr != 0:
self.erFile1s(DLFILREA) # File can not be read
return buf
#==========================================================================
def reFile2ee(self):
"""This sr reads one line form file unit iun, and
stops the program if end of file or an error occurs
"""
buf, ierr = self.reFile1()
if ierr < 0:
self.erFile1(DLFILEND) # Incomplete file
elif ierr != 0:
self.erFile1(DLFILREA) # File can not be read
return buf, ierr
#==========================================================================
def reFile1e(self):
"""This sr reads one line form file unit iun, and
stops the program if an error occurs (except from end of file)
"""
buf, ierr = self.reFile1()
if ierr > 0:
self.erFile1s (DLFILREA) # File can not be read
return buf, ierr
#==========================================================================
def reFile2e(self):
"""This sr reads one line form file unit iun, and
stops the program if an error occurs (except from end of file)
"""
buf, ierr = self.reFile1()
if ierr > 0:
self.erFile1(DLFILREA) # File can not be read
return buf, ierr
#===========================================================================
def reFile1 (iun, buf, ierr):
"""
c---- This sr reads a line from file unit iun and returns an error code
c if an error or end of file ocuured
c-----The following code works around a bug(?) of microsoft compiler.
c The first time the end of file is occured, the compiler returns ierr<0.
c If we try to read the file again it returns ierr>0.
c To remedy this, the first time we find ierr<0, we set isEOF=.true.
c The next time a read is a attempted, we don't read the file at all,
c but we just set ierr<0.
"""
#-----We assume that the compiler returns ierr<0 when end of file
if self.isEOFf: return None, -1
#-----Unreading is enabled here
if not self.isSavedf:
ierr = 0
try: self.dlineSavf = self.fr.next()
except StopIteration:
self.isEOFf = True
ierr = -1
except:
ierr = 1
else:
ierr = 0
self.linesf(i) += 1
if ierr != 0: return None, ierr
buf = dlineSavf.rstrip()
self.isSavedf = False
return buf, ierr
#===========================================================================
def unreFile1(self):
"Causes the next refile1xx to reread the same line."
self.isSavedf(i) = True
#==========================================================================
def wrFile1ee(self, buf):
"""
include 'fildat.inc'
integer*4 iun ! ARGUMENTS
character*(*) buf ! ARGUMENTS
integer*4 ierr
logical*4 isFull
c-----This Sr writes a line into file unit iun, and stops the program
c if an error ocurred or the disk is full.
call wrFile1 (iun, buf, isFull, ierr)
if (isFull) then
terr(1) = DLDSKFUL
call erFile1s (iun, terr, 1)
else if (ierr .ne. 0) then
terr(1) = DLFILWRI
call erFile1s (iun, terr, 1)
end if
return
end
"""
pass
#==========================================================================
def wrFile2ee (self, buf):
"""
include 'fildat.inc'
integer*4 iun, ierr ! ARGUMENTS
character*(*) buf ! ARGUMENTS
logical*4 isFull
c-----This Sr writes a line into file unit iun, and stops the program
c if an error ocurred or the disk is full.
call wrFile1 (iun, buf, isFull, ierr)
if (isFull) then
terr(1) = DLDSKFUL
call erFile1 (iun, terr, 1)
else if (ierr .ne. 0) then
terr(1) = DLFILWRI
call erFile1 (iun, terr, 1)
end if
return
end
"""
pass
#===========================================================================
self wrFile1(self, buf):
"""
include 'fildat.inc'
integer*4 iun, ierr ! ARGUMENTS
logical*4 isFull ! ARGUMENTS
character*(*) buf ! ARGUMENTS
integer*4 lens ! FUNCTIONS
external lens
integer*4 IFULLCODE ! CONSTANTS
parameter (IFULLCODE=20) ! Error code for disk full
integer*4 i, n
character*(MCDL) temp
c-----This Sr writes a line into file unit iun.
i = iun - uArx
n = lens(buf)
temp = buf(1:n)
call greekConv1 (temp(1:n), icodf(i))
write (iun, 10, iostat=ierr) temp(1:n)
10 format (a)
linesf(i) = linesf(i) + 1
isFull = ierr .eq. IFULLCODE ! Denotes disk full
return
end
"""
pass
#===========================================================================
def opFile1e(self, filnam1, stat1):
"Opens the file and fails if it can not open it."
#-------It is assumed that stat1 is at least 3 characters long
tf = self.opFile1(filnam1, stat1)
if (ierr .eq. 0) return
if (stat1.strip().lower() == 'opt') return # Optional data
t = filnam1 + ': ' + DLFILACC
self.erFile1s(terr1)
#===========================================================================
def opFile1(self, filnam1, stat1):
"Try to open given file."
stat = stat1[:3].strip().lower()
if stat == '':
stat = 'w'
elif stat == 'app': # append
stat = 'w+'
elif stat == 'opt':
stat = 'r' # Optional data
try: fr = file(filnam1, stat) # Try to open file
except IOError: return 1
import openfile
file1 = Struct()
file1.ext = ext; file1.stat = stat; file1.desc = desc; file1.iPro = iPro
file1.linesf = 0 # It signals that the file unit is used
openfile.files1.append(file1)
return 0
#===========================================================================
def optFile1(self, iun, icod)
"""
c-----This routine sets the options of a file unit:
c iun : It is the unit of the datafile. If iun=0 then we set the
c language of the messages of the library
c icod : It is the sum iGre + ...
c where iGre= 0: No Greek conversion
c 1: Text is tranformed to Win Greek after reading,
c or before writing
c 2: Text is tranformed to Dos Greek after reading,
c or before writing
"""
if iun == 0:
self.laFile1 (icod)
# self.datLang (icod) # This is for DatLin class
self.__class__.lang = icod
else:
self.icodf = icod
c===========================================================================
subroutine inpFile1 (mhn, iun, kat, stat, ncdl1)
include 'fildat.inc'
integer*4 iun, ncdl1 ! ARGUMENTS
character*(*) mhn, stat, kat ! ARGUMENTS
integer*4 lens1 ! FUNCTIONS
character*78 tg78
character*120 tg120
external lens1, tg78, tg120
integer*4 ierr
character*10 kat1
character*80 filnam
character*100 mhn1, mhn2
c-----This sr opens a file with the a prefix supplied by user and suffix kat.
c If a wrong file is given, it is asked again.
c If kat == ' ', then the use may supply and the suffix. Else he can't
c-----Form message
if (kat .eq. ' ') then
write (mhn1, 50) mhn
50 format (' FILE ', a, ': ')
else
write (mhn1, 60) mhn, kat
60 format (' FILE ', a, ' (.', a, '): ')
end if
c-----Get and open file
100 continue
call prints (mhn1(1:lens1(mhn1)+1))
read 10, filnam
10 format (a)
call getSufix (filnam, kat1)
if (filnam .eq. ' ') then
print 20, tg78(DLTRYAGA) ! Try again
20 format (1x, a, /' ')
else if (kat .ne. ' ' .and. kat1 .ne. ' ') then
print 30, tg78(DLFILSUF) ! No suffix allowed
30 format (/1x, a)
print 20, tg78(DLTRYAGA)
else
if (kat .ne. ' ') call putSufix (filnam, kat)
call opFile1 (iun, filnam, stat, ncdl1, ierr)
if (ierr .eq. 0) return
print 40, filnam(1:lens1(filnam)), tg78(DLFILACC) ! Can't access file
40 format (/1x, a, ': ', a)
print 20, tg78(DLTRYAGA)
end if
go to 100
end
c===========================================================================
subroutine rwFile1 (iun)
include 'fildat.inc'
integer*4 iun ! ARGUMENTS
integer*4 i
i = iun - uArx
linesf(i) = 0
isSavedf(i) = .false.
isEOFf(i) = .false.
rewind iun
return
end
c===========================================================================
subroutine dlFile1 (iun, nLen, MCDL1)
include 'fildat.inc'
integer*4 iun, nLen, MCDL1 ! ARGUMENTS
c-----This routine checks if constant MCDL is the same in the libraries
c and returns the line length defined in Sr opFile1
if (MCDL1. ne. MCDL) then
terr(1) = ' Severe Library Error: Constant "MCDL" is not'
terr(2) = ' the same in fildat.for and datlin.for!'
call erFile1s (0, terr, 2)
end if
nLen = ncdl(iun-uArx)
if (nLen .le. 0) nLen = 80
return
end
c============================================================================
subroutine erInc1 (nInc, mInc, uInc, text)
include 'fildat.inc'
integer*4 nInc, mInc, uInc
character*(*) text
integer*4 lens1 ! FUNCTIONS
external lens1
c-----This Sr adds 1 to variable nInc. If nInc becomes greater than mInc
c then an error message is generated. It also prints the file where the
c error happened if uInc <> 0.
nInc = nInc + 1
if (nInc .le. MINC) return
write (terr, 10) DLMAXNUM(1:lens1(DLMAXNUM)), text, mInc ! Maximum number of text reached
10 format (1x, a, 1x, a, ':', i10)
call erFile1s (uInc, terr, 1)
end
c===========================================================================
subroutine erSyntax1s (iun)
include 'fildat.inc'
integer*4 iun ! ARGUMENTS
c-----This Sr writes "syntax error" to the screen and to file mediate.tmp.
c Then, it stops the program.
terr(1) = DLERRSYN ! Syntax error
call erFile1s (iun, terr, 1)
end
c===========================================================================
subroutine erSyntax1 (iun)
include 'fildat.inc'
integer*4 iun ! ARGUMENTS
c-----This Sr writes "syntax error" to the screen and to file mediate.tmp.
c Then, it stops the program.
terr(1) = DLERRSYN
call erFile1 (iun, terr, 1)
return
end
#===========================================================================
def waFile1(self, terr1):
"This sr writes the warning to the screen and to file mediate.tmp."
self.erwaFile1(terr1, DLWARLIN) # Warning at line
#===========================================================================
def erFile1(self, terr1):
"This sr writes the error message to the screen and to file mediate.tmp."
self.erwaFile1(terr1, DLERRLIN) # Error at line
#===========================================================================
def erFile1s(self, terr1):
"""This Sr writes the error message to the screen and to file mediate.tmp.
Then, it stops the program.
"""
self.erFile1 (iun, terr1, nterr)
self.stopErr1()
#===========================================================================
def stopErr1(self):
"This routine stops the program and it says so."
prg(self.DLERRSTO) # Errors logged. Program stops.
self.uArx.write("%\n" % self.DLERRSTO)
self.doCloseFiles1() # Close all open files
self.errMedFile1() # Report error to mediate.tmp
sys.exit(1) # Stop the program
#===========================================================================
subroutine erwaFile1 (iun, terr1, nterr, erwa)
"This sr writes a message to the screen and to file mediate.tmp."
if self.f != None:
t = "%s %d %s %s" % (erwa, self.linesf, DLOFFIL, self.f.name)
prg(t)
self.uArx.write("%s\n" % t)
prg(terr1)
self.uArx.write("%s\n" % terr1)
#===========================================================================
def doCloseFiles1(self):
"Close all the opened files."
for ref in self.opened:
ref = ref()
if ref == None: continue
if self.f == None: continue
self.f.close()
#===========================================================================
def closeFiles1(self):
"Close all the opened files and report."
self.doCloseFiles1() # Close all opened files
self.okMedFile1() # Report Ok to mediate.tmp
#===========================================================================
def laFile1(self, langmes):
"Chooses language for the error messages."
c = self.__class__
ppro = 'egn', 'ydr'
If (langmes .eq. -1) Then
c.DLERRLIN = ' '
c.DLWARLIN = ' '
c.DLOFFIL = ' '
c.DLERRSTO = ' . .'
c.DLMAXNUM = ' '
c.DLERRSYN = ' .'
c.DLFILACC = ' .'
c.DLLIBUNI = ' filDat.lib file units '
c.DLFILEND = ' .'
c.DLFILREA = ' .'
c.DLDSKFUL = ' .'
c.DLFILWRI = ' .'
c.DLTRYAGA = ' .'
c.DLFILSUF = ' !'
c.DLMAXPRE = ' 2 .'
c.DLPREFIX = ' : '
c.DLPREFI1 = ' 1 : '
c.DLPREFI2 = ' 2 : '
c.DLMES01 = ' FILES .'
c.DLMES02 = ' .'
c.DLMES03 = ' "'+ppro[0]+'"'
c.DLMES04 = ' FILES:'
c.DLMES05 = ' 1 "'+ppro[0]+'" 2 "'+ppro[1]+'"'
c.DLFILDAT = '()'
c.DLFILRES = '()'
c.DLFILOPT = '()'
c.DLNOSUF = ' !'
c.DLNODESC = '( )'
c.DLGETPRE = ' '
c.DLERRPRV = ' :'
c.DLLOGOST = ' A. S T A M O S S. A. '
else:
c.DLERRLIN = 'Error at line'
c.DLWARLIN = 'Warning at line'
c.DLOFFIL = 'Of file'
c.DLERRSTO = 'Errors logged. Program stops.'
c.DLMAXNUM = 'Max number of'
c.DLERRSYN = 'Syntax error.'
c.DLFILACC = 'File can not be accessed'
c.DLLIBUNI = 'Library filDat.lib accepts file units between'
c.DLFILEND = 'Unexpected end of file.'
c.DLFILREA = 'File can not be read.'
c.DLDSKFUL = 'The disk is full.'
c.DLFILWRI = 'The file can not be written to.'
c.DLTRYAGA = 'Try again.'
c.DLFILSUF = 'Do not specify suffix in the filename!'
c.DLMAXPRE = 'Up to 2 prefixes are allowed.'
c.DLPREFIX = ' PREFIX : '
c.DLPREFI1 = ' PREFIX 1 : '
c.DLPREFI2 = ' PREFIX 2 : '
c.DLMES01 = 'The data files are composed by a prefix and a suffix.'
c.DLMES02 = 'The suffix is predefined, and the suffix is given by the user.'
c.DLMES03 = 'For example given the prefix "'+ppro[0]+'"'
c.DLMES04 = 'the program opens the following FILES:'
c.DLMES05 = 'For example given the prefix 1 "'+ppro[0]+'" and the prefix2 "'+ppro[1]+'"'
c.DLFILDAT = '(data)'
c.DLFILRES = '(results)'
c.DLFILOPT = '(optional)'
c.DLNOSUF = 'Do not append a suffix to the prefix!'
c.DLNODESC = '(No description available for previous program)'
c.DLGETPRE = 'Getting prefix from file'
c.DLERRPRV = 'Error in the execution of the previous program:'
c.DLLOGOST = '!$B3)"Y%U$B%V"U%T$!X7"G3'
c.DLLOGOST = ' A. S T A M O S S. A. '
|