gpgme-tool: Use membuf functions to build up strings.
[gpgme.git] / src / progress.c
1 /* progress.c -  status handler for progress status
2    Copyright (C) 2000 Werner Koch (dd9jn)
3    Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH
4
5    This file is part of GPGME.
6
7    GPGME is free software; you can redistribute it and/or modify it
8    under the terms of the GNU Lesser General Public License as
9    published by the Free Software Foundation; either version 2.1 of
10    the License, or (at your option) any later version.
11
12    GPGME is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Lesser General Public License for more details.
16
17    You should have received a copy of the GNU Lesser General Public
18    License along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include "util.h"
30 #include "context.h"
31
32
33 gpgme_error_t
34 _gpgme_progress_status_handler (void *priv, gpgme_status_code_t code,
35                                 char *args)
36 {
37   gpgme_ctx_t ctx = (gpgme_ctx_t) priv;
38   char *p;
39   char *args_cpy;
40   int type = 0;
41   int current = 0;
42   int total = 0;
43
44   if (code != GPGME_STATUS_PROGRESS || !*args || !ctx->progress_cb)
45     return 0;
46
47   args_cpy = strdup (args);
48   if (!args_cpy)
49     return gpg_error_from_errno (errno);
50
51   p = strchr (args_cpy, ' ');
52   if (p)
53     {
54       *p++ = 0;
55       if (*p)
56         {
57           type = *(unsigned char *)p;
58           p = strchr (p+1, ' ');
59           if (p)
60             {
61               *p++ = 0;
62               if (*p)
63                 {
64                   current = atoi (p);
65                   p = strchr (p+1, ' ');
66                   if (p)
67                     {
68                       *p++ = 0;
69                       total = atoi (p);
70                     }
71                 }
72             }
73         }
74     }
75
76   if (type != 'X')
77     ctx->progress_cb (ctx->progress_cb_value, args_cpy, type, current, total);
78
79   free (args_cpy);
80   return 0;
81 }