hbase

heirloom base
git clone git://git.2f30.org/hbase
Log | Files | Refs | README

ib_getw.c (2077B)


      1 /*
      2  * Copyright (c) 2003 Gunnar Ritter
      3  *
      4  * This software is provided 'as-is', without any express or implied
      5  * warranty. In no event will the authors be held liable for any damages
      6  * arising from the use of this software.
      7  *
      8  * Permission is granted to anyone to use this software for any purpose,
      9  * including commercial applications, and to alter it and redistribute
     10  * it freely, subject to the following restrictions:
     11  *
     12  * 1. The origin of this software must not be misrepresented; you must not
     13  *    claim that you wrote the original software. If you use this software
     14  *    in a product, an acknowledgment in the product documentation would be
     15  *    appreciated but is not required.
     16  *
     17  * 2. Altered source versions must be plainly marked as such, and must not be
     18  *    misrepresented as being the original software.
     19  *
     20  * 3. This notice may not be removed or altered from any source distribution.
     21  */
     22 /*	Sccsid @(#)ib_getw.c	1.5 (gritter) 7/16/04	*/
     23 
     24 #include	<stdlib.h>
     25 #include	<string.h>
     26 #include	"iblok.h"
     27 #include	"mbtowi.h"
     28 
     29 char *
     30 ib_getw(struct iblok *ip, wint_t *wc, int *len)
     31 {
     32 	size_t	rest;
     33 	int	c, i, n;
     34 
     35 	i = 0;
     36 	rest = ip->ib_mend - ip->ib_mcur;
     37 	if (rest && ip->ib_mcur > ip->ib_mbuf) {
     38 		do
     39 			ip->ib_mbuf[i] = ip->ib_mcur[i];
     40 		while (i++, --rest);
     41 	} else if (ip->ib_incompl) {
     42 		ip->ib_incompl = 0;
     43 		*wc = WEOF;
     44 		ip->ib_mend = ip->ib_mcur = NULL;
     45 		return NULL;
     46 	}
     47 	if (i == 0) {
     48 		c = ib_get(ip);
     49 		if (c == EOF) {
     50 			*wc = WEOF;
     51 			ip->ib_mend = ip->ib_mcur = NULL;
     52 			return NULL;
     53 		}
     54 		ip->ib_mbuf[i++] = (char)c;
     55 	}
     56 	if (ip->ib_mbuf[0] & 0200) {
     57 		while (ip->ib_mbuf[i-1] != '\n' && i < ip->ib_mb_cur_max &&
     58 				ip->ib_incompl == 0) {
     59 			c = ib_get(ip);
     60 			if (c != EOF)
     61 				ip->ib_mbuf[i++] = (char)c;
     62 			else
     63 				ip->ib_incompl = 1;
     64 		}
     65 		n = mbtowi(wc, ip->ib_mbuf, i);
     66 		if (n < 0) {
     67 			*len = 1;
     68 			*wc = WEOF;
     69 		} else if (n == 0) {
     70 			*len = 1;
     71 			*wc = '\0';
     72 		} else
     73 			*len = n;
     74 	} else {
     75 		*wc = ip->ib_mbuf[0];
     76 		*len = n = 1;
     77 	}
     78 	ip->ib_mcur = &ip->ib_mbuf[*len];
     79 	ip->ib_mend = &ip->ib_mcur[i - *len];
     80 	return ip->ib_mbuf;
     81 }