From 7e671ae37f6c951d4db18c446eed52685200b2e0 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Thu, 11 Sep 2008 16:47:45 +0200 Subject: rename swat => swat2, so that we don't conflict with samba3 metze (This used to be commit bf852a3dbeee8900fa36978f08a5cdceabcbd926) --- swat2/scripting/client/encoder.js | 84 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 swat2/scripting/client/encoder.js (limited to 'swat2/scripting/client/encoder.js') diff --git a/swat2/scripting/client/encoder.js b/swat2/scripting/client/encoder.js new file mode 100644 index 0000000000..2a4d174b0e --- /dev/null +++ b/swat2/scripting/client/encoder.js @@ -0,0 +1,84 @@ +/* + client side js functions for encoding/decoding objects into linear strings + + Copyright Andrew Tridgell 2005 + released under the GNU GPL Version 3 or later +*/ +/* + usage: + + enc = encodeObject(obj); + obj = decodeObject(enc); + + The encoded format of the object is a string that is safe to + use in URLs + + Note that only data elements are encoded, not functions +*/ + +function count_members(o) { + var i, count = 0; + for (i in o) { + count++; + } + return count; +} + +function encodeObject(o) { + var i, r = count_members(o) + ":"; + for (i in o) { + var t = typeof(o[i]); + if (t == 'object') { + r = r + "" + i + ":" + t + ":" + encodeObject(o[i]); + } else if (t == 'string') { + var s = encodeURIComponent(o[i]).replace(/%/g,'#'); + r = r + "" + i + ":" + t + ":" + s + ":"; + } else if (t == 'boolean' || t == 'number') { + r = r + "" + i + ":" + t + ":" + o[i] + ":"; + } else if (t == 'undefined' || t == 'null') { + r = r + "" + i + ":" + t + ":"; + } else if (t != 'function') { + alert("Unable to encode type " + t); + } + } + return r; +} + +function decodeObjectArray(a) { + var o = new Object(); + var i, count = a[a.i]; a.i++; + for (i=0;i