@prop $hash_utils."help_msg" {} rc
;;$hash_utils.("help_msg") = {"Utility object for Aloha Web Server"}
@prop $hash_utils."version" "1073547852" rc
;;$hash_utils.("aliases") = {"hash_utils"}
@verb $hash_utils:"add put" this none this
@program $hash_utils:add
"-- receives: 3 arguments --";
"-- LIST, STR1, STR2 --";
"-- creates {STR1, STR2} and appends it to LIST --";
"-- sends back LIST --";
"-- example: --";
"-- receives --";
"-- {{{\"var1\", \"1\"}, {\"var2\", \"2\"}}, \"var3\", \"3\"} --";
"-- and sends back --";
"-- {{\"var1\", \"1\"}, {\"var2\", \"2\"}, {\"var3\", \"3\"}} --";
if (length(args) != 3)
return E_ARGS;
endif
if (typeof(args[1]) != LIST)
return E_INVARG;
endif
if (typeof(args[2]) != STR)
return E_INVARG;
endif
if (typeof(args[3]) != STR)
return E_INVARG;
endif
hash = args[1];
key = args[2];
value = args[3];
index = this:haskey(hash, key);
if (index)
hash[index][2] = value;
else
hash = {@hash, {key, value}};
endif
return hash;
.
@verb $hash_utils:"parse_query" this none this
@program $hash_utils:parse_query
"-- specifically to parse stuff from GET method --";
"-- receives a STR and expect the folling syntax --";
"-- \"var=value\" OR \"var1=value1&var2=value2\" --";
"-- sends back: --";
"-- {{\"var1\", \"value1\"}, {\"var2\", \"value2\"}} --";
if (length(args) != 1)
return E_ARGS;
endif
if (typeof(args[1]) != STR)
return E_INVARG;
endif
query = args[1];
hash = {};
if (index(query, "&"))
pairs = $string_utils:explode(query, "&");
else
pairs = {query};
endif
pair = {};
for i in [1..length(pairs)]
if (!index(pairs[i], "="))
continue;
endif
pair = $string_utils:explode(pairs[i], "=");
if (length(pair) == 2)
pair[1] = this:url_decode(pair[1]);
pair[2] = this:url_decode(pair[2]);
else
pair[1] = this:url_decode(pair[1]);
pair = {@pair, ""};
endif
hash = {@hash, pair};
endfor
return hash;
.
@verb $hash_utils:"url_decode" this none this
@program $hash_utils:url_decode
"-- As it name suggests --";
"-- house cleaning stuff from guys coming from browsers --";
if (length(args) != 1)
return E_ARGS;
endif
if (typeof(args[1]) != STR)
return E_INVARG;
endif
url_encoded = args[1];
url_encoded = strsub(url_encoded, "+", " ");
url_encoded = strsub(url_encoded, "%20", " ");
url_encoded = strsub(url_encoded, "%21", "!");
url_encoded = strsub(url_encoded, "%22", "\"");
url_encoded = strsub(url_encoded, "%23", "#");
url_encoded = strsub(url_encoded, "%24", "$");
url_encoded = strsub(url_encoded, "%26", "&");
url_encoded = strsub(url_encoded, "%27", "'");
url_encoded = strsub(url_encoded, "%28", "(");
url_encoded = strsub(url_encoded, "%29", ")");
url_encoded = strsub(url_encoded, "%2C", ",");
url_encoded = strsub(url_encoded, "%2F", "/");
url_encoded = strsub(url_encoded, "%3A", ":");
url_encoded = strsub(url_encoded, "%3B", ";");
url_encoded = strsub(url_encoded, "%3C", "<");
url_encoded = strsub(url_encoded, "%3D", "=");
url_encoded = strsub(url_encoded, "%3E", ">");
url_encoded = strsub(url_encoded, "%3F", "?");
url_encoded = strsub(url_encoded, "%40", "@");
url_encoded = strsub(url_encoded, "%5B", "[");
url_encoded = strsub(url_encoded, "%5C", "\\");
url_encoded = strsub(url_encoded, "%5D", "]");
url_encoded = strsub(url_encoded, "%5E", "^");
url_encoded = strsub(url_encoded, "%60", "`");
url_encoded = strsub(url_encoded, "%7B", "{");
url_encoded = strsub(url_encoded, "%7C", "|");
url_encoded = strsub(url_encoded, "%7D", "}");
url_encoded = strsub(url_encoded, "%7E", "~");
url_encoded = strsub(url_encoded, "%25", "%");
url_encoded = strsub(url_encoded, "%2B", "+");
return url_encoded;
.
@verb $hash_utils:"haskey" this none this
@program $hash_utils:haskey
"-- accepts 2 arguments, a LIST and a STRING target --";
"-- the LIST is composed of SUBLISTS of 2 STR elements --";
"-- returns the SUBLIST position whose first STR element --";
"-- matches the target; returns 0 is the target is not --";
"-- found in any first STR element of SUBLISTS --";
"-- example --";
"-- receives --";
"-- {{{\"var1\", \"1\"}, {\"var2\", \"2\"}}, \"var2\"}--";
"-- returns 2 --";
"-- receives --";
"-- {{{\"var1\", \"1\"}, {\"var2\", \"2\"}}, \"var3\"}--";
"-- returns 0 --";
if (length(args) != 2)
return E_ARGS;
endif
if (typeof(args[1]) != LIST)
return E_INVARG;
endif
if (typeof(args[2]) != STR)
return E_INVARG;
endif
hash = args[1];
key = args[2];
len = length(hash);
for i in [1..len]
$command_utils:suspend_if_needed(0);
if (hash[i][1] == key)
return i;
endif
endfor
return 0;
.
@verb $hash_utils:"fetch get" this none this
@program $hash_utils:fetch
"-- mainly used to extract values from hash or env variables --";
"-- receives a LIST and a STR --";
"-- {{{\"var1\", \"1\"}, {\"var2\", \"2\"}}, \"var1\"} --";
"-- and sends: the value corresponding to the STR, that is: --";
"-- \"1\" --";
"-- returns an empty string if it found nothing --";
if (length(args) != 2)
return E_ARGS;
endif
if (typeof(args[1]) != LIST)
return E_INVARG;
endif
if (typeof(args[2]) != STR)
return E_INVARG;
endif
hash = args[1];
key = args[2];
index = this:haskey(hash, key);
if (!index)
return "";
endif
return hash[index][2];
.
@verb $hash_utils:"match_anystr_in_list" this none this
@program $hash_utils:match_anystr_in_list
"-- able to find a target in strings elements of a list --";
"-- see help index() as this can be improved if need arises --";
"-- return 0 if not found --";
"-- return a LIST if found => {1, N} where N is the element --";
"-- position in the LIST and 1 means successful --";
"-- example --";
"-- receives: {{\"un\", \"deux\", \"trois\"}, \"deux\"} --";
"-- returns {1, 2} --";
"-- receives: {{\"un\", \"deux\", \"trois\"}, \"doce\"} --";
"-- returns 0 --";
{leest, target} = {args[1], args[2]};
found = 0;
for t in [1..length(leest)]
$command_utils:suspend_if_needed(0);
if (index(leest[t], target) != 0)
return {1, t};
found = 1;
break t;
endif
endfor
if (found == 0)
return 0;
endif
.
@verb $hash_utils:"match_str_in_list1" this none this
@program $hash_utils:match_str_in_list1
"-- able to find a target in strings elements of a list --";
"-- return 0 if not found --";
"-- return a LIST if found => {1, N} where N is the element --";
"-- position in the LIST and 1 means successful --";
"-- example --";
"-- receives: {{\"un\", {whatever}}, {\"deux\", {whatever}}}, \"deux\"} --";
"-- returns {1, 2} --";
"-- receives: {{\"un\", {whatever}}, {\"deux\", {whatever}}}, \"deux\"} --";
"-- returns 0 --";
{leest, target} = {$list_utils:slice(args[1], 1), args[2]};
found = 0;
for t in [1..length(leest)]
$command_utils:suspend_if_needed(0);
if (leest[t] == target)
return {1, t};
found = 1;
break t;
endif
endfor
if (found == 0)
return 0;
endif
.
@verb $hash_utils:"HTML_format_BR" this none this
@program $hash_utils:HTML_format_BR
"-- returns the elements of a list with
between --";
"-- each elements --";
old = args[1];
new = {};
for element in (old)
(ticks_left() < $kahuna.ticks_threshold) && suspend(0);
new = {@new, element, "
"};
endfor
return new;
.