OSDN Git Service

should define MIN() when it was not defined
[lha/lha.git] / src / indicator.c
1 /* ------------------------------------------------------------------------ */
2 /* LHa for UNIX                                                             */
3 /*              indicator.c -- put indicator                                */
4 /*                                                                          */
5 /*      Modified                Nobutaka Watazaki                           */
6 /*                                                                          */
7 /*  Ver. 1.14   Source All chagned              1995.01.14  N.Watazaki      */
8 /*              Separated from append.c         2003.07.21  Koji Arai       */
9 /* ------------------------------------------------------------------------ */
10 #include "lha.h"
11
12 #define MAX_INDICATOR_COUNT     58
13
14 static off_t reading_size;
15
16 #ifdef NEED_INCREMENTAL_INDICATOR
17 static off_t indicator_count;
18 static long indicator_threshold;
19 #endif
20
21 #define ALIGN(size, threshold) (((size) + ((threshold)-1))/(threshold))
22
23 static void
24 carriage_return()
25 {
26     static int tty = -1;
27     if (tty == -1) {
28         if (isatty(1))          /* stdout */
29             tty = 1;
30         else
31             tty = 0;
32     }
33
34     if (tty)
35         fputs("\r", stdout);
36     else
37         fputs("\n", stdout);
38 }
39
40 void
41 start_indicator(name, size, msg, def_indicator_threshold)
42     char           *name;
43     off_t          size;
44     char           *msg;
45     long            def_indicator_threshold;
46 {
47 #ifdef NEED_INCREMENTAL_INDICATOR
48     long i;
49     int m;
50 #endif
51
52     if (quiet)
53         return;
54
55 #ifdef NEED_INCREMENTAL_INDICATOR
56     switch (quiet_mode) {
57     case 0:
58         m = MAX_INDICATOR_COUNT - strlen(name);
59         if (m < 1)      /* Bug Fixed by N.Watazaki */
60             m = 3;      /* (^_^) */
61         carriage_return();
62         printf("%s\t- %s :  ", name, msg);
63
64         indicator_threshold =
65             ALIGN(size, m*def_indicator_threshold) * def_indicator_threshold;
66
67         if (indicator_threshold)
68             i = ALIGN(size, indicator_threshold);
69         else
70             i = 0;
71
72         while (i--)
73             putchar('.');
74         indicator_count = 0;
75         carriage_return();
76         printf("%s\t- %s :  ", name, msg);
77         break;
78     case 1:
79         carriage_return();
80         printf("%s :", name);
81         break;
82     }
83 #else
84     printf("%s\t- ", name);
85 #endif
86     fflush(stdout);
87     reading_size = 0L;
88 }
89
90 #ifdef NEED_INCREMENTAL_INDICATOR
91 void
92 put_indicator(count)
93     long int        count;
94 {
95     reading_size += count;
96     if (!quiet && indicator_threshold) {
97         while (reading_size > indicator_count) {
98             putchar('o');
99             fflush(stdout);
100             indicator_count += indicator_threshold;
101         }
102     }
103 }
104 #endif
105
106 void
107 finish_indicator2(name, msg, pcnt)
108     char           *name;
109     char           *msg;
110     int             pcnt;
111 {
112     if (quiet)
113         return;
114
115     if (pcnt > 100)
116         pcnt = 100; /* (^_^) */
117 #ifdef NEED_INCREMENTAL_INDICATOR
118     carriage_return();
119     printf("%s\t- %s(%d%%)\n", name,  msg, pcnt);
120 #else
121     printf("%s\n", msg);
122 #endif
123     fflush(stdout);
124 }
125
126 void
127 finish_indicator(name, msg)
128     char           *name;
129     char           *msg;
130 {
131     if (quiet)
132         return;
133
134 #ifdef NEED_INCREMENTAL_INDICATOR
135     carriage_return();
136     printf("%s\t- %s\n", name, msg);
137 #else
138     printf("%s\n", msg);
139 #endif
140     fflush(stdout);
141 }