# HG changeset patch # User Claus Gittinger # Date 1038305194 -3600 # Node ID 9aa286d3a644825268ba2a55c1a4e16256d8b4d1 # Parent f25449fa7a846a2bd1c7f8c7cc2288c2df44f684 added newTemporary with template diff -r f25449fa7a84 -r 9aa286d3a644 Filename.st --- a/Filename.st Tue Nov 26 11:00:38 2002 +0100 +++ b/Filename.st Tue Nov 26 11:06:34 2002 +0100 @@ -468,36 +468,7 @@ whatever to it in order to really create something). See also: #newTemporary which looks for a good temp directory." - |pid nameString fn| - - (self ~~ ConcreteClass) ifTrue:[ - ^ ConcreteClass newTemporaryIn:aDirectoryPrefix - ]. - - "/ care for existing leftOver tempFiles - "/ from a previous boot of the OS - "/ (i.e. my pid could be the same as when executed - "/ the last time before system reboot ...) - - [fn isNil or:[fn exists]] whileTrue:[ - "/ although the above allows things to be redefined in concrete classes, - "/ the following should work on all systems ... - - NextTempFilenameIndex isNil ifTrue:[ - NextTempFilenameIndex := 1. - ]. - - pid := OperatingSystem getProcessId printString. - nameString := (self tempFileNameTemplate) bindWith:pid with:(NextTempFilenameIndex printString). - NextTempFilenameIndex := NextTempFilenameIndex + 1. - - (aDirectoryPrefix isNil or:[aDirectoryPrefix asString isEmpty]) ifFalse:[ - fn := aDirectoryPrefix asFilename construct:nameString - ] ifTrue:[ - fn := self named:nameString - ] - ]. - ^ fn + ^ self newTemporaryIn:aDirectoryPrefix nameTemplate:self tempFileNameTemplate "temp files in '/tmp': @@ -525,6 +496,73 @@ "Modified: / 11.8.1998 / 22:51:43 / cg" ! +newTemporaryIn:aDirectoryPrefix nameTemplate:template + "return a new unique filename - use this for temporary files. + The filenames returned are in aDirectoryPrefix and named after the given template, + in which %1 and %2 are expanded to the unix process id, and a unique number, incremented + with every call to this method respectively. + Notice: only a unique filename object is created and returned - no physical + file is created by this method (i.e. you have to send #writeStream or + whatever to it in order to really create something). + See also: #newTemporary which looks for a good temp directory." + + |pid nameString fn| + + (self ~~ ConcreteClass) ifTrue:[ + ^ ConcreteClass newTemporaryIn:aDirectoryPrefix nameTemplate:template + ]. + + "/ care for existing leftOver tempFiles + "/ from a previous boot of the OS + "/ (i.e. my pid could be the same as when executed + "/ the last time before system reboot ...) + + [fn isNil or:[fn exists]] whileTrue:[ + "/ although the above allows things to be redefined in concrete classes, + "/ the following should work on all systems ... + + NextTempFilenameIndex isNil ifTrue:[ + NextTempFilenameIndex := 1. + ]. + + pid := OperatingSystem getProcessId printString. + nameString := template bindWith:pid with:(NextTempFilenameIndex printString). + NextTempFilenameIndex := NextTempFilenameIndex + 1. + + (aDirectoryPrefix isNil or:[aDirectoryPrefix asString isEmpty]) ifFalse:[ + fn := aDirectoryPrefix asFilename construct:nameString + ] ifTrue:[ + fn := self named:nameString + ] + ]. + ^ fn + + "temp files in '/tmp': + + Filename newTemporary + " + + "temp files somewhere + (not recommended - use above since it can be controlled via shell variables): + + UnixFilename newTemporaryIn:'/tmp' nameTemplate:'foo%1_%2' + UnixFilename newTemporaryIn:'/tmp' nameTemplate:'foo%1_%2' + UnixFilename newTemporaryIn:'/usr/tmp' nameTemplate:'foo%1_%2' + UnixFilename newTemporaryIn:'/' nameTemplate:'foo%1_%2' + " + + "a local temp file: + + Filename newTemporaryIn:'' nameTemplate:'foo%1_%2' + Filename newTemporaryIn:nil nameTemplate:'foo%1_%2' + Filename newTemporaryIn:'.' nameTemplate:'foo%1_%2' + Filename newTemporaryIn:('source' asFilename) nameTemplate:'foo%1_%2' + " + + "Modified: / 7.9.1995 / 10:48:31 / claus" + "Modified: / 11.8.1998 / 22:51:43 / cg" +! + nullDevice "return the filename of the nullDevice (if available). returns nil, if the OperatingSystem does not support this." @@ -4516,7 +4554,7 @@ !Filename class methodsFor:'documentation'! version - ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.231 2002-11-08 16:15:46 penk Exp $' + ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.232 2002-11-26 10:06:34 cg Exp $' ! ! Filename initialize!