Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F96935
StringUtils.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Size
5 KB
Referenced Files
None
Subscribers
None
StringUtils.cpp
View Options
#include
<echo/Util/StringUtils.h>
#include
<echo/UTF8String.h>
#include
<boost/filesystem.hpp>
namespace
Echo
{
namespace
Utils
{
namespace
String
{
bool
SplitInTwo
(
const
std
::
string
&
stringToSplit
,
std
::
string
separators
,
std
::
string
&
left
,
std
::
string
&
right
)
{
std
::
string
temp
;
size_t
divider
=
stringToSplit
.
find_first_of
(
separators
);
//If a divider isn't found then we've failed to split.
if
(
divider
==
std
::
string
::
npos
)
{
return
false
;
}
//No failure means we have at least left.
left
=
stringToSplit
.
substr
(
0
,
divider
);
//if the divider was at the end of the string then right will be empty.
if
(
divider
+
1
==
stringToSplit
.
length
())
{
//Make sure it is empty.
right
.
clear
();
}
else
{
//Otherwise, right is the divider+1 to the end of the string.
right
=
stringToSplit
.
substr
(
divider
+
1
);
}
return
true
;
}
bool
SplitInTwo
(
const
std
::
string
&
stringToSplit
,
std
::
string
separators
,
std
::
string
&
left
,
UTF8String
&
right
)
{
std
::
string
rightTemp
;
bool
result
=
SplitInTwo
(
stringToSplit
,
separators
,
left
,
rightTemp
);
if
(
result
)
{
right
=
rightTemp
;
}
return
result
;
}
void
Split
(
const
std
::
string
&
stringToSplit
,
std
::
string
separators
,
std
::
vector
<
std
::
string
>
&
outStrings
)
{
std
::
string
temp
;
outStrings
.
clear
();
for
(
size_t
i
=
0
;
i
<
stringToSplit
.
size
();
++
i
)
{
if
(
separators
.
find_first_of
(
stringToSplit
[
i
])
!=
std
::
string
::
npos
)
{
outStrings
.
push_back
(
temp
);
temp
=
""
;
continue
;
}
temp
+=
stringToSplit
[
i
];
}
if
(
!
temp
.
empty
())
{
outStrings
.
push_back
(
temp
);
}
}
std
::
string
SplitLine
(
const
std
::
string
&
line
,
std
::
string
lineSeparators
,
std
::
string
rightPartSeparators
,
std
::
vector
<
std
::
string
>&
splitOut
)
{
size_t
firstEq
=
line
.
find_first_of
(
lineSeparators
);
if
(
firstEq
==
std
::
string
::
npos
)
return
line
;
std
::
string
token
=
line
.
substr
(
0
,
firstEq
);
std
::
string
ds
;
ds
=
line
.
substr
(
firstEq
+
1
,
line
.
length
()
-
firstEq
);
//split paramB up with ,
size_t
comma
=
0
;
comma
=
ds
.
find_first_of
(
rightPartSeparators
);
while
(
comma
!=
std
::
string
::
npos
)
{
comma
=
ds
.
find_first_of
(
rightPartSeparators
);
if
(
comma
==
std
::
string
::
npos
)
continue
;
if
(
comma
==
ds
.
length
())
{
comma
=
std
::
string
::
npos
;
continue
;
}
splitOut
.
push_back
(
ds
.
substr
(
0
,
comma
)
);
ds
=
ds
.
substr
(
comma
+
1
,
ds
.
length
()
-
comma
);
}
if
(
!
ds
.
empty
())
{
splitOut
.
push_back
(
ds
);
}
return
token
;
}
std
::
string
GetFileExtension
(
const
std
::
string
&
fileName
)
{
size_t
slash
=
fileName
.
find_last_of
(
'/'
);
if
(
slash
!=
std
::
string
::
npos
)
{
return
GetFileExtension
(
fileName
.
substr
(
slash
+
1
));
}
size_t
dot
=
fileName
.
find_last_of
(
'.'
);
//If there is no '.' or it is the last character.
if
(
dot
==
std
::
string
::
npos
||
dot
==
fileName
.
size
()
-
1
)
{
return
std
::
string
();
}
//Otherwise
return
fileName
.
substr
(
dot
+
1
);
}
std
::
string
GetPathFromFilename
(
const
std
::
string
&
fileName
)
{
size_t
slash
=
fileName
.
find_last_of
(
'/'
);
if
(
slash
==
std
::
string
::
npos
)
{
return
std
::
string
();
}
//Otherwise
return
fileName
.
substr
(
0
,
slash
+
1
);
}
std
::
string
ReducePath
(
const
std
::
string
&
pathString
)
{
std
::
vector
<
std
::
string
>
pathComponents
;
Split
(
pathString
,
"/"
,
pathComponents
);
if
(
pathString
.
empty
()
||
pathComponents
.
size
()
==
0
)
{
return
pathString
;
}
// We'll keep a flag for each element in the string. The flags will be used in a
// second pass to determine whether or not to append that element.
std
::
vector
<
bool
>
usePathComponents
(
true
,
pathComponents
.
size
());
// For the rest remove ".." and "." in a path
size_t
numberOfComponents
=
pathComponents
.
size
();
for
(
size_t
i
=
0
;
i
<
numberOfComponents
;
++
i
)
{
// Go up a level if the current directory is ..
if
(
pathComponents
[
i
]
==
".."
)
{
if
(
i
!=
0
)
{
// This one shouldn't be used, the previous one shouldn't either, but it might
// also be multiple ../../.. so we need to loop back.
usePathComponents
[
i
]
=
false
;
//If .. exists one after the next then this
int
d
=
1
;
while
(
d
<
i
&&
usePathComponents
[
i
-
d
]
==
false
)
{
d
++
;
}
usePathComponents
[
i
-
d
]
=
false
;
}
}
else
if
(
pathComponents
[
i
]
==
"."
)
{
//Mark this element not to use
usePathComponents
[
i
]
=
false
;
}
}
std
::
string
result
;
//Check for absolute paths and add the initial slash.
if
(
pathString
[
0
]
==
'/'
)
{
result
=
"/"
;
}
for
(
size_t
i
=
0
;
i
<
numberOfComponents
;
++
i
)
{
if
(
usePathComponents
[
i
]
&&
!
pathComponents
[
i
].
empty
())
{
if
(
result
.
empty
()
||
result
==
"/"
)
{
//Don't include an initial / for the first one
result
+=
pathComponents
[
i
];
}
else
{
result
+=
"/"
+
pathComponents
[
i
];
}
}
}
return
result
;
}
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-c++
Expires
Thu, Dec 5, 2:05 AM (6 h, 53 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
62533
Default Alt Text
StringUtils.cpp (5 KB)
Attached To
Mode
rEE Echo 3
Attached
Detach File
Event Timeline
Log In to Comment