1 /* virt-sparsify - interface to progress bar mini library
2 * Copyright (C) 2011 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <caml/alloc.h>
28 #include <caml/custom.h>
29 #include <caml/fail.h>
30 #include <caml/memory.h>
31 #include <caml/mlvalues.h>
35 #define Bar_val(v) (*((struct progress_bar **)Data_custom_val(v)))
38 progress_bar_finalize (value barv)
40 struct progress_bar *bar = Bar_val (barv);
43 progress_bar_free (bar);
46 static struct custom_operations progress_bar_custom_operations = {
47 (char *) "progress_bar_custom_operations",
48 progress_bar_finalize,
49 custom_compare_default,
51 custom_serialize_default,
52 custom_deserialize_default
56 virt_sparsify_progress_bar_init (value machine_readablev)
58 CAMLparam1 (machine_readablev);
60 struct progress_bar *bar;
61 int machine_readable = Bool_val (machine_readablev);
64 /* XXX Have to do this to get nl_langinfo to work properly. However
65 * we should really only call this from main.
67 setlocale (LC_ALL, "");
70 flags |= PROGRESS_BAR_MACHINE_READABLE;
71 bar = progress_bar_init (flags);
73 caml_raise_out_of_memory ();
75 barv = caml_alloc_custom (&progress_bar_custom_operations,
76 sizeof (struct progress_bar *), 0, 1);
83 virt_sparsify_progress_bar_reset (value barv)
86 struct progress_bar *bar = Bar_val (barv);
88 progress_bar_reset (bar);
90 CAMLreturn (Val_unit);
94 virt_sparsify_progress_bar_set (value barv,
95 value positionv, value totalv)
97 CAMLparam3 (barv, positionv, totalv);
98 struct progress_bar *bar = Bar_val (barv);
99 uint64_t position = Int64_val (positionv);
100 uint64_t total = Int64_val (totalv);
102 progress_bar_set (bar, position, total);
104 CAMLreturn (Val_unit);