cpp-utilities/io/path.cpp

71 lines
2.1 KiB
C++
Raw Normal View History

#include "./path.h"
#include <cstdlib>
2017-05-01 03:13:11 +02:00
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
namespace IoUtilities {
/*!
* \brief Returns the file name and extension of the specified \a path string.
*/
string fileName(const string &path)
{
size_t lastSlash = path.rfind('/');
size_t lastBackSlash = path.rfind('\\');
size_t lastSeparator;
2017-05-01 03:13:11 +02:00
if (lastSlash == string::npos && lastBackSlash == string::npos) {
return path;
2017-05-01 03:13:11 +02:00
} else if (lastSlash == string::npos) {
lastSeparator = lastBackSlash;
2017-05-01 03:13:11 +02:00
} else if (lastBackSlash == string::npos) {
lastSeparator = lastSlash;
} else {
lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
}
return path.substr(lastSeparator + 1);
}
/*!
* \brief Returns the directory of the specified \a path string (including trailing slash).
*/
string directory(const string &path)
{
size_t lastSlash = path.rfind('/');
size_t lastBackSlash = path.rfind('\\');
size_t lastSeparator;
2017-05-01 03:13:11 +02:00
if (lastSlash == string::npos && lastBackSlash == string::npos) {
return string();
2017-05-01 03:13:11 +02:00
} else if (lastSlash == string::npos) {
lastSeparator = lastBackSlash;
2017-05-01 03:13:11 +02:00
} else if (lastBackSlash == string::npos) {
lastSeparator = lastSlash;
} else {
lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
}
return path.substr(0, lastSeparator + 1);
}
/*!
* \brief Removes invalid characters from the specified \a fileName.
*
* The characters <, >, ?, !, *, |, /, :, \ and new lines are considered as invalid.
*/
void removeInvalidChars(std::string &fileName)
{
size_t startPos = 0;
2017-05-01 03:13:11 +02:00
static const char invalidPathChars[] = { '\"', '<', '>', '?', '!', '*', '|', '/', ':', '\\', '\n' };
for (const char *i = invalidPathChars, *end = invalidPathChars + sizeof(invalidPathChars); i != end; ++i) {
startPos = fileName.find(*i);
2017-05-01 03:13:11 +02:00
while (startPos != string::npos) {
fileName.replace(startPos, 1, string());
startPos = fileName.find(*i, startPos);
}
}
}
} // namespace IoUtilities