Just for completeness, here are patches that I believe will allow tex2lyx
to run successfully on Win95 and Win98 and will allow both LyX 1.3.x and
LyX 1.4.x to be used as a conversion filter on such machines. (It's likely
that Qt/WinFree will die a horrible death though...)

I'll wait for Luis Rivera to confirm that tex2lyx now works.

-- 
Angus
Index: src/support/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/ChangeLog,v
retrieving revision 1.149.2.52
diff -u -p -r1.149.2.52 ChangeLog
--- src/support/ChangeLog	20 Sep 2005 14:46:22 -0000	1.149.2.52
+++ src/support/ChangeLog	26 Sep 2005 14:28:55 -0000
@@ -1,3 +1,17 @@
+2005-09-26  Angus Leeming  <[EMAIL PROTECTED]>
+
+	* os_win32.h: new file, providing declarations of new classes
+	GetLongPath and GetFolderPath which wrap the Windows system functions
+	GetLongPathName and SHGetFolderPath, respecitvely so that LyX will work
+	as expected on Win98 and Win95 machines.
+
+	* os_win32.C (internal_path): always passes the input path through
+	GetLongPath.
+	Add definitions of the GetLongPath and GetFolderPath classes.
+
+	* package.C: replace explicit invocation of the system function
+	SHGetFolderPath with our new wrapper.
+
 2005-09-20  Jean-Marc Lasgouttes  <[EMAIL PROTECTED]>
 
 	* Makefile.am (EXTRA_DIST): add os_cygwin.C
Index: src/support/Makefile.am
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/Makefile.am,v
retrieving revision 1.53.2.3
diff -u -p -r1.53.2.3 Makefile.am
--- src/support/Makefile.am	20 Sep 2005 14:46:22 -0000	1.53.2.3
+++ src/support/Makefile.am	26 Sep 2005 14:28:55 -0000
@@ -5,7 +5,7 @@ noinst_LTLIBRARIES = libsupport.la
 INCLUDES = -I$(srcdir)/.. $(BOOST_INCLUDES)
 
 EXTRA_DIST = lyxstring.C lyxstring.h \
-	     os_cygwin.C os_unix.C os_win32.C os_os2.C
+	     os_cygwin.C os_unix.C os_win32.C os_win32.h os_os2.C
 
 if USE_LYXSTRING
 LYXSTRING = lyxstring.C lyxstring.h
Index: src/support/os_win32.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/os_win32.C,v
retrieving revision 1.8.2.16
diff -u -p -r1.8.2.16 os_win32.C
--- src/support/os_win32.C	29 Apr 2005 08:42:22 -0000	1.8.2.16
+++ src/support/os_win32.C	26 Sep 2005 14:28:55 -0000
@@ -4,14 +4,25 @@
 #include <config.h>
 
 #include "os.h"
+#include "support/os_win32.h"
 #include "support/filetools.h"
+#include "support/LAssert.h"
 #include "support/lstrings.h"
 #include "debug.h"
 
+#include <cstdlib>
+#include <vector>
+
 #include <windows.h>
 #include <io.h>
 #include <direct.h> // _getdrive
+#include <shlobj.h>  // SHGetFolderPath
 
+// Needed by MinGW:
+#ifndef SHGFP_TYPE_CURRENT
+# define SHGFP_TYPE_CURRENT 0
+#endif
+ 
 
 string const os::nulldev_ = "nul";
 os::shell_type os::shell_ = os::CMD_EXE;
@@ -138,7 +149,8 @@ string os::external_path(string const & 
 
 string os::internal_path(string const & p)
 {
-	return subst(p, "\\", "/");
+	static GetLongPath get_long_path;
+	return subst(get_long_path(p), "\\", "/");
 }
 
 
@@ -183,3 +195,230 @@ char os::path_separator()
 
 void os::cygwin_path_fix(bool)
 {}
+
+
+namespace {
+
+void bail_out()
+{
+#ifndef CXX_GLOBAL_CSTD
+	using std::exit;
+#endif
+	exit(1);
+}
+
+} // namespace anon 
+
+
+GetFolderPath::GetFolderPath()
+	: folder_module_(0),
+	  folder_path_func_(0)
+{
+	folder_module_ = LoadLibrary("shfolder.dll");
+	if (!folder_module_) {
+		lyxerr << "Unable to load shfolder.dll\nPlease install."
+		       << std::endl;
+		bail_out();
+	}
+
+	folder_path_func_ = reinterpret_cast<function_pointer>(::GetProcAddress(folder_module_, "SHGetFolderPathA"));
+	if (folder_path_func_ == 0) {
+		lyxerr << "Unable to find SHGetFolderPathA in shfolder.dll\n"
+		          "Don't know how to proceed. Sorry."
+		       << std::endl;
+		bail_out();
+	}
+}
+
+
+GetFolderPath::~GetFolderPath()
+{
+	if (folder_module_)
+		FreeLibrary(folder_module_);
+}
+
+
+// Given a folder ID, returns the folder name (in unix-style format).
+// Eg CSIDL_PERSONAL -> "C:/Documents and Settings/USERNAME/My Documents"
+string const GetFolderPath::operator()(folder_id _id) const
+{
+	char folder_path[PATH_MAX];
+
+	int id = 0;
+	switch (_id) {
+	case PERSONAL:
+		id = CSIDL_PERSONAL;
+		break;
+	case APPDATA:
+		id = CSIDL_APPDATA;
+		break;
+	default:
+		lyx::Assert(false);
+	}
+	HRESULT const result = (folder_path_func_)(0, id, 0,
+						   SHGFP_TYPE_CURRENT,
+						   folder_path);
+	return (result == 0) ? os::internal_path(folder_path) : string();
+}
+
+
+GetLongPath::GetLongPath()
+	: kernel32_dll_(0),
+	  system_func_(0)
+{
+	kernel32_dll_ = LoadLibrary("kernel32.dll");
+	if (kernel32_dll_) {
+		system_func_ = reinterpret_cast<function_pointer>(::GetProcAddress(kernel32_dll_, "GetLongPathNameA"));
+		if (!system_func_) {
+			FreeLibrary(kernel32_dll_);
+			kernel32_dll_ = 0;
+		}
+	}
+}
+
+
+GetLongPath::~GetLongPath()
+{
+	if (kernel32_dll_)
+		FreeLibrary(kernel32_dll_);
+}
+
+
+namespace {
+
+string const win95_get_long_path(string const & short_path);
+
+} // nonamespace anon 
+
+
+string const GetLongPath::operator()(string const & short_path) const
+{
+	return system_func_ ?
+		system_wrapper(short_path) :
+		win95_get_long_path(short_path);
+}
+
+
+string const GetLongPath::system_wrapper(string const & short_path) const
+{
+	std::vector<char> long_path(PATH_MAX);
+	DWORD result = system_func_(short_path.c_str(),
+				    &long_path[0], long_path.size());
+
+	if (result > long_path.size()) {
+		long_path.resize(result);
+		result = system_func_(short_path.c_str(),
+				      &long_path[0], long_path.size());
+		lyx::Assert(result <= long_path.size());
+	}
+
+	return (result == 0) ? short_path : &long_path[0];
+}
+
+
+namespace {
+
+string::size_type get_start_relative_path(string const & path)
+{
+	typedef string::size_type size_type;
+
+	// Match "//shr" or "//shr/", returning the index of the next character
+	// thereafter or string::npos.
+	if (path.size() > 2 &&
+	    path.substr(0,2) == "//" ||
+	    path.substr(0,2) == "\\\\") {
+		size_type const pos = path.find_first_of("/\\", 2);
+		if (pos == string::npos)
+			return pos;
+		if (pos+1 == path.size())
+			return string::npos;
+		return pos+1;
+	}
+
+	// Match "C:", "C:/", "prn:" or "prn:/", returning the index of
+	// the next character thereafter or string::npos.
+	size_type const colon = path.find_first_of(":");
+	if (colon == string::npos)
+		return 0;
+	else {
+		if (colon+1 == path.size())
+			return string::npos;
+		return path.find_first_not_of("/\\", colon+1);
+	}
+
+	// There was no root.
+	return 0;
+}
+
+
+string const win95_get_long_path(string const & short_path)
+{
+	/* The grammar of a Windows path can be written as the EBNF grammar:
+
+	   path ::= [root] [relative-path]  // an empty path is valid
+	   root ::= [root-name] [root-directory]
+	   root-directory ::= separator
+	   relative-path ::= path-element { separator path-element } [separator]
+	   path-element ::= name | parent-directory | directory-placeholder
+	   name ::= char { char }
+	   directory-placeholder ::= "."
+	   parent-directory ::= ".."
+	   separator ::= "/"  | "\"
+
+	   where a | b means "a or b",
+	         [a] means "zero or one instance of a"
+		 {a} means zero or more instances of a"
+
+	   Examples of valid "root-name"s:
+	         C:
+	         C:/
+		 //shr
+		 //shr/
+		 prn:
+		 prn:/
+
+	   This function should modify only the "name" component of
+	   "relative-path".
+	*/
+	string const sp = trim(short_path);
+
+	// Initialise "lp" with everything up to the beginning of the
+	// relative-path component of "sp". If "sp" doesn't have a
+	// relative-path component, return.
+	typedef string::size_type size_type;
+
+	size_type const start_rel_path = get_start_relative_path(sp);
+	if (start_rel_path == string::npos)
+		return sp;
+
+	string lp = (start_rel_path == 0) ?
+		string() : sp.substr(0, start_rel_path);
+
+	size_type begin = start_rel_path;
+	while (begin != string::npos) {
+		size_type const next_separator = sp.find_first_of("/\\", begin);
+		size_type const count = (next_separator == string::npos) ?
+			string::npos : (next_separator - begin);
+
+		string const short_elem = sp.substr(begin, count);
+
+		string const trial_path = lp + short_elem;
+		WIN32_FIND_DATA find_data;
+		if (INVALID_HANDLE_VALUE == ::FindFirstFile(trial_path.c_str(), &find_data)) {
+			return sp;
+		} else
+			lp += find_data.cFileName;
+
+		if (next_separator != string::npos)
+			lp += sp[next_separator];
+
+		if (next_separator == string::npos)
+			begin = string::npos;
+		else
+			begin = sp.find_first_not_of("/\\", next_separator);
+	}
+
+	return lp;
+}
+
+} // namespace anon
Index: src/support/os_win32.h
===================================================================
RCS file: src/support/os_win32.h
diff -N src/support/os_win32.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/support/os_win32.h	26 Sep 2005 14:28:56 -0000
@@ -0,0 +1,115 @@
+// -*- C++ -*-
+/**
+ * \file os_win32.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Angus Leeming
+ *
+ * Full author contact details are available in file CREDITS.
+ *
+ * These classes should be used only on Windows machines.
+ */
+
+#ifndef OS_WIN32_H
+#define OS_WIN32_H
+
+#include "LString.h"
+
+#if !defined(_WIN32)
+# error os_win32.h should be compiled only under Windows.
+#endif
+
+/* The GetLongPathNameA function declaration in
+ * winbase.h under MinGW or Cygwin is protected
+ * by the WINVER macro which is defined in windef.h
+ *
+ * We need to #include this file anyway to make available the
+ * DWORD, HMODULE et al. typedefs, so check/define WINVER now.
+ */
+#if defined(__MINGW32__)  || defined(__CYGWIN__) || defined(__CYGWIN32__)
+# if defined(WINVER) && WINVER < 0x0500
+#  error WINVER must be >= 0x0500
+# endif
+# define WINVER 0x0500
+#endif
+
+#include <windef.h>
+
+
+/** This class provides a portable wrapper to SHGetFolderPathA that can be
+ *  used on all versions of Windows >= Win95.
+ *
+ *  GetLongPathNameA is to be found in Kernel32.dll
+ *  on Win98 and newer flavours of Windows. The function
+ *  is declared in winbase.h on such a machine.
+ *
+ *  However, whilst
+ *
+ *     char spath[] = ...;
+ *     char lpath[PATH_MAX];
+ *     GetLongPathName(spath, lpath, PATH_MAX);
+ *
+ *  will compile on WinXP, the resulting executable will
+ *  fail on Win95. Thus, we need to check Kernel32.dll
+ *  dynamically for GetLongPathNameA and if it is not
+ *  found, revert to our own version that emulates it.
+ *
+ *  kernel32.dll is loaded dynamically in the constructor. If the .dll is
+ *  found not to contain GetLongPathNameA then it is unloaded immediately.
+ *  Otherwise, the .dll is unloaded in the destructor
+ */
+class GetLongPath {
+public:
+	GetLongPath();
+	~GetLongPath();
+
+	/** Wrapper for GetLongPathNameA.
+	 *  @returns the long path name associated with @c short_path.
+	 */
+	string const operator()(string const & short_path) const;
+private:
+	string const system_wrapper(string const & short_path) const;
+
+	typedef DWORD (__stdcall * function_pointer)(LPCTSTR, LPTSTR, DWORD);
+	HMODULE kernel32_dll_;
+	function_pointer system_func_;
+};
+
+
+/** Win98 and earlier don't have SHGetFolderPath in shell32.dll.
+ *  Microsoft recommend that we load shfolder.dll at run time and
+ *  access the function through that.
+ *
+ *  shfolder.dll is loaded dynamically in the constructor. If loading
+ *  fails or if the .dll is found not to contain SHGetFolderPathA then
+ *  the program exits immediately. Otherwise, the .dll is unloaded in
+ *  the destructor
+ *
+ *  The class makes SHGetFolderPath available through its function operator.
+ *  It will work on all versions of Windows >= Win95.
+ */
+class GetFolderPath {
+public:
+	enum folder_id {
+		/// CSIDL_PERSONAL
+		PERSONAL,
+		/// CSIDL_APPDATA
+		APPDATA
+	};
+
+	GetFolderPath();
+	~GetFolderPath();
+
+	/** Wrapper for SHGetFolderPathA, returning
+	 *  the path asscociated with @c id.
+	 */
+	string const operator()(folder_id id) const;
+private:
+	typedef HRESULT (__stdcall * function_pointer)(HWND, int, HANDLE, DWORD, LPCSTR);
+
+	HMODULE folder_module_;
+	function_pointer folder_path_func_;
+};
+
+#endif // OS_WIN32_H
Index: src/support/package.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/Attic/package.C,v
retrieving revision 1.1.2.6
diff -u -p -r1.1.2.6 package.C
--- src/support/package.C	15 Jul 2005 15:57:10 -0000	1.1.2.6
+++ src/support/package.C	26 Sep 2005 14:28:56 -0000
@@ -22,6 +22,11 @@
 #include "support/lstrings.h"
 #include "support/os.h"
 
+#if defined (USE_WINDOWS_PACKAGING)
+# include "support/os_win32.h"
+# include <boost/scoped_ptr.hpp>
+#endif
+
 #include <boost/tuple/tuple.hpp>
 
 #include <list>
@@ -35,32 +40,7 @@
 
 
 #if defined (USE_WINDOWS_PACKAGING)
-
-/*
- * MinGW's version of winver.h contains this comment:
- *
- * If you need Win32 API features newer the Win95 and WinNT then you must
- * define WINVER before including windows.h or any other method of including
- * the windef.h header.
- *
- * GetLongPathNameA requires WINVER == 0x0500.
- *
- * It doesn't matter if the Windows version is older than this because the
- * function will compile but will fail at run time. See
- * http://msdn.microsoft.com/library/en-us/mslu/winprog/microsoft_layer_for_unicode_apis_with_limited_support.asp
- */
-# if defined(__MINGW32__)
-#  define WINVER 0x0500
-# endif
-
 # include <windows.h>
-# include <shlobj.h>  // SHGetFolderPath
-
-  // Needed for MinGW:
-# ifndef SHGFP_TYPE_CURRENT
-#  define SHGFP_TYPE_CURRENT 0
-# endif
-
 #elif defined (USE_MACOSX_PACKAGING)
 # include <CoreServices/CoreServices.h> // FSFindFolder, FSRefMakePath
 #endif
@@ -121,6 +101,10 @@ std::pair<string, bool> const
 get_user_support_dir(string const & default_user_support_dir,
 		     string const & command_line_user_support_dir);
 
+#if defined (USE_WINDOWS_PACKAGING)
+boost::scoped_ptr<GetFolderPath> win32_folder_path;
+#endif // USE_WINDOWS_PACKAGING
+
 } // namespace anon
 
 
@@ -129,6 +113,10 @@ Package::Package(string const & command_
 		 string const & command_line_user_support_dir)
 	: explicit_user_support_dir_(false)
 {
+#if defined (USE_WINDOWS_PACKAGING)
+	win32_folder_path.reset(new GetFolderPath);
+#endif
+
 	home_dir_ = get_home_dir();
 	temp_dir_ = get_temp_dir();
 	document_dir_ = get_document_dir(home_dir_);
@@ -153,6 +141,10 @@ Package::Package(string const & command_
 		get_user_support_dir(default_user_support_dir,
 				     command_line_user_support_dir);
 
+#if defined (USE_WINDOWS_PACKAGING)
+	win32_folder_path.reset();
+#endif
+
 	lyxerr[Debug::INIT]
 		<< "<package>\n"
 		<< "\tbinary_dir " << binary_dir() << '\n'
@@ -187,21 +179,7 @@ string const relative_locale_dir();
 string const relative_system_support_dir();
 
 
-#if defined (USE_WINDOWS_PACKAGING)
-// Given a folder ID, returns the folder name (in unix-style format).
-// Eg CSIDL_PERSONAL -> "C:/Documents and Settings/USERNAME/My Documents"
-string const win32_folder_path(int folder_id)
-{
-	char folder_path[PATH_MAX + 1];
-	if (SUCCEEDED(SHGetFolderPath(0, folder_id, 0,
-				      SHGFP_TYPE_CURRENT, folder_path)))
-		return os::internal_path(folder_path);
-	return string();
-}
-#endif
-
-
-std::pair<string, string> const get_build_dirs(string const & abs_binary)
+ std::pair<string, string> const get_build_dirs(string const & abs_binary)
 {
 	string const check_text = "Checking whether LyX is run in place...";
 
@@ -259,7 +237,7 @@ string const get_document_dir(string con
 {
 #if defined (USE_WINDOWS_PACKAGING)
 	(void)home_dir; // Silence warning about unused variable.
-	return win32_folder_path(CSIDL_PERSONAL);
+	return (*win32_folder_path)(GetFolderPath::PERSONAL);
 #else // Posix-like.
 	return home_dir;
 #endif
@@ -317,7 +295,6 @@ string const get_temp_dir()
 	// Typical example: C:/TEMP/.
 	char path[PATH_MAX + 1];
 	GetTempPath(PATH_MAX, path);
-	GetLongPathName(path, path, PATH_MAX + 1);
 	return os::internal_path(path);
 #else // Posix-like.
 	return "/tmp";
@@ -325,8 +302,6 @@ string const get_temp_dir()
 }
 
 
-// If we use a 'lazy' lyxerr in the hope of setting the locale before
-// printing any messages, then we should ensure that it is flushed first.
 void bail_out()
 {
 #ifndef CXX_GLOBAL_CSTD
@@ -590,7 +565,7 @@ string const get_default_user_support_di
 	(void)home_dir; // Silence warning about unused variable.
 
 	string const user_dir = (string(PACKAGE) == "lyx") ? "LyX" : PACKAGE;
-	return AddPath(win32_folder_path(CSIDL_APPDATA), user_dir);
+	return AddPath((*win32_folder_path)(GetFolderPath::APPDATA), user_dir);
 
 #elif defined (USE_MACOSX_PACKAGING)
 	(void)home_dir; // Silence warning about unused variable.
Index: src/support/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/ChangeLog,v
retrieving revision 1.359
diff -u -p -r1.359 ChangeLog
--- src/support/ChangeLog	10 Sep 2005 15:48:17 -0000	1.359
+++ src/support/ChangeLog	26 Sep 2005 14:33:00 -0000
@@ -1,3 +1,17 @@
+2005-09-26  Angus Leeming  <[EMAIL PROTECTED]>
+
+	* os_win32.h: new file, providing declarations of new classes
+	GetLongPath and GetFolderPath which wrap the Windows system functions
+	GetLongPathName and SHGetFolderPath, respecitvely so that LyX will work
+	as expected on Win98 and Win95 machines.
+
+	* os_win32.C (internal_path): always passes the input path through
+	GetLongPath.
+	Add definitions of the GetLongPath and GetFolderPath classes.
+
+	* package.C: replace explicit invocation of the system function
+	SHGetFolderPath with our new wrapper.
+
 2005-09-10  Angus Leeming  <[EMAIL PROTECTED]>
 
 	* Makefile.am (EXTRA_DIST): add os_cygwin.C to the distribution.
Index: src/support/Makefile.am
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/Makefile.am,v
retrieving revision 1.95
diff -u -p -r1.95 Makefile.am
--- src/support/Makefile.am	10 Sep 2005 15:48:17 -0000	1.95
+++ src/support/Makefile.am	26 Sep 2005 14:33:00 -0000
@@ -4,7 +4,8 @@ SUBDIRS = . tests
 
 CLEANFILES += $(BUILT_SOURCES)
 
-EXTRA_DIST = package.C.in pch.h os_cygwin.C os_os2.C os_unix.C os_win32.C
+EXTRA_DIST = package.C.in pch.h \
+	os_cygwin.C os_os2.C os_unix.C os_win32.C os_win32.h
 
 noinst_LTLIBRARIES = libsupport.la
 
Index: src/support/os_win32.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/os_win32.C,v
retrieving revision 1.29
diff -u -p -r1.29 os_win32.C
--- src/support/os_win32.C	29 Apr 2005 08:42:02 -0000	1.29
+++ src/support/os_win32.C	26 Sep 2005 14:33:01 -0000
@@ -15,13 +15,25 @@
 #include <config.h>
 
 #include "support/os.h"
+#include "support/os_win32.h"
 #include "support/lstrings.h"
 
 #include "debug.h"
 
+#include <boost/assert.hpp>
+
+#include <cstdlib>
+#include <vector>
+
 #include <windows.h>
 #include <io.h>
 #include <direct.h> // _getdrive
+#include <shlobj.h>  // SHGetFolderPath
+
+// Needed by MinGW:
+#ifndef SHGFP_TYPE_CURRENT
+# define SHGFP_TYPE_CURRENT 0
+#endif
 
 using std::endl;
 using std::string;
@@ -31,7 +43,7 @@ namespace lyx {
 namespace support {
 namespace os {
 
-void os::init(int /* argc */, char * argv[])
+void init(int /* argc */, char * argv[])
 {
 	/* Note from Angus, 17 Jan 2005:
 	 *
@@ -159,7 +171,8 @@ string external_path(string const & p)
 // the Win32/DOS pathnames into Cygwin pathnames.
 string internal_path(string const & p)
 {
-	return subst(p, "\\", "/");
+	static GetLongPath get_long_path;
+	return subst(get_long_path(p), "\\", "/");
 }
 
 
@@ -210,6 +223,232 @@ char path_separator()
 
 void cygwin_path_fix(bool)
 {}
+
+
+namespace {
+
+void bail_out()
+{
+#ifndef CXX_GLOBAL_CSTD
+	using std::exit;
+#endif
+	exit(1);
+}
+
+} // namespace anon 
+
+
+GetFolderPath::GetFolderPath()
+	: folder_module_(0),
+	  folder_path_func_(0)
+{
+	folder_module_ = LoadLibrary("shfolder.dll");
+	if (!folder_module_) {
+		lyxerr << "Unable to load shfolder.dll\nPlease install."
+		       << std::endl;
+		bail_out();
+	}
+
+	folder_path_func_ = reinterpret_cast<function_pointer>(::GetProcAddress(folder_module_, "SHGetFolderPathA"));
+	if (folder_path_func_ == 0) {
+		lyxerr << "Unable to find SHGetFolderPathA in shfolder.dll\n"
+		          "Don't know how to proceed. Sorry."
+		       << std::endl;
+		bail_out();
+	}
+}
+
+
+GetFolderPath::~GetFolderPath()
+{
+	if (folder_module_)
+		FreeLibrary(folder_module_);
+}
+
+
+// Given a folder ID, returns the folder name (in unix-style format).
+// Eg CSIDL_PERSONAL -> "C:/Documents and Settings/USERNAME/My Documents"
+string const GetFolderPath::operator()(folder_id _id) const
+{
+	char folder_path[PATH_MAX];
+
+	int id = 0;
+	switch (_id) {
+	case PERSONAL:
+		id = CSIDL_PERSONAL;
+		break;
+	case APPDATA:
+		id = CSIDL_APPDATA;
+		break;
+	default:
+		BOOST_ASSERT(false);
+	}
+	HRESULT const result = (folder_path_func_)(0, id, 0,
+						   SHGFP_TYPE_CURRENT,
+						   folder_path);
+	return (result == 0) ? os::internal_path(folder_path) : string();
+}
+
+
+GetLongPath::GetLongPath()
+	: kernel32_dll_(0),
+	  system_func_(0)
+{
+	kernel32_dll_ = LoadLibrary("kernel32.dll");
+	if (kernel32_dll_) {
+		system_func_ = reinterpret_cast<function_pointer>(::GetProcAddress(kernel32_dll_, "GetLongPathNameA"));
+		if (!system_func_) {
+			FreeLibrary(kernel32_dll_);
+			kernel32_dll_ = 0;
+		}
+	}
+}
+
+
+GetLongPath::~GetLongPath()
+{
+	if (kernel32_dll_)
+		FreeLibrary(kernel32_dll_);
+}
+
+
+namespace {
+
+string const win95_get_long_path(string const & short_path);
+
+} // nonamespace anon 
+
+
+string const GetLongPath::operator()(string const & short_path) const
+{
+	return system_func_ ?
+		system_wrapper(short_path) :
+		win95_get_long_path(short_path);
+}
+
+
+string const GetLongPath::system_wrapper(string const & short_path) const
+{
+	std::vector<char> long_path(PATH_MAX);
+	DWORD result = system_func_(short_path.c_str(),
+				    &long_path[0], long_path.size());
+
+	if (result > long_path.size()) {
+		long_path.resize(result);
+		result = system_func_(short_path.c_str(),
+				      &long_path[0], long_path.size());
+		BOOST_ASSERT(result <= long_path.size());
+	}
+
+	return (result == 0) ? short_path : &long_path[0];
+}
+
+
+namespace {
+
+string::size_type get_start_relative_path(string const & path)
+{
+	typedef string::size_type size_type;
+
+	// Match "//shr" or "//shr/", returning the index of the next character
+	// thereafter or string::npos.
+	if (path.size() > 2 &&
+	    path.substr(0,2) == "//" ||
+	    path.substr(0,2) == "\\\\") {
+		size_type const pos = path.find_first_of("/\\", 2);
+		if (pos == string::npos)
+			return pos;
+		if (pos+1 == path.size())
+			return string::npos;
+		return pos+1;
+	}
+
+	// Match "C:", "C:/", "prn:" or "prn:/", returning the index of
+	// the next character thereafter or string::npos.
+	size_type const colon = path.find_first_of(":");
+	if (colon == string::npos)
+		return 0;
+	else {
+		if (colon+1 == path.size())
+			return string::npos;
+		return path.find_first_not_of("/\\", colon+1);
+	}
+
+	// There was no root.
+	return 0;
+}
+
+
+string const win95_get_long_path(string const & short_path)
+{
+	/* The grammar of a Windows path can be written as the EBNF grammar:
+
+	   path ::= [root] [relative-path]  // an empty path is valid
+	   root ::= [root-name] [root-directory]
+	   root-directory ::= separator
+	   relative-path ::= path-element { separator path-element } [separator]
+	   path-element ::= name | parent-directory | directory-placeholder
+	   name ::= char { char }
+	   directory-placeholder ::= "."
+	   parent-directory ::= ".."
+	   separator ::= "/"  | "\"
+
+	   where a | b means "a or b",
+	         [a] means "zero or one instance of a"
+		 {a} means zero or more instances of a"
+
+	   Examples of valid "root-name"s:
+	         C:
+	         C:/
+		 //shr
+		 //shr/
+		 prn:
+		 prn:/
+
+	   This function should modify only the "name" component of
+	   "relative-path".
+	*/
+	string const sp = lyx::support::trim(short_path);
+
+	// Initialise "lp" with everything up to the beginning of the
+	// relative-path component of "sp". If "sp" doesn't have a
+	// relative-path component, return.
+	typedef string::size_type size_type;
+
+	size_type const start_rel_path = get_start_relative_path(sp);
+	if (start_rel_path == string::npos)
+		return sp;
+
+	string lp = (start_rel_path == 0) ?
+		string() : sp.substr(0, start_rel_path);
+
+	size_type begin = start_rel_path;
+	while (begin != string::npos) {
+		size_type const next_separator = sp.find_first_of("/\\", begin);
+		size_type const count = (next_separator == string::npos) ?
+			string::npos : (next_separator - begin);
+
+		string const short_elem = sp.substr(begin, count);
+
+		string const trial_path = lp + short_elem;
+		WIN32_FIND_DATA find_data;
+		if (INVALID_HANDLE_VALUE == ::FindFirstFile(trial_path.c_str(), &find_data)) {
+			return sp;
+		} else
+			lp += find_data.cFileName;
+
+		if (next_separator != string::npos)
+			lp += sp[next_separator];
+
+		if (next_separator == string::npos)
+			begin = string::npos;
+		else
+			begin = sp.find_first_not_of("/\\", next_separator);
+	}
+
+	return lp;
+}
+} // namespace anon
 
 } // namespace os
 } // namespace support
Index: src/support/os_win32.h
===================================================================
RCS file: src/support/os_win32.h
diff -N src/support/os_win32.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/support/os_win32.h	26 Sep 2005 14:33:01 -0000
@@ -0,0 +1,123 @@
+// -*- C++ -*-
+/**
+ * \file os_win32.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Angus Leeming
+ *
+ * Full author contact details are available in file CREDITS.
+ *
+ * These classes should be used only on Windows machines.
+ */
+
+#ifndef OS_WIN32_H
+#define OS_WIN32_H
+
+#include <string>
+
+#if !defined(_WIN32)
+# error os_win32.h should be compiled only under Windows.
+#endif
+
+/* The GetLongPathNameA function declaration in
+ * winbase.h under MinGW or Cygwin is protected
+ * by the WINVER macro which is defined in windef.h
+ *
+ * We need to #include this file anyway to make available the
+ * DWORD, HMODULE et al. typedefs, so check/define WINVER now.
+ */
+#if defined(__MINGW32__)  || defined(__CYGWIN__) || defined(__CYGWIN32__)
+# if defined(WINVER) && WINVER < 0x0500
+#  error WINVER must be >= 0x0500
+# endif
+# define WINVER 0x0500
+#endif
+
+#include <windef.h>
+
+
+namespace lyx {
+namespace support {
+namespace os {
+
+/** This class provides a portable wrapper to SHGetFolderPathA that can be
+ *  used on all versions of Windows >= Win95.
+ *
+ *  GetLongPathNameA is to be found in Kernel32.dll
+ *  on Win98 and newer flavours of Windows. The function
+ *  is declared in winbase.h on such a machine.
+ *
+ *  However, whilst
+ *
+ *     char spath[] = ...;
+ *     char lpath[PATH_MAX];
+ *     GetLongPathName(spath, lpath, PATH_MAX);
+ *
+ *  will compile on WinXP, the resulting executable will
+ *  fail on Win95. Thus, we need to check Kernel32.dll
+ *  dynamically for GetLongPathNameA and if it is not
+ *  found, revert to our own version that emulates it.
+ *
+ *  kernel32.dll is loaded dynamically in the constructor. If the .dll is
+ *  found not to contain GetLongPathNameA then it is unloaded immediately.
+ *  Otherwise, the .dll is unloaded in the destructor
+ */
+class GetLongPath {
+public:
+	GetLongPath();
+	~GetLongPath();
+
+	/** Wrapper for GetLongPathNameA.
+	 *  @returns the long path name associated with @c short_path.
+	 */
+	std::string const operator()(std::string const & short_path) const;
+private:
+	std::string const system_wrapper(std::string const & short_path) const;
+
+	typedef DWORD (__stdcall * function_pointer)(LPCTSTR, LPTSTR, DWORD);
+	HMODULE kernel32_dll_;
+	function_pointer system_func_;
+};
+
+
+/** Win98 and earlier don't have SHGetFolderPath in shell32.dll.
+ *  Microsoft recommend that we load shfolder.dll at run time and
+ *  access the function through that.
+ *
+ *  shfolder.dll is loaded dynamically in the constructor. If loading
+ *  fails or if the .dll is found not to contain SHGetFolderPathA then
+ *  the program exits immediately. Otherwise, the .dll is unloaded in
+ *  the destructor
+ *
+ *  The class makes SHGetFolderPath available through its function operator.
+ *  It will work on all versions of Windows >= Win95.
+ */
+class GetFolderPath {
+public:
+	enum folder_id {
+		/// CSIDL_PERSONAL
+		PERSONAL,
+		/// CSIDL_APPDATA
+		APPDATA
+	};
+
+	GetFolderPath();
+	~GetFolderPath();
+
+	/** Wrapper for SHGetFolderPathA, returning
+	 *  the path asscociated with @c id.
+	 */
+	std::string const operator()(folder_id id) const;
+private:
+	typedef HRESULT (__stdcall * function_pointer)(HWND, int, HANDLE, DWORD, LPCSTR);
+
+	HMODULE folder_module_;
+	function_pointer folder_path_func_;
+};
+
+} // namespace os
+} // namespace support
+} // namespace lyx
+
+#endif // OS_WIN32_H
Index: src/support/package.C.in
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/package.C.in,v
retrieving revision 1.12
diff -u -p -r1.12 package.C.in
--- src/support/package.C.in	17 Jul 2005 01:59:18 -0000	1.12
+++ src/support/package.C.in	26 Sep 2005 14:33:01 -0000
@@ -24,7 +24,11 @@
 #include "support/lstrings.h"
 #include "support/os.h"
 
-#include <boost/assert.hpp>
+#if defined (USE_WINDOWS_PACKAGING)
+# include "support/os_win32.h"
+# include <boost/scoped_ptr.hpp>
+#endif
+
 #include <boost/filesystem/operations.hpp>
 #include <boost/tuple/tuple.hpp>
 
@@ -38,32 +42,7 @@
 #endif
 
 #if defined (USE_WINDOWS_PACKAGING)
-
-/*
- * MinGW's version of winver.h contains this comment:
- *
- * If you need Win32 API features newer the Win95 and WinNT then you must
- * define WINVER before including windows.h or any other method of including
- * the windef.h header.
- *
- * GetLongPathNameA requires WINVER == 0x0500.
- *
- * It doesn't matter if the Windows version is older than this because the
- * function will compile but will fail at run time. See
- * http://msdn.microsoft.com/library/en-us/mslu/winprog/microsoft_layer_for_unicode_apis_with_limited_support.asp
- */
-# if defined(__MINGW32__)
-#  define WINVER 0x0500
-# endif
-
 # include <windows.h>
-# include <shlobj.h>  // SHGetFolderPath
-
-  // Needed for MinGW:
-# ifndef SHGFP_TYPE_CURRENT
-#  define SHGFP_TYPE_CURRENT 0
-# endif
-
 #elif defined (USE_MACOSX_PACKAGING)
 # include <CoreServices/CoreServices.h> // FSFindFolder, FSRefMakePath
 #endif
@@ -135,6 +114,10 @@ std::pair<string, bool> const
 get_user_support_dir(string const & default_user_support_dir,
 		     string const & command_line_user_support_dir);
 
+#if defined (USE_WINDOWS_PACKAGING)
+boost::scoped_ptr<os::GetFolderPath> win32_folder_path;
+#endif
+
 } // namespace anon
 
 
@@ -144,6 +127,10 @@ Package::Package(string const & command_
 		 exe_build_dir_to_top_build_dir top_build_dir_location)
 	: explicit_user_support_dir_(false)
 {
+#if defined (USE_WINDOWS_PACKAGING)
+	win32_folder_path.reset(new os::GetFolderPath);
+#endif
+
 	home_dir_ = get_home_dir();
 	temp_dir_ = get_temp_dir();
 	document_dir_ = get_document_dir(home_dir_);
@@ -168,6 +155,10 @@ Package::Package(string const & command_
 		get_user_support_dir(default_user_support_dir,
 				     command_line_user_support_dir);
 
+#if defined (USE_WINDOWS_PACKAGING)
+	win32_folder_path.reset();
+#endif
+
 	lyxerr[Debug::INIT]
 		<< "<package>\n"
 		<< "\tbinary_dir " << binary_dir() << '\n'
@@ -234,20 +225,6 @@ string const relative_locale_dir();
 string const relative_system_support_dir();
 
 
-#if defined (USE_WINDOWS_PACKAGING)
-// Given a folder ID, returns the folder name (in unix-style format).
-// Eg CSIDL_PERSONAL -> "C:/Documents and Settings/USERNAME/My Documents"
-string const win32_folder_path(int folder_id)
-{
-	char folder_path[PATH_MAX + 1];
-	if (SUCCEEDED(SHGetFolderPath(0, folder_id, 0,
-				      SHGFP_TYPE_CURRENT, folder_path)))
-		return os::internal_path(folder_path);
-	return string();
-}
-#endif
-
-
 std::string const
 get_build_support_dir(std::string const & binary_dir,
 		      exe_build_dir_to_top_build_dir top_build_dir_location)
@@ -325,7 +302,7 @@ string const get_document_dir(string con
 {
 #if defined (USE_WINDOWS_PACKAGING)
 	(void)home_dir; // Silence warning about unused variable.
-	return win32_folder_path(CSIDL_PERSONAL);
+	return (*win32_folder_path)(os::GetFolderPath::PERSONAL);
 #else // Posix-like.
 	return home_dir;
 #endif
@@ -379,9 +356,8 @@ string const get_temp_dir()
 {
 #if defined (USE_WINDOWS_PACKAGING)
 	// Typical example: C:/TEMP/.
-	char path[PATH_MAX + 1];
+	char path[PATH_MAX];
 	GetTempPath(PATH_MAX, path);
-	GetLongPathName(path, path, PATH_MAX + 1);
 	return os::internal_path(path);
 #else // Posix-like.
 	return "/tmp";
@@ -389,8 +365,6 @@ string const get_temp_dir()
 }
 
 
-// If we use a 'lazy' lyxerr in the hope of setting the locale before
-// printing any messages, then we should ensure that it is flushed first.
 void bail_out()
 {
 #ifndef CXX_GLOBAL_CSTD
@@ -631,7 +605,8 @@ string const get_default_user_support_di
 #if defined (USE_WINDOWS_PACKAGING)
 	(void)home_dir; // Silence warning about unused variable.
 
-	return AddPath(win32_folder_path(CSIDL_APPDATA), PACKAGE);
+	return AddPath((*win32_folder_path)(os::GetFolderPath::APPDATA),
+		       PACKAGE);
 
 #elif defined (USE_MACOSX_PACKAGING)
 	(void)home_dir; // Silence warning about unused variable.

Reply via email to