1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
  
     | 
    
      /*
    UnDBX - Tool to extract e-mail messages from Outlook Express DBX files.
    Copyright (C) 2008-2013 Avi Rozen <avi.rozen@gmail.com>
    DBX file format parsing code is based on DbxConv - a DBX to MBOX
    Converter.  Copyright (C) 2008, 2009 Ulrich Krebs
    <ukrebs@freenet.de>
    RFC-2822 and RFC-2047 parsing code is adapted from GNU Mailutils -
    a suite of utilities for electronic mail, Copyright (C) 2002,
    2003, 2004, 2005, 2006, 2009, 2010 Free Software Foundation, Inc.
    This file is part of UnDBX.
    UnDBX is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "dbxprogress.h"
typedef struct dbx_progress_bar_s {
  int enabled;
  int verbose;
  float max;
  float last;
  float delta;
} dbx_progress_bar_t;
typedef struct dbx_progress_s {
  dbx_verbosity_t verbosity;
  dbx_progress_bar_t *bars;
  int count;
} dbx_progress_t;
static const char *_dbx_status_label[DBX_STATUS_LAST + 1] = {
  "OK",
  "DELETED",
  "MOVED",
  "WARNING",
  "ERROR",
  "???"
};
static void _dbx_progress_vprintf(dbx_status_t status,
                                  char *prefix,
                                  char *format,
                                  va_list ap,
                                  char *suffix)
{
  FILE *stream = (status < DBX_STATUS_WARNING)? stdout:stderr;
  fputs(prefix, stream);
  if (format)
    vfprintf(stream, format, ap);
  fputs(suffix, stream);
  fflush(stream);
}
static void _dbx_progress_printf(dbx_status_t status, char *format, ...)
{
  va_list ap;
  va_start(ap, format);
  _dbx_progress_vprintf(status, "", format, ap, "");
  va_end(ap);
}
static const char *_dbx_status_string(dbx_status_t status)
{
  if (status < DBX_STATUS_OK || status >= DBX_STATUS_LAST)
    status = DBX_STATUS_LAST;
  return _dbx_status_label[status];
}
static void _dbx_progress_update(dbx_status_t status,
                                 dbx_progress_bar_t *bar,
                                 unsigned int n,
                                 char *format,
                                 va_list ap)
{
  if (format == NULL || !bar->verbose) {
    if (n + 1 != 0) {
      if ((n + 1) < bar->max && n - bar->last < bar->delta)
        return;
      bar->last = n;
      _dbx_progress_printf(DBX_STATUS_OK, "\b\b\b\b\b\b%5.1f%%", (n + 1) / bar->max * 100.0);
      fflush(stdout);
    }
  }
  else {
    if (n + 1 != 0) 
      _dbx_progress_printf(DBX_STATUS_OK, "\n%5.1f%% ", (n + 1) / bar->max * 100.0);
    else
      _dbx_progress_printf(DBX_STATUS_OK, "\n       ");
    _dbx_progress_printf(DBX_STATUS_OK, "[%-7s] ", _dbx_status_string(status));
    _dbx_progress_vprintf(DBX_STATUS_OK, "", format, ap, "");
  }
}
dbx_progress_handle_t dbx_progress_new(dbx_verbosity_t level)
{
  dbx_progress_handle_t handle = (dbx_progress_handle_t) calloc(1, sizeof(dbx_progress_t));
  if (handle) 
    handle->verbosity = level;
  else 
    perror("dbx_progress_new (calloc)");
  return handle;
}
void dbx_progress_delete(dbx_progress_handle_t handle)
{
  if (handle) 
    free(handle);
}
  
void dbx_progress_push(dbx_progress_handle_t handle,
                       dbx_verbosity_t level,
                       unsigned int n,
                       char *format, ...)
{
  dbx_progress_bar_t *bar = NULL;
  dbx_progress_bar_t *bars = NULL;
  if (handle == NULL)
    return;
  
  bars = (dbx_progress_bar_t *)realloc(handle->bars,
                                       (handle->count + 1) * sizeof(dbx_progress_bar_t));
  if (bars == NULL) {
    perror("dbx_progress_push (realloc)");
    return;
  }
  
  handle->bars = bars;
  bar = handle->bars + handle->count;
  handle->count++;
  bar->enabled = (level <= handle->verbosity);
  bar->verbose = (level <  handle->verbosity);
  bar->last = 0;
  bar->max = (float) n;
  bar->delta = bar->max / 1000.0;
  if (bar->enabled) {
    va_list ap;
    va_start(ap, format);
    _dbx_progress_vprintf(DBX_STATUS_OK, "", format, ap, ":       ");
    va_end(ap);
  }
}
void dbx_progress_pop(dbx_progress_handle_t handle, char *format, ...)
{
  dbx_progress_bar_t *bar = handle->bars + handle->count - 1;
  dbx_progress_bar_t *bars = NULL;
  if (handle == NULL || handle->count == 0)
    return;
  
  if (bar->enabled) {
    va_list ap;
    va_start(ap, format);
    _dbx_progress_vprintf(DBX_STATUS_OK, "\n", format, ap, format? "\n":"");
    va_end(ap);
  }
  if (handle->count == 1) {
    free(handle->bars);
  }
  else {
    bars = (dbx_progress_bar_t *)realloc(handle->bars,
                                         (handle->count - 1) * sizeof(dbx_progress_bar_t));
    if (bars == NULL) {
      perror("dbx_progress_pop (realloc)");
      return;
    }
  }
  
  handle->bars = bars;
  handle->count--;
}
  
void dbx_progress_update(dbx_progress_handle_t handle,
                         dbx_status_t status,
                         unsigned int n,
                         char *format, ...)
{
  dbx_progress_bar_t *bar = NULL;
  if (handle == NULL || handle->count == 0)
    return;
  
  bar = handle->bars + handle->count - 1;
  if (bar->enabled) {
    va_list ap;
    va_start(ap, format);
    _dbx_progress_update(status, bar, n, format, ap);
    va_end(ap);
  }
}
void dbx_progress_message(dbx_progress_handle_t handle,
                          dbx_status_t status,
                          char *format, ...)
{
  dbx_progress_bar_t *bar = NULL;
  if (handle && handle->count > 0)
    bar = handle->bars + handle->count - 1;
  if (bar == NULL || bar->enabled) {
    va_list ap;
    va_start(ap, format);
    _dbx_progress_vprintf(status, "", format, ap, "\n");
    va_end(ap);
  }
}
 
     |