X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=augeas-c.c;h=5c330a80cf1a1e46d742d14bb8b1fbf5c6e64b2c;hb=62f4b1532b3b55da5b46043da9d44138ce5e48c4;hp=481811694fcff718e9f9402b7115d7463701e1bc;hpb=eb7b3379f07ff52c849b953b1a38cb2d4c3d43bf;p=ocaml-augeas.git diff --git a/augeas-c.c b/augeas-c.c index 4818116..5c330a8 100644 --- a/augeas-c.c +++ b/augeas-c.c @@ -33,7 +33,18 @@ typedef augeas *augeas_t; /* Raise an Augeas.Error exception. */ static void -raise_error (const char *msg) +raise_error (augeas_t t, const char *msg) +{ + const int code = aug_error (t); + + if (code == AUG_ENOMEM) + caml_raise_out_of_memory (); + + caml_raise_with_string (*caml_named_value ("Augeas.Error"), msg); +} + +static void +raise_init_error (const char *msg) { caml_raise_with_string (*caml_named_value ("Augeas.Error"), msg); } @@ -42,7 +53,10 @@ raise_error (const char *msg) static int flag_map[] = { /* AugSaveBackup */ AUG_SAVE_BACKUP, /* AugSaveNewFile */ AUG_SAVE_NEWFILE, - /* AugTypeCheck */ AUG_TYPE_CHECK + /* AugTypeCheck */ AUG_TYPE_CHECK, + /* AugNoStdinc */ AUG_NO_STDINC, + /* AugSaveNoop */ AUG_SAVE_NOOP, + /* AugNoLoad */ AUG_NO_LOAD, }; /* Wrap and unwrap augeas_t handles, with a finalizer. */ @@ -108,7 +122,7 @@ ocaml_augeas_create (value rootv, value loadpathv, value flagsv) t = aug_init (root, loadpath, flags); if (t == NULL) - raise_error ("Augeas.create"); + raise_init_error ("Augeas.create"); CAMLreturn (Val_augeas_t (t)); } @@ -147,7 +161,7 @@ ocaml_augeas_get (value tv, value pathv) } else if (r == 0) /* Return None */ optv = Val_int (0); else if (r == -1) /* Error or multiple matches */ - raise_error ("Augeas.get"); + raise_error (t, "Augeas.get"); else failwith ("Augeas.get: bad return value"); @@ -170,7 +184,7 @@ ocaml_augeas_exists (value tv, value pathv) else if (r == 0) /* Return false */ v = Val_int (0); else if (r == -1) /* Error or multiple matches */ - raise_error ("Augeas.exists"); + raise_error (t, "Augeas.exists"); else failwith ("Augeas.exists: bad return value"); @@ -190,7 +204,7 @@ ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv) before = beforev == Val_int (0) ? 0 : Int_val (Field (beforev, 0)); if (aug_insert (t, path, label, before) == -1) - raise_error ("Augeas.insert"); + raise_error (t, "Augeas.insert"); CAMLreturn (Val_unit); } @@ -206,7 +220,7 @@ ocaml_augeas_rm (value tv, value pathv) r = aug_rm (t, path); if (r == -1) - raise_error ("Augeas.rm"); + raise_error (t, "Augeas.rm"); CAMLreturn (Val_int (r)); } @@ -224,7 +238,7 @@ ocaml_augeas_match (value tv, value pathv) r = aug_match (t, path, &matches); if (r == -1) - raise_error ("Augeas.matches"); + raise_error (t, "Augeas.matches"); /* Copy the paths to a list. */ rv = Val_int (0); @@ -253,7 +267,7 @@ ocaml_augeas_count_matches (value tv, value pathv) r = aug_match (t, path, NULL); if (r == -1) - raise_error ("Augeas.count_matches"); + raise_error (t, "Augeas.count_matches"); CAMLreturn (Val_int (r)); } @@ -266,7 +280,20 @@ ocaml_augeas_save (value tv) augeas_t t = Augeas_t_val (tv); if (aug_save (t) == -1) - raise_error ("Augeas.save"); + raise_error (t, "Augeas.save"); + + CAMLreturn (Val_unit); +} + +/* val load : t -> unit */ +CAMLprim value +ocaml_augeas_load (value tv) +{ + CAMLparam1 (tv); + augeas_t t = Augeas_t_val (tv); + + if (aug_load (t) == -1) + raise_error (t, "Augeas.load"); CAMLreturn (Val_unit); }