Support for negative numbers in guac_write_int

This commit is contained in:
Michael Jumper 2011-04-01 00:53:53 -07:00
parent ab3f09dfb0
commit 693fe2a1f5
2 changed files with 15 additions and 5 deletions

View File

@ -123,7 +123,7 @@ GUACIO* guac_open(int fd);
* @param i The unsigned int to write.
* @return Zero on success, or non-zero if an error occurs while writing.
*/
ssize_t guac_write_int(GUACIO* io, unsigned long i);
ssize_t guac_write_int(GUACIO* io, long i);
/**
* Writes the given string to the given GUACIO object. The data

View File

@ -98,21 +98,31 @@ ssize_t __guac_write(GUACIO* io, const char* buf, int count) {
return retval;
}
ssize_t guac_write_int(GUACIO* io, unsigned long i) {
ssize_t guac_write_int(GUACIO* io, long i) {
char buffer[128];
char* ptr = &(buffer[127]);
int nonneg;
/* Obtain non-negative value */
if (i < 0) nonneg = -i;
else nonneg = i;
/* Generate numeric string */
*ptr = 0;
do {
ptr--;
*ptr = '0' + (i % 10);
*ptr = '0' + (nonneg % 10);
i /= 10;
nonneg /= 10;
} while (i > 0 && ptr >= buffer);
} while (nonneg > 0 && ptr >= buffer);
/* Prepend with dash if negative */
if (i < 0 && ptr >= buffer) *(--ptr) = '-';
return guac_write_string(io, ptr);