1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
// import Array;
// The magic below is for the 'install' program
#ifndef roxenp
#if !efun(roxenp)
#define roxenp this_object()
#endif
#endif
#ifndef IN_INSTALL
// string cvs_version = "$Id: newdecode.pike,v 1.7 1998/02/10 18:36:07 per Exp $";
#endif
#include <roxen.h>
void parse(string s, mapping mr);
void new_parse(string s, mapping mr);
private string decode_int(string foo, mapping m, string s, mapping res)
{
if(arrayp(res->res)) res->res += ({ (int)s }); else res->res = (int)s;
return "";
}
private string decode_module(string foo, mapping m, string s, mapping res)
{
if(arrayp(res->res))
res->res += ({ s });
else
res->res = s;
return "";
}
private string decode_float(string foo, mapping m, string s, mapping res)
{
if(arrayp(res->res)) res->res += ({ (float)s }); else res->res = (float)s;
return "";
}
private string decode_string(string foo, mapping m, string s, mapping res)
{
s = replace(s, ({ "%3e", "%3c" }), ({ ">", "<" }) );
if(arrayp(res->res)) res->res += ({ s }); else res->res = s;
return "";
}
private string decode_list(string foo, mapping m, string s, mapping res)
{
mapping myres = ([ "res":({}) ]);
parse(s, myres);
if(arrayp(res->res))
res->res += ({ aggregate_multiset(@myres->res) });
else
res->res = aggregate_multiset(@myres->res);
return "";
}
private string new_decode_list(string foo, mapping m, string s, mapping res)
{
mapping myres = ([ "res":({}) ]);
new_parse(s, myres);
if(arrayp(res->res))
res->res += ({ aggregate_multiset(@myres->res) });
else
res->res = aggregate_multiset(@myres->res);
return "";
}
private string decode_array(string foo, mapping m, string s, mapping res)
{
mapping myres = ([ "res":({}) ]);
parse(s, myres);
if(arrayp(res->res))
res->res += ({ myres->res });
else
res->res = myres->res;
return "";
}
private string new_decode_array(string foo, mapping m, string s, mapping res)
{
mapping myres = ([ "res":({}) ]);
new_parse(s, myres);
if(arrayp(res->res))
res->res += ({ myres->res });
else
res->res = myres->res;
return "";
}
private string new_decode_mapping(string foo, mapping m, string s, mapping res)
{
mapping myres = ([ "res":({ }) ]);
new_parse(s, myres);
if(arrayp(res->res))
res->res += ({ aggregate_mapping(@myres->res) });
else
res->res = aggregate_mapping(@myres->res);
return "";
}
private string decode_mapping(string foo, mapping m, string s, mapping res)
{
mapping myres = ([ "res":({ }) ]);
parse(s, myres);
if(arrayp(res->res))
res->res += ({ aggregate_mapping(@myres->res) });
else
res->res = aggregate_mapping(@myres->res);
return "";
}
private string decode_variable(string foo, mapping m, string s, mapping res)
{
mapping mr;
mr = ([ "res":0 ]);
parse(s, mr);
res[m->name] = mr->res;
return "";
}
private string new_decode_variable(string foo, mapping m, string s,
mapping res)
{
mapping mr;
mr = ([ "res":0 ]);
new_parse(s, mr);
res[m->name] = mr->res;
return "";
}
string name_of_module( object m )
{
#ifndef IN_INSTALL
string name;
mapping mod;
foreach(values(roxenp()->current_configuration->modules), mod)
{
if(mod->copies)
{
int i;
if(!zero_type(i=search(mod->copies, m)))
return mod->sname+"#"+i;
} else
if(mod->enabled==m)
return mod->sname+"#0";
}
return name;
#endif
}
void parse(string s, mapping mr)
{
parse_html(s, ([ ]),
(["array":decode_array,
"mapping":decode_mapping,
"list":decode_list,
"module":decode_module,
"int":decode_int,
"string":decode_string,
"float":decode_float ]), mr);
}
void new_parse(string s, mapping mr)
{
parse_html(s, ([ ]),
(["a":new_decode_array,
"map":new_decode_mapping,
"lst":new_decode_list,
"mod":decode_module,
"int":decode_int,
"str":decode_string,
"flt":decode_float ]), mr);
}
string decode_config_region(string foo, mapping mr, string s, mapping res2)
{
mapping res = ([ ]);
parse_html(s, ([]), ([ "variable":decode_variable ]), res);
res2[mr->name] = res;
return "";
}
string new_decode_config_region(string foo, mapping mr, string s, mapping res2)
{
mapping res = ([ ]);
parse_html(s, ([]), ([ "var":new_decode_variable ]), res);
res2[mr->name] = res;
return "";
}
mixed compat_decode_value( string val )
{
if(!val || !strlen(val)) return 0;
switch(val[0])
{
case '"':
return replace(val[1 .. strlen(val)-2], "%0A", "\n");
case '{':
return Array.map(val[1 .. strlen(val)-2]/"},{", compat_decode_value);
case '<':
return aggregate_multiset(Array.map(val[1 .. strlen(val)-2]/"},{", compat_decode_value));
default:
if(search(val,".") != -1)
return (float)val;
return (int)val;
}
}
private mapping compat_parse(string s)
{
mapping res = ([ ]);
string current;
foreach(s/"\n", s)
{
if(strlen(s))
{
switch(s[0])
{
case ';':
continue;
case '[':
sscanf(s, "[%s]", current);
res[ current ] = ([ ]);
break;
default:
string a, b;
sscanf(s, "%s=%s", a, b);
res[current][ a ] = compat_decode_value(b);
}
}
}
return res;
}
mapping decode_config_file(string s)
{
// werror(sprintf("Decoding \n%s\n",s));
mapping res = ([ ]);
if(!sizeof(s)) return res; // Empty file..
switch(s[0])
{
case ';':
// Old (and stupid...) configuration file format
perror("Reading very old (pre b11) configuration file format.\n");
return compat_parse(s);
break;
case '4': // Pre b15 configuration format. Could encode most stuff, but not
// everything.
perror("Reading old (pre b15) configuration file format.\n");
parse_html(s, ([]), ([ "region":decode_config_region ]), res);
return res;
case '5': // New (binary) format. Fast and lean, but not very readable
// for a human.. :-)
return decode_value(s[1..]); // C-function.
case '6': // Newer ((somewhat)readable) format. Can encode everything, _and_
// a mere human can edit it.
// trace(1);
parse_html(s, ([]), ([ "region":new_decode_config_region ]), res);
// trace(0);
// werror(sprintf("Decoded value is: %O\n", res));
return res;
}
}
#if 1
private string encode_mixed(mixed from)
{
if(stringp(from))
return "<str>"+replace(from, ({ ">", "<" }), ({ "%3e", "%3c" }) )
+ "</str>";
else if(intp(from))
return "<int>"+from+"</int>";
else if(floatp(from))
return "<flt>"+from+"</flt>";
else if(arrayp(from))
return "\n <a>\n "+Array.map(from, encode_mixed)*"\n "
+"\n </a>\n";
else if(multisetp(from))
return "\n <lst>\n "
+Array.map(indices(from),encode_mixed)*"\n "+"\n </lst>\n";
else if(objectp(from)) // Only modules.
return "<mod>"+name_of_module(from)+"</mod>";
else if(mappingp(from))
{
string res="<map>";
mixed i;
foreach(indices(from), i)
res += " " + encode_mixed(i) + " : " + encode_mixed(from[i])+"\n";
return res + " </map>\n";
}
}
string encode_config_region(mapping m)
{
string res = "";
string v;
foreach(indices(m), v)
res += " <var name="+sprintf("%-22s", "'"+v+"'>")+encode_mixed(m[v])
+" </var>\n";
return res;
}
string encode_regions(mapping r)
{
string v;
string res = "6 <- Do not remove this number! "
"Roxen Challenger save file format>\n\n";
foreach(indices(r), v)
res += "<region name='"+v+"'>\n" + encode_config_region(r[v])
+ "</region>\n\n";
return res;
}
#else
string encode_regions(mapping r)
{
mapping mr = copy_value(r);
string i, j;
foreach(indices(mr), i)
foreach(indices(mr[i]), j)
if(objectp(mr[i][j]))
mr[i][j] = name_of_module( mr[i][j] );
return "5"+encode_value(mr);
}
#endif
|