sandbox/io.cc

42 lines
647 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
2018-02-25 06:35:58 +00:00
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) {
2018-02-25 06:35:58 +00:00
interface->wrch('-');
2018-02-24 05:15:33 +00:00
n = ~n;
2018-02-25 06:35:58 +00:00
if (n == 0) {
neg = false;
n++;
}
}
else if (n == 0) {
interface->wrch('0');
return;
}
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;
2018-02-25 06:35:58 +00:00
interface->wrbuf(buf+i, buflen);
}