Advertisement
_LINKI

GreyScript EasyFS Library

Jul 18th, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.14 KB | None | 0 0
  1. //Name - EasyFileSystem
  2. //Author - LINKI
  3. //Version - 0.1
  4. EasyFS = { }
  5. //Constructor//
  6. EasyFS.ctor = function(computer)
  7.     self.computer = computer
  8. end function
  9. EasyFS.absPath = function(path = "/")
  10.     if not path or typeof(path) != "string" then return null
  11.    
  12.     if path[0] != "/" then path = self.computer.current_path + "/" + path
  13.    
  14.     //Parsing
  15.     stack = []; i = 0
  16.     while i < path.len
  17.        
  18.         dir = ""
  19.         //Skip all - "//"
  20.         while i < path.len and path[i] == "/"
  21.             i = i + 1
  22.         end while
  23.        
  24.         //Store names and: ".." "."
  25.         while i < path.len and path[i] != "/"
  26.             dir = dir + path[i]
  27.             i = i + 1
  28.         end while
  29.        
  30.         //FinalChecks
  31.         if dir == ".." then
  32.             if stack then stack.pop() else continue
  33.         else if dir == "." then
  34.             continue
  35.         else if dir then
  36.             stack.push(dir)
  37.         end if
  38.        
  39.         i = i + 1
  40.     end while
  41.    
  42.     return "/" + stack.join("/")
  43. end function
  44. EasyFS.getCurrentFolder = function()
  45.     return self.computer.File(self.computer.current_path)
  46. end function
  47. EasyFS.getParrentPath = function(path)
  48.     return parent_path(self.absPath(path))
  49. end function
  50.  
  51. EasyFS.findObject = function(path)
  52.     object = self.computer.File(self.absPath(path))
  53.     if not object then return null
  54.     return object
  55. end function
  56. EasyFS.findFolder = function(path)
  57.     object = self.computer.File(self.absPath(path))
  58.     if not object or not object.is_folder then return null
  59.     return object
  60. end function
  61. EasyFS.findFile = function(path)
  62.     object = self.computer.File(self.absPath(path))
  63.     if not object or object.is_folder then return null
  64.     return object
  65. end function
  66.  
  67. EasyFS.fullPathPredicate = { }
  68. EasyFS.fullPathPredicate.ctor = function(findObjectFunc, computer, absPath)
  69.     self.findObjectFunc = @findObjectFunc
  70.     self.computer = computer    //Game-Bugfix
  71.     self.absPath = @absPath //Game-Bugfix
  72. end function
  73. EasyFS.fullPathPredicate.Run = function(folder, objectName)
  74.     return self.findObjectFunc(folder.path + "/" + objectName)
  75. end function
  76. EasyFS.namePathPredicate = { }
  77. EasyFS.namePathPredicate.Run = function(folder, objectName)
  78.     result = []
  79.     for file in folder.get_files()
  80.         if file.name.split(".")[0] == objectName then result.push(file)
  81.     end for
  82.     return result
  83. end function
  84. EasyFS.extensionPathPredicate = { }
  85. EasyFS.extensionPathPredicate.Run = function(folder, extension)
  86.     result = []
  87.     for file in folder.get_files()
  88.         ext = file.name.split(".")[-1]
  89.         if ext != file.name and ext == extension then result.push(file)
  90.     end for
  91.     return result
  92. end function
  93.  
  94. EasyFS.findAll = function(startFolder, objectName, recursive, predicate)
  95.     if typeof(startFolder) == "string" then startFolder = self.findFolder(startFolder)
  96.     if not startFolder then return null
  97.    
  98.     result = []
  99.     object = predicate.Run(startFolder, objectName)
  100.     if object and typeof(object) == "list" then result = result + object
  101.    
  102.     if recursive then
  103.         for folder in startFolder.get_folders()
  104.             result = result + self.findAll(folder, objectName, true, predicate)
  105.         end for
  106.     end if
  107.     return result
  108. end function
  109. EasyFS.findObjects = function(startFolder, objectName, recursive = false)
  110.     self.fullPathPredicate.ctor(@self.findObject, self.computer, @self.absPath)
  111.     return self.findAll(startFolder, objectName, recursive, self.fullPathPredicate)
  112. end function
  113. EasyFS.findFolders = function(startFolder, folderName, recursive = false)
  114.     self.fullPathPredicate.ctor(@self.findFolder, self.computer, @self.absPath)
  115.     return self.findAll(startFolder, folderName, recursive, self.fullPathPredicate)
  116. end function
  117. EasyFS.findFiles = function(startFolder, fileNameFull, recursive = false)
  118.     self.fullPathPredicate.ctor(@self.findFile, self.computer, @self.absPath)
  119.     return self.findAll(startFolder, fileNameFull, recursive, self.fullPathPredicate)
  120. end function
  121. EasyFS.findFilesByName = function(startFolder, fileName, recursive = false)
  122.     return self.findAll(startFolder, fileName, recursive, self.namePathPredicate)
  123. end function
  124. EasyFS.findFilesByExtension = function(startFolder, fileExtension, recursive = false)
  125.     return self.findAll(startFolder, fileExtension, recursive, self.extensionPathPredicate)
  126. end function
  127.  
  128. EasyFS.existObject = function(path)
  129.     if not self.findObject(path) then return false
  130.     return true
  131. end function
  132. EasyFS.existFolder = function(path)
  133.     if not self.findFolder(path) then return false
  134.     return true
  135. end function
  136. EasyFS.existFile = function(path)
  137.     if not self.findFile(path) then return false
  138.     return true
  139. end function
  140.  
  141. EasyFS.concatFiles = function(files)
  142.     if not files or typeof(files) != "list" then return ""
  143.    
  144.     result = ""
  145.     for file in files
  146.         if not file.is_binary and file.content then
  147.             result = result + file.content + "\n"
  148.         end if
  149.     end for
  150.    
  151.     if result.len > 1 and result[-2] == "\" and result[-1] == "n" then result = result[:-2]
  152.     return result
  153. end function
  154. EasyFS.concatFilesFromFolders = function(startFolder, recursive = false)
  155.     if typeof(startFolder) == "string" then startFolder = self.findFolder(startFolder)
  156.     if not startFolder then return null
  157.    
  158.     result = self.concatFiles(startFolder.get_files())
  159.    
  160.     if recursive then
  161.         for folder in startFolder.get_folders()
  162.             result = result + self.concatFilesFromFolders(folder, true)
  163.         end for
  164.     end if
  165.     return result
  166. end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement