Skip to content

Commit 6c7eadc

Browse files
committed
added os::path::abspath
1 parent 1080f1a commit 6c7eadc

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

pystring.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,18 @@ namespace path
11281128
}
11291129

11301130

1131+
//////////////////////////////////////////////////////////////////////////////////////////////
1132+
///
1133+
///
1134+
std::string abspath(const std::string & path, const std::string & cwd)
1135+
{
1136+
std::string p = path;
1137+
if(!isabs(p)) p = join(cwd, p);
1138+
return normpath(p);
1139+
}
1140+
1141+
1142+
11311143
//////////////////////////////////////////////////////////////////////////////////////////////
11321144
///
11331145
///

pystring.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,14 @@ namespace path
323323

324324
bool isabs(const std::string & path);
325325

326+
//////////////////////////////////////////////////////////////////////////////////////////////
327+
/// @brief Return a normalized absolutized version of the pathname path.
328+
///
329+
/// NOTE: This differs from the interface of the python equivalent in that it requires you
330+
/// to pass in the current working directory as an argument.
331+
std::string abspath(const std::string & path, const std::string & cwd);
332+
333+
326334
//////////////////////////////////////////////////////////////////////////////////////////////
327335
/// @brief Join one or more path components intelligently. If any component is an absolute
328336
/// path, all previous components (on Windows, including the previous drive letter, if there

test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,12 @@ PYSTRING_ADD_TEST(pystring, translate)
163163
PYSTRING_CHECK_EQUAL(pystring::translate("", t2), "");
164164
PYSTRING_CHECK_EQUAL(pystring::translate("cheese", t2), "chooso");
165165
}
166+
167+
168+
PYSTRING_ADD_TEST(pystring, abspath)
169+
{
170+
PYSTRING_CHECK_EQUAL(pystring::os::path::abspath("", "/net"), "/net");
171+
PYSTRING_CHECK_EQUAL(pystring::os::path::abspath("../jeremys", "/net/soft_scratch/users/stevel"), "/net/soft_scratch/users/jeremys");
172+
PYSTRING_CHECK_EQUAL(pystring::os::path::abspath("../../../../tmp/a", "/net/soft_scratch/users/stevel"), "/tmp/a");
173+
174+
}

0 commit comments

Comments
 (0)