shithub: spread

ref: eaadfa055fc6b9e7074449a8ad6b0a4ff25cb346
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;
}