Update FSF address.
[libguestfs.git] / sparsify / progress_c.c
1 /* virt-sparsify - interface to progress bar mini library
2  * Copyright (C) 2011 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <locale.h>
26
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>
32
33 #include "progress.h"
34
35 #define Bar_val(v) (*((struct progress_bar **)Data_custom_val(v)))
36
37 static void
38 progress_bar_finalize (value barv)
39 {
40   struct progress_bar *bar = Bar_val (barv);
41
42   if (bar)
43     progress_bar_free (bar);
44 }
45
46 static struct custom_operations progress_bar_custom_operations = {
47   (char *) "progress_bar_custom_operations",
48   progress_bar_finalize,
49   custom_compare_default,
50   custom_hash_default,
51   custom_serialize_default,
52   custom_deserialize_default
53 };
54
55 value
56 virt_sparsify_progress_bar_init (value machine_readablev)
57 {
58   CAMLparam1 (machine_readablev);
59   CAMLlocal1 (barv);
60   struct progress_bar *bar;
61   int machine_readable = Bool_val (machine_readablev);
62   unsigned flags = 0;
63
64   /* XXX Have to do this to get nl_langinfo to work properly.  However
65    * we should really only call this from main.
66    */
67   setlocale (LC_ALL, "");
68
69   if (machine_readable)
70     flags |= PROGRESS_BAR_MACHINE_READABLE;
71   bar = progress_bar_init (flags);
72   if (bar == NULL)
73     caml_raise_out_of_memory ();
74
75   barv = caml_alloc_custom (&progress_bar_custom_operations,
76                             sizeof (struct progress_bar *), 0, 1);
77   Bar_val (barv) = bar;
78
79   CAMLreturn (barv);
80 }
81
82 value
83 virt_sparsify_progress_bar_reset (value barv)
84 {
85   CAMLparam1 (barv);
86   struct progress_bar *bar = Bar_val (barv);
87
88   progress_bar_reset (bar);
89
90   CAMLreturn (Val_unit);
91 }
92
93 value
94 virt_sparsify_progress_bar_set (value barv,
95                                 value positionv, value totalv)
96 {
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);
101
102   progress_bar_set (bar, position, total);
103
104   CAMLreturn (Val_unit);
105 }