Plan 9 from Bell Labs’s /usr/web/sources/contrib/maht/inferno/appl/lib/buf.b

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


implement Buf;

include "buf.m";
include "sys.m";
	sys : Sys;

new(s : string) : ref Data
{
	sys = load Sys Sys->PATH;
	d := ref Data;
	d.txt = s;
	d.ptr = 0;
	return d;
}

Data.seek(d: self ref Data, n, where: int): int
{
	case where {
		Sys->SEEKSTART =>
			d.ptr = n;
		Sys->SEEKRELA =>
			d.ptr += n;
		Sys->SEEKEND =>
			d.ptr = len d.txt - n;
	}

	if(d.ptr < 0) {
		d.ptr = 0;
		return d.ptr;
	}

	if(d.ptr > len d.txt) {
		d.ptr =  len d.txt;
		return d.ptr;
	}

	return d.ptr;
}

Data.getc(d: self ref Data) : int
{
	if(d.ptr == len d.txt)
		return EOF;

	i := sys->sprint("%s", d.txt[d.ptr:d.ptr+1]);
	(c, nil, nil) := sys->byte2char(array of byte i, 0);
	d.ptr++;
	return c;
}

Data.ungetc(d: self ref Data) : int
{
	d.ptr--;
	if(d.ptr < 0)
		d.ptr = 0;

	return d.ptr;
}

Data.offset(d: self ref Data) : int
{
	return d.ptr;
}

Data.gets(d: self ref Data, sep : int) : string
{
	b := array[sys->UTFmax] of byte;
	used : int;
	s := "";
	for(c := d.getc(); c != EOF; c = d.getc()) {
		used = sys->char2byte(c, b, 0);
		s += string b[0:used];
		if(c == sep)
			break;
	}
	return s;
}

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].