From 6d2acb543a0fd92e5626cff8ab2975c5f8f4caf0 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Wed, 29 May 2019 11:48:39 +0200 Subject: [PATCH] Split the Optstring_val helper Split the Optstring_val helper function, so it can be used also in more places. --- augeas-c.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/augeas-c.c b/augeas-c.c index 12ca51f..cf8e37f 100644 --- a/augeas-c.c +++ b/augeas-c.c @@ -136,6 +136,15 @@ raise_init_error (const char *msg) caml_raise_with_args (*exn, 4, args); } +static const char * +Optstring_val (value strv) +{ + if (strv == Val_int (0)) /* None */ + return NULL; + else /* Some string */ + return String_val (Field (strv, 0)); +} + /* Map OCaml flags to C flags. */ static const int flag_map[] = { /* AugSaveBackup */ AUG_SAVE_BACKUP, @@ -195,16 +204,10 @@ ocaml_augeas_create (value rootv, value loadpathv, value flagsv) { CAMLparam1 (rootv); const char *root = String_val (rootv); - const char *loadpath; + const char *loadpath = Optstring_val (loadpathv); int flags = 0, i; augeas_t t; - /* Optional loadpath. */ - loadpath = - loadpathv == Val_int (0) - ? NULL - : String_val (Field (loadpathv, 0)); - /* Convert list of flags to C. */ for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1)) { i = Int_val (Field (flagsv, 0)); @@ -402,12 +405,7 @@ ocaml_augeas_set (value tv, value pathv, value valuev) CAMLparam3 (tv, pathv, valuev); augeas_t t = Augeas_t_val (tv); const char *path = String_val (pathv); - const char *val; - - val = - valuev == Val_int (0) - ? NULL - : String_val (Field (valuev, 0)); + const char *val = Optstring_val (valuev); if (aug_set (t, path, val) == -1) raise_error (t, "Augeas.set"); -- 1.8.3.1