forked from ahupowerdns/metronome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metromisc.hh
42 lines (36 loc) · 949 Bytes
/
metromisc.hh
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
#pragma once
#include <string>
#include <stdexcept>
#include <errno.h>
#include <string.h>
#include <vector>
#include "dolog.hh"
using std::string;
inline void unixDie(const string &why)
{
throw std::runtime_error(why+": "+strerror(errno));
}
template <typename Container>
void
stringtok (Container &container, string const &in,
const char * const delimiters = " \t\n")
{
const string::size_type len = in.length();
string::size_type i = 0;
while (i<len) {
// eat leading whitespace
i = in.find_first_not_of (delimiters, i);
if (i == string::npos)
return; // nothing left but white space
// find the end of the token
string::size_type j = in.find_first_of (delimiters, i);
// push token
if (j == string::npos) {
container.push_back (in.substr(i));
return;
} else
container.push_back (in.substr(i, j-i));
// set up for next loop
i = j + 1;
}
}