December 03, 2020, 09:00:20 PM

Author Topic: [bmx] OOP File System Interface by Otus [ 1+ years ago ]  (Read 517 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] OOP File System Interface by Otus [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : OOP File System Interface
Author : Otus
Posted : 1+ years ago

Description : Install as a module or comment out the module lines and import directly. For module installations, bbdocs included.

TDir objects support EachIn, TFile objects can be used as url for stream functions, e.g. OpenStream.


Code :
Code: BlitzMax
  1. SuperStrict
  2.  
  3. Rem
  4. bbdoc: File system interface
  5. End Rem
  6. Module Otus.FS
  7.  
  8. ModuleInfo "Version: 1.00"
  9. ModuleInfo "Author: Jan Varho"
  10. ModuleInfo "License: Public domain (none)"
  11.  
  12. Import BRL.FileSystem
  13.  
  14. Rem
  15. bbdoc: A file in the file system
  16. about:
  17. Open files using #Open. TFile objects can be used with OpenStream.
  18. End Rem
  19. Type TFile
  20.        
  21.         Field _path:String
  22.        
  23.         Rem
  24.         bbdoc: Read the file
  25.         returns: A file stream
  26.         End Rem
  27.         Method Read:TStream()
  28.                 Return ReadFile(_path)
  29.         End Method
  30.        
  31.         Rem
  32.         bbdoc: Write the file
  33.         returns: A file stream
  34.         End Rem
  35.         Method Write:TStream()
  36.                 Return WriteFile(_path)
  37.         End Method
  38.        
  39.         Rem
  40.         bbdoc: Open the file
  41.         returns: A file stream
  42.         End Rem
  43.         Method OpenStream:TStream(readable% = True, writeable% = True)
  44.                 Return OpenFile(_path, readable, writeable)
  45.         End Method
  46.        
  47.         Rem
  48.         bbdoc: Remove the file
  49.         returns: True if successful
  50.         End Rem
  51.         Method Remove:Int()
  52.                 If DeleteFile(_path)
  53.                         _path = ""
  54.                         Return True
  55.                 End If
  56.                 Return False
  57.         End Method
  58.        
  59.         Rem
  60.         bbdoc: Rename the file
  61.         returns: True if successful
  62.         End Rem
  63.         Method Rename:Int(name:String)
  64.                 If RenameFile(_path, name)
  65.                         _path = RealPath(name)
  66.                         Return True
  67.                 End If
  68.                 Return False
  69.         End Method
  70.        
  71.         Rem
  72.         bbdoc: Copy the file
  73.         returns: A TFile object for the copy if successful
  74.         End Rem
  75.         Method Copy:TFile(dest:String)
  76.                 If CopyFile(_path, dest)
  77.                         Return Open(dest)
  78.                 End If
  79.                 Return Null
  80.         End Method
  81.        
  82.         Rem
  83.         bbdoc: Get the parent directory of the file
  84.         returns: A #TDir object
  85.         End Rem
  86.         Method GetDir:TDir()
  87.                 Return TDir.Open( ExtractDir(_path) )
  88.         End Method
  89.        
  90.         Rem
  91.         bbdoc: Get file extension
  92.         returns: A string representing the file extension
  93.         End Rem
  94.         Method GetExt:String()
  95.                 Return ExtractExt(_path)
  96.         End Method
  97.        
  98.         Rem
  99.         bbdoc: Get the filename
  100.         returns: A string representing the name of the file
  101.         End Rem
  102.         Method GetName:String()
  103.                 Return StripDir(_path)
  104.         End Method
  105.        
  106.         Rem
  107.         bbdoc: Get filemode
  108.         returns: The file's mode flags
  109.         End Rem
  110.         Method GetMode:Int()
  111.                 Return FileMode(_path)
  112.         End Method
  113.        
  114.         Rem
  115.         bbdoc: Set filemode
  116.         returns: True if successful
  117.         End Rem
  118.         Method SetMode:Int(mode:Int)
  119.                 SetFileMode _path, mode
  120.                 Return FileMode(_path)=mode
  121.         End Method
  122.        
  123.         Rem
  124.         bbdoc: Get path
  125.         returns: The full path of the file
  126.         End Rem
  127.         Method GetPath:String()
  128.                 Return _path
  129.         End Method
  130.        
  131.         Rem
  132.         bbdoc: Get filesize
  133.         returns: The size of the file in bytes
  134.         End Rem
  135.         Method GetSize:Int()
  136.                 Return FileSize(_path)
  137.         End Method
  138.        
  139.         Rem
  140.         bbdoc: Last modified
  141.         returns: The timestamp of the last modification to the file
  142.         End Rem
  143.         Method GetTime:Int()
  144.                 Return FileTime(_path)
  145.         End Method
  146.        
  147.         Rem
  148.         bbdoc: Creates a new file
  149.         returns: A TFile object if successful
  150.         End Rem
  151.         Function Create:TFile(path:String)
  152.                 If Not CreateFile(path) Then Return Null
  153.                 Return Open(path)
  154.         End Function
  155.        
  156.         Rem
  157.         bbdoc: Opens a file
  158.         returns: A TFile object if successful
  159.         about:
  160.         You can test if it's a directory by casting to #TDir
  161.         End Rem
  162.         Function Open:TFile(path:String)
  163.                 Local f:TFile
  164.                 Select FileType(path)
  165.                 Case FILETYPE_FILE
  166.                         f = New TFile
  167.                 Case FILETYPE_DIR
  168.                         f = New TDir
  169.                 Default
  170.                         Return Null
  171.                 End Select
  172.                 f._path = RealPath(path)
  173.                 Return f
  174.         End Function
  175.        
  176. End Type
  177.  
  178. Rem
  179. bbdoc: A directory in the filesystem
  180. about:
  181. You can open a directory using #Open and cycle the files in it using EachIn.
  182. Note that directories are also files.
  183. End Rem
  184. Type TDir Extends TFile
  185.        
  186.         Rem
  187.         bbdoc: Changes the current directory
  188.         End Rem
  189.         Method ChangeTo:Int()
  190.                 Return ChangeDir(_path)
  191.         End Method
  192.        
  193.         Method Read:TStream()
  194.                 Return Null
  195.         End Method
  196.        
  197.         Method Write:TStream()
  198.                 Return Null
  199.         End Method
  200.        
  201.         Method OpenStream:TStream(readable% = True, writeable% = True)
  202.                 Return Null
  203.         End Method
  204.        
  205.         Rem
  206.         bbdoc: Removes the directory
  207.         returns: True on success
  208.         about:
  209.         Removing directories is recursive!
  210.         End Rem
  211.         Method Remove:Int()
  212.                 If DeleteDir(_path, True)
  213.                         _path = ""
  214.                         Return True
  215.                 End If
  216.                 Return False
  217.         End Method
  218.        
  219.         Method Copy:TDir(dest:String)
  220.                 If CopyDir(_path, dest)
  221.                         Return Open(dest)
  222.                 End If
  223.                 Return Null
  224.         End Method
  225.        
  226.         Method GetExt:String()
  227.                 Return Null
  228.         End Method
  229.        
  230.         Rem
  231.         bbdoc: Open a file in the directory
  232.         returns: A #TFile object for the file
  233.         End Rem
  234.         Method OpenFile:TFile(name:String)
  235.                 Return TFile.Open(GetPath()+"/"+name)
  236.         End Method
  237.        
  238.         Rem
  239.         bbdoc: Open a sub directory
  240.         returns: A #TDir object for the directory
  241.         End Rem
  242.         Method OpenDir:TDir(name:String)
  243.                 Return TDir.Open(GetPath()+"/"+name)
  244.         End Method
  245.        
  246.         Rem
  247.         bbdoc: Create a file in the directory
  248.         returns: A #TFile object for the new file
  249.         End Rem
  250.         Method CreateFile:TFile(name:String)
  251.                 Return TFile.Create(GetPath()+"/"+name)
  252.         End Method
  253.        
  254.         Rem
  255.         bbdoc: Create a sub directory
  256.         returns: A #TDir object for the new directory
  257.         End Rem
  258.         Method CreateDir:TDir(name:String)
  259.                 Return TDir.Create(GetPath()+"/"+name)
  260.         End Method
  261.        
  262.         Method ObjectEnumerator:TDirEnum()
  263.                 Return TDirEnum.Create(Self)
  264.         End Method
  265.        
  266.         Rem
  267.         bbdoc: Creates a directory
  268.         returns: A TDir object if successful
  269.         End Rem
  270.         Function Create:TDir(path:String)
  271.                 If FileType(path)<>FILETYPE_DIR And Not CreateDir(path, True) Then Return Null
  272.                 Return Open(path)
  273.         End Function
  274.        
  275.         Rem
  276.         bbdoc: Current directory
  277.         returns: A TDir object for the current directory
  278.         End Rem
  279.         Function Current:TDir()
  280.                 Return Open(CurrentDir())
  281.         End Function
  282.        
  283.         Rem
  284.         bbdoc: Opens a directory
  285.         returns: A TDir object if successful
  286.         End Rem
  287.         Function Open:TDir(path:String)
  288.                 If FileType(path)<>FILETYPE_DIR Then Return Null
  289.                 Local d:TDir = New TDir
  290.                 d._path = RealPath(path)
  291.                 Return d
  292.         End Function
  293.        
  294. End Type
  295.  
  296. Type TDirEnum
  297.        
  298.         Field _dirname:String
  299.        
  300.         Field _handle:Int, _next:String
  301.        
  302.         Method Delete()
  303.                 If _handle CloseDir _handle
  304.         End Method
  305.        
  306.         Method HasNext:Int()
  307.                 If _next Then Return True
  308.                 If _handle CloseDir _handle
  309.                 _handle = 0
  310.                 Return False
  311.         End Method
  312.        
  313.         Method NextObject:Object()
  314.                 Local f:TFile = TFile.Open(_dirname+_next)
  315.                 _next = NextFile(_handle)
  316.                 While _next="." Or _next=".."
  317.                         _next = NextFile(_handle)
  318.                 Wend
  319.                 Return f
  320.         End Method
  321.        
  322.         Function Create:TDirEnum(d:TDir)
  323.                 Local e:TDirEnum = New TDirEnum
  324.                 e._dirname = d.GetPath() + "/"
  325.                 e._handle = ReadDir(d._path)
  326.                 e.NextObject
  327.                 Return e
  328.         End Function
  329.        
  330. End Type
  331.  
  332. New TFileStreamFactory
  333.  
  334. Type TFileStreamFactory Extends TStreamFactory
  335.        
  336.         Method CreateStream:TStream( url:Object, proto$, path$, readable%, writeable% )
  337.                 Local f:TFile = TFile(url)
  338.                 If Not f Return Null
  339.                 Return f.OpenStream(readable, writeable)
  340.         End Method
  341.        
  342. End Type


Comments :


Otus(Posted 1+ years ago)

 Sample use (logs files in the current directory with sizes):
Code: [Select]
SuperStrict

Framework BRL.StandardIO
Import Otus.FS

Local out:TStream = TDir.Current().CreateDir("output").CreateFile("log.txt").Write()

For Local f:TFile = EachIn TDir.Current()
out.WriteLine f.GetName() + "(" + f.GetSize() + ")"
Next


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal