"Fossies" - the Fresh Open Source Software Archive

Member "zsync-0.6.2/progress.c" (16 Sep 2010, 3025 Bytes) of package /linux/privat/old/zsync-0.6.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "progress.c" see the Fossies "Dox" file reference documentation.

    1 
    2 /*
    3  *   zsync - client side rsync over http
    4  *   Copyright (C) 2004,2005,2007,2009 Colin Phipps <cph@moria.org.uk>
    5  *
    6  *   This program is free software; you can redistribute it and/or modify
    7  *   it under the terms of the Artistic License v2 (see the accompanying 
    8  *   file COPYING for the full license terms), or, at your option, any later 
    9  *   version of the same license.
   10  *
   11  *   This program is distributed in the hope that it will be useful,
   12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  *   COPYING file for details.
   15  */
   16 
   17 #include "zsglobal.h"
   18 
   19 #include <stdio.h>
   20 #include <time.h>
   21 
   22 #ifdef WITH_DMALLOC
   23 # include <dmalloc.h>
   24 #endif
   25 
   26 #include "progress.h"
   27 
   28 int no_progress;
   29 
   30 /* progbar(chars, percent)
   31  * (Re)print progress bar with chars out of 20 shown and followed by the given
   32  * percentage done. */
   33 static void progbar(int j, float pcnt) {
   34     int i;
   35     char buf[21];
   36 
   37     for (i = 0; i < j && i < 20; i++)
   38         buf[i] = '#';
   39     for (; i < 20; i++)
   40         buf[i] = '-';
   41     buf[i] = 0;
   42     printf("\r%s %.1f%%", buf, pcnt);
   43 }
   44 
   45 /* do_progress(progress, percent, total_bytes_retrieved
   46  * Updates the supplied progress structure with the new % done given, and
   47  * recalculates the rolling download rate given the supplied
   48  * total_bytes_retrieved (and the current time) */
   49 void do_progress(struct progress *p, float pcnt, long long newdl) {
   50     /* If new or if time has passed, update progress display & data */
   51     time_t newtime = time(NULL);
   52     if (p->lasttime != newtime) {
   53         int passed = p->lasttime ? newtime - p->lasttime : 0;
   54         if (!p->lasttime)
   55             p->starttime = newtime;
   56         p->lasttime = newtime;
   57 
   58         /* Update progress bar displayed */
   59         progbar(pcnt * (20.0 / 100.0), pcnt);
   60 
   61         /* Each time 1s has passed, we update and redisplay our download rate */
   62         if (passed) {
   63             float rate = newdl - p->lastdl;
   64             int sleft = (100.0f - pcnt) / (pcnt - p->lastpcnt);
   65             if (passed != 1) {
   66                 rate /= passed;
   67                 sleft *= passed;
   68             }
   69             printf(" %.1f kBps ", rate / 1000.0);
   70             if (sleft < 60 * 1000)
   71                 printf("%d:%02d ETA  ", sleft / 60, sleft % 60);
   72             else
   73                 puts("        ");
   74         }
   75         p->lastdl = newdl;
   76         p->lastpcnt = pcnt;
   77         fflush(stdout);
   78     }
   79 }
   80 
   81 /* end_progress(progress, done)
   82  * Do one final update of the progress display (set to 100% if done is set to
   83  * true), and then release the progress data structures.
   84  */
   85 void end_progress(struct progress *p, int done) {
   86     if (done == 2)
   87         progbar(20, 100.0);
   88     else
   89         progbar(p->lastpcnt * (20.0 / 100.0), p->lastpcnt);
   90 
   91     {
   92         float rate = ((float)p->lastdl) / (p->lasttime - p->starttime + 0.5);
   93         printf(" %.1f kBps ", rate / 1000.0);
   94     }
   95     puts(done == 2 ? "DONE    \n" : !done ? "aborted    \n" : "        \n");
   96     fflush(stdout);
   97 }