shithub: neoventi

ref: 4b60d3dd32efd00a1d07cb08662926ba9836719f
dir: /util.c/

View raw version
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "neoventi.h"

int
stru64int(char *s, u64int *r)
{
	char *t;
	u64int n, nn, m;
	int c;

	m = ((u64int)~(u64int)0) / 10;
	n = 0;
	for(t = s; ; t++){
		c = *t;
		if(c < '0' || c > '9')
			break;
		if(n > m)
			return -1;
		nn = n * 10 + c - '0';
		if(nn < n)
			return -1;
		n = nn;
	}
	*r = n;
	return s != t && *t == '\0';
}

int
u64log2(u64int v)
{
	for(int i = 0; i < 64; i++)
		if((v >> i) <= 1)
			return i;
	return -1;
}

int
stru32int(char *s, u32int *r)
{
	u64int tmp;
	if(stru64int(s, &tmp) < 0)
		return -1;
	if(tmp & 0xFFFFFFFF00000000)
		return -1;
	*r = (u32int)(tmp & 0xFFFFFFFF);
	return 0;
}

int
Brdu32(Biobufhdr *bio, u32int *u32)
{
	char *line = Brdline(bio, '\n');
	if(line == nil)
		return 0;
	return stru32int(line, u32) == 0;
}