mirror of
https://codeberg.org/grunfink/snac2.git
synced 2024-11-22 05:15:04 +00:00
Backport from xs.
This commit is contained in:
parent
a4cf695e60
commit
d79591c277
3 changed files with 48 additions and 32 deletions
53
xs.h
53
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 */
|
||||
|
|
19
xs_json.h
19
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:
|
||||
|
|
|
@ -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 <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 */
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue