-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrutil.h
More file actions
89 lines (75 loc) · 1.97 KB
/
strutil.h
File metadata and controls
89 lines (75 loc) · 1.97 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
////////////////////////////////////////////////////////////////////
//
// $Id: strutil.h 2021/06/05 13:12:21 kanai Exp $
//
// STL string utility
//
// Copyright (c) 2021 Takashi Kanai
// Released under the MIT license
//
////////////////////////////////////////////////////////////////////
#ifndef _STRUTIL_H
#define _STRUTIL_H 1
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
class StrUtil {
public:
// First whitespace-delimited token from str -> fw
void first_word( std::string& str, std::string& fw ) {
std::istringstream in(str); in >> fw;
};
// n-th whitespace-delimited token (reads n tokens, returns last)
void nth_word( std::string& str, int n, std::string& fw ) {
std::istringstream in(str);
int i = 0;
std::string t;
while ( i < n ) { in >> t; ++i; }
fw = t;
};
// Number of whitespace-delimited tokens
int word_count( std::string& str ) {
std::istringstream in(str);
int count = 0;
std::string sstr;
while ( in >> sstr ) ++count;
return count;
};
// int to string
std::string itos( int n ) {
std::stringstream str_stream;
str_stream << n;
return str_stream.str();
};
// float to string
std::string ftos( float n ) {
std::stringstream str_stream;
str_stream << n;
return str_stream.str();
};
std::string GetExtension(const string &path) {
std::string ext;
size_t pos1 = path.rfind('.');
if(pos1 != std::string::npos){
ext = path.substr(pos1+1, path.size()-pos1);
std::string::iterator itr = ext.begin();
while(itr != ext.end()){
*itr = tolower(*itr);
itr++;
}
itr = ext.end()-1;
while(itr != ext.begin()){ // trim trailing NUL or space from extension
if(*itr == 0 || *itr == 32){
ext.erase(itr--);
}
else{
itr--;
}
}
}
return ext;
};
};
#endif // _STRUTIL_H