From d79591c277884772021cc2a9419da068b4abfa25 Mon Sep 17 00:00:00 2001 From: default Date: Fri, 7 Oct 2022 15:06:17 +0200 Subject: [PATCH] Backport from xs. --- xs.h | 53 +++++++++++++++++++++++++++++++++++++++++++---------- xs_json.h | 19 +------------------ xs_socket.h | 8 ++++---- 3 files changed, 48 insertions(+), 32 deletions(-) diff --git a/xs.h b/xs.h index 0537c33..89f2ca9 100644 --- a/xs.h +++ b/xs.h @@ -78,8 +78,9 @@ char *xs_dict_get(char *dict, const char *key); d_char *xs_dict_del(d_char *dict, const char *key); d_char *xs_dict_set(d_char *dict, const char *key, const char *data); d_char *xs_val_new(xstype t); -d_char *xs_number_new(float f); -float xs_number_get(char *v); +d_char *xs_number_new(double f); +double xs_number_get(char *v); +char *xs_number_str(char *v); extern int _xs_debug; @@ -176,7 +177,7 @@ int xs_size(const char *data) break; case XSTYPE_NUMBER: - len = sizeof(float) + 1; + len = 1 + xs_size(data + 1); break; @@ -699,30 +700,62 @@ d_char *xs_val_new(xstype t) } -d_char *xs_number_new(float f) +/** numbers */ + +d_char *xs_number_new(double f) /* adds a new number value */ { - d_char *v = xs_realloc(NULL, _xs_blk_size(1 + sizeof(float))); + d_char *v; + char tmp[64]; + + snprintf(tmp, sizeof(tmp), "%.15lf", f); + + /* strip useless zeros */ + if (strchr(tmp, '.') != NULL) { + char *ptr; + + for (ptr = tmp + strlen(tmp) - 1; *ptr == '0'; ptr--); + + if (*ptr != '.') + ptr++; + + *ptr = '\0'; + } + + /* alloc for the marker and the full string */ + v = xs_realloc(NULL, _xs_blk_size(1 + xs_size(tmp))); v[0] = XSTYPE_NUMBER; - memcpy(&v[1], &f, sizeof(float)); + memcpy(&v[1], tmp, xs_size(tmp)); return v; } -float xs_number_get(char *v) -/* gets the number as a float */ +double xs_number_get(char *v) +/* gets the number as a double */ { - float f = 0.0; + double f = 0.0; if (v[0] == XSTYPE_NUMBER) - memcpy(&f, &v[1], sizeof(float)); + f = atof(&v[1]); return f; } +char *xs_number_str(char *v) +/* gets the number as a string */ +{ + char *p = NULL; + + if (v[0] == XSTYPE_NUMBER) + p = &v[1]; + + return p; +} + + #endif /* XS_IMPLEMENTATION */ #endif /* _XS_H */ diff --git a/xs_json.h b/xs_json.h index 871dfcf..13c3ebb 100644 --- a/xs_json.h +++ b/xs_json.h @@ -91,24 +91,7 @@ d_char *_xs_json_dumps(d_char *s, char *data, int level, int indent) break; case XSTYPE_NUMBER: - { - char tmp[32]; - snprintf(tmp, sizeof(tmp), "%.15f", xs_number_get(data)); - - /* strip useless zeros */ - if (strchr(tmp, '.') != NULL) { - char *ptr; - - for (ptr = tmp + strlen(tmp) - 1; *ptr == '0'; ptr--); - - if (*ptr != '.') - ptr++; - - *ptr = '\0'; - } - - s = xs_str_cat(s, tmp); - } + s = xs_str_cat(s, xs_number_str(data)); break; case XSTYPE_LIST: diff --git a/xs_socket.h b/xs_socket.h index e4a18ec..a3fe6a4 100644 --- a/xs_socket.h +++ b/xs_socket.h @@ -4,7 +4,7 @@ #define _XS_SOCKET_H -int xs_socket_timeout(int s, float rto, float sto); +int xs_socket_timeout(int s, double rto, double sto); int xs_socket_server(char *addr, int port); FILE *xs_socket_accept(int rs); @@ -16,7 +16,7 @@ FILE *xs_socket_accept(int rs); #include -int xs_socket_timeout(int s, float rto, float sto) +int xs_socket_timeout(int s, double rto, double sto) /* sets the socket timeout in seconds */ { struct timeval tv; @@ -24,14 +24,14 @@ int xs_socket_timeout(int s, float rto, float sto) if (rto > 0.0) { tv.tv_sec = (int)rto; - tv.tv_usec = (int)((rto - (float)(int)rto) * 1000000.0); + tv.tv_usec = (int)((rto - (double)(int)rto) * 1000000.0); ret = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)); } if (sto > 0.0) { tv.tv_sec = (int)sto; - tv.tv_usec = (int)((sto - (float)(int)sto) * 1000000.0); + tv.tv_usec = (int)((sto - (double)(int)sto) * 1000000.0); ret = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)); }