// ReadVec.sr

// Last modified October 20, 1979  6:02 PM by Taft

get "BRAVO1.DF";
get "AltoFileSys.D";
get "OM.DF";
get "VM.Df";
get "Q.Df";
get "BFS.Def";

// Incoming Procedures

external [
	ult;
	errhlt;
	move;
	ugt
	movec;
	umin;
	ActOnPages;
	RealDA
	];

// Incoming Statics

external [
	fillInDA;
	DCread;
	DCwrite
	dnbp
	];

// Outgoing Procedures

external [
	ReadVec;
	RW;
	FindCfc
	];

// Outgoing Statics

// Local Statics


manifest [ 
	pmidCw = 5;
	pmidVecDest = 6
	pmidCwMaxMove = 7;
	pmidBwdF = 11
	cwFill = -2
	] 

// R E A D V E C
//
let ReadVec(cfa,cw,vecDest; numargs na) be
[ let bwdF = cfa >> CFA.fa.charPos rshift 1
if bwdF ne 0 then errhlt("fp1");
if na ne 3 then errhlt("na");
if cw eq cwFill then errhlt("cwF");
RW(cfa, cw, vecDest, DCread);
];

// R W
//
and RW(cfa, cw, vecDest, dc) be
[ let buf = vec #400;
let firstPage = cfa >> CFA.fa.pageNumber;
let firstDA = cfa >> CFA.fa.da;
while cw ne 0 do
	[ 
	manifest biteSize=64;
	let DAs=vec biteSize;
	let CAs = vec biteSize
	movec(DAs, DAs+biteSize-1, fillInDA)
	movec(CAs, CAs+biteSize-1, buf)
	DAs=DAs-firstPage; DAs!firstPage=firstDA
	let lastPage = firstPage;
	let fileId = vec 3;
	fileId ! 0 = cfa >> CFA.fp.version;
	move(lv cfa >> CFA.fp.serialNumber, fileId+1, 2);
	CAs = CAs-firstPage;
	let lastPageFound = nil;
	let numCharsLast = nil;
	if vecDest ne 0 then
		[
		for pgn = firstPage to firstPage+biteSize-1 do
			[ lastPage = pgn;
			if pgn eq 0 then
				[ CAs ! pgn = buf;
				if dc eq DCwrite then
					errhlt("p0");
				loop;
				]
			if ult(cw, #400) then
				[ 
// done by movec
// 			if (cw ne 0) then
// 					CAs ! lastPage = buf;
				break;
				];
			CAs ! pgn = vecDest
			vecDest = vecDest+#400;
			cw = cw-#400;
			];
		]

	if (dc eq DCwrite) & (vecDest ne 0) & ult(cw, #400) & (cw ne 0) then
		[ move(vecDest, buf, cw);
		];
	lastPageFound=ActOnPages(CAs, DAs, fileId, firstPage,
	  lastPage, dc, lv numCharsLast)
	cfa >> CFA.fa.da = DAs ! lastPageFound;
	cfa >> CFA.fa.pageNumber = lastPageFound;
	cfa >> CFA.fa.charPos = numCharsLast;
	if (vecDest ne 0) & (lastPageFound ne lastPage) then
			errhlt("lp");
	if (vecDest ne 0) & ult(cw, #400) & (cw ne 0) then
		[ move(buf, vecDest, cw);
		break;
		];

	if numCharsLast ne #1000 then break
	
	firstPage=lastPageFound+1; firstDA=DAs!firstPage
	]
] 

// T C B U F T O V E C
//
// and TcBufToVec(zone,FReturn) = valof
// [ let scanParams = zone >> CBZ.extra;
// let qBufRead = scanParams >> SCP.qBufRead
// let frameLC = scanParams >> SCP.fmCaller
// until LengthQ(qBufRead) eq 0 do
// 	[ let buf = Dequeue(qBufRead)
// 	let ca = buf >> BUF.ca
// 	Enqueue(scanParams >> SCP.qBufFree,buf)
// 	let pwSrc = ca+frameLC ! pmidBwdF
// 	if (frameLC ! pmidCw eq cwFill) then
// 		frameLC ! pmidCw = @pwSrc
// 	let tcwRem = frameLC ! pmidCw
// 	let tcwRemMove = frameLC ! pmidCwMaxMove
// 	let tcwScan = umin(#400-frameLC ! pmidBwdF,tcwRem)
// 	let tcwMove = umin(tcwScan,tcwRemMove)
// 	unless (tcwRemMove eq 0)  then
// 		[ move(pwSrc,frameLC ! pmidVecDest,tcwMove)
// 		frameLC ! pmidCwMaxMove = tcwRemMove-tcwMove
// 		frameLC ! pmidVecDest = frameLC ! pmidVecDest+tcwMove
// 		] 
// 	frameLC ! pmidBwdF = 0
// 	unless (tcwRem eq cwNil)  then
// 		frameLC ! pmidCw = tcwRem-tcwScan
// 	] 
// test (frameLC ! pmidCw eq 0) ifso
// 	resultis tcDone
// ifnot	resultis tcToYou
// ] 


// L F I LE 
//
and FindCfc(fptr,dblL) be
[ let cfa = vec lCFA
move(fptr,cfa,lFP)
cfa >> CFA.fa.pageNumber = 0
cfa >> CFA.fa.da = RealDA(cfa >> CFA.fp.leaderVirtualDa)
cfa >> CFA.fa.charPos = 0
ReadVec(cfa,-1,0)
dblL ! 0 = ((cfa >> CFA.fa.pageNumber-1) << FPG.hi) lshift 9
dblL ! 1 = (((cfa >> CFA.fa.pageNumber-1) << FPG.lo) lshift 9)+cfa >> CFA.fa.charPos
]