Commit 9e39f012 authored by Mikaël Salson's avatar Mikaël Salson

Tools: extract_basename() function

Extracts the basename of a path. There is a basename() function in C but takes char *
(not even const char*), which is not convenient with string in C++.
And basename() doesn't remove extension.

Unit tests added.
parent 2d45070d
......@@ -139,6 +139,21 @@ string extract_from_label(string str, int field, string separator)
return str.substr(found1+1, found2-found1-1);
}
string extract_basename(string path, bool remove_ext) {
size_t pos_lastdir = path.find_last_of('/');
if (pos_lastdir != std::string::npos) {
path = path.substr(pos_lastdir+1);
}
if (remove_ext) {
size_t lastdot = path.find_last_of('.');
if (lastdot != std::string::npos)
path = path.substr(0, lastdot);
}
return path;
}
int remove_trailing_whitespaces(string &str) {
int count = 0;
while (str.size() > 0 && (str[str.size() - 1] == '\r'
......
......@@ -102,6 +102,11 @@ int dna_to_int(const string &, int size);
string extract_from_label(string str, int field, string separator);
/**
* @return Extract basename of a file and extracts extension (by default
*/
string extract_basename(string path, bool remove_ext = true);
/**
* remove_trailing_whitespaces removes the whitespaces (ie. ' ', '\t', '\r')
* that may be at the end of the string
......
......@@ -89,6 +89,21 @@ void testRevcompInt() {
TEST_REVCOMP_INT, "revcomp: " << revcomp_int(dna_to_int("ATTAGGA", 7), 7) <<", dna_to_int: " << dna_to_int("TCCTAAT", 7));
}
void testExtractBasename() {
TAP_TEST(extract_basename("/var/toto/titi/tutu/bla.bli.bluc", true) == "bla.bli",
TEST_EXTRACT_BASENAME, extract_basename("/var/toto/titi/tutu/bla.bli.bluc", true));
TAP_TEST(extract_basename("/var/toto/titi/tutu/bla.bli.bluc", false) == "bla.bli.bluc",
TEST_EXTRACT_BASENAME, extract_basename("/var/toto/titi/tutu/bla.bli.bluc", false));
TAP_TEST(extract_basename("bla.bli.bluc", true) == "bla.bli",
TEST_EXTRACT_BASENAME, extract_basename("bla.bli.bluc", true));
TAP_TEST(extract_basename("bla.bli.bluc", false) == "bla.bli.bluc",
TEST_EXTRACT_BASENAME, extract_basename("bla.bli.bluc", false));
TAP_TEST(extract_basename("a_filename_without_extension", true) == "a_filename_without_extension",
TEST_EXTRACT_BASENAME, extract_basename("a_filename_without_extension", true));
TAP_TEST(extract_basename("/", true) == "",
TEST_EXTRACT_BASENAME, extract_basename("/", true));
}
void testTools() {
testFasta1();
testRevcomp();
......@@ -96,4 +111,5 @@ void testTools() {
testNucToInt();
testDNAToInt();
testRevcompInt();
testExtractBasename();
}
......@@ -15,6 +15,7 @@ enum {
TEST_NUC_TO_INT,
TEST_DNA_TO_INT,
TEST_REVCOMP_INT,
TEST_EXTRACT_BASENAME,
/* Storage tests */
TEST_ARRAY_KMERSTORE,
......@@ -103,6 +104,7 @@ inline void declare_tests() {
RECORD_TAP_TEST(TEST_NUC_TO_INT, "nuc_to_int()");
RECORD_TAP_TEST(TEST_DNA_TO_INT, "dna_to_int()");
RECORD_TAP_TEST(TEST_REVCOMP_INT, "revcomp_int()");
RECORD_TAP_TEST(TEST_EXTRACT_BASENAME, "extractBasename()");
RECORD_TAP_TEST(TEST_ARRAY_KMERSTORE, "Testing ArrayKmerStore");
RECORD_TAP_TEST(TEST_MAP_KMERSTORE, "Testing MapKmerStore");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment