filesystem.bmx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. SuperStrict
  2. Rem
  3. bbdoc: System/File system
  4. End Rem
  5. Module BRL.FileSystem
  6. ModuleInfo "Version: 1.13"
  7. ModuleInfo "Author: Mark Sibly"
  8. ModuleInfo "License: zlib/libpng"
  9. ModuleInfo "Copyright: Blitz Research Ltd"
  10. ModuleInfo "Modserver: BRL"
  11. ModuleInfo "History: 1.13"
  12. ModuleInfo "History: Added SetFileTime"
  13. ModuleInfo "History: Added MaxIO file tree walking"
  14. ModuleInfo "History: 1.12"
  15. ModuleInfo "History: Added file tree walker"
  16. ModuleInfo "History: 1.11"
  17. ModuleInfo "History: Added optional parameter timetype to FileTime"
  18. ModuleInfo "History: 1.10"
  19. ModuleInfo "History: Module is now SuperStrict"
  20. ModuleInfo "History: 1.09 Release"
  21. ModuleInfo "History: Fixed RealPath breaking win32 //server paths"
  22. ModuleInfo "History: 1.08 Release"
  23. ModuleInfo "History: Rebuild for StdC chmod_ linkage"
  24. ModuleInfo "History: 1.07 Release"
  25. ModuleInfo "History: Fixed RealPath failing with 'hidden' dirs"
  26. ModuleInfo "History: 1.06 Release"
  27. ModuleInfo "History: 1.05 Release"
  28. ModuleInfo "History: Fixed Win32 CreateDir"
  29. ModuleInfo "History: 1.04 Release"
  30. ModuleInfo "History: Cleaned up FixPath and RealPath"
  31. ModuleInfo "History: Added optional resurse parameter to CreateDir"
  32. Import Pub.StdC
  33. Import BRL.BankStream
  34. Import "glue.c"
  35. Const FILETYPE_NONE:Int=0,FILETYPE_FILE:Int=1,FILETYPE_DIR:Int=2,FILETYPE_SYM:Int=3
  36. Const FILETIME_MODIFIED:Int=0,FILETIME_CREATED:Int=1,FILETIME_ACCESSED:Int=2
  37. Private
  38. Function _RootPath:String( path:String )
  39. If MaxIO.ioInitialized Then
  40. Return "/"
  41. End If
  42. ?Win32
  43. If path.StartsWith( "//" )
  44. Return path[ ..path.Find( "/",2 )+1 ]
  45. EndIf
  46. Local i:Int=path.Find( ":" )
  47. If i<>-1 And path.Find( "/" )=i+1 Return path[..i+2]
  48. ?
  49. If path.StartsWith( "/" ) Return "/"
  50. End Function
  51. Function _IsRootPath:Int( path:String )
  52. Return path And _RootPath( path )=path
  53. End Function
  54. Function _IsRealPath:Int( path:String )
  55. Return _RootPath( path )<>""
  56. End Function
  57. ?Win32
  58. Function _CurrentDrive:String()
  59. Local cd:String=getcwd_()
  60. Local i:Int=cd.Find( ":" )
  61. If i<>-1 Return cd[..i]
  62. End Function
  63. ?
  64. Public
  65. Function FixPath( path:String Var,dirPath:Int=False )
  66. path=path.Replace("\","/")
  67. If Not MaxIO.ioInitialized Then
  68. ?Win32
  69. If path.StartsWith( "//" )
  70. If path.Find( "/",2 )=-1 path:+"/"
  71. Else
  72. Local i:Int=path.Find( ":" )
  73. If i<>-1 And ( i=path.length-1 Or path[i+1]<>Asc(":") )
  74. Local i2:Int=path.Find( "/" )
  75. If i2=-1 Or i2>i+1 path=path[..i+1]+"/"+path[i+1..]
  76. EndIf
  77. EndIf
  78. ?
  79. End If
  80. If dirPath And path.EndsWith( "/" )
  81. If Not _IsRootPath( path ) path=path[..path.length-1]
  82. EndIf
  83. End Function
  84. Rem
  85. bbdoc: Strips the directory from a file path
  86. End Rem
  87. Function StripDir:String( path:String )
  88. FixPath path
  89. Local i:Int=path.FindLast( "/" )
  90. If i<>-1 Return path[i+1..]
  91. Return path
  92. End Function
  93. Rem
  94. bbdoc: Strips the extension from a file path
  95. End Rem
  96. Function StripExt:String( path:String )
  97. FixPath path
  98. Local i:Int=path.FindLast( "." )
  99. If i<>-1 And path.Find( "/",i+1 )=-1 Return path[..i]
  100. Return path
  101. End Function
  102. Rem
  103. bbdoc: Strips the directory and extension from a file path
  104. End Rem
  105. Function StripAll:String( path:String )
  106. Return StripDir( StripExt( path ) )
  107. End Function
  108. Rem
  109. bbdoc: Strips trailing slash from a file path
  110. about:
  111. #StripSlash will not remove the trailing slash from a 'root' path. For example, "/"
  112. or (on Win32 only) "C:/".
  113. End Rem
  114. Function StripSlash:String( path:String )
  115. FixPath path
  116. If path.EndsWith( "/" ) And Not _IsRootPath( path ) path=path[..path.length-1]
  117. Return path
  118. End Function
  119. Rem
  120. bbdoc: Extracts the directory from a file path
  121. End Rem
  122. Function ExtractDir:String( path:String )
  123. FixPath path
  124. If path="." Or path=".." Or _IsRootPath( path ) Return path
  125. Local i:Int=path.FindLast( "/" )
  126. If i=-1 Return ""
  127. If _IsRootPath( path[..i+1] ) i:+1
  128. Return path[..i]
  129. End Function
  130. Rem
  131. bbdoc: Extracts the extension from a file path
  132. End Rem
  133. Function ExtractExt:String( path:String )
  134. FixPath path
  135. Local i:Int=path.FindLast( "." )
  136. If i<>-1 And path.Find( "/",i+1 )=-1 Return path[i+1..]
  137. End Function
  138. Rem
  139. bbdoc: Gets the Current Directory
  140. returns: The current directory
  141. End Rem
  142. Function CurrentDir:String()
  143. If MaxIO.ioInitialized Then
  144. Return "/"
  145. End If
  146. Local path:String=getcwd_()
  147. FixPath path
  148. Return path
  149. End Function
  150. Rem
  151. bbdoc: Gets the real, absolute path of a file path
  152. End Rem
  153. Function RealPath:String( path:String )
  154. ?Win32
  155. If Not MaxIO.ioInitialized And path.StartsWith( "/" ) And Not path.StartsWith( "//" )
  156. path=_CurrentDrive()+":"+path
  157. EndIf
  158. ?
  159. FixPath path
  160. Local cd:String=_RootPath( path )
  161. If cd
  162. If Not MaxIO.ioInitialized Then
  163. path=path[cd.length..]
  164. End If
  165. Else
  166. cd=CurrentDir()
  167. EndIf
  168. path:+"/"
  169. While path
  170. Local i:Int=path.Find( "/" )
  171. Local t:String=path[..i]
  172. path=path[i+1..]
  173. Select t
  174. Case ""
  175. Case "."
  176. Case ".."
  177. If Not _IsRootPath( cd ) cd=cd[..cd.FindLast("/")]
  178. Default
  179. If Not cd.EndsWith( "/" ) cd:+"/"
  180. cd:+t
  181. End Select
  182. Wend
  183. Return cd
  184. End Function
  185. Rem
  186. bbdoc: Gets the file type
  187. returns: 0 if file at @path doesn't exist, FILETYPE_FILE (1) if the file is a plain file or FILETYPE_DIR (2) if the file is a directory
  188. End Rem
  189. Function FileType:Int( path:String )
  190. FixPath path
  191. If MaxIO.ioInitialized Then
  192. Local stat:SMaxIO_Stat
  193. If Not MaxIO.Stat(path, stat) Return 0
  194. Select stat._filetype
  195. Case EMaxIOFileType.REGULAR Return FILETYPE_FILE
  196. Case EMaxIOFileType.DIRECTORY Return FILETYPE_DIR
  197. End Select
  198. Else
  199. Local Mode:Int,size:Long,mtime:Int,ctime:Int,atime:Int
  200. If stat_( path,Mode,size,mtime,ctime,atime ) Return 0
  201. Select Mode & S_IFMT_
  202. Case S_IFREG_ Return FILETYPE_FILE
  203. Case S_IFDIR_ Return FILETYPE_DIR
  204. End Select
  205. End If
  206. Return FILETYPE_NONE
  207. End Function
  208. Rem
  209. bbdoc: Gets file time
  210. returns: The time the file at @path was last modified.
  211. End Rem
  212. Function FileTime:Long( path:String, timetype:Int=FILETIME_MODIFIED )
  213. FixPath path
  214. If MaxIO.ioInitialized Then
  215. Local stat:SMaxIO_Stat
  216. If Not MaxIO.Stat(path, stat) Return 0
  217. Select timetype
  218. Case FILETIME_CREATED
  219. Return stat._createtime
  220. Case FILETIME_MODIFIED
  221. Return stat._modtime
  222. Case FILETIME_ACCESSED
  223. Return stat._accesstime
  224. EndSelect
  225. Else
  226. Local Mode:Int,size:Long,mtime:Int,ctime:Int,atime:Int
  227. If stat_( path,Mode,size,mtime,ctime,atime ) Return 0
  228. Select timetype
  229. Case FILETIME_CREATED
  230. Return ctime
  231. Case FILETIME_MODIFIED
  232. Return mtime
  233. Case FILETIME_ACCESSED
  234. Return atime
  235. EndSelect
  236. End If
  237. End Function
  238. Rem
  239. bbdoc: Sets the file modified or last accessed time.
  240. about: @time should be number of seconds since epoch.
  241. End Rem
  242. Function SetFileTime( path:String, time:Long, timeType:Int=FILETIME_MODIFIED)
  243. FixPath path
  244. If MaxIO.ioInitialized Then
  245. ' Not available
  246. Else
  247. Select timetype
  248. Case FILETIME_MODIFIED
  249. utime_(path, timeType, time)
  250. Case FILETIME_ACCESSED
  251. utime_(path, timeType, time)
  252. End Select
  253. End If
  254. End Function
  255. Rem
  256. bbdoc: Sets the file modified or last accessed time.
  257. about: @dateTime is the basic DateTime struct defined in pub.stdc .
  258. End Rem
  259. Function SetFileTime( path:String, dateTime:SDateTime, timeType:Int=FILETIME_MODIFIED)
  260. SetFileTime(path, dateTime.ToEpochSecs(), timeType)
  261. End Function
  262. Rem
  263. bbdoc: Gets file time
  264. returns: The time the file at @path was last modified as SDatetime struct.
  265. End Rem
  266. Function FileDateTime:SDateTime( path:String, timetype:Int=FILETIME_MODIFIED )
  267. Return SDateTime.FromEpoch( FileTime(path, timetype) )
  268. End Function
  269. Rem
  270. bbdoc: Sets the file modified or last accessed time.
  271. about: @dateTime is the basic DateTime struct defined in pub.stdc .
  272. End Rem
  273. Function SetFileDateTime( path:String, dateTime:SDateTime, timeType:Int=FILETIME_MODIFIED)
  274. SetFileTime(path, dateTime.ToEpochSecs(), timeType)
  275. End Function
  276. Rem
  277. bbdoc: Gets the file size
  278. returns: The size, in bytes, of the file at @path, or -1 if the file does not exist
  279. End Rem
  280. Function FileSize:Long( path:String )
  281. FixPath path
  282. If MaxIO.ioInitialized Then
  283. Local stat:SMaxIO_Stat
  284. If Not MaxIO.Stat(path, stat) Return -1
  285. Return stat._filesize
  286. Else
  287. Local Mode:Int,size:Long,mtime:Int,ctime:Int,atime:Int
  288. If stat_( path,Mode,size,mtime,ctime,atime ) Return -1
  289. Return size
  290. End If
  291. End Function
  292. Rem
  293. bbdoc: Gets the file mode
  294. returns: The file mode flags
  295. End Rem
  296. Function FileMode:Int( path:String )
  297. FixPath path
  298. If Not MaxIO.ioInitialized Then
  299. Local Mode:Int,size:Long,mtime:Int,ctime:Int,atime:Int
  300. If stat_( path,Mode,size,mtime,ctime,atime ) Return -1
  301. Return Mode & 511
  302. End If
  303. End Function
  304. Rem
  305. bbdoc: Sets file mode
  306. End Rem
  307. Function SetFileMode( path:String,Mode:Int )
  308. FixPath path
  309. If Not MaxIO.ioInitialized Then
  310. chmod_ path,Mode
  311. End If
  312. End Function
  313. Rem
  314. bbdoc: Creates a file
  315. returns: #True if successful
  316. End Rem
  317. Function CreateFile:Int( path:String )
  318. FixPath path
  319. If MaxIO.ioInitialized Then
  320. MaxIO.DeletePath(path)
  321. Local t:Byte Ptr = MaxIO.OpenWrite(path)
  322. If t MaxIO.Close(t)
  323. Else
  324. remove_ path
  325. Local t:Byte Ptr=fopen_( path,"wb" )
  326. If t fclose_ t
  327. End If
  328. If FileType( path )=FILETYPE_FILE Return True
  329. End Function
  330. Rem
  331. bbdoc: Creates a directory
  332. returns: #True if successful
  333. about:
  334. If @recurse is #True, any required subdirectories are also created.
  335. End Rem
  336. Function CreateDir:Int( path:String,recurse:Int=False )
  337. FixPath path,True
  338. If MaxIO.ioInitialized Then
  339. Return MaxIO.MkDir(path)
  340. Else
  341. If Not recurse
  342. mkdir_ path,1023
  343. Return FileType(path)=FILETYPE_DIR
  344. EndIf
  345. Local t:String
  346. path=RealPath(path)+"/"
  347. While path
  348. Local i:Int=path.find("/")+1
  349. t:+path[..i]
  350. path=path[i..]
  351. Select FileType(t)
  352. Case FILETYPE_DIR
  353. Case FILETYPE_NONE
  354. Local s:String=StripSlash(t)
  355. mkdir_ StripSlash(s),1023
  356. If FileType(s)<>FILETYPE_DIR Return False
  357. Default
  358. Return False
  359. End Select
  360. Wend
  361. Return True
  362. End If
  363. End Function
  364. Rem
  365. bbdoc: Deletes a file
  366. returns: #True if successful
  367. End Rem
  368. Function DeleteFile:Int( path:String )
  369. FixPath path
  370. If MaxIO.ioInitialized Then
  371. MaxIO.DeletePath(path)
  372. Else
  373. remove_ path
  374. End If
  375. Return FileType(path)=FILETYPE_NONE
  376. End Function
  377. Rem
  378. bbdoc: Renames a file
  379. returns: #True if successful
  380. End Rem
  381. Function RenameFile:Int( oldpath:String,newpath:String )
  382. If MaxIO.ioInitialized Then
  383. Return False
  384. End If
  385. FixPath oldpath
  386. FixPath newpath
  387. Return rename_( oldpath,newpath)=0
  388. End Function
  389. Rem
  390. bbdoc: Copies a file
  391. returns: #True if successful
  392. End Rem
  393. Function CopyFile:Int( src:String,dst:String )
  394. Local in:TStream=ReadStream( src ),ok:Int
  395. If in
  396. Local out:TStream=WriteStream( dst )
  397. If out
  398. Try
  399. CopyStream in,out
  400. ok=True
  401. Catch ex:TStreamWriteException
  402. End Try
  403. out.Close
  404. EndIf
  405. in.Close
  406. EndIf
  407. Return ok
  408. End Function
  409. Rem
  410. bbdoc: Copies a directory
  411. returns: #True if successful
  412. End Rem
  413. Function CopyDir:Int( src:String,dst:String )
  414. Function CopyDir_:Int( src:String,dst:String )
  415. If FileType( dst )=FILETYPE_NONE CreateDir dst
  416. If FileType( dst )<>FILETYPE_DIR Return False
  417. For Local file:String=EachIn LoadDir( src )
  418. Select FileType( src+"/"+file )
  419. Case FILETYPE_DIR
  420. If Not CopyDir_( src+"/"+file,dst+"/"+file ) Return False
  421. Case FILETYPE_FILE
  422. If Not CopyFile( src+"/"+file,dst+"/"+file ) Return False
  423. End Select
  424. Next
  425. Return True
  426. End Function
  427. FixPath src
  428. If FileType( src )<>FILETYPE_DIR Return False
  429. FixPath dst
  430. Return CopyDir_( src,dst )
  431. End Function
  432. Rem
  433. bbdoc: Deletes a directory
  434. returns: #True if successful
  435. about: Set @recurse to #True to delete all subdirectories and files recursively -
  436. but be careful!
  437. End Rem
  438. Function DeleteDir:Int( path:String,recurse:Int=False )
  439. FixPath path,True
  440. If recurse
  441. Local dir:Byte Ptr=ReadDir( path )
  442. If Not dir Return False
  443. Repeat
  444. Local t:String=NextFile( dir )
  445. If t="" Exit
  446. If t="." Or t=".." Continue
  447. Local f:String=path+"/"+t
  448. Select FileType( f )
  449. Case 1 DeleteFile f
  450. Case 2 DeleteDir f,True
  451. End Select
  452. Forever
  453. CloseDir dir
  454. EndIf
  455. rmdir_ path
  456. If FileType( path )=0 Return True
  457. End Function
  458. Rem
  459. bbdoc: Changes the current directory
  460. returns: True if successful
  461. End Rem
  462. Function ChangeDir:Int( path:String )
  463. If MaxIO.ioInitialized Then
  464. Return False
  465. Else
  466. FixPath path,True
  467. If chdir_( path )=0 Return True
  468. End If
  469. End Function
  470. Rem
  471. bbdoc: Opens a directory
  472. returns: A directory handle, or #Null if the directory does not exist
  473. about: Use #NextFile to get the next file in the directory.
  474. The directory must be closed with #CloseDir.
  475. End Rem
  476. Function ReadDir:Byte Ptr( path:String )
  477. FixPath path,True
  478. If MaxIO.ioInitialized Then
  479. Return bmx_blitzio_readdir(path)
  480. Else
  481. Return opendir_( path )
  482. End If
  483. End Function
  484. Rem
  485. bbdoc: Returns the next file in a directory
  486. returns: File name of next file in the directory opened using #ReadDir, or an empty #String if there are no more files to read.
  487. End Rem
  488. Function NextFile:String( dir:Byte Ptr )
  489. If MaxIO.ioInitialized Then
  490. Return bmx_blitzio_nextFile(dir)
  491. Else
  492. Return readdir_( dir )
  493. End If
  494. End Function
  495. Rem
  496. bbdoc: Closes a directory.
  497. about: Closes a directory opened with #ReadDir.
  498. End Rem
  499. Function CloseDir( dir:Byte Ptr )
  500. If MaxIO.ioInitialized Then
  501. bmx_blitzio_closeDir(dir)
  502. Else
  503. closedir_ dir
  504. End If
  505. End Function
  506. Rem
  507. bbdoc: Loads a directory
  508. returns: A string array containing contents of @dir
  509. about: The @skip_dots parameter, if true, removes the '.' (current) and '..'
  510. (parent) directories from the returned array.
  511. End Rem
  512. Function LoadDir:String[]( dir:String,skip_dots:Int=True )
  513. FixPath dir,True
  514. Local d:Byte Ptr=ReadDir( dir )
  515. If Not d Return Null
  516. Local i:String[100],n:Int
  517. Repeat
  518. Local f:String=NextFile( d )
  519. If Not f Exit
  520. If skip_dots And (f="." Or f="..") Continue
  521. If n=i.length i=i[..n+100]
  522. i[n]=f
  523. n=n+1
  524. Forever
  525. CloseDir d
  526. Return i[..n]
  527. End Function
  528. Rem
  529. bbdoc: Opens a file for input and/or output.
  530. about:
  531. This command is similar to the #OpenStream command but will attempt
  532. to cache the contents of the file to ensure serial streams such as
  533. http: based url's are seekable. Use the #CloseStream command when
  534. finished reading and or writing to a Stream returned by #OpenFile.
  535. End Rem
  536. Function OpenFile:TStream( url:Object,readable:Int=True,writeable:Int=True )
  537. Local stream:TStream=OpenStream( url,readable,writeable )
  538. If Not stream Return Null
  539. If stream.Pos()=-1 Return TBankStream.Create( TBank.Load(stream) )
  540. Return stream
  541. End Function
  542. Rem
  543. bbdoc: Opens a file For Input.
  544. about:
  545. This command is similar to the #ReadStream command but will attempt
  546. to cache the contents of the file to ensure serial streams such as
  547. http: based url's are seekable. Use the #CloseStream command when
  548. finished reading and or writing to a Stream returned by #OpenFile.
  549. End Rem
  550. Function ReadFile:TStream( url:Object )
  551. Return OpenFile( url,True,False )
  552. End Function
  553. Rem
  554. bbdoc: Opens a file for output.
  555. about:
  556. This command is identical to the #WriteStream command.
  557. End Rem
  558. Function WriteFile:TStream( url:Object )
  559. Return OpenFile( url,False,True )
  560. End Function
  561. Rem
  562. bbdoc: Closes a file stream.
  563. about:
  564. After performing file operations on an open file make sure to
  565. close the file stream with either #CloseFile or the identical
  566. #CloseStream command.
  567. End Rem
  568. Function CloseFile( stream:TStream )
  569. stream.Close
  570. End Function
  571. Rem
  572. bbdoc: Walks a file tree.
  573. End Rem
  574. Function WalkFileTree:Int(path:String, fileWalker:IFileWalker, options:EFileWalkOption = EFileWalkOption.None, maxDepth:Int = 0)
  575. FixPath(path)
  576. If MaxIO.ioInitialized Then
  577. If FileType(path) = FILETYPE_DIR Then
  578. Return FSWalkFileTree(path, fileWalker, options, 0, maxDepth)
  579. End If
  580. Else
  581. Return bmx_filesystem_walkfiletree(path, _walkfile, fileWalker, options, maxDepth)
  582. End If
  583. End Function
  584. Rem
  585. bbdoc: An interface for file tree traversal.
  586. End rem
  587. Interface IFileWalker
  588. Rem
  589. bbdoc: Called once for each file/folder traversed.
  590. about: Return EFileWalkResult.OK to continue the tree traversal, or EFileWalkResult.Terminate to exit early.
  591. The contents of @attributes is only valid for the duration of the call.
  592. End Rem
  593. Method WalkFile:EFileWalkResult(attributes:SFileAttributes Var)
  594. End Interface
  595. Rem
  596. bbdoc: File attributes
  597. End rem
  598. Struct SFileAttributes
  599. ?win32
  600. Field StaticArray name:Short[8192]
  601. ?not win32
  602. Field StaticArray name:Byte[8192]
  603. ?
  604. Field fileType:Short
  605. Field depth:Short
  606. Rem
  607. bbdoc: The size, in bytes, of the file.
  608. End Rem
  609. Field size:ULong
  610. Field creationTime:Int
  611. Field modifiedTime:Int
  612. Rem
  613. bbdoc: Returns the name of the file/directory.
  614. End rem
  615. Method GetName:String()
  616. ?win32
  617. Return String.FromWString(name)
  618. ?not win32
  619. Return String.FromUTF8String(name)
  620. ?
  621. End Method
  622. Method IsRegularFile:Int()
  623. Return fileType = FILETYPE_FILE
  624. End Method
  625. Method IsDirectory:Int()
  626. Return fileType = FILETYPE_DIR
  627. End Method
  628. Method IsSymbolicLink:Int()
  629. Return fileType = FILETYPE_SYM
  630. End Method
  631. End Struct
  632. Rem
  633. bbdoc:
  634. End rem
  635. Enum EFileWalkOption
  636. None
  637. FollowLinks
  638. End Enum
  639. Rem
  640. bbdoc:
  641. End rem
  642. Enum EFileWalkResult
  643. OK
  644. Terminate
  645. SkipSubtree
  646. SkipSiblings
  647. End Enum
  648. Private
  649. Function _walkFile:EFileWalkResult(fileWalker:IFileWalker, attributes:SFileAttributes Var) { nomangle }
  650. Return fileWalker.WalkFile(attributes)
  651. End Function
  652. Function FSWalkFileTree:Int(dir:string, fileWalker:IFileWalker, options:EFileWalkOption, depth:Int, maxDepth:Int)
  653. Local attributes:SFileAttributes
  654. ApplyAttributes(dir, depth, VarPtr attributes)
  655. Local res:EFileWalkResult = fileWalker.WalkFile(attributes)
  656. If res = EFileWalkResult.Terminate Then
  657. Return 1
  658. End If
  659. Local d:Byte Ptr = ReadDir(dir)
  660. If d Then
  661. Local f:String = NextFile(d)
  662. While f
  663. Local path:String = dir + "/" + f
  664. If ApplyAttributes(path, depth + 1, VarPtr attributes) Then
  665. If attributes.fileType = FILETYPE_DIR Then
  666. Local ret:Int = FSWalkFileTree(path, fileWalker, options, depth + 1, maxDepth)
  667. If ret Then
  668. CloseDir(d)
  669. Return ret
  670. End If
  671. Else
  672. res = fileWalker.WalkFile(attributes)
  673. If res = EFileWalkResult.Terminate Then
  674. CloseDir(d)
  675. Return 1
  676. End If
  677. End If
  678. End IF
  679. f = NextFile(d)
  680. Wend
  681. CloseDir(d)
  682. End If
  683. End Function
  684. Function ApplyAttributes:Int(path:String, depth:Int, attributes:SFileAttributes Ptr)
  685. Local stat:SMaxIO_Stat
  686. If Not MaxIO.Stat(path, stat) Then
  687. Return False
  688. End If
  689. Select stat._filetype
  690. Case EMaxIOFileType.REGULAR
  691. attributes.fileType = FILETYPE_FILE
  692. Case EMaxIOFileType.DIRECTORY
  693. attributes.fileType = FILETYPE_DIR
  694. Case EMaxIOFileType.SYMLINK
  695. attributes.fileType = FILETYPE_SYM
  696. End Select
  697. Local length:Size_T = 8192
  698. ?win32
  699. path.ToWStringBuffer(attributes.name, length)
  700. ?not win32
  701. path.ToUTF8StringBuffer(attributes.name, length)
  702. ?
  703. attributes.depth = depth
  704. attributes.size = stat._filesize
  705. attributes.creationTime = stat._createtime
  706. attributes.modifiedTime = stat._modtime
  707. Return True
  708. End Function
  709. Extern
  710. Function bmx_filesystem_walkfiletree:Int(path:String, callback:EFileWalkResult(fileWalker:IFileWalker, attributes:SFileAttributes Var), walker:IFileWalker, options:EFileWalkOption, maxDepth:Int)
  711. End Extern