Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Name - EasyFileSystem
- //Author - LINKI
- //Version - 0.1
- EasyFS = { }
- //Constructor//
- EasyFS.ctor = function(computer)
- self.computer = computer
- end function
- EasyFS.absPath = function(path = "/")
- if not path or typeof(path) != "string" then return null
- if path[0] != "/" then path = self.computer.current_path + "/" + path
- //Parsing
- stack = []; i = 0
- while i < path.len
- dir = ""
- //Skip all - "//"
- while i < path.len and path[i] == "/"
- i = i + 1
- end while
- //Store names and: ".." "."
- while i < path.len and path[i] != "/"
- dir = dir + path[i]
- i = i + 1
- end while
- //FinalChecks
- if dir == ".." then
- if stack then stack.pop() else continue
- else if dir == "." then
- continue
- else if dir then
- stack.push(dir)
- end if
- i = i + 1
- end while
- return "/" + stack.join("/")
- end function
- EasyFS.getCurrentFolder = function()
- return self.computer.File(self.computer.current_path)
- end function
- EasyFS.getParrentPath = function(path)
- return parent_path(self.absPath(path))
- end function
- EasyFS.findObject = function(path)
- object = self.computer.File(self.absPath(path))
- if not object then return null
- return object
- end function
- EasyFS.findFolder = function(path)
- object = self.computer.File(self.absPath(path))
- if not object or not object.is_folder then return null
- return object
- end function
- EasyFS.findFile = function(path)
- object = self.computer.File(self.absPath(path))
- if not object or object.is_folder then return null
- return object
- end function
- EasyFS.fullPathPredicate = { }
- EasyFS.fullPathPredicate.ctor = function(findObjectFunc, computer, absPath)
- self.findObjectFunc = @findObjectFunc
- self.computer = computer //Game-Bugfix
- self.absPath = @absPath //Game-Bugfix
- end function
- EasyFS.fullPathPredicate.Run = function(folder, objectName)
- return self.findObjectFunc(folder.path + "/" + objectName)
- end function
- EasyFS.namePathPredicate = { }
- EasyFS.namePathPredicate.Run = function(folder, objectName)
- result = []
- for file in folder.get_files()
- if file.name.split(".")[0] == objectName then result.push(file)
- end for
- return result
- end function
- EasyFS.extensionPathPredicate = { }
- EasyFS.extensionPathPredicate.Run = function(folder, extension)
- result = []
- for file in folder.get_files()
- ext = file.name.split(".")[-1]
- if ext != file.name and ext == extension then result.push(file)
- end for
- return result
- end function
- EasyFS.findAll = function(startFolder, objectName, recursive, predicate)
- if typeof(startFolder) == "string" then startFolder = self.findFolder(startFolder)
- if not startFolder then return null
- result = []
- object = predicate.Run(startFolder, objectName)
- if object and typeof(object) == "list" then result = result + object
- if recursive then
- for folder in startFolder.get_folders()
- result = result + self.findAll(folder, objectName, true, predicate)
- end for
- end if
- return result
- end function
- EasyFS.findObjects = function(startFolder, objectName, recursive = false)
- self.fullPathPredicate.ctor(@self.findObject, self.computer, @self.absPath)
- return self.findAll(startFolder, objectName, recursive, self.fullPathPredicate)
- end function
- EasyFS.findFolders = function(startFolder, folderName, recursive = false)
- self.fullPathPredicate.ctor(@self.findFolder, self.computer, @self.absPath)
- return self.findAll(startFolder, folderName, recursive, self.fullPathPredicate)
- end function
- EasyFS.findFiles = function(startFolder, fileNameFull, recursive = false)
- self.fullPathPredicate.ctor(@self.findFile, self.computer, @self.absPath)
- return self.findAll(startFolder, fileNameFull, recursive, self.fullPathPredicate)
- end function
- EasyFS.findFilesByName = function(startFolder, fileName, recursive = false)
- return self.findAll(startFolder, fileName, recursive, self.namePathPredicate)
- end function
- EasyFS.findFilesByExtension = function(startFolder, fileExtension, recursive = false)
- return self.findAll(startFolder, fileExtension, recursive, self.extensionPathPredicate)
- end function
- EasyFS.existObject = function(path)
- if not self.findObject(path) then return false
- return true
- end function
- EasyFS.existFolder = function(path)
- if not self.findFolder(path) then return false
- return true
- end function
- EasyFS.existFile = function(path)
- if not self.findFile(path) then return false
- return true
- end function
- EasyFS.concatFiles = function(files)
- if not files or typeof(files) != "list" then return ""
- result = ""
- for file in files
- if not file.is_binary and file.content then
- result = result + file.content + "\n"
- end if
- end for
- if result.len > 1 and result[-2] == "\" and result[-1] == "n" then result = result[:-2]
- return result
- end function
- EasyFS.concatFilesFromFolders = function(startFolder, recursive = false)
- if typeof(startFolder) == "string" then startFolder = self.findFolder(startFolder)
- if not startFolder then return null
- result = self.concatFiles(startFolder.get_files())
- if recursive then
- for folder in startFolder.get_folders()
- result = result + self.concatFilesFromFolders(folder, true)
- end for
- end if
- return result
- end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement