[master] f7bc45c Move the numerical value functions to _utils
Poul-Henning Kamp
phk at FreeBSD.org
Mon Dec 11 09:22:12 UTC 2017
commit f7bc45c32cfcdc494484ab75e477f049d3efa4a3
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Sun Dec 10 11:33:03 2017 +0000
Move the numerical value functions to _utils
diff --git a/lib/libvcc/vcc_compile.h b/lib/libvcc/vcc_compile.h
index 074e66c..d19e64f 100644
--- a/lib/libvcc/vcc_compile.h
+++ b/lib/libvcc/vcc_compile.h
@@ -281,9 +281,6 @@ void *TlAlloc(struct vcc *tl, unsigned len);
char *TlDup(struct vcc *tl, const char *s);
/* vcc_expr.c */
-double vcc_DoubleVal(struct vcc *tl);
-void vcc_Duration(struct vcc *tl, double *);
-unsigned vcc_UintVal(struct vcc *tl);
void vcc_Expr(struct vcc *tl, vcc_type_t typ);
void vcc_Expr_Call(struct vcc *tl, const struct symbol *sym);
void vcc_Expr_Init(struct vcc *tl);
@@ -309,6 +306,12 @@ void Resolve_Sockaddr(struct vcc *tl, const char *host, const char *defport,
const char **ipv4, const char **ipv4_ascii, const char **ipv6,
const char **ipv6_ascii, const char **p_ascii, int maxips,
const struct token *t_err, const char *errid);
+double vcc_TimeUnit(struct vcc *);
+void vcc_ByteVal(struct vcc *, double *);
+void vcc_NumVal(struct vcc *, double *, int *);
+double vcc_DoubleVal(struct vcc *tl);
+void vcc_Duration(struct vcc *tl, double *);
+unsigned vcc_UintVal(struct vcc *tl);
/* vcc_storage.c */
void vcc_stevedore(struct vcc *vcc, const char *stv_name);
diff --git a/lib/libvcc/vcc_expr.c b/lib/libvcc/vcc_expr.c
index efc84de..ec3d1d4 100644
--- a/lib/libvcc/vcc_expr.c
+++ b/lib/libvcc/vcc_expr.c
@@ -49,161 +49,6 @@ struct expr {
#define EXPR_STR_CONST (1<<2) // Last STRING_LIST elem is "..."
struct token *t1, *t2;
};
-
-/*--------------------------------------------------------------------
- * Recognize and convert units of time, return seconds.
- */
-
-static double
-vcc_TimeUnit(struct vcc *tl)
-{
- double sc = 1.0;
-
- assert(tl->t->tok == ID);
- if (vcc_IdIs(tl->t, "ms"))
- sc = 1e-3;
- else if (vcc_IdIs(tl->t, "s"))
- sc = 1.0;
- else if (vcc_IdIs(tl->t, "m"))
- sc = 60.0;
- else if (vcc_IdIs(tl->t, "h"))
- sc = 60.0 * 60.0;
- else if (vcc_IdIs(tl->t, "d"))
- sc = 60.0 * 60.0 * 24.0;
- else if (vcc_IdIs(tl->t, "w"))
- sc = 60.0 * 60.0 * 24.0 * 7.0;
- else if (vcc_IdIs(tl->t, "y"))
- sc = 60.0 * 60.0 * 24.0 * 365.0;
- else {
- VSB_printf(tl->sb, "Unknown time unit ");
- vcc_ErrToken(tl, tl->t);
- VSB_printf(tl->sb,
- ". Legal are 'ms', 's', 'm', 'h', 'd', 'w' and 'y'\n");
- vcc_ErrWhere(tl, tl->t);
- return (1.0);
- }
- vcc_NextToken(tl);
- return (sc);
-}
-
-/*--------------------------------------------------------------------
- * Recognize and convert { CNUM } to unsigned value
- * The tokenizer made sure we only get digits.
- */
-
-unsigned
-vcc_UintVal(struct vcc *tl)
-{
- unsigned d = 0;
- const char *p;
-
- Expect(tl, CNUM);
- for (p = tl->t->b; p < tl->t->e; p++) {
- d *= 10;
- d += *p - '0';
- }
- vcc_NextToken(tl);
- return (d);
-}
-
-/*--------------------------------------------------------------------
- * Recognize and convert { CNUM [ '.' [ CNUM ] ] } to double value
- * The tokenizer made sure we only get digits and a '.'
- */
-
-static void
-vcc_NumVal(struct vcc *tl, double *d, int *frac)
-{
- double e = 0.1;
- const char *p;
-
- *frac = 0;
- *d = 0.0;
- Expect(tl, CNUM);
- if (tl->err) {
- *d = NAN;
- return;
- }
- for (p = tl->t->b; p < tl->t->e; p++) {
- *d *= 10;
- *d += *p - '0';
- }
- vcc_NextToken(tl);
- if (tl->t->tok != '.')
- return;
- *frac = 1;
- vcc_NextToken(tl);
- if (tl->t->tok != CNUM)
- return;
- for (p = tl->t->b; p < tl->t->e; p++) {
- *d += (*p - '0') * e;
- e *= 0.1;
- }
- vcc_NextToken(tl);
-}
-
-double
-vcc_DoubleVal(struct vcc *tl)
-{
- double d;
- int i;
-
- vcc_NumVal(tl, &d, &i);
- return (d);
-}
-
-/*--------------------------------------------------------------------*/
-
-void
-vcc_Duration(struct vcc *tl, double *d)
-{
- double v, sc;
-
- v = vcc_DoubleVal(tl);
- ERRCHK(tl);
- ExpectErr(tl, ID);
- sc = vcc_TimeUnit(tl);
- *d = v * sc;
-}
-
-/*--------------------------------------------------------------------*/
-
-static void
-vcc_ByteVal(struct vcc *tl, double *d)
-{
- double v, sc;
-
- v = vcc_DoubleVal(tl);
- ERRCHK(tl);
- if (tl->t->tok != ID) {
- VSB_printf(tl->sb, "Expected BYTES unit (B, KB, MB...) got ");
- vcc_ErrToken(tl, tl->t);
- VSB_printf(tl->sb, "\n");
- vcc_ErrWhere(tl, tl->t);
- return;
- }
- if (vcc_IdIs(tl->t, "B"))
- sc = 1.;
- else if (vcc_IdIs(tl->t, "KB"))
- sc = 1024.;
- else if (vcc_IdIs(tl->t, "MB"))
- sc = 1024. * 1024.;
- else if (vcc_IdIs(tl->t, "GB"))
- sc = 1024. * 1024. * 1024.;
- else if (vcc_IdIs(tl->t, "TB"))
- sc = 1024. * 1024. * 1024. * 1024.;
- else {
- VSB_printf(tl->sb, "Unknown BYTES unit ");
- vcc_ErrToken(tl, tl->t);
- VSB_printf(tl->sb,
- ". Legal are 'B', 'KB', 'MB', 'GB' and 'TB'\n");
- vcc_ErrWhere(tl, tl->t);
- return;
- }
- vcc_NextToken(tl);
- *d = v * sc;
-}
-
/*--------------------------------------------------------------------
* Facility for carrying expressions around and do text-processing on
* them.
diff --git a/lib/libvcc/vcc_utils.c b/lib/libvcc/vcc_utils.c
index 9db35bb..8d83897 100644
--- a/lib/libvcc/vcc_utils.c
+++ b/lib/libvcc/vcc_utils.c
@@ -29,6 +29,7 @@
#include "config.h"
+#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
@@ -249,3 +250,158 @@ Resolve_Sockaddr(struct vcc *tl,
VSB_destroy(&rss->vsb);
FREE_OBJ(rss);
}
+
+/*--------------------------------------------------------------------
+ * Recognize and convert units of time, return seconds.
+ */
+
+double
+vcc_TimeUnit(struct vcc *tl)
+{
+ double sc = 1.0;
+
+ assert(tl->t->tok == ID);
+ if (vcc_IdIs(tl->t, "ms"))
+ sc = 1e-3;
+ else if (vcc_IdIs(tl->t, "s"))
+ sc = 1.0;
+ else if (vcc_IdIs(tl->t, "m"))
+ sc = 60.0;
+ else if (vcc_IdIs(tl->t, "h"))
+ sc = 60.0 * 60.0;
+ else if (vcc_IdIs(tl->t, "d"))
+ sc = 60.0 * 60.0 * 24.0;
+ else if (vcc_IdIs(tl->t, "w"))
+ sc = 60.0 * 60.0 * 24.0 * 7.0;
+ else if (vcc_IdIs(tl->t, "y"))
+ sc = 60.0 * 60.0 * 24.0 * 365.0;
+ else {
+ VSB_printf(tl->sb, "Unknown time unit ");
+ vcc_ErrToken(tl, tl->t);
+ VSB_printf(tl->sb,
+ ". Legal are 'ms', 's', 'm', 'h', 'd', 'w' and 'y'\n");
+ vcc_ErrWhere(tl, tl->t);
+ return (1.0);
+ }
+ vcc_NextToken(tl);
+ return (sc);
+}
+
+/*--------------------------------------------------------------------
+ * Recognize and convert { CNUM } to unsigned value
+ * The tokenizer made sure we only get digits.
+ */
+
+unsigned
+vcc_UintVal(struct vcc *tl)
+{
+ unsigned d = 0;
+ const char *p;
+
+ Expect(tl, CNUM);
+ for (p = tl->t->b; p < tl->t->e; p++) {
+ d *= 10;
+ d += *p - '0';
+ }
+ vcc_NextToken(tl);
+ return (d);
+}
+
+/*--------------------------------------------------------------------
+ * Recognize and convert { CNUM [ '.' [ CNUM ] ] } to double value
+ * The tokenizer made sure we only get digits and a '.'
+ */
+
+void
+vcc_NumVal(struct vcc *tl, double *d, int *frac)
+{
+ double e = 0.1;
+ const char *p;
+
+ *frac = 0;
+ *d = 0.0;
+ Expect(tl, CNUM);
+ if (tl->err) {
+ *d = NAN;
+ return;
+ }
+ for (p = tl->t->b; p < tl->t->e; p++) {
+ *d *= 10;
+ *d += *p - '0';
+ }
+ vcc_NextToken(tl);
+ if (tl->t->tok != '.')
+ return;
+ *frac = 1;
+ vcc_NextToken(tl);
+ if (tl->t->tok != CNUM)
+ return;
+ for (p = tl->t->b; p < tl->t->e; p++) {
+ *d += (*p - '0') * e;
+ e *= 0.1;
+ }
+ vcc_NextToken(tl);
+}
+
+double
+vcc_DoubleVal(struct vcc *tl)
+{
+ double d;
+ int i;
+
+ vcc_NumVal(tl, &d, &i);
+ return (d);
+}
+
+/*--------------------------------------------------------------------*/
+
+void
+vcc_Duration(struct vcc *tl, double *d)
+{
+ double v, sc;
+
+ v = vcc_DoubleVal(tl);
+ ERRCHK(tl);
+ ExpectErr(tl, ID);
+ sc = vcc_TimeUnit(tl);
+ *d = v * sc;
+}
+
+/*--------------------------------------------------------------------*/
+
+void
+vcc_ByteVal(struct vcc *tl, double *d)
+{
+ double v, sc;
+
+ v = vcc_DoubleVal(tl);
+ ERRCHK(tl);
+ if (tl->t->tok != ID) {
+ VSB_printf(tl->sb, "Expected BYTES unit (B, KB, MB...) got ");
+ vcc_ErrToken(tl, tl->t);
+ VSB_printf(tl->sb, "\n");
+ vcc_ErrWhere(tl, tl->t);
+ return;
+ }
+ if (vcc_IdIs(tl->t, "B"))
+ sc = 1.;
+ else if (vcc_IdIs(tl->t, "KB"))
+ sc = 1024.;
+ else if (vcc_IdIs(tl->t, "MB"))
+ sc = 1024. * 1024.;
+ else if (vcc_IdIs(tl->t, "GB"))
+ sc = 1024. * 1024. * 1024.;
+ else if (vcc_IdIs(tl->t, "TB"))
+ sc = 1024. * 1024. * 1024. * 1024.;
+ else {
+ VSB_printf(tl->sb, "Unknown BYTES unit ");
+ vcc_ErrToken(tl, tl->t);
+ VSB_printf(tl->sb,
+ ". Legal are 'B', 'KB', 'MB', 'GB' and 'TB'\n");
+ vcc_ErrWhere(tl, tl->t);
+ return;
+ }
+ vcc_NextToken(tl);
+ *d = v * sc;
+}
+
More information about the varnish-commit
mailing list