Plan 9 from Bell Labs’s /usr/web/sources/contrib/fernan/nhc98/src/prelude/Binary/CopyBytes.gc

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


module NHC.Binary
  ( copyBytes
  ) where

import NHC.GreenCard
import BinHandle ({-type-}BinHandle(..))
import BinPtr    ({-type-}BinPtr(..))

%-#include "cLowBinary.h"
    -- %-#include <stdio.h>


%fun copyBytes :: BinHandle -> BinHandle -> Int -> IO BinPtr
%call (binHandle sbh) (binHandle dbh) (int bytes)
%code  /* The source bitstream is assumed to be aligned to a */
%      /* byte boundary (at both ends) */
%  destptr  = (dbh->file ? vtell(dbh) : mtell(dbh));
%  if ((destptr+(bytes*8)) > dbh->highwater) dbh->highwater = destptr+(bytes*8);
%  closecache(sbh);
%  closecache(dbh);
%  if (sbh->file) {
%    vsync(sbh);
%    if (dbh->file) {
%      /*FILE->FILE*/
%      char buf[BUFSIZ];
%      vsync(dbh);
%      while (bytes>BUFSIZ) {
%        read(sbh->loc.fd,buf,BUFSIZ);
%        write(dbh->loc.fd,buf,BUFSIZ);
%        bytes -= BUFSIZ;
%      }
%      if (bytes) {
%        read(sbh->loc.fd,buf,bytes);
%        write(dbh->loc.fd,buf,bytes);
%      }
%    } else {
%      /*FILE->MEM*/
%      memcheck(dbh,bytes);
%      read(sbh->loc.fd,
%           (void*)(stableRef(dbh->loc.sp)+1+EXTRA)+dbh->adjust.here, bytes);
%      dbh->adjust.here += bytes;
%      if (dbh->adjust.here > dbh->attrib.size)
%        dbh->attrib.size = dbh->adjust.here;
%      /*fprintf(stderr,"directPut: %d bytes from file to mem at 0x%x\n", bytes,
%           (void*)(stableRef(dbh->loc.sp)+1+EXTRA)+dbh->adjust.here);*/
%    }
%  } else {
%    if (dbh->file) {
%      /*MEM->FILE*/
%      vsync(dbh);
%      write(dbh->loc.fd,
%            (void*)(stableRef(sbh->loc.sp)+1+EXTRA)+sbh->adjust.here, bytes);
%      /*fprintf(stderr,"directPut: %d bytes from mem at 0x%x to file\n", bytes,
%            (void*)(stableRef(sbh->loc.sp)+1+EXTRA)+sbh->adjust.here);*/
%    } else {
%      /*MEM->MEM*/
%      memcheck(dbh,bytes);
%      memcpy((void*)(stableRef(dbh->loc.sp)+1+EXTRA)+dbh->adjust.here,
%             (void*)(stableRef(sbh->loc.sp)+1+EXTRA)+sbh->adjust.here, bytes);
%      dbh->adjust.here += bytes;
%      if (dbh->adjust.here > dbh->attrib.size)
%        dbh->attrib.size = dbh->adjust.here;
%    }
%  }
%  opencache(sbh);
%  opencache(dbh);
%result (binPtr destptr)


Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].