Filename.st
changeset 1049 6f7b6c18a748
parent 932 f57ec42ceb44
child 1080 541f7ef2fcd4
equal deleted inserted replaced
1048:c535c5a73cba 1049:6f7b6c18a748
   214     "
   214     "
   215      Filename defaultDirectoryName 
   215      Filename defaultDirectoryName 
   216     "
   216     "
   217 !
   217 !
   218 
   218 
       
   219 fromComponents:aCollectionOfDirectoryNames
       
   220     "create & return a new filename from components given in
       
   221      aCollectionOfDirectoryNames. If the first component is the name of the
       
   222      root directory (i.e. '/') an absolute path-filename is returned."
       
   223 
       
   224     |sep s|
       
   225 
       
   226     sep := self separator asString.
       
   227     s := ''.
       
   228     aCollectionOfDirectoryNames keysAndValuesDo:[:index :component |
       
   229         index == 1 ifTrue:[
       
   230             (component ~= sep 
       
   231             or:[aCollectionOfDirectoryNames size == 1]) ifTrue:[
       
   232                 s := s , component
       
   233             ]
       
   234         ] ifFalse:[
       
   235             s := s , '/' , component
       
   236         ].
       
   237     ].
       
   238     ^ self named:s
       
   239 
       
   240     "
       
   241      Filename fromComponents:#('/' 'foo' 'bar' 'baz')  
       
   242      Filename fromComponents:#('foo' 'bar' 'baz')  
       
   243      Filename fromComponents:#('/')  
       
   244 
       
   245      Filename fromComponents:
       
   246          (Filename components:('.' asFilename pathName))
       
   247 
       
   248      Filename fromComponents:
       
   249          (Filename components:('.' asFilename name)) 
       
   250     "
       
   251 
       
   252     "Modified: 29.2.1996 / 20:18:34 / cg"
       
   253 !
       
   254 
   219 fromUser
   255 fromUser
   220     "show a box to enter a filename. Return a filename instance or
   256     "show a box to enter a filename. Return a filename instance or
   221      nil (if cancel was pressed)."
   257      nil (if cancel was pressed)."
   222 
   258 
   223     |name|
   259     |name|
   227     ^ nil
   263     ^ nil
   228 
   264 
   229     "
   265     "
   230      Filename fromUser
   266      Filename fromUser
   231     "
   267     "
       
   268 !
       
   269 
       
   270 homeDirectory
       
   271     "return your homeDirectory.
       
   272      Some OperatingSystems do not support this - on those, the defaultDirectory
       
   273      (which is the currentDirectory) is returned."
       
   274 
       
   275     |s|
       
   276 
       
   277     s := OperatingSystem getHomeDirectory.
       
   278     s isNil ifTrue:[
       
   279         ^ self defaultDirectory
       
   280     ].
       
   281     ^ s asFilename
       
   282 
       
   283     "
       
   284      Filename homeDirectory        
       
   285     "
       
   286 
       
   287     "Modified: 29.2.1996 / 21:00:31 / cg"
   232 !
   288 !
   233 
   289 
   234 named:aString
   290 named:aString
   235     "return a filename for a directory named aString.
   291     "return a filename for a directory named aString.
   236      This is the same as 'aString asFilename'."
   292      This is the same as 'aString asFilename'."
   324     "Modified: 7.9.1995 / 10:48:31 / claus"
   380     "Modified: 7.9.1995 / 10:48:31 / claus"
   325     "Modified: 4.11.1995 / 20:26:23 / cg"
   381     "Modified: 4.11.1995 / 20:26:23 / cg"
   326 ! !
   382 ! !
   327 
   383 
   328 !Filename class methodsFor:'queries'!
   384 !Filename class methodsFor:'queries'!
       
   385 
       
   386 components:aString
       
   387     "separate the pathName given by aString into 
       
   388      a collection containing the directory components and the files name as
       
   389      the final component.
       
   390      If the argument names an absolute path, the first component will be the
       
   391      name of the root directory (i.e. '/')."
       
   392 
       
   393     |components|
       
   394 
       
   395     components := aString asCollectionOfSubstringsSeparatedBy:(self separator).
       
   396     components first isEmpty ifTrue:[
       
   397         components at:1 put:(self separator asString)
       
   398     ].
       
   399     ^ components
       
   400 
       
   401 
       
   402     "
       
   403      Filename components:'/foo/bar/baz' 
       
   404      Filename components:'foo/bar/baz'  
       
   405     "
       
   406 
       
   407     "Modified: 29.2.1996 / 20:06:38 / cg"
       
   408 !
   329 
   409 
   330 defaultClass
   410 defaultClass
   331     "ST-80 compatibility:
   411     "ST-80 compatibility:
   332      in ST-80, different subclasses of Filename are used for different
   412      in ST-80, different subclasses of Filename are used for different
   333      OperatingSystems; defaultClass is supposed to return an appropriate class.
   413      OperatingSystems; defaultClass is supposed to return an appropriate class.
   441 	^ aString copyFrom:2
   521 	^ aString copyFrom:2
   442     ].
   522     ].
   443     ^ aString
   523     ^ aString
   444 
   524 
   445     "Modified: 7.9.1995 / 10:44:56 / claus"
   525     "Modified: 7.9.1995 / 10:44:56 / claus"
       
   526 !
       
   527 
       
   528 maxLength
       
   529     "return the maximum number of characters a filename may be in size.
       
   530      This depends on the OperatingSystem."
       
   531 
       
   532     ^ OperatingSystem maxFileNameLength
       
   533 
       
   534     "Created: 29.2.1996 / 20:57:11 / cg"
       
   535     "Modified: 29.2.1996 / 20:57:46 / cg"
   446 !
   536 !
   447 
   537 
   448 parentDirectoryName 
   538 parentDirectoryName 
   449     "return the name used for the parent directory.
   539     "return the name used for the parent directory.
   450      This is '..' for unix and dos-like systems. 
   540      This is '..' for unix and dos-like systems. 
  1044     "
  1134     "
  1045 ! !
  1135 ! !
  1046 
  1136 
  1047 !Filename methodsFor:'file utilities'!
  1137 !Filename methodsFor:'file utilities'!
  1048 
  1138 
       
  1139 edit
       
  1140     "start an editView on the file represented by the receiver"
       
  1141 
       
  1142     EditTextView openOn:self asString
       
  1143 
       
  1144     "
       
  1145      'smalltalk.rc' asFilename edit
       
  1146     "
       
  1147 !
       
  1148 
       
  1149 fileIn
       
  1150     "load smalltalk code from the file"
       
  1151 
       
  1152     ^ self readStream fileIn
       
  1153 ! !
       
  1154 
       
  1155 !Filename methodsFor:'instance creation'!
       
  1156 
       
  1157 construct:subname
       
  1158     "taking the receiver as a directory name, construct a new
       
  1159      filename for an entry within this directory 
       
  1160      (i.e. for a file or a subdirectory in that directory).
       
  1161      See also: #withSuffix: (which is different, but often needed)"
       
  1162 
       
  1163     ^ (self constructString:subname) asFilename
       
  1164 
       
  1165     "
       
  1166      '/tmp' asFilename construct:'foo'    
       
  1167      '/' asFilename construct:'foo'         
       
  1168      '/usr/tmp' asFilename construct:'foo'
       
  1169      '/foo/bar' asFilename construct:'baz' 
       
  1170     "
       
  1171 
       
  1172     "Modified: 29.2.1996 / 20:55:06 / cg"
       
  1173 !
       
  1174 
       
  1175 constructString:subname
       
  1176     "taking the receiver as a directory name, construct a new
       
  1177      filenames string for an entry within this directory 
       
  1178      (i.e. for a file or a subdirectory in that directory)."
       
  1179 
       
  1180     |sepString|
       
  1181 
       
  1182     sepString := self class separator asString.
       
  1183     nameString = sepString ifTrue:[
       
  1184         "I am the root"
       
  1185         ^ sepString  , subname
       
  1186     ].
       
  1187     ^ nameString , sepString , subname asString
       
  1188 
       
  1189     "
       
  1190      '/tmp' asFilename constructString:'foo'   
       
  1191      '/' asFilename constructString:'foo'         
       
  1192      '/usr/tmp' asFilename constructString:'foo'
       
  1193      '/foo/bar' asFilename constructString:'baz' 
       
  1194     "
       
  1195 
       
  1196     "Modified: 7.9.1995 / 10:15:22 / claus"
       
  1197     "Modified: 29.2.1996 / 20:55:18 / cg"
       
  1198 ! !
       
  1199 
       
  1200 !Filename methodsFor:'misc'!
       
  1201 
       
  1202 , aString
       
  1203     "this allows filenames to understand how names are concatenated.
       
  1204      Returns a string consisting of the receivers name, concatenated
       
  1205      by aString. Notice this is NOT the same as construct:, which inserts
       
  1206      a directory delimiter and returns a new fileName instance.
       
  1207      See also: #withSuffix: which is new and better."
       
  1208 
       
  1209     ^ (nameString , aString asString)
       
  1210 
       
  1211     "
       
  1212      'Makefile' asFilename , '.bak'        
       
  1213      ('Makefile' asFilename , '.bak') asFilename  
       
  1214      'Makefile' asFilename withSuffix:'bak' 
       
  1215      'Makefile' asFilename construct:'.bak'     
       
  1216     "
       
  1217 
       
  1218     "Modified: 29.2.1996 / 20:54:12 / cg"
       
  1219 ! !
       
  1220 
       
  1221 !Filename methodsFor:'printing & storing'!
       
  1222 
       
  1223 printOn:aStream
       
  1224     "append a printed representation of the receiver to aStream."
       
  1225 
       
  1226     aStream nextPutAll:'FileName('''.
       
  1227     nameString printOn:aStream.
       
  1228     aStream nextPutAll:''')'
       
  1229 !
       
  1230 
       
  1231 storeOn:aStream
       
  1232     "append a printed representation of the receiver to aStream,
       
  1233      which allows reconstructing it via readFrom:"
       
  1234 
       
  1235     aStream nextPut:$(.
       
  1236     nameString storeOn:aStream.
       
  1237     aStream nextPutAll:' asFilename)'
       
  1238 ! !
       
  1239 
       
  1240 !Filename methodsFor:'private accessing'!
       
  1241 
       
  1242 setName:aString
       
  1243     "set the filename"
       
  1244 
       
  1245     nameString := aString
       
  1246 ! !
       
  1247 
       
  1248 !Filename methodsFor:'queries'!
       
  1249 
       
  1250 canBeWritten
       
  1251     "same as isWritable - for ST-80 compatibility"
       
  1252 
       
  1253     ^ self isWritable
       
  1254 
       
  1255     "
       
  1256      '/foo/bar' asFilename canBeWritten 
       
  1257      '/tmp' asFilename canBeWritten   
       
  1258      'Makefile' asFilename canBeWritten   
       
  1259     "
       
  1260 !
       
  1261 
       
  1262 exists
       
  1263     "return true, if such a file exists."
       
  1264 
       
  1265     ^ OperatingSystem isValidPath:nameString
       
  1266 
       
  1267     "
       
  1268      '/foo/bar' asFilename exists 
       
  1269      '/tmp' asFilename exists  
       
  1270      'Makefile' asFilename exists   
       
  1271     "
       
  1272 !
       
  1273 
       
  1274 isAbsolute
       
  1275     "return true, if the receiver represents an absolute pathname
       
  1276      (in contrast to one relative to the current directory)."
       
  1277 
       
  1278     ^ (nameString startsWith:self class separator)
       
  1279 
       
  1280     "
       
  1281      '/foo/bar' asFilename isAbsolute   
       
  1282      '..' asFilename isAbsolute         
       
  1283      '..' asAbsoluteFilename isAbsolute         
       
  1284      'source/SBrowser.st' asFilename isAbsolute  
       
  1285     "
       
  1286 !
       
  1287 
       
  1288 isDirectory
       
  1289     "return true, if the receiver represents an existing,
       
  1290      readable directories pathname."
       
  1291 
       
  1292     ^ OperatingSystem isDirectory:nameString
       
  1293 
       
  1294     "
       
  1295      '/foo/bar' asFilename isDirectory
       
  1296      '/tmp' asFilename isDirectory
       
  1297      'Makefile' asFilename isDirectory   
       
  1298     "
       
  1299 !
       
  1300 
       
  1301 isExecutable
       
  1302     "return true, if such a file exists and is executable (by Unix's definition).
       
  1303      For directories, true is returned if the directory can be entered.
       
  1304      See isExecutableProgram for a related check."
       
  1305 
       
  1306     ^ OperatingSystem isExecutable:nameString
       
  1307 
       
  1308     "
       
  1309      '/foo/bar' asFilename isExecutable 
       
  1310      '/tmp' asFilename isExecutable   
       
  1311      'Makefile' asFilename isExecutable   
       
  1312      '/bin/ls' asFilename isExecutable   
       
  1313     "
       
  1314 !
       
  1315 
       
  1316 isExecutableProgram
       
  1317     "return true, if such a file exists and is an executable program.
       
  1318      (i.e. for directories, false is returned.)"
       
  1319 
       
  1320     ^ (OperatingSystem isExecutable:nameString)
       
  1321       and:[(OperatingSystem isDirectory:nameString) not]
       
  1322 
       
  1323     "
       
  1324      '/tmp' asFilename isExecutable         
       
  1325      '/bin/ls' asFilename isExecutable       
       
  1326      '/tmp' asFilename isExecutableProgram   
       
  1327      '/bin/ls' asFilename isExecutableProgram    
       
  1328     "
       
  1329 !
       
  1330 
       
  1331 isReadable
       
  1332     "return true, if such a file exists and is readable."
       
  1333 
       
  1334     ^ OperatingSystem isReadable:nameString
       
  1335 
       
  1336     "
       
  1337      '/foo/bar' asFilename isReadable   
       
  1338      '/tmp' asFilename isReadable      
       
  1339      'Makefile' asFilename isReadable   
       
  1340     "
       
  1341 !
       
  1342 
       
  1343 isRelative
       
  1344     "return true, if this name is interpreted relative to some
       
  1345      directory (opposite of absolute)"
       
  1346 
       
  1347     ^ self isAbsolute not
       
  1348 !
       
  1349 
       
  1350 isSymbolicLink
       
  1351     "return true, if the file represented by the receiver is a symbolic
       
  1352      link. Notice that not all OS's support symbolic links; those that do
       
  1353      not will always return false."
       
  1354 
       
  1355     ^ OperatingSystem isSymbolicLink:nameString
       
  1356 
       
  1357     "
       
  1358      'Make.proto' asFilename isSymbolicLink  
       
  1359      'Makefile' asFilename isSymbolicLink   
       
  1360     "
       
  1361 !
       
  1362 
       
  1363 isWritable
       
  1364     "return true, if such a file exists and is writable."
       
  1365 
       
  1366     ^ OperatingSystem isWritable:nameString
       
  1367 
       
  1368     "
       
  1369      '/foo/bar' asFilename isWritable 
       
  1370      '/tmp' asFilename isWritable   
       
  1371      'Makefile' asFilename isWritable   
       
  1372     "
       
  1373 !
       
  1374 
       
  1375 separator
       
  1376     "return the directory-separator character"
       
  1377 
       
  1378     ^ self class separator
       
  1379 
       
  1380     "Modified: 29.2.1996 / 20:52:01 / cg"
       
  1381 ! !
       
  1382 
       
  1383 !Filename methodsFor:'queries-contents'!
       
  1384 
  1049 contentsOfEntireFile
  1385 contentsOfEntireFile
  1050     "return the contents of the file as a string"
  1386     "return the contents of the file as a string"
  1051 
  1387 
  1052     |s contents|
  1388     |s contents|
  1053 
  1389 
  1056 	contents := s contents
  1392 	contents := s contents
  1057     ] valueNowOrOnUnwindDo:[s close].
  1393     ] valueNowOrOnUnwindDo:[s close].
  1058     ^ contents
  1394     ^ contents
  1059 !
  1395 !
  1060 
  1396 
  1061 edit
  1397 directoryContents
  1062     "start an editView on the file represented by the receiver"
  1398     "return the contents of the directory as a collection of strings"
  1063 
  1399 
  1064     EditTextView openOn:self asString
  1400     ^ (FileDirectory directoryNamed:self asString) contents
  1065 
  1401 
  1066     "
  1402     "
  1067      'smalltalk.rc' asFilename edit
  1403      '.' asFilename directoryContents
  1068     "
  1404     "
  1069 !
  1405 !
  1070 
  1406 
  1071 fileIn
  1407 filesMatching:aPattern
  1072     "load smalltalk code from the file"
  1408     "given the receiver, representing a directory;
  1073 
  1409      return a collection of files matching a pattern."
  1074     ^ self readStream fileIn
  1410 
       
  1411     ^ self directoryContents select:[:name | aPattern match:name]
       
  1412 
       
  1413     "
       
  1414      Filename currentDirectory filesMatching:'M*' 
       
  1415      '/etc' asFilename filesMatching:'[a-z]*' 
       
  1416      '../../libbasic' asFilename filesMatching:'[A-D]*.st'  
       
  1417     "
       
  1418 
       
  1419     "Modified: 29.2.1996 / 20:30:31 / cg"
  1075 ! !
  1420 ! !
  1076 
  1421 
  1077 !Filename methodsFor:'instance creation'!
  1422 !Filename methodsFor:'queries-path & name'!
  1078 
       
  1079 construct:subname
       
  1080     "taking the receiver as a directory name, construct a new
       
  1081      filename for an entry within this directory (i.e. for a file
       
  1082      or a subdirectory in that directory)."
       
  1083 
       
  1084     ^ (self constructString:subname) asFilename
       
  1085 
       
  1086     "
       
  1087      '/tmp' asFilename construct:'foo'    
       
  1088      '/' asFilename construct:'foo'         
       
  1089      '/usr/tmp' asFilename construct:'foo'
       
  1090      '/foo/bar' asFilename construct:'baz' 
       
  1091     "
       
  1092 !
       
  1093 
       
  1094 constructString:subname
       
  1095     "taking the receiver as a directory name, construct a new
       
  1096      filenames string for an entry within this directory (i.e. for a file
       
  1097      or a subdirectory in that directory)."
       
  1098 
       
  1099     |sepString|
       
  1100 
       
  1101     sepString := self class separator asString.
       
  1102     nameString = sepString ifTrue:[
       
  1103 	"I am the root"
       
  1104 	^ sepString  , subname
       
  1105     ].
       
  1106     ^ nameString , sepString , subname asString
       
  1107 
       
  1108     "
       
  1109      '/tmp' asFilename constructString:'foo'   
       
  1110      '/' asFilename constructString:'foo'         
       
  1111      '/usr/tmp' asFilename constructString:'foo'
       
  1112      '/foo/bar' asFilename constructString:'baz' 
       
  1113     "
       
  1114 
       
  1115     "Modified: 7.9.1995 / 10:15:22 / claus"
       
  1116 ! !
       
  1117 
       
  1118 !Filename methodsFor:'misc'!
       
  1119 
       
  1120 , aString
       
  1121     "this allows filenames to understand how names are concatenated.
       
  1122      Returns a string consisting of the receivers name, concatenated
       
  1123      by aString. Notice this is NOT the same as construct:, which inserts
       
  1124      a directory delimiter and returns a new fileName instance."
       
  1125 
       
  1126     ^ (nameString , aString asString)
       
  1127 
       
  1128     "
       
  1129      'Makefile' asFilename , '.bak' 
       
  1130      'Makefile' asFilename construct:'.bak' 
       
  1131     "
       
  1132 ! !
       
  1133 
       
  1134 !Filename methodsFor:'printing & storing'!
       
  1135 
       
  1136 printOn:aStream
       
  1137     "append a printed representation of the receiver to aStream."
       
  1138 
       
  1139     aStream nextPutAll:'FileName('''.
       
  1140     nameString printOn:aStream.
       
  1141     aStream nextPutAll:''')'
       
  1142 !
       
  1143 
       
  1144 storeOn:aStream
       
  1145     "append a printed representation of the receiver to aStream,
       
  1146      which allows reconstructing it via readFrom:"
       
  1147 
       
  1148     aStream nextPut:$(.
       
  1149     nameString storeOn:aStream.
       
  1150     aStream nextPutAll:' asFilename)'
       
  1151 ! !
       
  1152 
       
  1153 !Filename methodsFor:'private accessing'!
       
  1154 
       
  1155 setName:aString
       
  1156     "set the filename"
       
  1157 
       
  1158     nameString := aString
       
  1159 ! !
       
  1160 
       
  1161 !Filename methodsFor:'queries'!
       
  1162 
  1423 
  1163 baseName
  1424 baseName
  1164     "return my baseName as a string.
  1425     "return my baseName as a string.
  1165      - thats the file/directory name without leading parent-dirs."
  1426      - thats the file/directory name without leading parent-dirs."
  1166 
  1427 
  1175      '../../libpr' asFilename baseName        
  1436      '../../libpr' asFilename baseName        
  1176      '../../libbasic/Object.st' asFilename baseName        
  1437      '../../libbasic/Object.st' asFilename baseName        
  1177     "
  1438     "
  1178 !
  1439 !
  1179 
  1440 
  1180 canBeWritten
       
  1181     "same as isWritable - for ST-80 compatibility"
       
  1182 
       
  1183     ^ self isWritable
       
  1184 
       
  1185     "
       
  1186      '/foo/bar' asFilename canBeWritten 
       
  1187      '/tmp' asFilename canBeWritten   
       
  1188      'Makefile' asFilename canBeWritten   
       
  1189     "
       
  1190 !
       
  1191 
       
  1192 directory
  1441 directory
  1193     "return the directory name part of the file/directory as a filename.
  1442     "return the directory name part of the file/directory as a new filename.
  1194      - thats a filename for the directory where the file/dir represented by
  1443      - thats a filename for the directory where the file/dir represented by
  1195        the receiver is contained in."
  1444        the receiver is contained in.
       
  1445      (this is almost equivalent to #directoryName or #head, but returns
       
  1446       a Filename instance instead of a string )."
  1196 
  1447 
  1197     ^ self directoryName asFilename
  1448     ^ self directoryName asFilename
  1198 
  1449 
  1199     "
  1450     "
  1200      '/foo/bar' asFilename directory
  1451      '/foo/bar' asFilename directory      
       
  1452      '/foo/bar' asFilename directoryName  
       
  1453      '/foo/bar' asFilename head  
       
  1454 
  1201      '.' asFilename directory        
  1455      '.' asFilename directory        
  1202      '..' asFilename directory       
  1456      '..' asFilename directory       
  1203      '../..' asFilename directory     
  1457      '../..' asFilename directory     
  1204     "
  1458     "
  1205 !
  1459 
  1206 
  1460     "Modified: 29.2.1996 / 20:50:14 / cg"
  1207 directoryContents
       
  1208     "return the contents of the directory as a collection of strings"
       
  1209 
       
  1210     ^ (FileDirectory directoryNamed:self asString) contents
       
  1211 
       
  1212     "
       
  1213      '.' asFilename directoryContents
       
  1214     "
       
  1215 !
  1461 !
  1216 
  1462 
  1217 directoryName
  1463 directoryName
  1218     "return the directory name part of the file/directory as a string.
  1464     "return the directory name part of the file/directory as a string.
  1219      - thats the name of the directory where the file/dir represented by
  1465      - thats the name of the directory where the file/dir represented by
  1220        the receiver is contained in.
  1466        the receiver is contained in.
  1221      See also: #directoryPathName"
  1467      (this is almost equivalent to #directory, but returns
       
  1468       a string instead of a Filename instance).
       
  1469      See also: #directoryPathName.
       
  1470      Compatibility note: use #head for ST-80 compatibility."
  1222 
  1471 
  1223     ^ OperatingSystem directoryNameOf:nameString "/ (self pathName)
  1472     ^ OperatingSystem directoryNameOf:nameString "/ (self pathName)
  1224 
  1473 
  1225     "
  1474     "
  1226      '/foo/bar/' asFilename directoryName    
  1475      '/foo/bar/' asFilename directoryName    
       
  1476      '/foo/bar/' asFilename directory     
       
  1477 
  1227      '/foo/bar' asFilename directoryName    
  1478      '/foo/bar' asFilename directoryName    
  1228      'bitmaps' asFilename directoryName        
  1479      'bitmaps' asFilename directoryName        
  1229      'bitmaps' asFilename directoryPathName        
  1480      'bitmaps' asFilename directoryPathName        
  1230      '.' asFilename directoryName        
  1481      '.' asFilename directoryName        
  1231      '..' asFilename directoryName       
  1482      '..' asFilename directoryName       
  1232      '../..' asFilename directoryName     
  1483      '../..' asFilename directoryName     
  1233      '../..' asFilename directoryPathName     
  1484      '../..' asFilename directoryPathName     
  1234     "
  1485     "
  1235 
  1486 
  1236     "Modified: 7.9.1995 / 10:42:03 / claus"
  1487     "Modified: 7.9.1995 / 10:42:03 / claus"
       
  1488     "Modified: 29.2.1996 / 20:23:49 / cg"
  1237 !
  1489 !
  1238 
  1490 
  1239 directoryPathName
  1491 directoryPathName
  1240     "return the full directory pathname part of the file/directory as a string.
  1492     "return the full directory pathname part of the file/directory as a string.
  1241      - thats the full pathname of the directory where the file/dir represented by
  1493      - thats the full pathname of the directory where the file/dir represented by
  1242        the receiver is contained in.
  1494        the receiver is contained in.
  1243      See also: directoryName"
  1495      See also: #directoryName, #directory"
  1244 
  1496 
  1245     ^ OperatingSystem directoryNameOf:(self pathName)
  1497     ^ OperatingSystem directoryNameOf:(self pathName)
  1246 
  1498 
  1247     "
  1499     "
  1248      '/foo/bar/' asFilename directoryPathName    
  1500      '/foo/bar/' asFilename directoryPathName    
  1249      '/foo/bar' asFilename directoryPathName    
  1501      '/foo/bar' asFilename directoryPathName    
  1250      '.' asFilename directoryPathName        
  1502 
       
  1503      '.' asFilename directoryPathName      
       
  1504      '.' asFilename directoryName     
       
  1505      '.' asFilename directory          
       
  1506 
  1251      '..' asFilename directoryPathName       
  1507      '..' asFilename directoryPathName       
       
  1508      '..' asFilename directoryName       
       
  1509      '..' asFilename directory
       
  1510 
  1252      '../..' asFilename directoryPathName     
  1511      '../..' asFilename directoryPathName     
  1253     "
  1512     "
  1254 
  1513 
  1255     "Modified: 7.9.1995 / 10:42:13 / claus"
  1514     "Modified: 7.9.1995 / 10:42:13 / claus"
  1256 !
  1515     "Modified: 29.2.1996 / 20:24:44 / cg"
  1257 
       
  1258 exists
       
  1259     "return true, if such a file exists."
       
  1260 
       
  1261     ^ OperatingSystem isValidPath:nameString
       
  1262 
       
  1263     "
       
  1264      '/foo/bar' asFilename exists 
       
  1265      '/tmp' asFilename exists  
       
  1266      'Makefile' asFilename exists   
       
  1267     "
       
  1268 !
  1516 !
  1269 
  1517 
  1270 filenameCompletion
  1518 filenameCompletion
  1271     "try to complete the recevier filename.
  1519     "try to complete the receiver filename.
  1272      This method has both a return value and a side effect on the receiver:
  1520      This method has both a return value and a side effect on the receiver:
  1273        it returns a collection of matching filename objects,
  1521        it returns a collection of matching filename objects,
  1274        and leaves changes the receivers filename-string to the longest common
  1522        and leaves changes the receivers filename-string to the longest common
  1275        match.
  1523        match.
  1276      If none matches, the returned collection is empty and the recevier is unchanged.
  1524      If none matches, the returned collection is empty and the recevier is unchanged.
  1284      'Make' asFilename filenameCompletion 
  1532      'Make' asFilename filenameCompletion 
  1285      'Makef' asFilename filenameCompletion;yourself  
  1533      'Makef' asFilename filenameCompletion;yourself  
  1286      '/u' asFilename filenameCompletion             
  1534      '/u' asFilename filenameCompletion             
  1287      '../../libpr' asFilename inspect filenameCompletion    
  1535      '../../libpr' asFilename inspect filenameCompletion    
  1288     "
  1536     "
       
  1537 
       
  1538     "Modified: 29.2.1996 / 20:28:36 / cg"
  1289 !
  1539 !
  1290 
  1540 
  1291 filenameCompletionIn:aDirectory
  1541 filenameCompletionIn:aDirectory
  1292     "try to complete the recevier filename.
  1542     "try to complete the receiver filename.
  1293      This method has both a return value and a side effect on the receiver:
  1543      This method has both a return value and a side effect on the receiver:
  1294        it returns a collection of matching filename objects,
  1544        it returns a collection of matching filename objects,
  1295        and leaves changes the receivers filename-string to the longest common
  1545        and leaves changes the receivers filename-string to the longest common
  1296        match.
  1546        match.
  1297      If none matches, the returned collection is empty and the recevier is unchanged.
  1547      If none matches, the returned collection is empty and the recevier is unchanged.
  1301     |dir baseName matching matchLen try allMatching 
  1551     |dir baseName matching matchLen try allMatching 
  1302      sepString parentString prefix nMatch|
  1552      sepString parentString prefix nMatch|
  1303 
  1553 
  1304     sepString := self class separator asString.
  1554     sepString := self class separator asString.
  1305     (nameString endsWith:sepString) ifTrue:[
  1555     (nameString endsWith:sepString) ifTrue:[
  1306 	^ #()
  1556         ^ #()
  1307     ].
  1557     ].
  1308 
  1558 
  1309     parentString := self class parentDirectoryName.
  1559     parentString := self class parentDirectoryName.
  1310     baseName := self baseName.
  1560     baseName := self baseName.
  1311     baseName ~= nameString ifTrue:[
  1561     baseName ~= nameString ifTrue:[
  1312 	prefix := self directoryName.
  1562         prefix := self directoryName.
  1313     ].
  1563     ].
  1314 
  1564 
  1315     self isAbsolute ifTrue:[
  1565     self isAbsolute ifTrue:[
  1316 	dir := self directory
  1566         dir := self directory
  1317     ] ifFalse:[
  1567     ] ifFalse:[
  1318 	aDirectory isNil ifTrue:[
  1568         aDirectory isNil ifTrue:[
  1319 	    dir := self directory
  1569             dir := self directory
  1320 	] ifFalse:[
  1570         ] ifFalse:[
  1321 	    dir := (aDirectory construct:nameString) directory
  1571             dir := (aDirectory construct:nameString) directory
  1322 	]
  1572         ]
  1323     ].
  1573     ].
  1324 
  1574 
  1325     matching := OrderedCollection new.
  1575     matching := OrderedCollection new.
  1326     dir directoryContents do:[:fileName |
  1576     dir directoryContents do:[:fileName |
  1327 	((fileName ~= '.') and:[fileName ~= parentString]) ifTrue:[
  1577         ((fileName ~= '.') and:[fileName ~= parentString]) ifTrue:[
  1328 	    (fileName startsWith:baseName) ifTrue:[
  1578             (fileName startsWith:baseName) ifTrue:[
  1329 		matching add:fileName
  1579                 matching add:fileName
  1330 	    ]
  1580             ]
  1331 	]
  1581         ]
  1332     ].
  1582     ].
  1333     (nMatch := matching size) > 1 ifTrue:[
  1583     (nMatch := matching size) > 1 ifTrue:[
  1334 	"
  1584         "
  1335 	 find the longest common prefix
  1585          find the longest common prefix
  1336 	"
  1586         "
  1337 	matchLen := baseName size.
  1587         matchLen := baseName size.
  1338 	matchLen > matching first size ifTrue:[
  1588         matchLen > matching first size ifTrue:[
  1339 	    try := baseName.
  1589             try := baseName.
  1340 	    allMatching := false
  1590             allMatching := false
  1341 	] ifFalse:[
  1591         ] ifFalse:[
  1342 	    try := matching first copyTo:matchLen.
  1592             try := matching first copyTo:matchLen.
  1343 	    allMatching := true.
  1593             allMatching := true.
  1344 	].
  1594         ].
  1345 
  1595 
  1346 	[allMatching] whileTrue:[
  1596         [allMatching] whileTrue:[
  1347 	    matching do:[:aName |
  1597             matching do:[:aName |
  1348 		(aName startsWith:try) ifFalse:[
  1598                 (aName startsWith:try) ifFalse:[
  1349 		    allMatching := false
  1599                     allMatching := false
  1350 		]
  1600                 ]
  1351 	    ].
  1601             ].
  1352 	    allMatching ifTrue:[
  1602             allMatching ifTrue:[
  1353 		matchLen <  matching first size ifTrue:[
  1603                 matchLen <  matching first size ifTrue:[
  1354 		    matchLen := matchLen + 1.
  1604                     matchLen := matchLen + 1.
  1355 		    try := matching first copyTo:matchLen.
  1605                     try := matching first copyTo:matchLen.
  1356 		] ifFalse:[
  1606                 ] ifFalse:[
  1357 		    allMatching := false
  1607                     allMatching := false
  1358 		]
  1608                 ]
  1359 	    ] ifFalse:[
  1609             ] ifFalse:[
  1360 		try := matching first copyTo:matchLen - 1.
  1610                 try := matching first copyTo:matchLen - 1.
  1361 	    ]
  1611             ]
  1362 	].
  1612         ].
  1363 	"
  1613         "
  1364 	 and set my name to the last full match
  1614          and set my name to the last full match
  1365 	"
  1615         "
  1366 	nameString := try
  1616         nameString := try
  1367     ].
  1617     ].
  1368 
  1618 
  1369     "
  1619     "
  1370      if I had a directory-prefix, change names in collection ...
  1620      if I had a directory-prefix, change names in collection ...
  1371     "
  1621     "
  1372     prefix notNil ifTrue:[
  1622     prefix notNil ifTrue:[
  1373 	prefix = '/' ifTrue:[
  1623         prefix = '/' ifTrue:[
  1374 	    "/ avoid introducing double slashes
  1624             "/ avoid introducing double slashes
  1375 	    prefix := ''
  1625             prefix := ''
  1376 	].
  1626         ].
  1377 	matching := matching collect:[:n | prefix , '/' , n].
  1627         matching := matching collect:[:n | prefix , '/' , n].
  1378 	nMatch == 1 ifTrue:[
  1628         nMatch == 1 ifTrue:[
  1379 	    nameString := matching first
  1629             nameString := matching first
  1380 	] ifFalse:[
  1630         ] ifFalse:[
  1381 	    nMatch > 1 ifTrue:[
  1631             nMatch > 1 ifTrue:[
  1382 		nameString := prefix , '/' , nameString
  1632                 nameString := prefix , '/' , nameString
  1383 	    ]
  1633             ]
  1384 	]
  1634         ]
  1385     ] ifFalse:[
  1635     ] ifFalse:[
  1386 	nMatch == 1 ifTrue:[
  1636         nMatch == 1 ifTrue:[
  1387 	    nameString := matching first
  1637             nameString := matching first
  1388 	]
  1638         ]
  1389     ].
  1639     ].
  1390 
  1640 
  1391     "
  1641     "
  1392      return the match-set, so caller can decide what to do
  1642      return the match-set, so caller can decide what to do
  1393      (i.e. show the matches, output a warning etc ...)
  1643      (i.e. show the matches, output a warning etc ...)
  1404      'Make' asFilename filenameCompletion    
  1654      'Make' asFilename filenameCompletion    
  1405      'Makef' asFilename filenameCompletion
  1655      'Makef' asFilename filenameCompletion
  1406      '/u' asFilename filenameCompletion             
  1656      '/u' asFilename filenameCompletion             
  1407      '../../libpr' asFilename filenameCompletion    
  1657      '../../libpr' asFilename filenameCompletion    
  1408     "
  1658     "
  1409 !
  1659 
  1410 
  1660     "Modified: 29.2.1996 / 20:28:45 / cg"
  1411 filesMatching:aPattern
  1661 !
  1412     ^ self directoryContents select:[:name | aPattern match:name]
  1662 
  1413 
  1663 head 
  1414     "
  1664     "return the directory name as a string. 
  1415      Filename currentDirectory filesMatching:'M*' 
  1665      An alias for directoryName, for ST-80 compatiblity.
  1416     "
  1666      (this is almost equivalent to #directory, but returns
  1417 !
  1667       a string instead of a Filename instance)"
  1418 
  1668 
  1419 isAbsolute
  1669     ^ self directoryName
  1420     "return true, if the receiver represents an absolute pathname
  1670 
  1421      (in contrast to one relative to the current directory)."
  1671     "
  1422 
  1672      Filename currentDirectory head  
  1423     ^ (nameString startsWith:self class separator)
  1673      'Makefile' asFilename head    
  1424 
  1674      '/foo/bar/baz.st' asFilename head  
  1425     "
  1675     "
  1426      '/foo/bar' asFilename isAbsolute   
  1676 
  1427      '..' asFilename isAbsolute         
  1677     "Modified: 29.2.1996 / 20:21:25 / cg"
  1428      '..' asAbsoluteFilename isAbsolute         
       
  1429      'source/SBrowser.st' asFilename isAbsolute  
       
  1430     "
       
  1431 !
       
  1432 
       
  1433 isDirectory
       
  1434     "return true, if the receiver represents an existing,
       
  1435      readable directories pathname."
       
  1436 
       
  1437     ^ OperatingSystem isDirectory:nameString
       
  1438 
       
  1439     "
       
  1440      '/foo/bar' asFilename isDirectory
       
  1441      '/tmp' asFilename isDirectory
       
  1442      'Makefile' asFilename isDirectory   
       
  1443     "
       
  1444 !
       
  1445 
       
  1446 isExecutable
       
  1447     "return true, if such a file exists and is executable (by Unix's definition).
       
  1448      For directories, true is returned if the directory can be entered.
       
  1449      See isExecutableProgram for a related check."
       
  1450 
       
  1451     ^ OperatingSystem isExecutable:nameString
       
  1452 
       
  1453     "
       
  1454      '/foo/bar' asFilename isExecutable 
       
  1455      '/tmp' asFilename isExecutable   
       
  1456      'Makefile' asFilename isExecutable   
       
  1457      '/bin/ls' asFilename isExecutable   
       
  1458     "
       
  1459 !
       
  1460 
       
  1461 isExecutableProgram
       
  1462     "return true, if such a file exists and is an executable program.
       
  1463      (i.e. for directories, false is returned.)"
       
  1464 
       
  1465     ^ (OperatingSystem isExecutable:nameString)
       
  1466       and:[(OperatingSystem isDirectory:nameString) not]
       
  1467 
       
  1468     "
       
  1469      '/tmp' asFilename isExecutable         
       
  1470      '/bin/ls' asFilename isExecutable       
       
  1471      '/tmp' asFilename isExecutableProgram   
       
  1472      '/bin/ls' asFilename isExecutableProgram    
       
  1473     "
       
  1474 !
       
  1475 
       
  1476 isReadable
       
  1477     "return true, if such a file exists and is readable."
       
  1478 
       
  1479     ^ OperatingSystem isReadable:nameString
       
  1480 
       
  1481     "
       
  1482      '/foo/bar' asFilename isReadable   
       
  1483      '/tmp' asFilename isReadable      
       
  1484      'Makefile' asFilename isReadable   
       
  1485     "
       
  1486 !
       
  1487 
       
  1488 isRelative
       
  1489     "return true, if this name is interpreted relative to some
       
  1490      directory (opposite of absolute)"
       
  1491 
       
  1492     ^ self isAbsolute not
       
  1493 !
       
  1494 
       
  1495 isSymbolicLink
       
  1496     "return true, if the file represented by the receiver is a symbolic
       
  1497      link. Notice that not all OS's support symbolic links; those that do
       
  1498      not will always return false."
       
  1499 
       
  1500     ^ OperatingSystem isSymbolicLink:nameString
       
  1501 
       
  1502     "
       
  1503      'Make.proto' asFilename isSymbolicLink  
       
  1504      'Makefile' asFilename isSymbolicLink   
       
  1505     "
       
  1506 !
       
  1507 
       
  1508 isWritable
       
  1509     "return true, if such a file exists and is writable."
       
  1510 
       
  1511     ^ OperatingSystem isWritable:nameString
       
  1512 
       
  1513     "
       
  1514      '/foo/bar' asFilename isWritable 
       
  1515      '/tmp' asFilename isWritable   
       
  1516      'Makefile' asFilename isWritable   
       
  1517     "
       
  1518 !
  1678 !
  1519 
  1679 
  1520 name
  1680 name
  1521     "return the name of the file represented by the receiver as a string.
  1681     "return the name of the file represented by the receiver as a string.
  1522      This may or may not be a relative name (i.e. include ..'s).
  1682      This may or may not be a relative name (i.e. include ..'s).
  1569     "
  1729     "
  1570 
  1730 
  1571     "Modified: 18.1.1996 / 21:36:46 / cg"
  1731     "Modified: 18.1.1996 / 21:36:46 / cg"
  1572 !
  1732 !
  1573 
  1733 
       
  1734 suffix
       
  1735     "return my suffix.
       
  1736      The suffix is the namepart after the final period character,
       
  1737      or the empty string, if the name does not contain a period."
       
  1738 
       
  1739     ^ self prefixAndSuffix at:2
       
  1740 
       
  1741     "
       
  1742      'abc.st' asFilename suffix   
       
  1743      'abc' asFilename suffix      
       
  1744      'a.b.c' asFilename suffix    
       
  1745     "
       
  1746 
       
  1747     "Modified: 7.9.1995 / 11:09:03 / claus"
       
  1748 !
       
  1749 
       
  1750 tail
       
  1751     "the files name without directory prefix as a string. 
       
  1752      An alias for baseName, for ST-80 compatiblity."
       
  1753 
       
  1754     ^ self baseName
       
  1755 
       
  1756     "
       
  1757      Filename currentDirectory tail 
       
  1758      'Makefile' asFilename tail    
       
  1759      '/foo/bar/baz.st' asFilename tail  
       
  1760     "
       
  1761 
       
  1762     "Modified: 29.2.1996 / 20:19:50 / cg"
       
  1763 ! !
       
  1764 
       
  1765 !Filename methodsFor:'suffixes'!
       
  1766 
  1574 prefixAndSuffix
  1767 prefixAndSuffix
  1575     "return an array consisting of my prefix and suffix.
  1768     "return an array consisting of my prefix and suffix.
  1576      The suffix is the namepart after the final period character,
  1769      The suffix is the namepart after the final period character,
  1577      the prefix everything before, except for the period.
  1770      the prefix everything before, except for the period.
  1578      (on some systems, the suffix-character may be different from a period).
  1771      (on some systems, the suffix-character may be different from a period).
  1584     |nm idx|
  1777     |nm idx|
  1585 
  1778 
  1586     nm := self baseName.
  1779     nm := self baseName.
  1587     idx := nm lastIndexOf:(self class suffixSeparator).
  1780     idx := nm lastIndexOf:(self class suffixSeparator).
  1588     idx == 0 ifTrue:[
  1781     idx == 0 ifTrue:[
  1589 	^ Array with:nm with:''
  1782         ^ Array with:nm with:''
  1590     ].
  1783     ].
  1591     ^ Array 
  1784     ^ Array 
  1592 	with:(nm copyTo:idx-1)
  1785         with:(nm copyTo:idx-1)
  1593 	with:(nm copyFrom:idx+1)
  1786         with:(nm copyFrom:idx+1)
  1594 
  1787 
  1595     "
  1788     "
  1596      'abc.st' asFilename prefixAndSuffix  
  1789      'abc.st' asFilename prefixAndSuffix  
  1597      'abc' asFilename prefixAndSuffix  
  1790      'abc' asFilename prefixAndSuffix  
  1598      'a.b.c' asFilename prefixAndSuffix 
  1791      'a.b.c' asFilename prefixAndSuffix 
  1603     "
  1796     "
  1604 
  1797 
  1605     "Modified: 7.9.1995 / 11:15:42 / claus"
  1798     "Modified: 7.9.1995 / 11:15:42 / claus"
  1606 !
  1799 !
  1607 
  1800 
  1608 separator
  1801 withSuffix:aSuffix
  1609     "return the directory-separator character (or string)"
  1802     "return a new filename for the receivers name with a suffix.
  1610 
  1803      If the name already has a sufix, the new suffix replacaes it;
  1611     ^ self class separator
  1804      otherwise, the new suffix simply appended to the name."
  1612 !
  1805 
  1613 
  1806     ^ self class named:
  1614 suffix
  1807         (self withoutSuffix name 
  1615     "return my suffix.
  1808          , self class suffixSeparator asString 
  1616      The suffix is the namepart after the final period character,
  1809          , aSuffix asString)
  1617      or the empty string, if the name does not contain a period."
  1810 
  1618 
  1811     "
  1619     ^ self prefixAndSuffix at:2
  1812      'abc.st' asFilename withSuffix:'o'         
  1620 
  1813      'abc' asFilename withSuffix:'o'             
  1621     "
  1814      'a.b.c' asFilename withSuffix:'o'            
  1622      'abc.st' asFilename suffix   
  1815      '/foo/bar/baz.st' asFilename withSuffix:'c'   
  1623      'abc' asFilename suffix      
  1816      '/foo/bar/baz.c' asFilename withSuffix:'st'   
  1624      'a.b.c' asFilename suffix    
  1817     "
  1625     "
  1818 
  1626 
  1819     "Modified: 7.9.1995 / 11:15:42 / claus"
  1627     "Modified: 7.9.1995 / 11:09:03 / claus"
  1820     "Modified: 29.2.1996 / 20:50:03 / cg"
  1628 !
  1821 !
  1629 
  1822 
  1630 tail
  1823 withoutSuffix
  1631     "the files name without directory prefix as a string. 
  1824     "return a new filename for the receivers name without the suffix.
  1632      An alias for baseName, for ST-80 compatiblity."
  1825      If the name has no suffix, the receiver is returned."
  1633 
  1826 
  1634     ^ self baseName
  1827     |idx|
       
  1828 
       
  1829     idx := nameString lastIndexOf:(self class suffixSeparator).
       
  1830     idx == 0 ifTrue:[^ self].
       
  1831 
       
  1832     ^ self class named:(nameString copyTo:(idx - 1))
       
  1833 
       
  1834     "
       
  1835      'abc.st' asFilename withoutSuffix         
       
  1836      'abc' asFilename withoutSuffix            
       
  1837      'a.b.c' asFilename withoutSuffix           
       
  1838      '/foo/bar/baz.c' asFilename withoutSuffix   
       
  1839     "
       
  1840 
       
  1841     "Modified: 7.9.1995 / 11:15:42 / claus"
       
  1842     "Modified: 29.2.1996 / 20:49:59 / cg"
  1635 ! !
  1843 ! !
  1636 
  1844 
  1637 !Filename class methodsFor:'documentation'!
  1845 !Filename class methodsFor:'documentation'!
  1638 
  1846 
  1639 version
  1847 version
  1640     ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.50 1996-02-04 19:06:34 cg Exp $'
  1848     ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.51 1996-02-29 20:01:28 cg Exp $'
  1641 ! !
  1849 ! !