From: Pino Toscano Date: Fri, 15 Sep 2017 14:42:08 +0000 (+0200) Subject: Add Augeas.source X-Git-Tag: v0.6~20 X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=05f00ac539c2cd0cf5dcbf90d031023a7e973ae7;p=ocaml-augeas.git Add Augeas.source Easy way to get the root node representing a source file. --- diff --git a/augeas-c.c b/augeas-c.c index 160fe34..885b829 100644 --- a/augeas-c.c +++ b/augeas-c.c @@ -385,3 +385,30 @@ ocaml_augeas_transform (value tv, value lensv, value filev, value modev) CAMLreturn (Val_unit); } + +/* val source : t -> path -> path option */ +CAMLprim value +ocaml_augeas_source (value tv, value pathv) +{ + CAMLparam2 (tv, pathv); + CAMLlocal2 (optv, v); + augeas_t t = Augeas_t_val (tv); + const char *path = String_val (pathv); + char *file_path; + int r; + + r = aug_source (t, path, &file_path); + if (r == 0) { + if (file_path) { /* Return Some file_path */ + v = caml_copy_string (file_path); + optv = caml_alloc (1, 0); + Field (optv, 0) = v; + free (file_path); + } else /* Return None */ + optv = Val_int (0); + } + else /* Error */ + raise_error (t, "Augeas.source"); + + CAMLreturn (optv); +} diff --git a/augeas.ml b/augeas.ml index 8d6ac6d..a2d345d 100644 --- a/augeas.ml +++ b/augeas.ml @@ -78,6 +78,8 @@ external set : t -> path -> value option -> unit = "ocaml_augeas_set" external transform : t -> string -> string -> transform_mode -> unit = "ocaml_augeas_transform" +external source : t -> path -> path option + = "ocaml_augeas_source" let () = Callback.register_exception "Augeas.Error" (Error (AugErrInternal, "", "", "")) diff --git a/augeas.mli b/augeas.mli index 327dc02..dfada4a 100644 --- a/augeas.mli +++ b/augeas.mli @@ -127,3 +127,8 @@ val set : t -> path -> value option -> unit val transform : t -> string -> string -> transform_mode -> unit (** [transform t lens file mode] adds or removes (depending on [mode]) the transformation of the specified [lens] for [file]. *) + +val source : t -> path -> path option + (** [source t path] returns the path to the node representing the + file to which [path] belongs, or [None] if [path] does not + represent any file. *)