source: tags/arb_5.0/GENOM_IMPORT/tools.h

Last change on this file was 5861, checked in by westram, 15 years ago
  • type fixes; mostly format arguments (warnings from 64bit-compile)
File size: 4.3 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 _CPP_CCTYPE
18#include <cctype>
19#endif
20#ifndef _CPP_ALGORITHM
21#include <algorithm>
22#endif
23
24
25inline bool beginsWith(const string& str, const string& start) {
26    return str.find(start) == 0;
27}
28
29inline bool endsWith(const string& str, const string& postfix) {
30    size_t slen = str.length();
31    size_t plen = postfix.length();
32
33    if (plen>slen) { return false; }
34    return str.substr(slen-plen) == postfix;
35}
36
37inline void appendSpaced(string& str, const string& toAppend) {
38    if (!toAppend.empty()) {
39        if (!str.empty()) str.append(1, ' ');
40        str.append(toAppend);
41    }
42}
43
44bool parseInfix(const string &str, const string& prefix, const string& postfix, string& foundInfix);
45
46// --------------------------------------------------------------------------------
47
48#define CURRENT_REST string(pos, end).c_str()
49
50struct StringParser {
51    stringCIter pos, end;
52
53    StringParser(const string& str) : pos(str.begin()), end(str.end()) {}
54
55    bool atEnd() const { return pos == end; }
56
57    unsigned char at() const { gi_assert(pos != end); return *pos; }
58
59    stringCIter getPosition() const { return pos; }
60    void setPosition(const stringCIter& position) { pos = position; }
61    void advance(size_t offset) { std::advance(pos, offset); }
62
63    string rest() const { return string(pos, end); }
64
65    stringCIter find(char c) {
66        while (pos != end && *pos != c) {
67            ++pos;
68        }
69        return pos;
70    }
71
72
73    size_t eatSpaces() {
74        int spaces = 0;
75        while (pos != end && *pos == ' ') {
76            ++pos;
77            ++spaces;
78        }
79        return spaces;
80    }
81
82    size_t expectSpaces(size_t count = 1, bool allowMore = true) {
83        size_t spaces      = eatSpaces();
84        bool   validNumber = allowMore ? spaces >= count : spaces == count;
85
86        if (!validNumber) {
87            throw GBS_global_string("Expected %zu%s spaces, found %zu (before '%s')",
88                                    count, allowMore ? " or more" : "",
89                                    spaces, CURRENT_REST);
90        }
91        return spaces;
92    }
93
94    size_t lookingAt(const char *content) {
95        // returns 0 if different content is seen (or if content is "")
96        // otherwise it returns the string length of content
97
98        size_t      p;
99        stringCIter look = pos;
100        for (p = 0; content[p]; ++p, ++look) {
101            if (content[p] != *look) {
102                return 0;
103            }
104        }
105        return p;
106    }
107
108    void expectContent(const char *content) {
109        size_t len = lookingAt(content);
110        if (!len) throw GBS_global_string("Expected to see '%s' (found='%s')", content, CURRENT_REST);
111        std::advance(pos, len); // eat the found content
112    }
113
114    string extractWord(const char *delimiter = " ") {
115        if (atEnd() || strchr(delimiter, *pos) != 0) {
116            throw GBS_global_string("Expected non-delimiter at '%s'", CURRENT_REST);
117        }
118
119        stringCIter start = pos++;
120
121        while (!atEnd() && strchr(delimiter, *pos) == 0) ++pos;
122        return string(start, pos);
123    }
124
125    long eatNumber(bool &eaten) {
126        long lnum = 0;
127        char c;
128       
129        eaten = false;
130        for (; isdigit(c = *pos); ++pos) {
131            lnum  = lnum*10+(c-'0');
132            eaten = true;
133        }
134
135        return lnum;
136    }
137
138    long extractNumber() {
139        bool seen_digits;
140        long lnum = eatNumber(seen_digits);
141       
142        if (!seen_digits) throw GBS_global_string("Expected number, found '%s'", CURRENT_REST);
143        return lnum;
144    }
145};
146
147#else
148#error tools.h included twice
149#endif // TOOLS_H
150
Note: See TracBrowser for help on using the repository browser.