2 zip_progress.c -- progress reporting
3 Copyright (C) 2017 Dieter Baron and Thomas Klausner
5 This file is part of libzip, a library to manipulate ZIP archives.
6 The authors can be contacted at <libzip@nih.at>
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in
15 the documentation and/or other materials provided with the
17 3. The names of the authors may not be used to endorse or promote
18 products derived from this software without specific prior
21 THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
22 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #define _ZIP_COMPILING_DEPRECATED
43 zip_progress_callback callback;
44 void (*ud_free)(void *);
51 double last_update; /* last value callback function was called with */
53 double start; /* start of sub-progress section */
54 double end; /* end of sub-progress section */
59 _zip_progress_end(zip_progress_t *progress) {
60 _zip_progress_update(progress, 1.0);
65 _zip_progress_free(zip_progress_t *progress) {
66 if (progress == NULL) {
70 if (progress->ud_free) {
71 progress->ud_free(progress->ud);
79 _zip_progress_new(zip_t *za, double precision, zip_progress_callback callback, void (*ud_free)(void *), void *ud) {
80 zip_progress_t *progress = (zip_progress_t *)malloc(sizeof(*progress));
82 if (progress == NULL) {
83 zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
88 progress->callback = callback;
89 progress->ud_free = ud_free;
91 progress->precision = precision;
98 _zip_progress_start(zip_progress_t *progress) {
99 if (progress == NULL) {
103 progress->last_update = 0.0;
104 progress->callback(progress->za, 0.0, progress->ud);
109 _zip_progress_subrange(zip_progress_t *progress, double start, double end) {
110 if (progress == NULL) {
114 progress->start = start;
117 _zip_progress_update(progress, 0.0);
121 _zip_progress_update(zip_progress_t *progress, double sub_current) {
124 if (progress == NULL) {
128 current = ZIP_MIN(ZIP_MAX(sub_current, 0.0), 1.0) * (progress->end - progress->start) + progress->start;
130 if (current - progress->last_update > progress->precision) {
131 progress->callback(progress->za, current, progress->ud);
132 progress->last_update = current;
138 zip_register_progress_callback_with_state(zip_t *za, double precision, zip_progress_callback callback, void (*ud_free)(void *), void *ud) {
139 zip_progress_t *progress = NULL;
141 if (callback != NULL) {
142 if ((progress = _zip_progress_new(za, precision, callback, ud_free, ud)) == NULL) {
147 _zip_progress_free(za->progress);
148 za->progress = progress;
155 zip_progress_callback_t callback;
160 _zip_legacy_progress_callback(zip_t *za, double progress, void *vud) {
161 struct legacy_ud *ud = (struct legacy_ud *)vud;
163 ud->callback(progress);
167 zip_register_progress_callback(zip_t *za, zip_progress_callback_t progress_callback)
169 struct legacy_ud *ud;
171 if (progress_callback == NULL) {
172 zip_register_progress_callback_with_state(za, 0, NULL, NULL, NULL);
175 if ((ud = (struct legacy_ud *)malloc(sizeof(*ud))) == NULL) {
179 ud->callback = progress_callback;
181 if (zip_register_progress_callback_with_state(za, 0.001, _zip_legacy_progress_callback, free, ud) < 0) {