Native character input

This commit is contained in:
Carsten Larsen 2017-01-24 20:48:02 +01:00
parent b207afe5a2
commit 7e1dfa1e71
17 changed files with 97 additions and 14 deletions

View File

@ -26,10 +26,12 @@
#include "clib.h"
#include "lib/charbuf.h"
#include "lib/charval.h"
#include "lib/aengine.h"
AnsiConoleEngine::AnsiConoleEngine(const char *prompt)
AnsiConoleEngine::AnsiConoleEngine(const char *prompt, CharValidator *validator)
{
this->validator = validator;
AllocAndCopy(&this->prompt, prompt);
linebuf = new CharBuffer();
out = new CharBuffer();
@ -190,7 +192,7 @@ const char* AnsiConoleEngine::ProcessChar(const unsigned char character)
cursor--;
endpos--;
linebuf->ptr = endpos;
} else if (ch >= 32 && ch <= 126) {
} else if (validator->Validate(ch)) {
// Insert in middle of line
if (cursor != endpos) {
char *i = endpos;

View File

@ -34,6 +34,7 @@
*/
class CharBuffer;
class CharValidator;
/**
* @brief ANSI console controller.
@ -43,7 +44,7 @@ class CharBuffer;
*/
class AnsiConoleEngine {
public:
AnsiConoleEngine(const char *prompt);
AnsiConoleEngine(const char *prompt, CharValidator *validator);
~AnsiConoleEngine();
void StartInput();
@ -63,6 +64,7 @@ private:
static const int lineSize = 1024;
char **lines;
CharBuffer *linebuf;
CharValidator *validator;
unsigned int len;
char *cursor;
char *endpos;

36
app/lib/charval.h Normal file
View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2015-2017 Carsten Sonne Larsen <cs@innolan.dk>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef _CHAR_VALIDATOR_H
#define _CHAR_VALIDATOR_H
class CharValidator {
public:
virtual ~CharValidator() {}
virtual bool Validate(char c) = 0;
};
#endif /* _CHAR_VALIDATOR_H */

View File

@ -26,14 +26,15 @@
#include "clib.h"
#include <stdio.h>
#include "lib/charval.h"
#include "main/nodes.h"
#include "main/evaluator.h"
#include "system/console_stdc.h"
StandardConsole::StandardConsole(const char *prompt) :
StandardConsole::StandardConsole(const char *prompt, CharValidator *validator) :
ConsoleBase(prompt)
{
proc = new AnsiConoleEngine(prompt);
proc = new AnsiConoleEngine(prompt, validator);
}
StandardConsole::~StandardConsole()

View File

@ -34,6 +34,7 @@
*/
#include "clib.h"
#include "lib/charval.h"
#include "lib/aengine.h"
#include "system/console.h"
@ -43,7 +44,7 @@
*/
class StandardConsole : public ConsoleBase {
public:
StandardConsole(const char *prompt);
StandardConsole(const char *prompt, CharValidator *validator);
virtual ~StandardConsole();
virtual int GetStackSize();
virtual void Run();

View File

@ -160,3 +160,20 @@ char* Language::UntagText(const char* text)
return lastText;
}
bool Language::CharIsQuote(unsigned long character)
{
return (character == '"');
}
bool Language::CharIsOperator(unsigned long character)
{
static const unsigned int count = sizeof(operators) / sizeof(operatordef);
for (unsigned int i = 0; i < count; i++) {
if (operators[i].chr == (char)character) {
return true;
}
}
return false;
}

View File

@ -27,13 +27,14 @@
#ifndef _LANGUAGE_BASE_H
#define _LANGUAGE_BASE_H
#include "lib/charval.h"
#include "localize/lex.h"
#include "localize/help.h"
#include "localize/text.h"
#include "localize/ident.h"
#include "localize/kword.h"
class Language {
class Language : public CharValidator {
public:
Language();
virtual ~Language();
@ -48,7 +49,10 @@ public:
virtual bool CharIsPunct(unsigned long character) = 0;
virtual bool CharIsSpace(unsigned long character) = 0;
virtual bool CharIsCntrl(unsigned long character) = 0;
virtual bool CharIsQuote(unsigned long character);
virtual bool CharIsOperator(unsigned long character) ;
virtual bool StrIsEqualLoc(const char *s1, const char *s2) = 0;
virtual bool Validate(char c) = 0;
protected:
virtual char* Translate(textdef *def) = 0;

View File

@ -135,6 +135,15 @@ bool AmigaLanguage::StrIsEqualLoc(const char* s1, const char* s2)
return (StrnCmp(locale, (char*)s1, (char*)s2, -1, SC_COLLATE1) == 0);
}
bool AmigaLanguage::Validate(char c)
{
return CharIsAlNum(c) ||
CharIsPunct(c) ||
CharIsSpace(c) ||
CharIsQuote(c) ||
CharIsOperator(c);
}
/* for numeric values */
//STRPTR loc_DecimalPoint; /* character before the decimals */
//STRPTR loc_GroupSeparator; /* separates groups of digits */

View File

@ -45,6 +45,7 @@ public:
bool CharIsSpace(unsigned long character);
bool CharIsCntrl(unsigned long character);
bool StrIsEqualLoc(const char *s1, const char *s2);
bool Validate(char c);
protected:
char* Translate(textdef *def);

View File

@ -117,4 +117,9 @@ char* PosixLanguage::Translate(textdef* def)
return (char*)def->text;
}
bool PosixLanguage::Validate(char c)
{
return (c >= 32 && c <= 126);
}
#endif

View File

@ -50,6 +50,7 @@ public:
bool CharIsSpace(unsigned long character);
bool CharIsCntrl(unsigned long character);
bool StrIsEqualLoc(const char *s1, const char *s2);
bool Validate(char c);
protected:
char* Translate(textdef *def);

View File

@ -58,7 +58,6 @@ StandardLanguage::~StandardLanguage()
if (kwordbase != NOMEM) {
delete kwordbase;
}
}
char* StandardLanguage::Translate(textdef* def)
@ -125,6 +124,10 @@ bool StandardLanguage::StrIsEqualLoc(const char* s1, const char* s2)
return StrIsEqual(s1, s2);
}
bool StandardLanguage::Validate(char c)
{
return (c >= 32 && c <= 126);
}
void StandardLanguage::LoadCatalogs()
{

View File

@ -44,6 +44,7 @@ public:
bool CharIsSpace(unsigned long character);
bool CharIsCntrl(unsigned long character);
bool StrIsEqualLoc(const char *s1, const char *s2);
bool Validate(char c);
protected:
char* Translate(textdef *def);

View File

@ -64,7 +64,7 @@ AmigaProgram::~AmigaProgram()
void AmigaProgram::Initialize(int argc, char **argv)
{
if(argc < 2) {
Console = new AmigaWindow(Preferences->GetPrompt());
Console = new AmigaWindow(Preferences->GetPrompt(), Language);
return;
}

View File

@ -56,7 +56,7 @@ void StandardProgram::Initialize(int argc, char **argv)
{
if(argc < 2) {
// STDC version only has a console
Console = new StandardConsole(Preferences->GetPrompt());
Console = new StandardConsole(Preferences->GetPrompt(), Language);
return;
}
@ -76,7 +76,7 @@ void StandardProgram::Initialize(int argc, char **argv)
line->DeleteLastChar();
if (line->Is("shell")) {
Console = new StandardConsole(Preferences->GetPrompt());
Console = new StandardConsole(Preferences->GetPrompt(), Language);
}
}

View File

@ -33,10 +33,10 @@
#ifdef AMIGA
AmigaWindow::AmigaWindow(const char *prompt) :
AmigaWindow::AmigaWindow(const char *prompt, CharValidator *validator) :
ConsoleBase(prompt)
{
proc = new AnsiConoleEngine(prompt);
proc = new AnsiConoleEngine(prompt, validator);
window = NULL;
//menu = NULL;
writereq.st = NULL;

View File

@ -77,7 +77,7 @@ typedef union
*/
class AmigaWindow : public ConsoleBase {
public:
AmigaWindow(const char *prompt);
AmigaWindow(const char *prompt, CharValidator *validator);
~AmigaWindow();
int GetStackSize();