From 4d1d3ce7dee30640d451aecb8c4803f90ee41bb2 Mon Sep 17 00:00:00 2001 From: Luca S Date: Tue, 9 Feb 2016 20:33:01 +0100 Subject: [PATCH 1/4] Create Config.md --- File Formats/Documents/Config.md | 66 ++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 File Formats/Documents/Config.md diff --git a/File Formats/Documents/Config.md b/File Formats/Documents/Config.md new file mode 100644 index 0000000..6430208 --- /dev/null +++ b/File Formats/Documents/Config.md @@ -0,0 +1,66 @@ +# *COS 1:* Configuration File + +## Quick information + +| Information | | +| ----------- | ------------------------- | +| Type | Plain text file format | +| MIME | `text/config` | +| Extensions | `.cfg` or `.config` | + +## Technical Details +This format is based upon plain text and as such technical details that apply to plain text also apply to this format. + +##Config data + +Config data is saved in the following way: + +``` +Key1: Value1 +Key2: Value2 +``` + +Values are saved in the normal Lua way(Strings are enclosed with "", tables are enclosed with {},...) + +Every entry has to have it's own line, the following is __wrong__: +``` +Key1:{Hello="World", +World="Hello"} +``` +Correct way: +``` +Key1:{Hello="World",World="Hello"} +``` +##API to Convert config format to table and vice versa: +http://pastebin.com/FrVe9hF3 +##Code for making a config from a table: +``` +if type("config") ~= "string" then + return nil,"Config must be a string" +end +local lines = {} +for line in (config.."\n"):gmatch("([^\n]*)\n") do + table.insert(lines,line) +end +local result = {} +for i,v in pairs(lines) do + local t = {} + for a in (v..":"):gmatch("([^:]*):") do + table.insert(t,a) + end + if #t ~= 2 then + return nil,"Line "..i.." is invalid!" + else + result[t[1]] = loadstring("return "..t[2])() + end +end +return result +``` +##Code for making a table from a config: +``` +local res = "" +for i,v in pairs(t) do + res = res..i..":"..v.."\n" +end +return res:sub(1,#res-1) +``` From 7fff796ae488df573cf46bdd3d1d6479c989cc1b Mon Sep 17 00:00:00 2001 From: Luca0208 Date: Tue, 9 Feb 2016 21:08:59 +0100 Subject: [PATCH 2/4] fixe the errors ardera pointed out. --- File Formats/Documents/Config.md | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/File Formats/Documents/Config.md b/File Formats/Documents/Config.md index 6430208..b92696d 100644 --- a/File Formats/Documents/Config.md +++ b/File Formats/Documents/Config.md @@ -33,7 +33,7 @@ Key1:{Hello="World",World="Hello"} ``` ##API to Convert config format to table and vice versa: http://pastebin.com/FrVe9hF3 -##Code for making a config from a table: +##Code for making a table from a config: ``` if type("config") ~= "string" then return nil,"Config must be a string" @@ -56,11 +56,23 @@ for i,v in pairs(lines) do end return result ``` -##Code for making a table from a config: +##Code for making a config from a table: ``` -local res = "" -for i,v in pairs(t) do - res = res..i..":"..v.."\n" +local s = "" +local tk, tv +for k, v in pairs(configTable) do + tk, tv = type(k), type(v) + if tk == "string" or tk == "number" then + if tv ~= "string" and tv ~= "nubmer" and tv ~= "boolean" and tv ~= "table" then + error("Invalid type for value: Expected string, number, boolean, table; got "..tv) + end + if tv == "table" then + tv = tostring(textutils.serialize(tv)):gsub("\n", "") + end + s = s .. tk .. ": " ..tostring(tv) .. "\n" + else + error("Invalid type for key: Expected string, number; got "..tk) + end end -return res:sub(1,#res-1) +return s ``` From e9429f9b5f33a922a859a7408d4469f4766bcbb2 Mon Sep 17 00:00:00 2001 From: Luca0208 Date: Tue, 9 Feb 2016 21:14:34 +0100 Subject: [PATCH 3/4] Updated pastebin code Nearly forgot about that --- File Formats/Documents/Config.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/File Formats/Documents/Config.md b/File Formats/Documents/Config.md index b92696d..6e1f069 100644 --- a/File Formats/Documents/Config.md +++ b/File Formats/Documents/Config.md @@ -32,7 +32,7 @@ Correct way: Key1:{Hello="World",World="Hello"} ``` ##API to Convert config format to table and vice versa: -http://pastebin.com/FrVe9hF3 +http://pastebin.com/1GGq3fyw ##Code for making a table from a config: ``` if type("config") ~= "string" then From 6f1e7bd1e453f3aa706d5a2a8c0877c29059f0f7 Mon Sep 17 00:00:00 2001 From: Luca S Date: Tue, 9 Feb 2016 21:40:06 +0100 Subject: [PATCH 4/4] Fixed code markdown --- File Formats/Documents/Config.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/File Formats/Documents/Config.md b/File Formats/Documents/Config.md index 6e1f069..05003c2 100644 --- a/File Formats/Documents/Config.md +++ b/File Formats/Documents/Config.md @@ -34,7 +34,7 @@ Key1:{Hello="World",World="Hello"} ##API to Convert config format to table and vice versa: http://pastebin.com/1GGq3fyw ##Code for making a table from a config: -``` +```lua if type("config") ~= "string" then return nil,"Config must be a string" end @@ -57,7 +57,7 @@ end return result ``` ##Code for making a config from a table: -``` +```lua local s = "" local tk, tv for k, v in pairs(configTable) do