Ferreteria/v0.6/clade/IO/Aspect/Connx/Buffer/File: Difference between revisions
Jump to navigation
Jump to search
(Created page with "{{page/clade/v2 |fam= {{!}} align=right {{!}} <code>{{l/ver/clade|IO\Aspect\Connx|Buffer}}</code> {{!}} align=center {{!}} ⇒ <code>{{l/ver/clade|IO\Aspect\Connx\Buffer|File}}</code> ⇒ {{!}} align=left {{!}} <poem> <code>/Reader/</code> <code>/Writer/</code> </poem> |alia= {{!-!}} '''ActionIface''' {{!!}} <code>{{l/ver/clade/full|Sys\Events|ItWent}}</code> {{!-!}} '''Base'''* [ca,i] {{!!}} <code>{{l/ver/clade/full|IO\Aspect\Connx|Buffer}}</code> {{!...") |
No edit summary |
||
| Line 18: | Line 18: | ||
==About== | ==About== | ||
* '''Purpose''': a Buffer which moves data between a stream and a file | * '''Purpose''': a Buffer which moves data between a stream and a file | ||
** This is the direction-agnostic base class, | ** This is the direction-agnostic base class, necessarily abstract. | ||
* '''Assumes''' display is updateable (CLI: yes, web: no). | |||
** {{hashtag|TODO}} {{fmt/date|2025|11|28}}: this needs to be device-agnostic, somehow | |||
==History== | ==History== | ||
* {{fmt/date|2024|12|07}} started | * {{fmt/date|2024|12|07}} started | ||
| Line 93: | Line 95: | ||
if ($nRem > 0) { | if ($nRem > 0) { | ||
$sRem = $this->Formatter()->format($nRem); | $sRem = $this->Formatter()->format($nRem); | ||
$ftRem = "rem: $sRem | $ftRem = "rem: $sRem {{!}} "; | ||
} | } | ||
} | } | ||
Revision as of 14:56, 28 November 2025
| ||||||||||||||||||||
About
- Purpose: a Buffer which moves data between a stream and a file
- This is the direction-agnostic base class, necessarily abstract.
- Assumes display is updateable (CLI: yes, web: no).
History
Code
interface iFile extends BaseIface {}
abstract class caFile extends BaseClass {
// ++ CONFIG ++ //
abstract protected function ModesArray() : array;
// -- CONFIG -- //
// ++ SETUP ++ //
public function __construct(private FileIface $oData) {}
protected function DataFile() : FileIface { return $this->oData; }
// -- SETUP -- //
// ++ LIFECYCLE ++ //
protected function ActualOpen() : ActionIface {
$oData = $this->DataFile();
$oMode = new FModeClass($this->ModesArray());
$oDataIO = $oData->InOut();
$oDataIO->Mode($oMode);
#echo $oData->ReflectThis()->Report();
$oAct = $oDataIO->Open();
if (!$oAct->GetOkay()) {
self::GotToHere('File error detected here.');
echo self::Screen()->ErrorIt('File error').': '.$oAct->GetStatusText().CRLF;
}
$this->RewindAll(); // reset the contents
$this->StartClock(); // reset the time counter
return $oAct;
}
protected function ActualShut() : ActionIface {
$oDataIO = $this->DataFile()->InOut();
$oAct = $oDataIO->Shut();
$this->UpdateStatus();
echo CRLF;
return $oAct;
}
private $onWhenStart;
protected function StartClock() { $this->onWhenStart = new DateTime; }
protected function DurationString() : string {
$onNow = new DateTime;
$onDiff = $this->onWhenStart->diff($onNow);
return $onDiff->format('%h:%I:%S');
}
// -- LIFECYCLE -- //
// ++ UI ++ //
private $onFmt = NULL; protected function Formatter() : NumberFormatter {
return $this->onFmt ?? ($this->onFmt = new NumberFormatter('en_US',NumberFormatter::DECIMAL));
}
protected function UpdateStatus() {
static $nMaxWd = 0;
static $sInBytes = '0';
$nTot = $this->SpentByteCount();
$sTot = $this->Formatter()->format($nTot);
$nFil = $this->DataFile()->Size();
$ftRem = ''; // default
if (is_int($nFil) && ($nFil > 0)) {
$nRem = $nFil - $nTot;
if ($nRem > 0) {
$sRem = $this->Formatter()->format($nRem);
$ftRem = "rem: $sRem | ";
}
}
$nMem = memory_get_usage();
$sMem = $this->Formatter()->format($nMem);
$sTime = $this->DurationString();
#echo "\r ($sTime) $ftInBytes $sTot written (mem used: $sMem)";
echo "\r ($sTime) $sTot bytes ({$ftRem}mem: $sMem)"; // TODO: clear remainder of line
#echo CRLF.$this->DataFile()->Ident()->ReflectThis()->Report(); die();
}
// ACTION: calls UpdateStatus() if enough time has passed
protected function OnStatusChange() {
static $ntLast = 0.0;
$ntNow = microtime(TRUE);
if ($ntNow - $ntLast > 0.05) { // update display every 0.05 seconds
$ntLast = $ntNow;
$this->UpdateStatus();
// reset the timeout clock
$ok = set_time_limit(10); // NOTE: seems to always return FALSE; TODO: make this configurable
}
}
// -- UI -- //
}