1- --[[
2- Copyright (c) 2012 Carreras Nicolas
3-
4- Permission is hereby granted, free of charge, to any person obtaining a copy
5- of this software and associated documentation files (the "Software"), to deal
6- in the Software without restriction, including without limitation the rights
7- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8- copies of the Software, and to permit persons to whom the Software is
9- furnished to do so, subject to the following conditions:
10-
11- The above copyright notice and this permission notice shall be included in all
12- copies or substantial portions of the Software.
13-
14- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20- SOFTWARE.
1+ --[[
2+ Copyright (c) 2012 Carreras Nicolas
3+
4+ Permission is hereby granted, free of charge, to any person obtaining a copy
5+ of this software and associated documentation files (the "Software"), to deal
6+ in the Software without restriction, including without limitation the rights
7+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+ copies of the Software, and to permit persons to whom the Software is
9+ furnished to do so, subject to the following conditions:
10+
11+ The above copyright notice and this permission notice shall be included in all
12+ copies or substantial portions of the Software.
13+
14+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+ SOFTWARE.
2121--]]
2222--- Lua INI Parser.
2323-- It has never been that simple to use INI files with Lua.
@@ -29,52 +29,52 @@ local LIP = {};
2929-- @param fileName The name of the INI file to parse. [string]
3030-- @return The table containing all data from the INI file. [table]
3131function LIP .load (fileName )
32- assert (type (fileName ) == ' string' , ' Parameter "fileName" must be a string.' );
33- local file = assert (io.open (fileName , ' r' ), ' Error loading file : ' .. fileName );
34- local data = {};
35- local section ;
36- for line in file :lines () do
37- local tempSection = line :match (' ^%[([^%[%]]+)%]$' );
38- if (tempSection )then
39- section = tonumber (tempSection ) and tonumber (tempSection ) or tempSection ;
40- data [section ] = data [section ] or {};
41- end
42- local param , value = line :match (' ^([%w|_]+)%s-=%s-(.+)$' );
43- if (param and value ~= nil )then
44- if (tonumber (value ))then
45- value = tonumber (value );
46- elseif (value == ' true' )then
47- value = true ;
48- elseif (value == ' false' )then
49- value = false ;
50- end
51- if (tonumber (param ))then
52- param = tonumber (param );
53- end
54- data [section ][param ] = value ;
55- end
56- end
57- file :close ();
58- return data ;
32+ assert (type (fileName ) == ' string' , ' Parameter "fileName" must be a string.' );
33+ local file = assert (io.open (fileName , ' r' ), ' Error loading file : ' .. fileName );
34+ local data = {};
35+ local section ;
36+ for line in file :lines () do
37+ local tempSection = line :match (' ^%[([^%[%]]+)%]$' );
38+ if (tempSection )then
39+ section = tonumber (tempSection ) and tonumber (tempSection ) or tempSection ;
40+ data [section ] = data [section ] or {};
41+ end
42+ local param , value = line :match (' ^([%w|_]+)%s-=%s-(.+)$' );
43+ if (param and value ~= nil )then
44+ if (tonumber (value ))then
45+ value = tonumber (value );
46+ elseif (value == ' true' )then
47+ value = true ;
48+ elseif (value == ' false' )then
49+ value = false ;
50+ end
51+ if (tonumber (param ))then
52+ param = tonumber (param );
53+ end
54+ data [section ][param ] = value ;
55+ end
56+ end
57+ file :close ();
58+ return data ;
5959end
6060
6161--- Saves all the data from a table to an INI file.
6262-- @param fileName The name of the INI file to fill. [string]
6363-- @param data The table containing all the data to store. [table]
6464function LIP .save (fileName , data )
65- assert (type (fileName ) == ' string' , ' Parameter "fileName" must be a string.' );
66- assert (type (data ) == ' table' , ' Parameter "data" must be a table.' );
67- local file = assert (io.open (fileName , ' w+b' ), ' Error loading file :' .. fileName );
68- local contents = ' ' ;
69- for section , param in pairs (data ) do
70- contents = contents .. (' [%s]\n ' ):format (section );
71- for key , value in pairs (param ) do
72- contents = contents .. (' %s=%s\n ' ):format (key , tostring (value ));
73- end
74- contents = contents .. ' \n ' ;
75- end
76- file :write (contents );
77- file :close ();
65+ assert (type (fileName ) == ' string' , ' Parameter "fileName" must be a string.' );
66+ assert (type (data ) == ' table' , ' Parameter "data" must be a table.' );
67+ local file = assert (io.open (fileName , ' w+b' ), ' Error loading file :' .. fileName );
68+ local contents = ' ' ;
69+ for section , param in pairs (data ) do
70+ contents = contents .. (' [%s]\n ' ):format (section );
71+ for key , value in pairs (param ) do
72+ contents = contents .. (' %s=%s\n ' ):format (key , tostring (value ));
73+ end
74+ contents = contents .. ' \n ' ;
75+ end
76+ file :write (contents );
77+ file :close ();
7878end
7979
8080return LIP ;
0 commit comments