source: tags/ms_r18q1/GENOM_IMPORT/tools.h

Last change on this file was 16763, checked in by westram, 6 years ago
File size: 3.9 KB
Line 
1// ================================================================ //
2//                                                                  //
3//   File      : tools.h                                            //
4//   Purpose   :                                                    //
5//                                                                  //
6//   Coded by Ralf Westram (coder@reallysoft.de) in November 2006   //
7//   Institute of Microbiology (Technical University Munich)        //
8//   http://www.arb-home.de/                                        //
9//                                                                  //
10// ================================================================ //
11#ifndef TOOLS_H
12#define TOOLS_H
13
14#ifndef TYPES_H
15#include "types.h"
16#endif
17#ifndef _GLIBCXX_CCTYPE
18#include <cctype>
19#endif
20#ifndef _GLIBCXX_ALGORITHM
21#include <algorithm>
22#endif
23
24inline void appendSpaced(std::string& str, const std::string& toAppend) {
25    if (!toAppend.empty()) {
26        if (!str.empty()) str.append(1, ' ');
27        str.append(toAppend);
28    }
29}
30
31// --------------------------------------------------------------------------------
32
33#define CURRENT_REST std::string(pos, end).c_str()
34
35struct StringParser {
36    stringCIter pos, end;
37
38    StringParser(const std::string& str) : pos(str.begin()), end(str.end()) {}
39
40    bool atEnd() const { return pos == end; }
41
42    unsigned char at() const { gi_assert(pos != end); return *pos; }
43
44    stringCIter getPosition() const { return pos; }
45    void setPosition(const stringCIter& position) { pos = position; }
46    void advance(size_t offset) { std::advance(pos, offset); }
47
48    std::string rest() const { return std::string(pos, end); }
49
50    stringCIter find(char c) {
51        while (pos != end && *pos != c) {
52            ++pos;
53        }
54        return pos;
55    }
56
57
58    size_t eatSpaces() {
59        int spaces = 0;
60        while (pos != end && *pos == ' ') {
61            ++pos;
62            ++spaces;
63        }
64        return spaces;
65    }
66
67    size_t expectSpaces(size_t count = 1, bool allowMore = true) {
68        size_t spaces      = eatSpaces();
69        bool   validNumber = allowMore ? spaces >= count : spaces == count;
70
71        if (!validNumber) {
72            throw GBS_global_string("Expected %zu%s spaces, found %zu (before '%s')",
73                                    count, allowMore ? " or more" : "",
74                                    spaces, CURRENT_REST);
75        }
76        return spaces;
77    }
78
79    size_t lookingAt(const char *content) {
80        // returns 0 if different content is seen (or if content is "")
81        // otherwise it returns the string length of content
82
83        size_t      p;
84        stringCIter look = pos;
85        for (p = 0; content[p]; ++p, ++look) {
86            if (content[p] != *look) {
87                return 0;
88            }
89        }
90        return p;
91    }
92
93    void expectContent(const char *content) {
94        size_t len = lookingAt(content);
95        if (!len) throw GBS_global_string("Expected to see '%s' (found='%s')", content, CURRENT_REST);
96        std::advance(pos, len); // eat the found content
97    }
98
99    std::string extractWord(const char *delimiter = " ") {
100        if (atEnd() || strchr(delimiter, *pos)) {
101            throw GBS_global_string("Expected non-delimiter at '%s'", CURRENT_REST);
102        }
103
104        stringCIter start = pos++;
105
106        while (!atEnd() && !strchr(delimiter, *pos)) ++pos;
107        return std::string(start, pos);
108    }
109
110    long eatNumber(bool &eaten) {
111        long lnum = 0;
112        char c;
113
114        eaten = false;
115        for (; isdigit(c = *pos); ++pos) {
116            lnum  = lnum*10+(c-'0');
117            eaten = true;
118        }
119
120        return lnum;
121    }
122
123    long extractNumber() {
124        bool seen_digits;
125        long lnum = eatNumber(seen_digits);
126
127        if (!seen_digits) throw GBS_global_string("Expected number, found '%s'", CURRENT_REST);
128        return lnum;
129    }
130};
131
132#else
133#error tools.h included twice
134#endif // TOOLS_H
135
Note: See TracBrowser for help on using the repository browser.