source: tags/svn.1.5.4/TEMPLATES/output.h

Last change on this file was 7044, checked in by westram, 15 years ago
  • fixed include wrappers (merges [7006] [7007])
    • use gcc-style include-wrappers
    • use same convention for local wrappers in TREEGEN as rest of ARB
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 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 _GLIBCXX_CSTDARG
19#include <cstdarg>
20#endif
21#ifndef ATTRIBUTES_H
22#include <attributes.h>
23#endif
24
25//  ---------------------
26//      class output
27//  ---------------------
28
29class output {
30private:
31    int   indentation;
32    bool  printing_points;
33    int   max_points;
34    int   points_printed;
35    int   points_per_line;
36    FILE *out;
37
38    inline void cr() const { fputc('\n', stdout); }
39
40    inline void  print_indent() {
41        for (int i = 0; i<indentation; ++i) fputc(' ', stdout);
42    }
43
44    void goto_indentation() {
45        if (printing_points) {
46            new_point_line();
47            // here we stop printing points
48            points_printed  = 0;
49            max_points      = 0;
50            printing_points = false;
51        }
52        else print_indent();
53    }
54
55    void new_point_line() {
56        if (max_points) {   // do we know max_points ?
57            fprintf(stdout, " %3i%%", int(double(points_printed)/max_points*100+.5));
58        }
59        cr();
60        print_indent();
61    }
62
63public:
64    output(FILE *out_ = stdout, int breakPointsAt = 60)
65        : indentation(0)
66        , printing_points(false)
67        , max_points(0)
68        , points_printed(0)
69        , points_per_line(breakPointsAt)
70        , out(out_)
71    {}
72
73    void indent(int howMuch) { indentation   += howMuch; }
74    void unindent(int howMuch) { indentation -= howMuch; }
75
76    void vput(const char *s, va_list argPtr) __ATTR__VFORMAT_MEMBER(1);
77    void put(const char *s, ...) __ATTR__FORMAT_MEMBER(1);
78
79    void put() {
80        goto_indentation();
81        cr();
82    }
83
84    void setMaxPoints(int maxP) {
85        max_points = maxP;
86    }
87
88    void point() { // for use as simple progress indicator
89        if (!printing_points) {
90            goto_indentation();
91            printing_points = true;
92        }
93        else if ((points_printed%points_per_line) == 0 && points_printed) {
94            new_point_line();
95        }
96
97        fputc('.', stdout);
98        ++points_printed;
99        fflush(stdout);
100    }
101};
102
103inline void output::vput(const char *s, va_list argPtr) {
104    goto_indentation();
105    vfprintf(stdout, s, argPtr);
106    cr();
107}
108
109inline void output::put(const char *s, ...) {
110    va_list parg;
111    va_start(parg, s);
112    vput(s, parg);
113}
114
115
116
117//  ---------------------
118//      class indent
119//  ---------------------
120// create an instance of indent to increase indentation
121// indentation automatically resets when that instance leaves the scope
122
123class indent {
124private:
125    int     indentation;
126    output& out;
127
128public:
129    indent(output& out_, int ind = 2) : indentation(ind), out(out_) {
130        out.indent(indentation);
131    }
132    ~indent() {
133        out.unindent(indentation);
134    }
135};
136
137
138
139#else
140#error output.h included twice
141#endif // OUTPUT_H
142
Note: See TracBrowser for help on using the repository browser.