Enabled ANSI and Videotex servers and frames

This commit is contained in:
Deon George
2018-12-09 09:43:18 +11:00
parent 6c91c69bd9
commit 0eb71ef7c6
11 changed files with 429 additions and 243 deletions

View File

@@ -28,22 +28,26 @@ use Illuminate\Support\Facades\Log;
*
* @package App\Classes
*/
class Frame
abstract class Frame
{
private $frame = NULL;
private $output = NULL;
private $frame_length = 22;
private $frame_width = 40;
protected $frame = NULL;
protected $output = NULL;
private $header_length = 20; // 20
private $pagenum_length = 9; // 11 (prefixed with a color, suffixed with frame)
private $cost_length = 7; // 9 (prefixed with a color, suffixed with unit)
private $cost_unit = 'u';
// All this vars should be overridden in the child class
/*
protected $frame_length = 22;
protected $frame_width = 40;
protected $header_length = 20; // 20
protected $pagenum_length = 9; // 11 (prefixed with a color, suffixed with frame)
protected $cost_length = 7; // 9 (prefixed with a color, suffixed with unit)
protected $cost_unit = 'u';
*/
public $fields = NULL; // The fields in this frame.
// Magic Fields that are pre-filled
private $fieldmap = [
protected $fieldmap = [
'a'=>'address#',
'd'=>'%date',
];
@@ -80,6 +84,7 @@ class Frame
}
// Calculate fields and render output.
$this->fields = collect(); // Fields in this frame.
$this->fields($startline);
}
@@ -118,115 +123,7 @@ class Frame
*
* @param int $startline
*/
public function fields($startline=0,$fieldchar='.')
{
$infield = FALSE; // In a field
$fieldtype = NULL; // Type of field
$fieldlength = 0; // Length of field
$this->fields = collect(); // Fields in this frame.
if ($startline)
$this->output .= str_repeat(DOWN,$startline);
// $fieldadrline = 1;
// Scan the frame for a field start
for ($y=$startline;$y<=$this->frame_length;$y++)
{
// Fields can only be on a single line
$fieldx = $fieldy = FALSE;
for ($x=0;$x<$this->frame_width;$x++)
{
$posn = $y*40+$x;
// If the frame is not big enough, fill it with spaces.
$byte = ord(isset($this->frame->content{$posn}) ? $this->frame->content{$posn} : ' ')%128;
// Check for start-of-field
if ($byte == ord(ESC)) { // Esc designates start of field (Esc-K is end of edit)
$infield = TRUE;
$fieldlength = 1;
$fieldtype = ord(substr($this->frame->content,$posn+1,1))%128;
$this->output .= $fieldchar;
} else {
if ($infield) {
if ($byte == $fieldtype) {
$fieldlength++;
$byte = ord($fieldchar); // Replace field with $fieldchar.
if ($fieldx === FALSE) {
$fieldx = $x;
$fieldy = $y;
}
// Is this a magic field?
// @todo For page redisplay *00, we should show entered contents - for refresh *09 we should show updated contents
if (array_get($this->fieldmap,chr($fieldtype)) ) {
$field = $this->fieldmap[chr($fieldtype)];
//dump(['infield','byte'=>$byte,'fieldtype'=>$fieldtype,'field'=>$field,'strpos'=>strpos($field,'#')]);
/*
// address field has many lines. increment when hit on first character.
if ($fieldlength == 1 && strpos($field,'#') !== false) {
$field = str_replace('#',$fieldadrline,$field);
dump(['field'=>$field,'fieldadrline'=>$fieldadrline,'fieldadrline'=>$fieldadrline]);
$fieldadrline++;
}
*/
// Replace field with Date
if ($field == '%date') {
// Drop the last dot and replace it.
if ($fieldlength == 2) {
$datetime = date('D d M H:ia');
$this->output = rtrim($this->output,$fieldchar);
$this->output .= $datetime{0};
}
if ($fieldlength > 1 AND $fieldlength <= strlen($datetime))
$byte = ord($datetime{$fieldlength-1});
}
// @todo user data
/* else if (isset($user[$field])) {
if ($fieldlength <= strlen($user[$field])) {
$byte = ord($user[$field]{$fieldlength-1});
}
} /*else // pre-load field contents. PAM or *00 ?
if (isset($fields[what]['value'])) {
*/
}
} else {
$this->fields->push(new FrameFields([
'type'=>chr($fieldtype),
'length'=>$fieldlength,
'x'=>$fieldx-1, // Adjust for the ESC char
'y'=>$fieldy,
]));
Log::debug(sprintf('Frame: %s, Field found at [%s,%s], Type: %s, Length: %s',$this->page(),$fieldx-1,$fieldy,$fieldtype,$fieldlength));
$infield = FALSE;
$fieldx = $fieldy = FALSE;
}
}
}
// truncate end of lines @todo havent validated this code or used it?
if (isset($pageflags['tru']) && substr($this->frame->content,$posn,40-$x) === str_repeat(' ',40-$x)) {
$this->output .= CR . LF;
break;
}
if (! $infield OR $fieldlength > 1)
$this->output .= ($byte < 32) ? ESC.chr($byte+64) : chr($byte);
}
}
}
abstract public function fields($startline=0,$fieldchar='.');
/**
* Returns the current frame.
@@ -349,7 +246,7 @@ class Frame
else
$color = GREEN;
return sprintf($color.'% '.$this->cost_length.'.0f%s',$cost,$this->cost_unit);
return sprintf($color.'% '.static::$cost_length.'.0f%s',$cost,static::$cost_unit);
}
/**
@@ -360,9 +257,9 @@ class Frame
*/
private function render_header(string $header)
{
$filler = ($this->strlenv($header) < $this->header_length) ? str_repeat(' ',$this->header_length-$this->strlenv($header)) : '';
$filler = ($this->strlenv($header) < static::$header_length) ? str_repeat(' ',static::$header_length-$this->strlenv($header)) : '';
return substr($header.$filler,0,$this->header_length+substr_count($this->header,ESC));
return substr($header.$filler,0,static::$header_length+strlen($header)-$this->strlenv($header));
}
/**
@@ -381,7 +278,7 @@ class Frame
if (strlen($frame) !== 1)
throw new \Exception('Frame invalid',500);
return sprintf(WHITE.'% '.$this->pagenum_length.'.0f%s',$num,$frame);
return sprintf(WHITE.'% '.static::$pagenum_length.'.0f%s',$num,$frame);
}
/**
@@ -403,11 +300,9 @@ class Frame
* @param $text
* @return int
*/
function strlenv($text):int {
return strlen($text)-substr_count($text,ESC);
}
abstract function strlenv($text):int;
public static function testFrame()
public static function testFrame(Server $so)
{
// Simulate a DB load
$o = new \App\Models\Frame;
@@ -419,7 +314,8 @@ class Frame
$o->index = 'a';
// Header
$o->content .= substr(R_RED.'T'.R_BLUE.'E'.R_GREEN.'S'.R_YELLOW.'T-12345678901234567890',0,20).
$sid = R_RED.'T'.R_BLUE.'E'.R_GREEN.'S'.R_YELLOW.'T';
$o->content .= substr($sid.'-'.str_repeat('12345678901234567890',4),0,static::$header_length+(strlen($sid)-$so->strlenv($sid))).
R_WHITE.'999999999a'.R_RED.sprintf('%07.0f',999).'u';
$o->content .= str_repeat('+-',18).' '.R_RED.'01';