Support for negative numbers in guac_write_int
This commit is contained in:
parent
ab3f09dfb0
commit
693fe2a1f5
@ -123,7 +123,7 @@ GUACIO* guac_open(int fd);
|
|||||||
* @param i The unsigned int to write.
|
* @param i The unsigned int to write.
|
||||||
* @return Zero on success, or non-zero if an error occurs while writing.
|
* @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
|
* Writes the given string to the given GUACIO object. The data
|
||||||
|
@ -98,21 +98,31 @@ ssize_t __guac_write(GUACIO* io, const char* buf, int count) {
|
|||||||
return retval;
|
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 buffer[128];
|
||||||
char* ptr = &(buffer[127]);
|
char* ptr = &(buffer[127]);
|
||||||
|
int nonneg;
|
||||||
|
|
||||||
|
/* Obtain non-negative value */
|
||||||
|
if (i < 0) nonneg = -i;
|
||||||
|
else nonneg = i;
|
||||||
|
|
||||||
|
/* Generate numeric string */
|
||||||
|
|
||||||
*ptr = 0;
|
*ptr = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
|
||||||
ptr--;
|
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);
|
return guac_write_string(io, ptr);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user