File: Util.hpp

package info (click to toggle)
prjtrellis 1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 83,000 kB
  • sloc: cpp: 20,813; python: 16,246; sh: 375; makefile: 262; asm: 80; ansic: 58
file content (103 lines) | stat: -rw-r--r-- 2,311 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef LIBTRELLIS_UTIL_HPP
#define LIBTRELLIS_UTIL_HPP

#include <string>
#include <sstream>
#include <iomanip>
#include <cstdint>
#include <vector>
#include <boost/range/adaptor/reversed.hpp>

using namespace std;

namespace Trellis {
enum class VerbosityLevel {
    ERROR,
    NOTE,
    DEBUG,
};
extern VerbosityLevel verbosity;

inline string uint32_to_hexstr(uint32_t val) {
    ostringstream os;
    os << "0x" << hex << setw(8) << setfill('0') << val;
    return os.str();
}

inline string to_string(const vector<bool> &bv) {
    ostringstream os;
    for (auto bit : boost::adaptors::reverse(bv))
        os << (bit ? '1' : '0');
    return os.str();
}

inline istream &operator>>(istream &in, vector<bool> &bv) {
    bv.clear();
    string s;
    in >> s;
    for (auto c : boost::adaptors::reverse(s)) {
        assert((c == '0') || (c == '1'));
        bv.push_back((c == '1'));
    }
    return in;
}

// Skip whitespace, optionally including newlines
inline void skip_blank(istream &in, bool nl = false) {
    int c = in.peek();
    while (in && (((c == ' ') || (c == '\t')) || (nl && ((c == '\n') || (c == '\r'))))) {
        in.get();
        c = in.peek();
    }
}
// Return true if end of line (or file)
inline bool skip_check_eol(istream &in) {
    skip_blank(in, false);
    if (!in)
        return false;
    int c = in.peek();
    // Comments count as end of line
    if (c == '#') {
        in.get();
        c = in.peek();
        while (in && c != EOF && c != '\n') {
            in.get();
            c = in.peek();
        }
        return true;
    }
    return (c == EOF || c == '\n');
}


// Skip past blank lines and comments
inline void skip(istream &in) {
    skip_blank(in, true);
    while (in && (in.peek() == '#')) {
        // Skip comment line
        skip_check_eol(in);
        skip_blank(in, true);
    }
}

// Return true if at the end of a record (or file)
inline bool skip_check_eor(istream &in) {
    skip(in);
    int c = in.peek();
    return (c == EOF || c == '.');
}

// Return true if at the end of file
inline bool skip_check_eof(istream &in) {
    skip(in);
    int c = in.peek();
    return (c == EOF);
}


}
#define fmt(x) (static_cast<const std::ostringstream&>(std::ostringstream() << x).str())

#define UNUSED(x) (void)(x)

#endif //LIBTRELLIS_UTIL_HPP