| 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 | class output { |
|---|
| 29 | private: |
|---|
| 30 | int indentation; |
|---|
| 31 | bool printing_points; |
|---|
| 32 | int max_points; |
|---|
| 33 | int points_printed; |
|---|
| 34 | int points_per_line; |
|---|
| 35 | FILE *out; |
|---|
| 36 | |
|---|
| 37 | inline void cr() const { fputc('\n', stdout); } |
|---|
| 38 | |
|---|
| 39 | inline void print_indent() { |
|---|
| 40 | for (int i = 0; i<indentation; ++i) fputc(' ', stdout); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | void goto_indentation() { |
|---|
| 44 | if (printing_points) { |
|---|
| 45 | new_point_line(); |
|---|
| 46 | // here we stop printing points |
|---|
| 47 | points_printed = 0; |
|---|
| 48 | max_points = 0; |
|---|
| 49 | printing_points = false; |
|---|
| 50 | } |
|---|
| 51 | else print_indent(); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | void new_point_line() { |
|---|
| 55 | if (max_points) { // do we know max_points ? |
|---|
| 56 | fprintf(stdout, " %3i%%", int(double(points_printed)/max_points*100+.5)); |
|---|
| 57 | } |
|---|
| 58 | cr(); |
|---|
| 59 | print_indent(); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | public: |
|---|
| 63 | output(FILE *out_ = stdout, int breakPointsAt = 60) : |
|---|
| 64 | indentation(0), |
|---|
| 65 | printing_points(false), |
|---|
| 66 | max_points(0), |
|---|
| 67 | points_printed(0), |
|---|
| 68 | points_per_line(breakPointsAt), |
|---|
| 69 | out(out_) |
|---|
| 70 | {} |
|---|
| 71 | |
|---|
| 72 | void indent(int howMuch) { indentation += howMuch; } |
|---|
| 73 | void unindent(int howMuch) { indentation -= howMuch; } |
|---|
| 74 | |
|---|
| 75 | void vput(const char *s, va_list argPtr) __ATTR__VFORMAT_MEMBER(1); |
|---|
| 76 | void put(const char *s, ...) __ATTR__FORMAT_MEMBER(1); |
|---|
| 77 | |
|---|
| 78 | void put() { |
|---|
| 79 | goto_indentation(); |
|---|
| 80 | cr(); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | void setMaxPoints(int maxP) { |
|---|
| 84 | max_points = maxP; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | void point() { // for use as simple progress indicator |
|---|
| 88 | if (!printing_points) { |
|---|
| 89 | goto_indentation(); |
|---|
| 90 | printing_points = true; |
|---|
| 91 | } |
|---|
| 92 | else if ((points_printed%points_per_line) == 0 && points_printed) { |
|---|
| 93 | new_point_line(); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | fputc('.', stdout); |
|---|
| 97 | ++points_printed; |
|---|
| 98 | fflush(stdout); |
|---|
| 99 | } |
|---|
| 100 | }; |
|---|
| 101 | |
|---|
| 102 | inline void output::vput(const char *s, va_list argPtr) { |
|---|
| 103 | goto_indentation(); |
|---|
| 104 | vfprintf(stdout, s, argPtr); |
|---|
| 105 | cr(); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | inline void output::put(const char *s, ...) { |
|---|
| 109 | va_list parg; |
|---|
| 110 | va_start(parg, s); |
|---|
| 111 | vput(s, parg); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | // --------------------- |
|---|
| 117 | // class indent |
|---|
| 118 | |
|---|
| 119 | // create an instance of indent to increase indentation |
|---|
| 120 | // indentation automatically resets when that instance leaves the scope |
|---|
| 121 | |
|---|
| 122 | class indent { |
|---|
| 123 | private: |
|---|
| 124 | int indentation; |
|---|
| 125 | output& out; |
|---|
| 126 | |
|---|
| 127 | public: |
|---|
| 128 | indent(output& out_, int ind = 2) : indentation(ind), out(out_) { |
|---|
| 129 | out.indent(indentation); |
|---|
| 130 | } |
|---|
| 131 | ~indent() { |
|---|
| 132 | out.unindent(indentation); |
|---|
| 133 | } |
|---|
| 134 | }; |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | #else |
|---|
| 139 | #error output.h included twice |
|---|
| 140 | #endif // OUTPUT_H |
|---|
| 141 | |
|---|