Add Augeas.source
[ocaml-augeas.git] / augeas-c.c
index 160fe34..885b829 100644 (file)
@@ -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);
+}