[6] | 1 | var DEBUG = false; |
---|
| 2 | |
---|
| 3 | function str_tab() |
---|
| 4 | { |
---|
| 5 | this.response = null; |
---|
| 6 | |
---|
| 7 | this.tab_to_str = function(TAB) |
---|
| 8 | { return this._tab_to_str("", TAB); |
---|
| 9 | } |
---|
| 10 | |
---|
| 11 | this._tab_to_str = function(pre, TAB) |
---|
| 12 | { var content = ""; |
---|
| 13 | var HAS_VALUES = false; |
---|
| 14 | for(var key in TAB) |
---|
| 15 | { HAS_VALUES = true; |
---|
| 16 | if(typeof(TAB[key]) == "object") content += this._tab_to_str(pre + key.length + ":" + key, TAB[key]); |
---|
| 17 | else content += pre + key.length + ":" + key + ("" + TAB[key]).length + ":" + TAB[key] + "\n"; |
---|
| 18 | } |
---|
| 19 | if(HAS_VALUES) return content; |
---|
| 20 | return pre + "a:\n"; |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | this.str_to_tab = function(content) |
---|
| 24 | { var TAB = {}; |
---|
| 25 | var n = 0; |
---|
| 26 | while(n < content.length) |
---|
| 27 | { var LINE_FOUND = false; |
---|
| 28 | var words = new Array(); |
---|
| 29 | while(n < content.length && !LINE_FOUND) |
---|
| 30 | { var length = ""; |
---|
| 31 | var word = ""; |
---|
| 32 | var ENDS_WITH_EMPTY_TAB = false; |
---|
| 33 | while(n < content.length && content.charAt(n) != ':') { length += content.charAt(n); n++; } n++; |
---|
| 34 | if(length == "a") ENDS_WITH_EMPTY_TAB = true; |
---|
| 35 | else |
---|
| 36 | { for(var k = 0; k < parseInt(length); k++) { word += content.charAt(n); n++; } |
---|
| 37 | words.push(word); |
---|
| 38 | } |
---|
| 39 | LINE_FOUND = ENDS_WITH_EMPTY_TAB || n == content.length || content.charAt(n) == '\n'; |
---|
| 40 | } |
---|
| 41 | n++; |
---|
| 42 | if(words.length > 0) TAB = this.affect(TAB, words, ENDS_WITH_EMPTY_TAB); |
---|
| 43 | } |
---|
| 44 | return TAB; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | this.affect = function(TAB, words, ENDS_WITH_EMPTY_TAB) |
---|
| 48 | { if(words.length > (ENDS_WITH_EMPTY_TAB ? 1 : 2)) |
---|
| 49 | { if(typeof(TAB[words[0]]) != "object") TAB[words[0]] = {}; |
---|
| 50 | var _words = new Array(); |
---|
| 51 | var FIRST = true; |
---|
| 52 | for(var k = 0; k < words.length; k++) |
---|
| 53 | { if(FIRST) FIRST = false; |
---|
| 54 | else _words.push(words[k]); |
---|
| 55 | } |
---|
| 56 | TAB[words[0]] = this.affect(TAB[words[0]], _words, ENDS_WITH_EMPTY_TAB); |
---|
| 57 | } |
---|
| 58 | else TAB[words[0]] = (ENDS_WITH_EMPTY_TAB ? {} : words[1]); |
---|
| 59 | return TAB; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | this.str_to_hex = function(str) |
---|
| 63 | { var hex = ""; |
---|
| 64 | for(var n = 0; n < str.length; n++) |
---|
| 65 | { var hex_char = str.charCodeAt(n).toString(16); |
---|
| 66 | if(hex_char.length == 1) hex_char = "0" + hex_char; |
---|
| 67 | hex += hex_char; |
---|
| 68 | } |
---|
| 69 | return hex; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | this.hex_to_str = function(hex) |
---|
| 73 | { var n = 0; |
---|
| 74 | var str = ""; |
---|
| 75 | while(n + 2 <= hex.length) { str += unescape("%" + hex.substr(n, 2)); n += 2; } |
---|
| 76 | return str; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | this.request = function(file, req_tab, callback, str_tab_inst) |
---|
| 80 | { if(window.XMLHttpRequest && !(window.ActiveXObject)) |
---|
| 81 | { try { req = new XMLHttpRequest(); } |
---|
| 82 | catch(e) { req = false; } |
---|
| 83 | } |
---|
| 84 | else if(window.ActiveXObject) |
---|
| 85 | { try { req = new ActiveXObject("Msxml2.XMLHTTP"); } |
---|
| 86 | catch(e) |
---|
| 87 | { try { req = new ActiveXObject("Microsoft.XMLHTTP"); } |
---|
| 88 | catch(e) { req = false; } |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | if(req) |
---|
| 92 | { req.open("POST", file, true); |
---|
| 93 | req.onreadystatechange = function() |
---|
| 94 | { if(req.readyState == 4) |
---|
| 95 | { if(DEBUG) alert(str_tab_inst.hex_to_str(req.responseText)); |
---|
| 96 | str_tab_inst.response = str_tab_inst.str_to_tab(str_tab_inst.hex_to_str(req.responseText)); |
---|
| 97 | if(typeof(str_tab_inst.response["erreur"]) != "undefined") alert(str_tab_inst.response["erreur"]); |
---|
| 98 | eval(callback); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); |
---|
| 102 | req.send("post_request=" + this.str_to_hex(this.tab_to_str(req_tab))); |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | var str_tab = new str_tab(); |
---|