Advertisement
VarietyCereal

directory module

Aug 20th, 2023
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.91 KB | None | 0 0
  1. -- created by var/openpli.
  2.  
  3. type directory = {
  4.     _name: string,
  5.     _maxSize: number,
  6.     _currentSize: number,
  7.     _files: {}
  8. }
  9.  
  10. type file = {
  11.     _name: string,
  12.     _size: number,
  13.     _directory: string
  14. }
  15.  
  16. local Dirs = {} -- holds the directories
  17. local Directory = {} -- holds the functions
  18.  
  19. function Directory.mkdir(name: string, size: number)
  20.     if not name then name = "New Folder" return end
  21.     if not size then return end
  22.    
  23.     local newDirectory: directory = {
  24.         _name = name, -- name of the directory
  25.         _maxSize = size, -- max size in bytes, once max size is equal to size, then files cannot be added anymore
  26.         _currentSize = 0, -- current size of the whole directory (0 bytes is default size)
  27.         _files = {} -- files within the directory
  28.     }
  29.    
  30.     local sameDirIndex = 0
  31.    
  32.     for _, dir in Dirs do
  33.         local _name = dir._name
  34.        
  35.         if _name == newDirectory._name then
  36.             sameDirIndex += 1
  37.             newDirectory._name = `{newDirectory._name} ({sameDirIndex})`
  38.             table.insert(Dirs, newDirectory)
  39.         end
  40.     end
  41.    
  42.     print(`Directory {newDirectory._name} has been created`)
  43. end
  44.  
  45. function Directory.mk(name: string, size: number, directory: string)
  46.     local newFile: file = {
  47.         _name = name, -- name of the file
  48.         _size = size, -- size of the file in bytes
  49.     }
  50.    
  51.     for _, dir in Dirs do
  52.         local _name = dir._name
  53.         local _currentSize = dir._currentSize
  54.         local _maxSize = dir._maxSize
  55.        
  56.         if _name == directory then
  57.             if string.find(_name, "FULL") then
  58.                 warn(`Directory {_name} is full!`)
  59.             else
  60.                 newFile._directory = directory
  61.                 table.insert(dir._files, newFile)
  62.                 dir._currentSize += newFile._size
  63.             end
  64.         end
  65.        
  66.         local sameFileIndex = 0
  67.        
  68.         for _, checkedFile: file in dir._files do
  69.             if checkedFile._name == newFile._name then
  70.                 sameFileIndex += 1
  71.                 newFile._name = `{newFile._name} ({sameFileIndex})`
  72.             end
  73.         end
  74.        
  75.         if _currentSize == _maxSize then
  76.             _name = `{_name} (FULL)`
  77.         end
  78.     end
  79. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement