Ferreteria/v0.6/clade/IO/Aspect/Connx/Buffer/File

From Woozle Writes Code
< Ferreteria‎ | v0.6‎ | clade‎ | IO‎ | Aspect‎ | Connx‎ | Buffer
Revision as of 15:05, 28 November 2025 by Woozle (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
clade: IO\Aspect\Connx\Buffer\File
Clade Family
Buffer File
Clade Aliases
Alias Clade
ActionIface Sys\Events\ItWent
Base* [ca,i] IO\Aspect\Connx\Buffer
FModeClass Sys\FileSys\Mode
FileIface Sys\FileSys\Node\Fi
Subpages

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).
    • #TODO 2025-11-28: display output needs to be device-agnostic, somehow

History

Code

as of 2025-11-27:

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 -- //
}