Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Project info:
- Name: File List API
- Creator: Cutecurtain
- Language: Lua
- Website: None
- License: GNU GPL
- Version: 1.0
- ]]
- --[[
- Changelog:
- 1.0:
- Public Release
- ]]
- --[[
- LICENSE:
- File List API - Simplified File Management!
- Copyright © 2013 Cutecurtain
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- ]]
- --Check if the file exists
- exist = function(path)
- local f = assert(io.open(path, 'r'), path.." does not exist")
- f:close()
- return true
- end
- --Returns the file's path or name depended on the rValue("name" or "path")
- fPath = function(path, rValue)
- if exist(path) and rValue then
- local slash = 0
- for i = 1, #path do
- if path:sub(i, i) == "/" then
- slash = i
- end
- end
- if rValue == "path" then
- return path:sub(1, slash)
- elseif rValue == "name" then
- return path:sub(slash+1)
- end
- end
- return ""
- end
- --Get table with all lines from file
- fTable = function(path)
- if exist(path) then
- local f = io.open(path, 'r')
- local _table = {}
- local line = f:read('*l')
- local i = 1
- repeat
- _table[i] = line
- line = f:read('*l')
- i = i+1
- until not line
- f:close()
- return _table
- end
- return {}
- end
- --Get specific line from file
- fLine = function(path, line)
- local _table = fTable(path)
- if _table[line] then
- return _table[line]
- end
- return ""
- end
- --Get all text from file
- fText = function(path)
- if exist(path) then
- local f = io.open(path, 'r')
- local str = f:read('*a')
- f:close()
- return str
- end
- return ""
- end
- --Standard write
- fWrite = function(path, str)
- if exist(path) then
- local f = io.open(path, 'w')
- f:write(str)
- f:close()
- end
- end
- --Standard append
- fAppend = function(path, str)
- if exist(path) then
- local f = io.open(path, 'a')
- f:write(str..'\n')
- f:close()
- end
- end
- --Standard write at top
- fTop = function(path, str)
- local _str = fText(path)
- fWrite(path, str..'\n'.._str)
- end
- --Write from table
- fWriteTable = function(path, _table)
- local str = ""
- for _, line in pairs(_table) do
- str = str..line..'\n'
- end
- fWrite(path, str)
- end
- --Append from table
- fAppendTable = function(path, _table)
- local str = ""
- for _, line in pairs(_table) do
- str = str..line..'\n'
- end
- fAppend(path, str)
- end
- --Write at top from table
- fTopTable = function(path, _table)
- local str = ""
- for _, line in pairs(_table) do
- str = str..line..'\n'
- end
- fTop(path, str)
- end
- --Replacing a line in a file
- fReplaceLine = function(path, line, str)
- local _table = fTable(path)
- _table[line] = str
- fWriteTable(path, _table)
- end
- --Mounting & unmounting files for more easy configuration
- --[[
- The point of this part is only to get all the information you might would like to have
- at once, for example: characters amount, line amount ect...
- All though this part is quite unnecessary, I just thought it would be fun to do
- something extra. Otherwise just remove this part from the code if it is for no
- use to your code.
- ]]
- files = {} --Table with all the mounted files
- mount = { --Table with the mounting functions
- file = function(file)
- if exist(file) then
- files[#files+1] = {
- type = "real",
- name = fPath(file, "name"),
- path = fPath(file, "path"),
- text = fText(file),
- lines = fTable(file),
- charAmount = #fText(file),
- lineAmount = #fTable(file)
- }
- return #files
- end
- return nil
- end;
- virtual = function(name, path, text) --Here is when it becomes interesting. You can mount a virtual file.
- local last = 1
- local _table = {}
- for i = 1, #text do
- if text:sub(i, i) == "\n" or i == #text then
- if text:sub(i, i) == "\n" then _table[#_table+1] = text:sub(last, i-1)
- else _table[#_table+1] = text:sub(last, i) end
- last = i+1
- end
- end
- files[#files+1] = {
- type = "virtual",
- name = name,
- path = path,
- text = text,
- lines = _table,
- charAmount = #text,
- lineAmount = #_table
- }
- return #files
- end;
- remove = function(file) --You may enter either the array number or the name of the file to remove it
- if type(file) == "number" then
- table.remove(files, file)
- return true
- elseif type(file) == "string" then
- for k, v in pairs(files) do
- if v.name == file then
- table.remove(files, tonumber(k))
- return true
- end
- end
- end
- return false
- end;
- replaceLine = function(file, line, str)
- if type(file) == "number" then
- files[file].lines[line] = str
- local str = ""
- for i = 1, #files[file].lines do
- str = str..files[file].lines[i].."\n"
- end
- files[file].text = str
- elseif type(file) == "string" then
- for k, v in pairs(files) do
- if v.name == file then
- files[k].lines[line] = str
- local str = ""
- for i = 1, #files[k].lines do
- str = str..files[k].lines[i].."\n"
- end
- files[k].text = str
- break
- end
- end
- end
- end;
- create = function(file)
- if type(file) == "number" then
- local f = io.open(files[file].path..files[file].name, 'w')
- f:write(files[file].text)
- f:close()
- elseif type(file) == "string" then
- for k, v in pairs(files) do
- if v.name == file then
- local f = io.open(files[k].path..files[k].name, 'w')
- f:write(files[k].text)
- f:close()
- break
- end
- end
- end
- end
- }
Advertisement
Add Comment
Please, Sign In to add comment