Skip to content

Commit e8893e9

Browse files
committed
upgrade ws client/server
1 parent 5ab1204 commit e8893e9

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

noson/src/private/builtin.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,26 @@ int string_to_timeout(const char *str, unsigned *ms)
218218
return 0;
219219
}
220220

221+
int string_to_size(const char *str, int64_t *sz)
222+
{
223+
float q = 0.0;
224+
char u[2] = { 0, 0 };
225+
int n = sscanf(str, "%f%c%c", &q, u, u+1);
226+
if (n < 1 || q < 0)
227+
return -(EINVAL);
228+
if (*u == 0)
229+
*sz = (int64_t)q;
230+
else if (memcmp("k", u, 2) == 0)
231+
*sz = (int64_t)(q * (1<<10));
232+
else if (memcmp("m", u, 2) == 0)
233+
*sz = (int64_t)(q * (1<<20));
234+
else if (memcmp("g", u, 2) == 0)
235+
*sz = (int64_t)(q * (1<<30));
236+
else
237+
return -(EINVAL);
238+
return 0;
239+
}
240+
221241
int hex_to_num(const char *str, int *num)
222242
{
223243
int val = 0;

noson/src/private/builtin.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ extern int string_to_double(const char *str, double *dbl);
6464
#define string_to_timeout __str2timeout
6565
extern int string_to_timeout(const char *str, unsigned *ms);
6666

67+
#define string_to_size __str2size
68+
extern int string_to_size(const char *str, int64_t *sz);
69+
6770
#define hex_to_num __hex2num
6871
extern int hex_to_num(const char *str, int *num);
6972

0 commit comments

Comments
 (0)