Backport from xs.

This commit is contained in:
default 2022-10-07 15:06:17 +02:00
parent a4cf695e60
commit d79591c277
3 changed files with 48 additions and 32 deletions

53
xs.h
View File

@ -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_del(d_char *dict, const char *key);
d_char *xs_dict_set(d_char *dict, const char *key, const char *data); d_char *xs_dict_set(d_char *dict, const char *key, const char *data);
d_char *xs_val_new(xstype t); d_char *xs_val_new(xstype t);
d_char *xs_number_new(float f); d_char *xs_number_new(double f);
float xs_number_get(char *v); double xs_number_get(char *v);
char *xs_number_str(char *v);
extern int _xs_debug; extern int _xs_debug;
@ -176,7 +177,7 @@ int xs_size(const char *data)
break; break;
case XSTYPE_NUMBER: case XSTYPE_NUMBER:
len = sizeof(float) + 1; len = 1 + xs_size(data + 1);
break; 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 */ /* 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; v[0] = XSTYPE_NUMBER;
memcpy(&v[1], &f, sizeof(float)); memcpy(&v[1], tmp, xs_size(tmp));
return v; return v;
} }
float xs_number_get(char *v) double xs_number_get(char *v)
/* gets the number as a float */ /* gets the number as a double */
{ {
float f = 0.0; double f = 0.0;
if (v[0] == XSTYPE_NUMBER) if (v[0] == XSTYPE_NUMBER)
memcpy(&f, &v[1], sizeof(float)); f = atof(&v[1]);
return f; 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_IMPLEMENTATION */
#endif /* _XS_H */ #endif /* _XS_H */

View File

@ -91,24 +91,7 @@ d_char *_xs_json_dumps(d_char *s, char *data, int level, int indent)
break; break;
case XSTYPE_NUMBER: case XSTYPE_NUMBER:
{ s = xs_str_cat(s, xs_number_str(data));
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);
}
break; break;
case XSTYPE_LIST: case XSTYPE_LIST:

View File

@ -4,7 +4,7 @@
#define _XS_SOCKET_H #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); int xs_socket_server(char *addr, int port);
FILE *xs_socket_accept(int rs); FILE *xs_socket_accept(int rs);
@ -16,7 +16,7 @@ FILE *xs_socket_accept(int rs);
#include <netinet/in.h> #include <netinet/in.h>
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 */ /* sets the socket timeout in seconds */
{ {
struct timeval tv; struct timeval tv;
@ -24,14 +24,14 @@ int xs_socket_timeout(int s, float rto, float sto)
if (rto > 0.0) { if (rto > 0.0) {
tv.tv_sec = (int)rto; 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)); ret = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
} }
if (sto > 0.0) { if (sto > 0.0) {
tv.tv_sec = (int)sto; 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)); ret = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv));
} }