-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstrutils.h
More file actions
70 lines (49 loc) · 1.51 KB
/
Copy pathstrutils.h
File metadata and controls
70 lines (49 loc) · 1.51 KB
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
/********************************************************/
/* Author: Matt Zucker <mzucker1@swarthmore.edu> */
/* Copyright (c) 2013 Matt Zucker. All rights reserved. */
/********************************************************/
#ifndef _STRUTILS_H_
#define _STRUTILS_H_
#include <string>
#include <ctype.h>
inline std::string directoryOf(const std::string& filename) {
size_t pos = filename.rfind('/');
if (pos == size_t(-1)) {
return ".";
} else {
return filename.substr(0, pos);
}
}
inline std::string combineDir(const std::string& dir, const std::string& rel) {
if (rel.length() && rel[0] == '/') {
return rel;
} else {
return dir + "/" + rel;
}
}
inline std::string lower(const std::string& s) {
std::string rval = s;
for (size_t i=0; i<s.length(); ++i) { rval[i] = tolower(rval[i]); }
return rval;
}
inline std::string upper(const std::string& s) {
std::string rval = s;
for (size_t i=0; i<s.length(); ++i) { rval[i] = toupper(rval[i]); }
return rval;
}
inline std::string trimws(const std::string& s) {
size_t p0 = 0;
while (p0 < s.length() && isspace(s[p0])) { ++p0; }
size_t p1 = s.length()-1;
while (p1 > p0 && isspace(s[p1])) { --p1; }
return s.substr(p0, p1-p0+1);
}
inline bool split(const std::string& s, char c,
std::string& s1, std::string& s2) {
size_t p = s.find(c);
if (p == std::string::npos) { return false; }
s1 = trimws(s.substr(0, p));
s2 = trimws(s.substr(p+1, s.length()-p-1));
return true;
}
#endif