source: tags/arb_5.3/TEMPLATES/output.h

Last change on this file was 5675, checked in by westram, 15 years ago
  • removed automatic timestamps (the best they were good for, were vc-conflicts)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
Line 
1//  ==================================================================== //
2//                                                                       //
3//    File      : output.h                                               //
4//    Purpose   : class for indented output to FILE                      //
5//                                                                       //
6//                                                                       //
7//  Coded by Ralf Westram (coder@reallysoft.de) in September 2003        //
8//  Copyright Department of Microbiology (Technical University Munich)   //
9//                                                                       //
10//  Visit our web site at: http://www.arb-home.de/                       //
11//                                                                       //
12//                                                                       //
13//  ==================================================================== //
14
15#ifndef OUTPUT_H
16#define OUTPUT_H
17
18#ifndef _STDARG_H
19#include <stdarg.h>
20#endif
21
22#ifndef ATTRIBUTES_H
23#include <attributes.h>
24#endif
25
26//  ---------------------
27//      class output
28//  ---------------------
29
30class output {
31private:
32    int   indentation;
33    bool  printing_points;
34    int   max_points;
35    int   points_printed;
36    int   points_per_line;
37    FILE *out;
38
39    inline void cr() const { fputc('\n', stdout); }
40
41    inline void  print_indent() {
42        for (int i = 0; i<indentation; ++i) fputc(' ', stdout);
43    }
44
45    void goto_indentation() {
46        if (printing_points) {
47            new_point_line();
48            // here we stop printing points
49            points_printed  = 0;
50            max_points      = 0;
51            printing_points = false;
52        }
53        else print_indent();
54    }
55
56    void new_point_line() {
57        if (max_points) {   // do we know max_points ?
58            fprintf(stdout, " %3i%%", int(double(points_printed)/max_points*100+.5));
59        }
60        cr();
61        print_indent();
62    }
63
64public:
65    output(FILE *out_ = stdout, int breakPointsAt = 60)
66        : indentation(0)
67        , printing_points(false)
68        , max_points(0)
69        , points_printed(0)
70        , points_per_line(breakPointsAt)
71        , out(out_)
72    {}
73
74    void indent(int howMuch) { indentation   += howMuch; }
75    void unindent(int howMuch) { indentation -= howMuch; }
76
77    void vput(const char *s, va_list argPtr) __ATTR__VFORMAT_MEMBER(1);
78    void put(const char *s, ...) __ATTR__FORMAT_MEMBER(1);
79
80    void put()  {
81        goto_indentation();
82        cr();
83    }
84
85    void setMaxPoints(int maxP) {
86        max_points = maxP;
87    }
88
89    void point() {
90        if (!printing_points) {
91            goto_indentation();
92            printing_points = true;
93        }
94        else if ((points_printed%points_per_line) == 0 && points_printed) {
95            new_point_line();
96        }
97
98        fputc('.', stdout);
99        ++points_printed;
100        fflush(stdout);
101    }
102};
103
104inline void output::vput(const char *s, va_list argPtr) {
105    goto_indentation();
106    vfprintf(stdout, s, argPtr);
107    cr();
108}
109
110inline void output::put(const char *s, ...) {
111    va_list parg;
112    va_start(parg, s);
113    vput(s, parg);
114}
115
116
117
118//  ---------------------
119//      class indent
120//  ---------------------
121// create an instance of indent to increase indentation
122// indentation automatically resets when that instance leaves the scope
123
124class indent {
125private:
126    int     indentation;
127    output& out;
128
129public:
130    indent(output& out_, int ind = 2) : indentation(ind), out(out_) {
131        out.indent(indentation);
132    }
133    ~indent() {
134        out.unindent(indentation);
135    }
136};
137
138
139
140#else
141#error output.h included twice
142#endif // OUTPUT_H
143
Note: See TracBrowser for help on using the repository browser.