-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathndlstrutil.cpp
75 lines (72 loc) · 1.99 KB
/
ndlstrutil.cpp
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
#include "ndlstrutil.h"
std::string ndlstrutil::dirSep()
{
return "/";
}
void ndlstrutil::tokenise(std::vector<std::string>& tokens,
const std::string& str,
const std::string& delimiters)
{
std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
std::string::size_type pos = str.find_first_of(delimiters, lastPos);
while (std::string::npos!=pos
|| std::string::npos!=lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
bool ndlstrutil::getline(std::istream& in, std::string& line)
{
bool val = false;
do
{
if(std::getline(in, line))
val = true;
}
while(line[0]=='#');
return val;
}
void ndlstrutil::wrapOutputText(std::ostream& out, const std::string description, const int width, const int padding)
{
std::string padStr="";
for(int i=0; i<padding; i++)
padStr += " ";
std::vector<std::string> tokens;
ndlstrutil::tokenise(tokens, description);
size_t remain = width-padding;
out << padStr;
for(size_t i=0; i<tokens.size(); i++)
{
remain -= (tokens[i].size()+1);
if (remain<1)
{
out << std::endl;
out << padStr;
remain=width-padding-tokens[i].size()-1;
}
out << tokens[i] << " ";
}
}
std::string ndlstrutil::itoa(long value, int base)
{
enum { kMaxDigits = std::numeric_limits<long>::digits };
std::string buf;
buf.reserve( kMaxDigits ); // Pre-allocate enough space.
if (base < 2 || base > 16) throw ndlexceptions::Error("Unrecognised base in ndlstrutil::itoa()");
long quotient = value;
do
{
buf += "0123456789abcdef"[ std::abs( quotient % base ) ];
quotient /= base;
}
while ( quotient );
// Append the negative sign for base 10
if ( value < 0 && base == 10) buf += '-';
std::reverse( buf.begin(), buf.end() );
return buf;
}