Skip to content
Snippets Groups Projects
Commit b9df7ec5 authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Add K_CP437 mode flag for use with inkey() - translates UTF-8 to CP437

If you're writing, say, a message editor and you don't want to deal with
UTF-8 input at all, specify this mode flag to convert any translatable UTF-8
chars (that have CP437 equivalents) to CP437 chars. Non-translatable UTF-8
chars are just ignored (treated the same as a timeout/no-input).
parent af7852df
No related branches found
No related tags found
No related merge requests found
......@@ -21,6 +21,8 @@
#include "sbbs.h"
#include "petdefs.h"
#include "unicode.h"
#include "utf8.h"
int sbbs_t::kbincom(unsigned int timeout)
{
......@@ -97,17 +99,15 @@ void sbbs_t::translate_input(uchar* buf, size_t len)
int sbbs_t::inkey(int mode, unsigned int timeout)
{
int ch=0;
const int no_input = (mode & K_NUL) ? NOINP : 0; // distinguish between timeout and '\0'
ch=kbincom(timeout);
if(sys_status&SS_SYSPAGE)
sbbs_beep(400 + sbbs_random(800), ch == NOINP ? 100 : 10);
if(ch == NOINP) {
if(mode & K_NUL) // distinguish between timeout and '\0'
return NOINP;
return 0;
}
if(ch == NOINP)
return no_input;
if(cfg.node_misc&NM_7BITONLY
&& (!(sys_status&SS_USERON) || term_supports(NO_EXASCII)))
......@@ -122,6 +122,31 @@ int sbbs_t::inkey(int mode, unsigned int timeout)
return(handle_ctrlkey(ch,mode));
}
/* Translate (not control character) input into CP437 */
if (mode & K_CP437) {
if ((ch & 0x80) && term_supports(UTF8)) {
char utf8[UTF8_MAX_LEN] = { (char)ch };
int len = utf8_decode_firstbyte(ch);
if (len < 2 || len > sizeof(utf8))
return no_input;
for (int i = 1; i < len; ++i) {
ch = kbincom(timeout);
if (!(ch & 0x80) || (ch == NOINP))
break;
utf8[i] = ch;
}
if (ch & 0x80) {
enum unicode_codepoint codepoint;
len = utf8_getc(utf8, len, &codepoint);
if (len < 1)
return no_input;
ch = unicode_to_cp437(codepoint);
if (ch == 0)
return no_input;
}
}
}
if(mode&K_UPPER)
ch=toupper(ch);
......
......@@ -659,6 +659,7 @@ typedef enum { /* Values for xtrn_t.event */
#define K_TRIM (1<<23) /* Trimmed white-space */
#define K_CTRLKEYS (1<<24) /* No control-key handling/eating in inkey()*/
#define K_NUL (1<<25) /* Return NOINP on timeout instead of '\0' */
#define K_CP437 (1<<26) /* Translate (UTF-8) input to CP437 chars */
/* Bits in 'mode' for putmsg and printfile */
#define P_NONE 0 /* No mode flags */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment