sbbs/load/funcs.js

171 lines
3.2 KiB
JavaScript
Raw Normal View History

// String repeat.
2019-10-03 04:08:48 +00:00
if (!String.prototype.repeat) {
2019-10-15 10:48:16 +00:00
String.prototype.repeat = function(count) {
'use strict';
if (this == null) {
throw new TypeError('can\'t convert ' + this + ' to object');
}
var str = '' + this;
count = +count;
if (count != count) {
count = 0;
}
if (count < 0) {
throw new RangeError('repeat count must be non-negative');
}
if (count == Infinity) {
throw new RangeError('repeat count must be less than infinity');
}
count = Math.floor(count);
if (str.length == 0 || count == 0) {
return '';
}
// Ensuring count is a 31-bit integer allows us to heavily optimize the
// main part. But anyway, most current (August 2014) browsers can't handle
// strings 1 << 28 chars or longer, so:
if (str.length * count >= 1 << 28) {
throw new RangeError('repeat count must not overflow maximum string size');
}
var rpt = '';
for (;;) {
if ((count & 1) == 1) {
rpt += str;
}
count >>>= 1;
if (count == 0) {
break;
}
str += str;
}
return rpt;
};
2019-10-03 04:08:48 +00:00
}
2020-05-27 11:56:12 +00:00
/**
* Turn off the cursor
*/
2019-10-03 04:08:48 +00:00
function cursorOff() {
2020-03-26 06:22:46 +00:00
ansi.send('ext_mode','clear','cursor');
2019-10-15 10:48:16 +00:00
console.gotoxy(0,24);
}
/**
* Find a message base by code
*
* @param code
* @returns {number | string|boolean}
*/
function findMsgBase(code) {
2019-10-15 10:48:16 +00:00
if (! code)
code = "tex_data";
// Find the actual sub-code
var sub_code;
for (var s in msg_area.sub) {
var sub = msg_area.sub[s];
if (sub.code.substr(-code.length).toLowerCase() == code)
return sub.code;
}
return false;
2019-10-03 04:08:48 +00:00
}
/**
* Return an argument from argv, or an error if it doesnt exit
*
* @param key
* @param error
*/
function getArg(key,error,abort) {
2019-10-15 10:48:16 +00:00
index = argv.indexOf(key);
2019-10-03 04:08:48 +00:00
2019-10-15 10:48:16 +00:00
if ((index !== -1) && (! (argv[index+1] === undefined || argv[index+1].match(/^-/)))) {
return argv[index+1];
}
2019-10-03 04:08:48 +00:00
2019-10-15 10:48:16 +00:00
if (abort) {
log(LOG_ERROR,error);
exit(1);
}
2019-10-03 04:08:48 +00:00
}
2019-10-20 11:31:15 +00:00
function loadOptions() {
2020-02-26 11:54:39 +00:00
ini = new File(file_cfgname(system.mods_dir,'ansitex/ctrl/videotex.ini'));
2019-10-20 11:31:15 +00:00
if (!ini.open("r")) {
return undefined;
}
val = ini.iniGetObject(null);
ini.close();
return val;
}
function msgBaseImport(msgbase,page,text) {
2019-10-15 10:48:16 +00:00
var msgbase = new MsgBase(findMsgBase(msgbase));
log(LOG_DEBUG,'Sending ['+page+'] to message base ['+msgbase.cfg.code+']');
var hdr = { to:'All', from:'Videotex', subject:'Frame: '+page };
var body = '';
body += text+"\r\n";
body += "--- " + js.exec_file + " " + '1.0' + "\r\n";
return msgbase.save_msg(hdr, body);
}
2019-10-03 04:08:48 +00:00
/**
* Return the frame as a string
*/
function pageStr(page) {
2020-03-26 06:22:46 +00:00
if (page.frame==null)
return null;
2019-10-15 10:48:16 +00:00
if (! page.index)
page.index = 'a';
2019-10-03 04:08:48 +00:00
return page.frame+page.index;
}
2019-10-22 10:57:25 +00:00
/**
* Save the frame
*
* @param frame
*/
function saveFrame(frame) {
2020-02-26 11:54:39 +00:00
file = system.mods_dir+'ansitex/text/'+frame.page+'.tex';
2019-10-22 10:57:25 +00:00
w = new File(file);
if (! w.open('w')) {
log(LOG_ERROR,'! ERROR: Unable to create TEX file for '+frame.page);
exit(1);
}
w.write(JSON.stringify(frame));
w.close();
log(LOG_DEBUG,'Saved file: '+frame.page+'.tex');
}
2020-05-27 11:56:12 +00:00
/**
* Send a message to the baseline.
*
* @param text
* @param reposition
*/
2019-10-03 04:08:48 +00:00
function sendBaseline(text,reposition) {
console.pushxy();
console.gotoxy(0,24);
console.print(text);
console.cleartoeol();
if (! reposition) {
console.popxy();
}
2020-05-27 11:56:12 +00:00
}
this;