resize: Use guestfish progress bar mini-library.
[libguestfs.git] / resize / progress_c.c
1 /* virt-resize - 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., 675 Mass Ave, Cambridge, MA 02139, 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_resize_progress_bar_init (value unitv)
57 {
58   CAMLparam1 (unitv);
59   CAMLlocal1 (barv);
60   struct progress_bar *bar;
61
62   /* XXX Have to do this to get nl_langinfo to work properly.  However
63    * we should really only call this from main.
64    */
65   setlocale (LC_ALL, "");
66
67   bar = progress_bar_init (0);
68   if (bar == NULL)
69     caml_raise_out_of_memory ();
70
71   barv = caml_alloc_custom (&progress_bar_custom_operations,
72                             sizeof (struct progress_bar *), 0, 1);
73   Bar_val (barv) = bar;
74
75   CAMLreturn (barv);
76 }
77
78 value
79 virt_resize_progress_bar_reset (value barv)
80 {
81   CAMLparam1 (barv);
82   struct progress_bar *bar = Bar_val (barv);
83
84   progress_bar_reset (bar);
85
86   CAMLreturn (Val_unit);
87 }
88
89 value
90 virt_resize_progress_bar_set (value barv,
91                               value positionv, value totalv)
92 {
93   CAMLparam3 (barv, positionv, totalv);
94   struct progress_bar *bar = Bar_val (barv);
95   uint64_t position = Int64_val (positionv);
96   uint64_t total = Int64_val (totalv);
97
98   progress_bar_set (bar, position, total);
99
100   CAMLreturn (Val_unit);
101 }