update API and path checking logic

This commit is contained in:
octacian 2016-12-02 11:24:08 -08:00
parent d96135fb6a
commit 0af7680f8e
2 changed files with 20 additions and 14 deletions

23
API.md
View File

@ -25,43 +25,48 @@ Checks the file specified through `path` returning true if it exists, and false
### mkdir
**Usage:** `datalib.mkdir(path)`
Make a directory at the specified `path`.
Make a directory at the specified `path`. Returns `true` if directory already exists.
### rmdir
**Usage:** `datalib.rmdir(path)`
Recursively delete all contents of a directory (files, sub-directories, etc...)
Recursively delete all contents of a directory (files, sub-directories, etc...). Returns `false` if directory does not exist.
### create
**Usage:** `datalib.create(path)`
Create file in location specified by `path`. Returns true if already exists, nothing if creation is successful, also printing like output to the log.
Create file in location specified by `path`. Returns `true` if already exists, nothing if creation is successful.
### write
**Usage:** `datalib.write(path, data, serialize)`
Write data to file. File path is specified in `path`. Data to write is specified with field `data` and supports any value including strings, booleans, and integers. The `serialize` option tells datalib whether or not to run `minetest.serialize` on the data before writing, and is by default set to `true` if left blank.
Write data to file. File path is specified in `path`. Data to write is specified with field `data` and supports any value including strings, booleans, and integers. The `serialize` option tells datalib whether or not to run `minetest.serialize` on the data before writing, and is by default set to `false` if left blank.
### append
**Usage:** `datalib.append(path, data, serialize)`
Append data to file. File path is specified in `path`. Data to append is specified with field `data` and supports any value including strings, booleans, and integers. The `serialize` option tells datalib whether or not to run `minetest.serialize` on the data before writing, and is by default set to `false` if left blank.
### read
**Usage:** `datalib.read(path, deserialize)`
Load data from file (`path`) and return through variable `data`. The `deserialize` option tells datalib whether or not to run `minetest.deserialize` on the data before returning, and is by default set to `true` if left blank.
Load data from file (`path`) and return through variable `data`. The `deserialize` option tells datalib whether or not to run `minetest.deserialize` on the data before returning, and is by default set to `true` if left blank. Returns `false` if file does not exist.
### copy
**Usage:** `datalib.copy(path, new)`
**Shortcut:** `datalib.cp(path, new)`
Copy the contents of a file to another file. `path` indicates the location of the original, while `new` indicates the path where the copy will be placed.
Copy the contents of a file to another file. `path` indicates the location of the original, while `new` indicates the path where the copy will be placed. Returns false if file specified by `path` does not exist.
### table.write
**Usage:** `datalib.table.write(path, table)`
Write table to file. File path is specified in `path`. Table to write is specified with field `table` and is designed for use with tables. The only difference between `write_table` and `write_file` is that `write_table` always serializes the data with `minetest.serialize` before writing because tables cannot be directly written to files, however, they must first be converted to strings through serialization.
### table.load
**Usage:** `datalib.table.load(path)`
### table.read
**Usage:** `datalib.table.read(path)`
Load table from file (`path`) and return through variable `table`. The only difference between `load_table` and `load_file` is that `load_table` always deserializes the data with `minetest.deserialize` to convert previously written string back to a table.
Load table from file (`path`) and return through variable `table`. The only difference between `load_table` and `load_file` is that `load_table` always deserializes the data with `minetest.deserialize` to convert previously written string back to a table. Returns `false` if file specified by `path` does not exist.
### dofile
**Usage:** `datalib.dofile(path)`

View File

@ -54,12 +54,16 @@ function datalib.mkdir(path)
os.execute('mkdir "'..path..'"') -- create directory with os mkdir command
return
end
else
end
f:close() -- close file
return true
end
-- remove directory
function datalib.rmdir(path)
function datalib.rmdir(path, log)
if not io.open(path) then return false end -- file doesn't exist
-- [local function] remove files
local function rm_files(ppath, files)
for _, f in ipairs(files) do
@ -98,7 +102,6 @@ end
-- write to file
function datalib.write(path, data, serialize, log)
if datalib.exists(path) ~= true then return false end -- check if exists
if not serialize then local serialize = false end -- if blank serialize = true
local f = io.open(path, "w") -- open file for writing
if serialize == true then local data = minetest.serialize(data) end -- serialize data
@ -109,7 +112,6 @@ end
-- append to file
function datalib.append(path, data, serialize, log)
if datalib.exists(path) ~= true then return false end -- check if exists
if not serialize then local serialize = false end -- if blank serialize = true
local f = io.open(path, "a") -- open file for writing
if serialize == true then local data = minetest.serialize(data) end -- serialize data
@ -131,7 +133,7 @@ end
function datalib.copy(path, new, log)
if not datalib.exists(path) then
datalib.log(path.." does not exist. Cannot copy file.")
return path.." does not exist."
return false
end -- check if path exists
local old = datalib.read(path, false) -- read
datalib.create(new, false) -- create new
@ -143,7 +145,6 @@ end
-- write table to file
function datalib.table.write(path, intable, log)
if datalib.exists(path) ~= true then return false end -- check if exists
local intable = minetest.serialize(intable) -- serialize intable
local f = io.open(path, "w") -- open file for writing
f:write(intable) -- write intable