Added guac_parse_int, which handles 64-bit integers regardless of native arch.

This commit is contained in:
Michael Jumper 2011-04-29 01:35:17 -07:00
parent 8242c37f11
commit 532ad2137b
3 changed files with 31 additions and 2 deletions

View File

@ -114,6 +114,17 @@ typedef struct GUACIO {
*/ */
GUACIO* guac_open(int fd); GUACIO* guac_open(int fd);
/**
* Parses the given string as a decimal number, returning the result as
* a 64-bit signed value. This value will be 64-bit regardless of platform.
*
* @param str The string to parse into a 64-bit integer.
* @return The 64-bit integer representation of the number in the given
* string, undefined if the string does not contain a properly
* formatted number.
*/
int64_t guac_parse_int(const char* str);
/** /**
* Writes the given unsigned int to the given GUACIO object. The data * Writes the given unsigned int to the given GUACIO object. The data
* written may be buffered until the buffer is flushed automatically or * written may be buffered until the buffer is flushed automatically or

View File

@ -53,9 +53,9 @@ __guac_instruction_handler_mapping __guac_instruction_handler_map[] = {
}; };
/* Guacamole instruction handlers */ /* Guacamole instruction handlers */
#include <stdio.h>
int __guac_handle_sync(guac_client* client, guac_instruction* instruction) { int __guac_handle_sync(guac_client* client, guac_instruction* instruction) {
guac_timestamp_t timestamp = atol(instruction->argv[0]); guac_timestamp_t timestamp = guac_parse_int(instruction->argv[0]);
/* Error if timestamp is in future */ /* Error if timestamp is in future */
if (timestamp > client->last_sent_timestamp) if (timestamp > client->last_sent_timestamp)

View File

@ -98,6 +98,24 @@ ssize_t __guac_write(GUACIO* io, const char* buf, int count) {
return retval; return retval;
} }
int64_t guac_parse_int(const char* str) {
int sign = 1;
int64_t num = 0;
for (; *str != '\0'; str++) {
if (*str == '-')
sign = -sign;
else
num = num * 10 + (*str - '0');
}
return num * sign;
}
ssize_t guac_write_int(GUACIO* io, int64_t i) { ssize_t guac_write_int(GUACIO* io, int64_t i) {
char buffer[128]; char buffer[128];