Gulligagardinen

fList (Lua)

Feb 13th, 2013
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.97 KB | None | 0 0
  1. --[[
  2.         Project info:
  3.        
  4.         Name: File List API
  5.         Creator: Cutecurtain
  6.         Language: Lua
  7.         Website: None
  8.         License: GNU GPL
  9.  
  10.         Version: 1.0
  11. ]]
  12.  
  13. --[[
  14.         Changelog:
  15.           1.0:
  16.             Public Release
  17. ]]
  18.  
  19. --[[
  20.         LICENSE:
  21.        
  22.         File List API - Simplified File Management!
  23.         Copyright © 2013 Cutecurtain
  24.  
  25.         This program is free software: you can redistribute it and/or modify
  26.         it under the terms of the GNU General Public License as published by
  27.         the Free Software Foundation, either version 3 of the License, or
  28.         (at your option) any later version.
  29.  
  30.         This program is distributed in the hope that it will be useful,
  31.         but WITHOUT ANY WARRANTY; without even the implied warranty of
  32.         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  33.         GNU General Public License for more details.
  34.  
  35.         You should have received a copy of the GNU General Public License
  36.         along with this program.  If not, see <http://www.gnu.org/licenses/>.
  37. ]]
  38.  
  39. --Check if the file exists
  40. exist = function(path)
  41.     local f = assert(io.open(path, 'r'), path.." does not exist")
  42.     f:close()
  43.     return true
  44. end
  45.  
  46. --Returns the file's path or name depended on the rValue("name" or "path")
  47. fPath = function(path, rValue)
  48.     if exist(path) and rValue then
  49.         local slash = 0
  50.         for i = 1, #path do
  51.             if path:sub(i, i) == "/" then
  52.                 slash = i
  53.             end
  54.         end
  55.         if rValue == "path" then
  56.             return path:sub(1, slash)
  57.         elseif rValue == "name" then
  58.             return path:sub(slash+1)
  59.         end
  60.     end
  61.     return ""
  62. end
  63.  
  64. --Get table with all lines from file
  65. fTable = function(path)
  66.     if exist(path) then
  67.         local f = io.open(path, 'r')
  68.         local _table = {}
  69.         local line = f:read('*l')
  70.         local i = 1
  71.         repeat
  72.             _table[i] = line
  73.             line = f:read('*l')
  74.             i = i+1
  75.         until not line
  76.         f:close()
  77.         return _table
  78.     end
  79.     return {}
  80. end
  81.  
  82. --Get specific line from file
  83. fLine = function(path, line)
  84.     local _table = fTable(path)
  85.     if _table[line] then
  86.         return _table[line]
  87.     end
  88.     return ""
  89. end
  90.  
  91. --Get all text from file
  92. fText = function(path)
  93.     if exist(path) then
  94.         local f = io.open(path, 'r')
  95.         local str = f:read('*a')
  96.         f:close()
  97.         return str
  98.     end
  99.     return ""
  100. end
  101.  
  102. --Standard write
  103. fWrite = function(path, str)
  104.     if exist(path) then
  105.         local f = io.open(path, 'w')
  106.         f:write(str)
  107.         f:close()
  108.     end
  109. end
  110.  
  111. --Standard append
  112. fAppend = function(path, str)
  113.     if exist(path) then
  114.         local f = io.open(path, 'a')
  115.         f:write(str..'\n')
  116.         f:close()
  117.     end
  118. end
  119.  
  120. --Standard write at top
  121. fTop = function(path, str)
  122.     local _str = fText(path)
  123.     fWrite(path, str..'\n'.._str)
  124. end
  125.  
  126. --Write from table
  127. fWriteTable = function(path, _table)
  128.     local str = ""
  129.     for _, line in pairs(_table) do
  130.         str = str..line..'\n'
  131.     end
  132.     fWrite(path, str)
  133. end
  134.  
  135. --Append from table
  136. fAppendTable = function(path, _table)
  137.     local str = ""
  138.     for _, line in pairs(_table) do
  139.         str = str..line..'\n'
  140.     end
  141.     fAppend(path, str)
  142. end
  143.  
  144. --Write at top from table
  145. fTopTable = function(path, _table)
  146.     local str = ""
  147.     for _, line in pairs(_table) do
  148.         str = str..line..'\n'
  149.     end
  150.     fTop(path, str)
  151. end
  152.  
  153. --Replacing a line in a file
  154. fReplaceLine = function(path, line, str)
  155.     local _table = fTable(path)
  156.     _table[line] = str
  157.     fWriteTable(path, _table)
  158. end
  159.  
  160. --Mounting & unmounting files for more easy configuration
  161.     --[[
  162.         The point of this part is only to get all the information you might would like to have
  163.         at once, for example: characters amount, line amount ect...
  164.         All though this part is quite unnecessary, I just thought it would be fun to do
  165.         something extra. Otherwise just remove this part from the code if it is for no
  166.         use to your code.
  167.     ]]
  168. files = {} --Table with all the mounted files
  169. mount = { --Table with the mounting functions
  170.     file = function(file)
  171.         if exist(file) then
  172.             files[#files+1] = {
  173.                 type = "real",
  174.                 name = fPath(file, "name"),
  175.                 path = fPath(file, "path"),
  176.                 text = fText(file),
  177.                 lines = fTable(file),
  178.                 charAmount = #fText(file),
  179.                 lineAmount = #fTable(file)
  180.             }
  181.             return #files
  182.         end
  183.         return nil
  184.     end;
  185.  
  186.     virtual = function(name, path, text) --Here is when it becomes interesting. You can mount a virtual file.
  187.         local last = 1
  188.         local _table = {}
  189.         for i = 1, #text do
  190.             if text:sub(i, i) == "\n" or i == #text then
  191.                 if text:sub(i, i) == "\n" then _table[#_table+1] = text:sub(last, i-1)
  192.                 else _table[#_table+1] = text:sub(last, i) end
  193.                 last = i+1
  194.             end
  195.         end
  196.         files[#files+1] = {
  197.             type = "virtual",
  198.             name = name,
  199.             path = path,
  200.             text = text,
  201.             lines = _table,
  202.             charAmount = #text,
  203.             lineAmount = #_table
  204.         }
  205.         return #files
  206.     end;
  207.  
  208.     remove = function(file) --You may enter either the array number or the name of the file to remove it
  209.         if type(file) == "number" then
  210.             table.remove(files, file)
  211.             return true
  212.         elseif type(file) == "string" then
  213.             for k, v in pairs(files) do
  214.                 if v.name == file then
  215.                     table.remove(files, tonumber(k))
  216.                     return true
  217.                 end
  218.             end
  219.         end
  220.         return false
  221.     end;
  222.    
  223.     replaceLine = function(file, line, str)
  224.         if type(file) == "number" then
  225.             files[file].lines[line] = str
  226.             local str = ""
  227.             for i = 1, #files[file].lines do
  228.                 str = str..files[file].lines[i].."\n"
  229.             end
  230.             files[file].text = str
  231.         elseif type(file) == "string" then
  232.             for k, v in pairs(files) do
  233.                 if v.name == file then
  234.                     files[k].lines[line] = str
  235.                     local str = ""
  236.                     for i = 1, #files[k].lines do
  237.                         str = str..files[k].lines[i].."\n"
  238.                     end
  239.                     files[k].text = str
  240.                     break
  241.                 end
  242.             end
  243.         end
  244.     end;
  245.    
  246.     create = function(file)
  247.         if type(file) == "number" then
  248.             local f = io.open(files[file].path..files[file].name, 'w')
  249.             f:write(files[file].text)
  250.             f:close()
  251.         elseif type(file) == "string" then
  252.             for k, v in pairs(files) do
  253.                 if v.name == file then
  254.                     local f = io.open(files[k].path..files[k].name, 'w')
  255.                     f:write(files[k].text)
  256.                     f:close()
  257.                     break
  258.                 end
  259.             end
  260.         end
  261.     end
  262. }
Advertisement
Add Comment
Please, Sign In to add comment