Whats wrong in my code?
The iniRemoveSection() return false and do not remove the ini content.
I use tickitcfg as example to learn code (is still WIP) , my tool need add/remove [area:NAME] sections, the add just work, but the
iniRemoveSection() does not.
I gues that "ini" variable was global so, i can use in add_read() and del_area(). before use the iniRemoveSection() i test and the file still
open. I can not find the cause. Any idea?
Maybe the uifc (i first time use this) or file open mode are culpit ?
The code:
http://paste.debian.net/plain/1143042
load("uifcdefs.js");
load("sbbsdefs.js");
// Backward compatability hack.
if (typeof uifc.list.CTX === "undefined") {
uifc.list.CTX = function () {
this.cur = 0;
this.bar = 0;
}
}
uifc.init("MHS Gateway to WG");
js.on_exit("uifc.bail();");
const menu_fmt = "%-20s %s";
var ini = new File(system.ctrl_dir + "mhsgate.ini");
if (! ini.open(ini.exists ? 'r+':'w+')) {
uifc.msg("Error on open .ini file: " + ini.name);
uifc.bail();
exit(1);
}
function cfg_globals(){
var cmd = 0;
var ctx_globals = new uifc.list.CTX();
while(cmd >= 0) {
var menu = [format(menu_fmt, "Gateway Name",
ini.iniGetValue('global', 'gateway_name')),
"Paths"];
cmd = uifc.list(WIN_ORG|WIN_ACT|WIN_MID|WIN_ESC, "Global
Options", menu, ctx_globals);
switch(cmd) {
case 0:
uifc.help_text = help('gateway_name');
var val = ini.iniGetValue('global','gateway_name','');
tmp = uifc.input(WIN_MID|WIN_SAV,'Gateway Name',val,50, K_EDIT);
if (tmp !== undefined ) {
ini.iniSetValue('global','gateway_name', tmp);
}
break;
case 1:
cfg_paths();
break;
case -1:
//exit
break;
default:
uifc.msg("Unhandled Return: "+cmd);
break;
}
}
}
function cfg_area(area) {
uifc.msg("edit:" + area);
}
function del_area(area) {
ini.debug = true;
print(ini.is_open);
uifc.msg(area);
ini.iniRemoveSection("area:" + area);
}
function add_area(area) {
area = area.toUpperCase();
ini.iniSetValue("area:" + area, "target",'');
ini.iniSetValue("area:" + area, "created",strftime("%d-%m-%Y
%H:%M:%S"));
}
function cfg_areas() {
var area = 0;
var tmp;
var ctx_areas = new uifc.list.CTX();
while(area >= 0) {
var areas_list = [];
var areas;
areas = ini.iniGetSections("area:");
for (a in areas) {
areas_list.push(areas[a].slice(5));
}
areas_list = areas_list.map(function(v){return v.toUpperCase();});
area = uifc.list(WIN_SAV|WIN_ACT|WIN_DEL|WIN_INS|WIN_DELACT,
"Select Area", areas_list, ctx_areas);
if (area == -1) {
break;
}
else if (area == areas.length || (area & MSK_INS) == MSK_INS) {
area &= MSK_OFF;
tmp = uifc.input(WIN_SAV|WIN_MID, "Area", 30);
if ((tmp !== undefined) && (tmp != "")) {
add_area(tmp);
}
else {
uifc.msg("breask");
break;
}
}
else if (area & MSK_DEL) {
area &= MSK_OFF;
del_area(areas[area]);
}
else {
cfg_area(areas[area]);
}
}
}
function cfg_paths() {
var ctx_paths = new uifc.list.CTX();
var cmd = 0;
uifc.help_text = help('paths');
while(cmd >= 0) {
var menu = [
format(menu_fmt, "Pickup From",
ini.iniGetValue('paths','pickup')),
format(menu_fmt, "Send To",ini.iniGetValue('paths','sendto'))
];
cmd = uifc.list(WIN_ORG|WIN_ACT|WIN_MID|WIN_ESC, "Paths
Options", menu, ctx_paths);
switch(cmd) {
case 0:
var val = ini.iniGetValue('paths','pickup','');
tmp = uifc.input(WIN_MID|WIN_SAV,'Pickup from directory',val,1024, K_EDIT);
if (tmp !== undefined) {
ini.iniSetValue('paths','pickup', tmp);
}
break;
case 1:
var val = ini.iniGetValue('paths','sendto','');
tmp = uifc.input(WIN_MID|WIN_SAV,'Sent to
directory',val,1024, K_EDIT);
if (tmp !== undefined) {
ini.iniSetValue('paths','sendto', tmp);
}
break;
case -1:
//exit
break;
default:
uifc.msg("Unhandled Return: "+cmd);
break;
}
}
}
function main() {
var cmd = 0;
var ctx_main = new uifc.list.CTX();
while(cmd >= 0) {
uifc.help_text = help("main");
var menu = ["Global Options","Areas configuration"];
cmd = uifc.list(WIN_ORG|WIN_ACT|WIN_MID|WIN_ESC, "MSH Gateway
Options", menu, ctx_main);
switch(cmd) {
case 0:
cfg_globals();
break;
case 1:
cfg_areas();
break;
case -1:
//exit
return;
break;
default:
uifc.msg("Unhandled Return: "+cmd);
break;
}
}
uifc.bail();
}
function help(item) {
switch (item) {
case 'main':
str = "Global configurations";
break;
case 'gateway_name':
str = "Name for `this` MHS gateway";
break;
case 'paths':
str = "Setup the paths for the gateway\n\n";
str += "\1Pickup from\1\n\nThis is the directory that
MHSGate will \1pickup\1 messages.\n";
str += "Verify that will be the same value as OUTMSG (WG
side Level 4 config option)";
str += "\n\n";
str += "\1Sent to\1\n\nThis is the directory that MHSGate
will \1send\1 messages.\n";
str += "Verify that will be the same value as INMSG (WG side
Level 4 config option)";
break;
default:
str = '';
}
return str;
}
main();
ini.close();