forsyth20121205-174063a80cc13 years agoHistory
318 lines · 6.6 KB · Text
RawDownload
.TH BUFIO 2.SH NAMEbufio, bufiofill \- buffered input/output module.SH SYNOPSIS.EXinclude "bufio.m"; bufio := load Bufio Bufio->PATH;Iobuf: import bufio; SEEKSTART:  con Sys->SEEKSTART;SEEKRELA:   con Sys->SEEKRELA;SEEKEND:    con Sys->SEEKEND; OREAD:      con Sys->OREAD;OWRITE:     con Sys->OWRITE;ORDWR:      con Sys->ORDWR; EOF:        con -1;ERROR:      con -2; Iobuf: adt {    seek:   fn(b: self ref Iobuf, n: big, where: int): big;    offset: fn(b: self ref Iobuf): big;     read:   fn(b: self ref Iobuf, a: array of byte, n: int): int;    write:  fn(b: self ref Iobuf, a: array of byte, n: int): int;     getb:   fn(b: self ref Iobuf): int;    getc:   fn(b: self ref Iobuf): int;    gets:   fn(b: self ref Iobuf, sepchar: int): string;    gett:   fn(b: self ref Iobuf, sepstring: string): string;     ungetb: fn(b: self ref Iobuf): int;    ungetc: fn(b: self ref Iobuf): int;     putb:   fn(b: self ref Iobuf, b: byte): int;    putc:   fn(b: self ref Iobuf, c: int): int;    puts:   fn(b: self ref Iobuf, s: string): int;     flush:  fn(b: self ref Iobuf): int;    close:  fn(b: self ref Iobuf);     setfill: fn(b: self ref Iobuf, f: BufioFill);}; open:   fn(name: string, mode: int): ref Iobuf;create: fn(name: string, mode, perm: int): ref Iobuf;fopen:  fn(fd: ref Sys->FD, mode: int): ref Iobuf;aopen: fn(a: array of byte): ref Iobuf;sopen:  fn(s: string): ref Iobuf; BufioFill: module{    fill:   fn(b: ref Bufio->Iobuf): int;};.EE.SH DESCRIPTION.B Bufioprovides an interface for buffered I/O.A buffer is an adt whichis created with.BR open ,.BR fopen ,.BR create ,.B aopenand.BR sopen ..PP.B Opentakes two parameters, a.I filenameand a.IR mode .The mode must beone of.BR OREAD ,.BR OWRITE ,or.B ORDWR(also defined in the.B Sysmodule)..PP.B Createis similar, butcreates a new file if necessary, with file permissionsspecified by.IR perm(see.B createin.IR sys-open (2)),or truncates an existing file (without changing its permissions),before opening it in the given.IR mode ,and returning a reference to an.B Iobufinstance..PPBuffered I/O on an already open file is made possible using.BR "fopen" ,which takes a file descriptor.I fdand an open.IR mode ,which must be compatible with the mode of the file descriptor..PPThe file open functions return a.B ref.B Iobufto be used in subsequent calls.  Thus:.PP.EX	lc := bufio->open("/net/tcp/0/local", bufio->OREAD);	addr := lc.gets('\en');	lc = nil;.EE.PPwill open the file.B /net/tcp/0/localand read a line (including the terminating newlinecharacter) from this file to initialize the string variable.BR addr .The file is closed implicitly by discarding (assigning.B nilto) the onlyreference to its.BR Iobuf ..PPThe function.B aopenmakes the contents of an array of byte.I areadable through an Iobuf (it may not be written).The function.B sopensimilarly makes the contents of a string.I sreadable..PPProcesses can share the same instance of.B Bufioand safely open and close different files concurrently, but two processes must not access the same.B Iobufconcurrently; they must coordinate their access using some external mechanism(eg,.IR lock (2))..PPEach output file must be flushed or closed individually (see.B flushand.B closeoperations below)..PPThe calls implemented by.B Iobufare:.PP.TF setfill.PD.TP.BR seek , \ read ", and " writeEach has the same parameters as its complement in.B Sys(see.IR sys-seek (2),.IR sys-read (2)).Note that.BR SEEKSTARTetc. are defined by.B Bufioas well as by.BR Sys ,for use by.BR seek ..TP.B offsetReturn the current file offset in bytes, taking account of buffered data..TP.B getbRead a single byte from the buffered stream and return its valueas an.BR int ..TP.B getcRead a single Unicode character, encoded in UTF(see.IR utf (6)),andreturn its value as an.BR int ..TP.B getsRead a line, up to and including a character specified by.IR sepchar ,typically a newline.If none is found, read to the end of the file.The returned string includes the terminating character..TP.B gettRead characters until one of the characters in.IR sepstring .The returned string includes the separator.If none of the separator characters is found,read to the end of the file..TP.BR ungetb , \ ungetcUndoes the effect of the last.B getbor.BR getc ,so that a subsequent read will reread the byte.RB ( ungetb ),or reread the byte(s) of a UTF-encoded character.RB ( ungetc ).TP.BR putb , \ putc ", and " putsEachwrites its argument, a byte, a character, ora string, respectively.Text is encoded in UTF..TP.B setfillAssociates a.B BufioFillmodule instance.I fwith.B Iobuf.IR b ;discussed below..TP.B flushFlush remaining data in the buffer, if necessary.For files opened for writing, data is flushed to the file.For files opened for reading, any internally buffered data is discarded,and the next read will read from the file..TP.B closeFlush remaining data in the buffer, if necessary, close theassociated file, and discard buffers associated with the file.After close, no further method calls are allowed on the.B iobufadt..PPThe.B BufioFillmodule interface can be ignored by most applications.It allows an.B Iobufto be used to read data from an arbitrary source.There is no `standard' implementation to load.Instead,an application using this interface uses a separate.B BufioFillmodule instance such as.IR bufio-chanfill (2),or provides one itself using.IR sys-self (2).The resulting module reference is associated,using.BR setfill ,with an.B Iobufpreviously obtained by.BR sopen(the string parameter limits the buffer space allocated).It is up to the.B BufioFillmodule's implementation how its.B fillfunction replenishes the buffer;it should return the number of bytes now in the buffer, or.BR Bufio->EOF ..SH SOURCE.B /appl/lib/bufio.b.SH SEE ALSO.IR bufio-chanfill (2),.IR intro (2),.IR sys-open (2),.IR sys-read (2),.IR sys-seek (2).SH DIAGNOSTICSCalls that return a.B reftype(eg..RB open ,.BR fopen ,.BR gets ,and.BR gett )return.B nilwhen encountering end of file or errors.  When an error occurs, theerror string, printable with the.B %r format,will usually be set as a consequence of an error in the underlying.B Sysmodule.The other calls return.B EOFupon encountering end of file, and.B ERRORwhen encountering other errors..SH BUGSA given.B Iobufinstance may not be accessed concurrently..PPAn.B Iobufinstance must be manipulated by the same module instance that created it..PPThe.B BufioFillinterface is subject to change.