Skip to content

Instantly share code, notes, and snippets.

@alexmchale
Created March 29, 2010 13:08
Show Gist options
  • Save alexmchale/347814 to your computer and use it in GitHub Desktop.
Save alexmchale/347814 to your computer and use it in GitHub Desktop.
static int getDoubleFromObject(redisClient *c, robj *o, double *value) {
double parsedValue;
char *eptr = NULL;
if (o && o->type != REDIS_STRING) {
addReplySds(c,sdsnew("-ERR value is not a double\r\n"));
return REDIS_ERR;
}
if (o == NULL)
parsedValue = 0;
else if (o->encoding == REDIS_ENCODING_RAW)
parsedValue = strtod(o->ptr, &eptr);
else if (o->encoding == REDIS_ENCODING_INT)
parsedValue = (long)o->ptr;
else
redisAssert(1 != 1);
if (eptr != NULL && *eptr != '\0') {
addReplySds(c,sdsnew("-ERR value is not a double\r\n"));
return REDIS_ERR;
}
*value = parsedValue;
return REDIS_OK;
}
static int getLongFromObject(redisClient *c, robj *o, long *value) {
long parsedValue;
char *eptr = NULL;
if (o && o->type != REDIS_STRING) {
addReplySds(c,sdsnew("-ERR value is not an integer\r\n"));
return REDIS_ERR;
}
if (o == NULL)
parsedValue = 0;
else if (o->encoding == REDIS_ENCODING_RAW)
parsedValue = strtol(o->ptr, &eptr, 10);
else if (o->encoding == REDIS_ENCODING_INT)
parsedValue = (long)o->ptr;
else
redisAssert(1 != 1);
if (eptr != NULL && *eptr != '\0') {
addReplySds(c,sdsnew("-ERR value is not an integer\r\n"));
return REDIS_ERR;
}
*value = parsedValue;
return REDIS_OK;
}
static int getLongLongFromObject(redisClient *c, robj *o, long long *value) {
long long parsedValue;
char *eptr = NULL;
if (o && o->type != REDIS_STRING) {
addReplySds(c,sdsnew("-ERR value is not an integer\r\n"));
return REDIS_ERR;
}
if (o == NULL)
parsedValue = 0;
else if (o->encoding == REDIS_ENCODING_RAW)
parsedValue = strtoll(o->ptr, &eptr, 10);
else if (o->encoding == REDIS_ENCODING_INT)
parsedValue = (long)o->ptr;
else
redisAssert(1 != 1);
if (eptr != NULL && *eptr != '\0') {
addReplySds(c,sdsnew("-ERR value is not an integer\r\n"));
return REDIS_ERR;
}
*value = parsedValue;
return REDIS_OK;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment