mirror of https://git.kore.io/kore.git
Add kore_strtodouble().
Use it for http_argument_get_float() and http_argument_get_double().
This commit is contained in:
parent
b7669f49f7
commit
439a3b36f0
|
@ -8,7 +8,7 @@ worker_max_connections 5000
|
|||
|
||||
tls_dhparam dh2048.pem
|
||||
|
||||
validator v_id regex ^-?[0-9]*$
|
||||
validator v_id regex ^-?[0-9]*.?[0-9]+$
|
||||
|
||||
domain * {
|
||||
certfile cert/server.pem
|
||||
|
|
|
@ -1,11 +1,15 @@
|
|||
#include <kore/kore.h>
|
||||
#include <kore/http.h>
|
||||
|
||||
#include <float.h>
|
||||
|
||||
int page(struct http_request *);
|
||||
|
||||
int
|
||||
page(struct http_request *req)
|
||||
{
|
||||
float fl;
|
||||
double dbl;
|
||||
int16_t s16;
|
||||
u_int16_t u16;
|
||||
int32_t s32;
|
||||
|
@ -40,6 +44,12 @@ page(struct http_request *req)
|
|||
if (http_argument_get_uint64(req, "id", &u64))
|
||||
kore_buf_appendf(buf, "uint64\t%lu\n", u64);
|
||||
|
||||
if (http_argument_get_float(req, "id", &fl))
|
||||
kore_buf_appendf(buf, "float\t%g\n", fl);
|
||||
|
||||
if (http_argument_get_double(req, "id", &dbl))
|
||||
kore_buf_appendf(buf, "double\t%g\n", dbl);
|
||||
|
||||
data = kore_buf_release(buf, &len);
|
||||
http_response(req, 200, data, len);
|
||||
kore_free(data);
|
||||
|
|
|
@ -58,6 +58,8 @@ extern "C" {
|
|||
#define HTTP_ARG_TYPE_STRING 6
|
||||
#define HTTP_ARG_TYPE_INT64 7
|
||||
#define HTTP_ARG_TYPE_UINT64 8
|
||||
#define HTTP_ARG_TYPE_FLOAT 9
|
||||
#define HTTP_ARG_TYPE_DOUBLE 10
|
||||
|
||||
#define HTTP_STATE_ERROR 0
|
||||
#define HTTP_STATE_CONTINUE 1
|
||||
|
@ -108,6 +110,16 @@ struct http_arg {
|
|||
COPY_ARG_TYPE(nval, type); \
|
||||
} while (0)
|
||||
|
||||
#define COPY_ARG_DOUBLE(min, max, type) \
|
||||
do { \
|
||||
int err; \
|
||||
type nval; \
|
||||
nval = kore_strtodouble(q->s_value, min, max, &err); \
|
||||
if (err != KORE_RESULT_OK) \
|
||||
return (KORE_RESULT_ERROR); \
|
||||
COPY_ARG_TYPE(nval, type); \
|
||||
} while (0)
|
||||
|
||||
#define COPY_ARG_INT(min, max, type) \
|
||||
do { \
|
||||
int err; \
|
||||
|
@ -159,6 +171,11 @@ struct http_arg {
|
|||
#define http_argument_get_int64(r, n, o) \
|
||||
http_argument_type(r, n, NULL, o, HTTP_ARG_TYPE_INT64)
|
||||
|
||||
#define http_argument_get_float(r, n, o) \
|
||||
http_argument_type(r, n, NULL, o, HTTP_ARG_TYPE_FLOAT)
|
||||
|
||||
#define http_argument_get_double(r, n, o) \
|
||||
http_argument_type(r, n, NULL, o, HTTP_ARG_TYPE_DOUBLE)
|
||||
|
||||
struct http_file {
|
||||
char *name;
|
||||
|
|
|
@ -587,6 +587,7 @@ int kore_split_string(char *, const char *, char **, size_t);
|
|||
void kore_strip_chars(char *, const char, char **);
|
||||
int kore_snprintf(char *, size_t, int *, const char *, ...);
|
||||
long long kore_strtonum(const char *, int, long long, long long, int *);
|
||||
double kore_strtodouble(const char *, long double, long double, int *);
|
||||
int kore_base64_encode(const void *, size_t, char **);
|
||||
int kore_base64_decode(const char *, u_int8_t **, size_t *);
|
||||
void *kore_mem_find(void *, size_t, void *, size_t);
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include <ctype.h>
|
||||
#include <fcntl.h>
|
||||
#include <inttypes.h>
|
||||
#include <float.h>
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
@ -735,6 +736,12 @@ http_argument_get(struct http_request *req, const char *name,
|
|||
case HTTP_ARG_TYPE_UINT64:
|
||||
COPY_AS_INTTYPE_64(u_int64_t, 0);
|
||||
return (KORE_RESULT_OK);
|
||||
case HTTP_ARG_TYPE_FLOAT:
|
||||
COPY_ARG_DOUBLE(-FLT_MAX, FLT_MAX, float);
|
||||
return (KORE_RESULT_OK);
|
||||
case HTTP_ARG_TYPE_DOUBLE:
|
||||
COPY_ARG_DOUBLE(-DBL_MAX, DBL_MAX, double);
|
||||
return (KORE_RESULT_OK);
|
||||
case HTTP_ARG_TYPE_STRING:
|
||||
*out = q->s_value;
|
||||
return (KORE_RESULT_OK);
|
||||
|
|
32
src/utils.c
32
src/utils.c
|
@ -225,6 +225,38 @@ kore_strtonum64(const char *str, int sign, int *err)
|
|||
return ((sign) ? (u_int64_t)ll : l);
|
||||
}
|
||||
|
||||
double
|
||||
kore_strtodouble(const char *str, long double min, long double max, int *err)
|
||||
{
|
||||
double d;
|
||||
char *ep;
|
||||
|
||||
if (min > max) {
|
||||
*err = KORE_RESULT_ERROR;
|
||||
return (0);
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
d = strtod(str, &ep);
|
||||
if (d == 0 || errno == ERANGE || str == ep || *ep != '\0') {
|
||||
*err = KORE_RESULT_ERROR;
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (d < min) {
|
||||
*err = KORE_RESULT_ERROR;
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (d > max) {
|
||||
*err = KORE_RESULT_ERROR;
|
||||
return (0);
|
||||
}
|
||||
|
||||
*err = KORE_RESULT_OK;
|
||||
return (d);
|
||||
}
|
||||
|
||||
int
|
||||
kore_split_string(char *input, const char *delim, char **out, size_t ele)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue