sandbox/io.cc

34 lines
544 B
C++
Raw Normal View History

#include "defs.h"
#include "io.h"
#include <string.h>
2018-02-24 05:15:33 +00:00
static constexpr size_t nbuflen = 11;
void
write_num(IO &interface, KF_INT n)
{
2018-02-24 05:15:33 +00:00
// TODO(kyle): make the size of the buffer depend on the size of
// KF_INT.
2018-02-24 05:15:33 +00:00
char buf[nbuflen];
uint8_t i = nbuflen;
memset(buf, 0, i);
2018-02-24 05:15:33 +00:00
bool neg = n < 0;
if (n < 0) {
interface.wrch('-');
2018-02-24 05:15:33 +00:00
n = ~n;
}
while (n != 0) {
char ch = (n % 10) + '0';
2018-02-24 05:15:33 +00:00
if (neg && (i == nbuflen)) ch++;
buf[i-1] = ch;
i--;
n /= 10;
}
2018-02-24 05:15:33 +00:00
uint8_t buflen = nbuflen - i % nbuflen;
interface.wrbuf(buf+i, buflen);
}