-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenericFeatures.cpp
More file actions
135 lines (123 loc) · 2.92 KB
/
genericFeatures.cpp
File metadata and controls
135 lines (123 loc) · 2.92 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*!
* \project_name BiM Encoder/Decoder
* \file genericFeatures.hpp
* \brief the generic features
* \details This class is used to define several generic functions useful to the application or any of projet classes without instantiate this class. Basically, all functions are declared here can be called from anywhere without instantiation
* \authors Marco Dos Santos Oliveira
* \version 1.0.0
* \date 2 August 2013
* \copyright This software is published in MPLv2.0
*
*/
#include "genericFeatures.hpp"
std::string genericFeatures::removePrefix
(
std::string filename,
std::string prefix
)
{
int firstindex = filename.find_first_of(prefix);
// returns what comes after first string "prefix"
return filename.substr (firstindex+prefix.size(),filename.size()-(firstindex+prefix.size()));
}
std::string genericFeatures::removeSuffix
(
std::string filename,
std::string suffix
)
{
int lastindex = filename.find_last_of(suffix);
// returns what comes after string "suffix"
return filename.substr (lastindex+suffix.size(),filename.size()-(lastindex+suffix.size()));
}
std::string genericFeatures::int2str
(
int futurestring
)
{
std::ostringstream oss;
// write the output stream
oss << futurestring;
return oss.str();
}
bool genericFeatures::hasExtension
(
std::string filename
)
{
// find last "."
int lastindex = filename.find_last_of(".");
// test if empty
if (lastindex == -1) {
return false;
}
// copy what comes after last dot
std::string extension = filename.substr
(
lastindex+1,
filename.size()-(lastindex+1)
);
// if the length is superior to 1 character, true else false
return (extension.size()>0) ? true : false ;
}
bool genericFeatures::isExtension
(
std::string filename,
std::string extension
)
{
// find last "."
int lastindex = filename.find_last_of(".");
// copy what comes after last dot
std::string str2 = filename.substr (lastindex+1,filename.size()-(lastindex+1));
// loop through each character and makes it lower-case.
// stop at end of string (\0)
for(int i = 0; str2[i] != '\0'; i++){
str2[i] = tolower(str2[i]);
}
// return the result
return (extension.compare(str2) == 0) ? true : false ;
}
bool genericFeatures::fileExists
(
std::string filename
)
{
std::ifstream f(filename.c_str());
if (f.good()) {
f.close(); return true;
}
f.close(); return false;
}
bool genericFeatures::isHexadecimal
(
std::string data
)
{
if (data.size() > 0) {
bool hexPrefix = false;
if (data.compare(0,2,"0x") == 0 or data.compare(0,2,"0X") == 0) {
hexPrefix = !hexPrefix;
}
if(data.find_first_not_of("0123456789abcdefABCDEF", ((hexPrefix)?2:0)) == std::string::npos)
{
return true;
} else {
return false;
}
}
return false;
}
bool genericFeatures::hasHexadecimalPrefix
(
std::string data
)
{
bool hexPrefix = false;
if (data.size() > 0) {
if (data.compare(0,2,"0x") == 0 or data.compare(0,2,"0X") == 0) {
hexPrefix = !hexPrefix;
}
}
return hexPrefix;
}