[RELEASE]Data_tool ( lua & i/o easy way )

09/07/2012 20:50 d3m0n3#1
hi all,

i will release a small but useful tool to manage the read / write file I / O system in lua commonly used in many scripts also creates a backup copy of the edited file (in write mode)


copy in questlib.lua:

Code:
DATA_READ = 0
DATA_WRITE = 1


function data_tool(DATA_MODE, DATA_FILE, DATA_LINE)
    local x = 1
    local file = ""
    if DATA_MODE == DATA_READ then
        file = io.open(DATA_FILE, "r")
        while true do
            local line = file:read("*l")
            if line == nil then 
                break 
            end
            text = string.gsub(line, "\n", "")
            if x == DATA_LINE then 
                io.close(file)
                return text
            end
            x = x + 1
        end
        io.close(file)
    elseif DATA_MODE == DATA_WRITE then
        os.rename(DATA_FILE, DATA_FILE..".BAK")
        file = io.open(DATA_FILE, "w")
        file:write(DATA_LINE)
        io.close(file)
        os.execute("chmod 777 "..DATA_FILE)
    end
end
how to use it:

first be copied to questlib.lua and we should be all right here ... xD

VARIABLES:

DATA_MODE = indicates how to act on the file (DATA_READ / 0 = read, DATA_WRITE / 1 = write)

Data_file = specifies the file on which to act (absolute path)

DATA_LINE = in read mode indicates the number of the line to be read, in write mode the text to write.

as use read / write:
as an example we use the file: file.txt located in the path / home / game /

Code:
read example:

local data = "/home/game/file.txt"
local linea = 3
local result = data_tool(DATA_READ, data, linea)

write example:

local data = "/home/game/file.txt"
local linea = "ciao"
data_tool(DATA_WRITE, data, linea)
In the first case we read from file.txt line number 3
in the second case we wrote in file.txt ciao

I hope I was clear enough and useful

P.S.: if you want you can add the flag to increase the functionality, but this is up to you ...

enjoy

d3m0n3
09/08/2012 00:59 --DarkScorp--#2
No One comment? I Think it's so usefull to control I/O Lua in a better way. Now i use this system! Is So GOOD
09/08/2012 02:38 Red Firestar#3
Nice *-*


nicht vergessen auf den Spoiler drücken schadet nicht :)
09/08/2012 11:49 Laben#4
thanks a lot
09/13/2012 17:30 miguelmig#5
What about a DATA_READ_ALL and a DATA_APPEND format?
I think it would be a good idea.
09/14/2012 17:40 d3m0n3#6
Quote:
Originally Posted by miguelmig View Post
What about a DATA_READ_ALL and a DATA_APPEND format?
I think it would be a good idea.
as I wrote in the first post you can add an input variable to handle the flag and expand the full functionality of the tool of course with the addition of other flags should be made ​​more checks on operations