The Go Programming Language

Text file src/libbio/brdline.c

     1	/*
     2	http://code.google.com/p/inferno-os/source/browse/libbio/brdline.c
     3	
     4		Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     5		Revisions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com).  All rights reserved.
     6	
     7	Permission is hereby granted, free of charge, to any person obtaining a copy
     8	of this software and associated documentation files (the "Software"), to deal
     9	in the Software without restriction, including without limitation the rights
    10	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11	copies of the Software, and to permit persons to whom the Software is
    12	furnished to do so, subject to the following conditions:
    13	
    14	The above copyright notice and this permission notice shall be included in
    15	all copies or substantial portions of the Software.
    16	
    17	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    20	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23	THE SOFTWARE.
    24	*/
    25	
    26	#include	<u.h>
    27	#include	<libc.h>
    28	#include	<bio.h>
    29	
    30	void*
    31	Brdline(Biobuf *bp, int delim)
    32	{
    33		char *ip, *ep;
    34		int i, j;
    35	
    36		i = -bp->icount;
    37		if(i == 0) {
    38			/*
    39			 * eof or other error
    40			 */
    41			if(bp->state != Bractive) {
    42				if(bp->state == Bracteof)
    43					bp->state = Bractive;
    44				bp->rdline = 0;
    45				bp->gbuf = bp->ebuf;
    46				return 0;
    47			}
    48		}
    49	
    50		/*
    51		 * first try in remainder of buffer (gbuf doesn't change)
    52		 */
    53		ip = (char*)bp->ebuf - i;
    54		ep = memchr(ip, delim, i);
    55		if(ep) {
    56			j = (ep - ip) + 1;
    57			bp->rdline = j;
    58			bp->icount += j;
    59			return ip;
    60		}
    61	
    62		/*
    63		 * copy data to beginning of buffer
    64		 */
    65		if(i < bp->bsize)
    66			memmove(bp->bbuf, ip, i);
    67		bp->gbuf = bp->bbuf;
    68	
    69		/*
    70		 * append to buffer looking for the delim
    71		 */
    72		ip = (char*)bp->bbuf + i;
    73		while(i < bp->bsize) {
    74			j = read(bp->fid, ip, bp->bsize-i);
    75			if(j <= 0) {
    76				/*
    77				 * end of file with no delim
    78				 */
    79				memmove(bp->ebuf-i, bp->bbuf, i);
    80				bp->rdline = i;
    81				bp->icount = -i;
    82				bp->gbuf = bp->ebuf-i;
    83				return 0;
    84			}
    85			bp->offset += j;
    86			i += j;
    87			ep = memchr(ip, delim, j);
    88			if(ep) {
    89				/*
    90				 * found in new piece
    91				 * copy back up and reset everything
    92				 */
    93				ip = (char*)bp->ebuf - i;
    94				if(i < bp->bsize){
    95					memmove(ip, bp->bbuf, i);
    96					bp->gbuf = (unsigned char*)ip;
    97				}
    98				j = (ep - (char*)bp->bbuf) + 1;
    99				bp->rdline = j;
   100				bp->icount = j - i;
   101				return ip;
   102			}
   103			ip += j;
   104		}
   105	
   106		/*
   107		 * full buffer without finding
   108		 */
   109		bp->rdline = bp->bsize;
   110		bp->icount = -bp->bsize;
   111		bp->gbuf = bp->bbuf;
   112		return 0;
   113	}
   114	
   115	int
   116	Blinelen(Biobuf *bp)
   117	{
   118	
   119		return bp->rdline;
   120	}

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.