Modify NormalizePath to work with boost 1.33.1, add regressions test

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@13297 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Lars Gullik Bjønnes 2006-03-05 18:11:11 +00:00
parent 3f103ffaa7
commit c0ce7ff659
7 changed files with 77 additions and 35 deletions

View File

@ -1,3 +1,8 @@
2006-03-05 Lars Gullik Bjønnes <larsbj@lyx.org>
* filetools.C (NormalizePath): Change to use boost::filesystem and
alter regex usage in preparation for boost 1.33.1.
2006-02-12 Jean-Marc Lasgouttes <lasgouttes@lyx.org>
* package.C.in (relative_system_support_dir): fix for win32 and

View File

@ -594,42 +594,16 @@ string const ExpandPath(string const & path)
// Also converts paths like /foo//bar ==> /foo/bar
string const NormalizePath(string const & path)
{
string TempBase;
string RTemp;
string Temp;
// Normalize paths like /foo//bar ==> /foo/bar
static boost::regex regex("/{2,}");
string const tmppath = boost::regex_merge(path, regex, "/");
if (os::is_absolute_path(path))
RTemp = path;
else
// Make implicit current directory explicit
RTemp = "./" + path;
fs::path const npath = fs::path(tmppath, fs::no_check).normalize();
// Normalise paths like /foo//bar ==> /foo/bar
boost::RegEx regex("/{2,}");
RTemp = regex.Merge(RTemp, "/");
while (!RTemp.empty()) {
// Split by next /
RTemp = split(RTemp, Temp, '/');
if (Temp == ".") {
TempBase = "./";
} else if (Temp == "..") {
// Remove one level of TempBase
string::difference_type i = TempBase.length() - 2;
while (i > 0 && TempBase[i] != '/')
--i;
if (i >= 0 && TempBase[i] == '/')
TempBase.erase(i + 1, string::npos);
else
TempBase = "../";
} else {
TempBase += Temp + '/';
}
}
// returns absolute path
return TempBase;
if (!npath.is_complete())
return "./" + npath.string() + '/';
return npath.string() + '/';
}

View File

@ -1,3 +1,13 @@
2006-03-05 Lars Gullik Bjønnes <larsbj@lyx.org>
* Makefile.am: update for filetools test
* filetools.C: test prog
* regfiles/filetools: regression data
* test_filetools: new test driver
2005-02-25 Lars Gullik Bjønnes <larsbj@gullik.net>
* Makefile.am (makeregfiles): rename from regfiles

View File

@ -1,15 +1,17 @@
include $(top_srcdir)/config/common.am
EXTRA_DIST = pch.h test_convert test_lstrings regfiles
EXTRA_DIST = pch.h test_convert test_filetools test_lstrings regfiles
BUILT_SOURCES = $(PCH_FILE)
TESTS = \
test_convert \
test_filetools \
test_lstrings
check_PROGRAMS = \
convert \
filetools \
lstrings
AM_CPPFLAGS += $(BOOST_INCLUDES)
@ -19,6 +21,11 @@ convert_SOURCES = \
convert.C \
boost.C
filetools_LDADD = ../../debug.o ../libsupport.la $(BOOST_REGEX) $(BOOST_FILESYSTEM)
filetools_SOURCES = \
filetools.C \
boost.C
lstrings_LDADD = ../lstrings.o
lstrings_SOURCES = \
lstrings.C \

30
src/support/tests/filetools.C Executable file
View File

@ -0,0 +1,30 @@
#include "../filetools.h"
#include <iostream>
using namespace lyx::support;
using namespace std;
string _(string const & str)
{
return str;
}
void test_NormalizePath()
{
cout << NormalizePath("foo/../bar") << endl;
cout << NormalizePath("foo/./bar") << endl;
cout << NormalizePath("./foo/../bar") << endl;
cout << NormalizePath("./foo/./bar") << endl;
cout << NormalizePath("/foo/../bar") << endl;
cout << NormalizePath("/foo/./bar") << endl;
cout << NormalizePath("foo//bar") << endl;
cout << NormalizePath("./foo//bar") << endl;
cout << NormalizePath("/foo//bar") << endl;
}
int main()
{
test_NormalizePath();
}

View File

@ -0,0 +1,9 @@
./bar/
./foo/bar/
./bar/
./foo/bar/
/bar/
/foo/bar/
./foo/bar/
./foo/bar/
/foo/bar/

View File

@ -0,0 +1,7 @@
#!/bin/bash
regfile=`cat ${srcdir}/regfiles/filetools`
output=`./filetools`
test "$regfile" = "$output"
exit $?