Software /
code /
prosody
Annotate
tools/serialize.lua @ 521:436e4617c858
Fix license (left some boilerplate in)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 03 Dec 2008 14:59:27 +0000 |
parent | 519:cccd610a0ef9 |
rev | line source |
---|---|
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
1 -- Prosody IM v0.1 |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
2 -- Copyright (C) 2008 Matthew Wild |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
3 -- Copyright (C) 2008 Waqas Hussain |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
4 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
5 -- This program is free software; you can redistribute it and/or |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
6 -- modify it under the terms of the GNU General Public License |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
7 -- as published by the Free Software Foundation; either version 2 |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
8 -- of the License, or (at your option) any later version. |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
9 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
10 -- This program is distributed in the hope that it will be useful, |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
13 -- GNU General Public License for more details. |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
14 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
15 -- You should have received a copy of the GNU General Public License |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
16 -- along with this program; if not, write to the Free Software |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
18 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
19 |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
482
diff
changeset
|
20 |
482
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 local indent = function(i) |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 return string.rep("\t", i); |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 end |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 local function basicSerialize (o) |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 if type(o) == "number" or type(o) == "boolean" then |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 return tostring(o); |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 else -- assume it is a string -- FIXME make sure it's a string. throw an error otherwise. |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 return (string.format("%q", tostring(o)):gsub("\\\n", "\\n")); |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 end |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 end |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 local function _simplesave (o, ind, t) |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 if type(o) == "number" then |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 table.insert(t, tostring(o)); |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 elseif type(o) == "string" then |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 table.insert(t, (string.format("%q", o):gsub("\\\n", "\\n"))); |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 elseif type(o) == "table" then |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 table.insert(t, "{\n"); |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 for k,v in pairs(o) do |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 table.insert(t, indent(ind)); |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 table.insert(t, "["); |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 table.insert(t, basicSerialize(k)); |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 table.insert(t, "] = "); |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 _simplesave(v, ind+1, t); |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 table.insert(t, ",\n"); |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 end |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 table.insert(t, indent(ind-1)); |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 table.insert(t, "}"); |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 elseif type(o) == "boolean" then |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 table.insert(t, (o and "true" or "false")); |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 else |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 error("cannot serialize a " .. type(o)) |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 end |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 end |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 local t_concat = table.concat; |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 module "serialize" |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 function serialize(o) |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 local t = {}; |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 _simplesave(o, 1, t); |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 return t_concat(t); |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 end |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 |
b86082df0bc0
ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 return _M; |