shithub: spread

ref: 39a4f1b284bab0a4587a845c53cdb53bcae625c8
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*
ptoa(P p)
{
	char ab[10];
	char buf[25];
	int r;
	int m = 'Z' - 'A' + 1;
	char *a = ab;
	char *b;
	
	do {
		r = p.x%m;
		*a = r + 'A' - 1;
		a++;
		p.x /= m;
	} while (p.x > 0 && a < (ab + sizeof(ab)));
	*a = 0;
	
	b = buf;
	a--;
	while (a >= ab) {
		*b = *a;
		a--;
		b++;
	}
	*b = 0;
	
	snprint(b, sizeof(buf) - (buf-b), "%d", p.y);
	return strdup(buf);
}