shithub: spread

ref: 28539de81a1a7beb10e8281a9825b9c39e8482cf
dir: /util.c/

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

void
toupperil(char *s)
{
	while (*s) {
		if (*s >= 'a' && *s <= 'z')
			*s = (*s)-'a'+'A';
		s++;
	}
}

int
aton(char *a, char **b)
{
	int ret = 0;
	
	while (*a && *a >= 'A' && *a <= 'Z') {
		ret *= 'Z' - 'A' + 1;
		ret += *a - 'A' + 1;
		a++;
	}
	if (b)
		*b = a;
	return ret;
}

P
atop(char *a)
{
	P p;
	char *n;
	
	toupperil(a);
	p.x = aton(a, &n);
	p.y = atoi(n);
	return p;
}

char*
ntoa(int n)
{
	char ab[10];
	char buf[10];
	int r;
	int m = 'Z' - 'A' + 1;
	char *a = ab;
	char *b;
	
	do {
		r = n%m;
		*a = r + 'A' - 1;
		a++;
		n /= m;
	} while (n > 0 && a < (ab + sizeof(ab)));
	*a = 0;
	
	b = buf;
	a--;
	while (a >= ab) {
		*b = *a;
		a--;
		b++;
	}
	*b = 0;
	return buf;
}

char*
ptoa(P p)
{
	char buf[25];
	
	snprint(buf, sizeof(buf), "%s%d", ntoa(p.x), p.y);
	return buf;
}