var DEBUG = false; function str_tab() { this.response = null; this.tab_to_str = function(TAB) { return this._tab_to_str("", TAB); } this._tab_to_str = function(pre, TAB) { var content = ""; var HAS_VALUES = false; for(var key in TAB) { HAS_VALUES = true; if(typeof(TAB[key]) == "object") content += this._tab_to_str(pre + key.length + ":" + key, TAB[key]); else content += pre + key.length + ":" + key + ("" + TAB[key]).length + ":" + TAB[key] + "\n"; } if(HAS_VALUES) return content; return pre + "a:\n"; } this.str_to_tab = function(content) { var TAB = {}; var n = 0; while(n < content.length) { var LINE_FOUND = false; var words = new Array(); while(n < content.length && !LINE_FOUND) { var length = ""; var word = ""; var ENDS_WITH_EMPTY_TAB = false; while(n < content.length && content.charAt(n) != ':') { length += content.charAt(n); n++; } n++; if(length == "a") ENDS_WITH_EMPTY_TAB = true; else { for(var k = 0; k < parseInt(length); k++) { word += content.charAt(n); n++; } words.push(word); } LINE_FOUND = ENDS_WITH_EMPTY_TAB || n == content.length || content.charAt(n) == '\n'; } n++; if(words.length > 0) TAB = this.affect(TAB, words, ENDS_WITH_EMPTY_TAB); } return TAB; } this.affect = function(TAB, words, ENDS_WITH_EMPTY_TAB) { if(words.length > (ENDS_WITH_EMPTY_TAB ? 1 : 2)) { if(typeof(TAB[words[0]]) != "object") TAB[words[0]] = {}; var _words = new Array(); var FIRST = true; for(var k = 0; k < words.length; k++) { if(FIRST) FIRST = false; else _words.push(words[k]); } TAB[words[0]] = this.affect(TAB[words[0]], _words, ENDS_WITH_EMPTY_TAB); } else TAB[words[0]] = (ENDS_WITH_EMPTY_TAB ? {} : words[1]); return TAB; } this.str_to_hex = function(str) { var hex = ""; for(var n = 0; n < str.length; n++) { var hex_char = str.charCodeAt(n).toString(16); if(hex_char.length == 1) hex_char = "0" + hex_char; hex += hex_char; } return hex; } this.hex_to_str = function(hex) { var n = 0; var str = ""; while(n + 2 <= hex.length) { str += unescape("%" + hex.substr(n, 2)); n += 2; } return str; } this.request = function(file, req_tab, callback, str_tab_inst) { if(window.XMLHttpRequest && !(window.ActiveXObject)) { try { req = new XMLHttpRequest(); } catch(e) { req = false; } } else if(window.ActiveXObject) { try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { req = false; } } } if(req) { req.open("POST", file, true); req.onreadystatechange = function() { if(req.readyState == 4) { if(DEBUG) alert(str_tab_inst.hex_to_str(req.responseText)); str_tab_inst.response = str_tab_inst.str_to_tab(str_tab_inst.hex_to_str(req.responseText)); if(typeof(str_tab_inst.response["erreur"]) != "undefined") alert(str_tab_inst.response["erreur"]); eval(callback); } } req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); req.send("post_request=" + this.str_to_hex(this.tab_to_str(req_tab))); } } } var str_tab = new str_tab();