Lars Gullik Bjønnes wrote:
> | @@ -180,10 +182,20 @@ void FileInfo::init()
> |  
> |  void FileInfo::dostat(bool link)
> |  {
> | + string name(fname_);
> | +#ifdef _WIN32
> | + // Win32 stat() doesn't dig trailing slashes
> | + if (name.at(name.size()-1) == '/') name.erase(name.size() -1);
> | +#endif
> 
> at? We don't use excedptions so you will get an abort instead...

Ok: if (!name.empty() && name[name.size()-1] == '/') ...

> And why not just remove trailing slash for other systems as well?
> Why not make sure that fname_ is stored without trailing slash.

Good idea.

> | +#ifdef HAVE_LSTAT
> |  if (link)
> | -  status_ = ::lstat(fname_.c_str(), &buf_);
> | +  status_ = ::lstat(name.c_str(), &buf_);
> |  else
> | -  status_ = ::stat(fname_.c_str(), &buf_);
> | +  status_ = ::stat(name.c_str(), &buf_);
> | +#else
> | + status_ = ::stat(name.c_str(), &buf_);
> | +#endif
> 
> We should perhaps even think about having lyx::support::[l]stat
> wrappers.

I think that's unnecessary. This is the only place that uses these system
functions. The code is quite clear as it is.

> (Actually I'd like to get rid of FileInfo.)

Fair enough. However, that is a longer term and separate goal. My goal here
is to enable LyX to compile on Windows without degrading the quality of
the code in the repository.

Try the attached patch. Happier with it?

-- 
Angus
Index: config/configure.ac
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/config/Attic/configure.ac,v
retrieving revision 1.24.2.25
diff -u -p -r1.24.2.25 configure.ac
--- config/configure.ac	16 Dec 2004 01:03:37 -0000	1.24.2.25
+++ config/configure.ac	4 Jan 2005 08:36:35 -0000
@@ -256,8 +256,10 @@ dnl work correctly because of some confl
 dnl We aim to remove this eventually, since we should test as much as
 dnl possible with the compiler which will use the functions (JMarc)
 AC_LANG_PUSH(C)
-AC_CHECK_FUNCS(memmove memset strchr putenv setenv mkfifo mkstemp mktemp)
+AC_CHECK_FUNCS(memmove memset strchr putenv setenv mkfifo mkstemp mktemp \
+    lstat readlink)
 AC_LANG_POP(C)
+
 
 dnl Until this is fixed in autoconf we provide our own version
 AC_FUNC_SELECT_ARGTYPES
Index: config/configure.in
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/config/Attic/configure.in,v
retrieving revision 1.17.2.23
diff -u -p -r1.17.2.23 configure.in
--- config/configure.in	6 Oct 2004 15:03:39 -0000	1.17.2.23
+++ config/configure.in	4 Jan 2005 08:36:36 -0000
@@ -259,7 +259,8 @@ dnl work correctly because of some confl
 dnl We aim to remove this eventually, since we should test as much as
 dnl possible with the compiler which will use the functions (JMarc)
 AC_LANG_C
-AC_CHECK_FUNCS(memmove memset strchr putenv setenv mkfifo mkstemp mktemp)
+AC_CHECK_FUNCS(memmove memset strchr putenv setenv mkfifo mkstemp mktemp \
+    lstat readlink)
 AC_LANG_CPLUSPLUS
 
 dnl Until this is fixed in autoconf we provide our own version
Index: src/support/FileInfo.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/FileInfo.C,v
retrieving revision 1.18.2.2
diff -u -p -r1.18.2.2 FileInfo.C
--- src/support/FileInfo.C	15 Dec 2004 21:40:03 -0000	1.18.2.2
+++ src/support/FileInfo.C	4 Jan 2005 08:36:36 -0000
@@ -10,8 +10,8 @@
 
 #include <config.h>
 
-//#include <sys/types.h>
-//#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #include <cerrno>
 #include "FileInfo.h"
@@ -138,7 +138,18 @@ char typeLetter(mode_t i)
 	return '?';
 }
 
+string const stored_path(string const & path)
+{
+	if (path.empty())
+		return path;
 
+	// Win32 stat() doesn't dig trailing slashes.
+	// Posix stat() doesn't care, but we'll remove it anyway.
+	if (path[path.size()-1] == '/')
+		return path.substr(0, path.size()-1);
+	return path;
+}
+	
 } // namespace anon
 
 
@@ -149,7 +160,7 @@ FileInfo::FileInfo()
 
 
 FileInfo::FileInfo(string const & path, bool link)
-	: fname_(path)
+	: fname_(stored_path(path))
 {
 	init();
 	dostat(link);
@@ -174,10 +185,15 @@ void FileInfo::init()
 
 void FileInfo::dostat(bool link)
 {
+#ifdef HAVE_LSTAT
 	if (link)
 		status_ = ::lstat(fname_.c_str(), &buf_);
 	else
 		status_ = ::stat(fname_.c_str(), &buf_);
+#else
+	status_ = ::stat(fname_.c_str(), &buf_);
+#endif
+
 	if (status_)
 		err_ = errno;
 }
@@ -185,7 +201,7 @@ void FileInfo::dostat(bool link)
 
 FileInfo & FileInfo::newFile(string const & path, bool link)
 {
-	fname_  = path;
+	fname_  = stored_path(path);
 	status_ = 0;
 	err_    = NoErr;
 	dostat(link);
@@ -308,7 +324,11 @@ bool FileInfo::isOK() const
 bool FileInfo::isLink() const
 {
 	lyx::Assert(isOK());
+#ifdef S_ISLNK
 	return S_ISLNK(buf_.st_mode);
+#else
+	return false;
+#endif
 }
 
 
Index: src/support/filetools.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/filetools.C,v
retrieving revision 1.146.2.8
diff -u -p -r1.146.2.8 filetools.C
--- src/support/filetools.C	20 Dec 2004 16:59:01 -0000	1.146.2.8
+++ src/support/filetools.C	4 Jan 2005 08:36:38 -0000
@@ -1247,6 +1247,7 @@ string const MakeDisplayPath(string cons
 
 bool LyXReadLink(string const & file, string & link, bool resolve)
 {
+#ifdef HAVE_READLINK
 	char linkbuffer[512];
 	// Should be PATH_MAX but that needs autconf support
 	int const nRead = ::readlink(file.c_str(),
@@ -1259,6 +1260,9 @@ bool LyXReadLink(string const & file, st
 	else
 		link = linkbuffer;
 	return true;
+#else
+	return false;
+#endif
 }
 
 

Reply via email to