]> git.lyx.org Git - lyx.git/blobdiff - src/support/Package.cpp
CMake: fix merged build, seems GCC could not handle the namespaces correctly
[lyx.git] / src / support / Package.cpp
index 5ce3d563321e5bb0ff448181df48dc888aed31ad..138f06b9eefb0a89733a062bab5e50195e7ac7b5 100644 (file)
@@ -1,6 +1,6 @@
 // -*- C++ -*-
 /**
- * \file package.C
+ * \file package.cpp
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
 
 #include "support/Package.h"
 
-#include "debug.h"
-#include "gettext.h"
-
+#include "support/debug.h"
 #include "support/environment.h"
+#include "support/ExceptionMessage.h"
 #include "support/filetools.h"
+#include "support/gettext.h"
 #include "support/lstrings.h"
-#include "support/ExceptionMessage.h"
 #include "support/os.h"
 
 #if defined (USE_WINDOWS_PACKAGING)
@@ -39,7 +38,7 @@
 # include <CoreServices/CoreServices.h> // FSFindFolder, FSRefMakePath
 #endif
 
-using std::string;
+using namespace std;
 
 namespace lyx {
 namespace support {
@@ -74,7 +73,7 @@ Package const & package()
        // Commented out because package().locale_dir() can be called
        // from the message translation code in Messages.cpp before
        // init_package() is called. Lars is on the case...
-       // BOOST_ASSERT(initialised_);
+       // LASSERT(initialised_, /**/);
        return package_;
 }
 
@@ -96,8 +95,6 @@ FileName const get_locale_dir(FileName const & system_support_dir);
 FileName const get_system_support_dir(FileName const & abs_binary,
                                    string const & command_line_system_support_dir);
 
-FileName const get_temp_dir();
-
 FileName const get_default_user_support_dir(FileName const & home_dir);
 
 bool userSupportDir(FileName const & default_user_support_dir,
@@ -116,17 +113,14 @@ Package::Package(string const & command_line_arg0,
        : explicit_user_support_dir_(false)
 {
        home_dir_ = get_home_dir();
-       system_temp_dir_ = get_temp_dir();
+       // Specification of temp_dir_ may be reset by LyXRC,
+       // but the default is fixed for a given OS.
+       system_temp_dir_ = FileName::tempPath();
        temp_dir_ = system_temp_dir_;
        document_dir_ = get_document_dir(home_dir_);
 
        FileName const abs_binary = abs_path_from_binary_name(command_line_arg0);
-       string const bdir = onlyPath(abs_binary.absFilename());
-       // We may be using libtools
-       if (suffixIs(bdir, ".libs/"))
-               binary_dir_ = FileName(addPath(bdir, "../"));
-       else
-               binary_dir_ = FileName(bdir);
+       binary_dir_ = FileName(onlyPath(abs_binary.absFilename()));
 
        // Is LyX being run in-place from the build tree?
        buildDirs(abs_binary, top_build_dir_location,
@@ -141,17 +135,16 @@ Package::Package(string const & command_line_arg0,
 
        FileName const default_user_support_dir =
                get_default_user_support_dir(home_dir_);
-       FileName user_support_dir_;
+
        explicit_user_support_dir_ = userSupportDir(default_user_support_dir,
                                     command_line_user_support_dir, user_support_dir_);
 
        FileName const configure_script(addName(system_support().absFilename(), "configure.py"));
        configure_command_ = os::python() + ' ' +
-                       quoteName(configure_script.toFilesystemEncoding()) +
+                       quoteName(configure_script.toFilesystemEncoding(), quote_python) +
                        with_version_suffix();
 
-       lyxerr[Debug::INIT]
-               << "<package>\n"
+       LYXERR(Debug::INIT, "<package>\n"
                << "\tbinary_dir " << binary_dir().absFilename() << '\n'
                << "\tsystem_support " << system_support().absFilename() << '\n'
                << "\tbuild_support " << build_support().absFilename() << '\n'
@@ -160,7 +153,16 @@ Package::Package(string const & command_line_arg0,
                << "\tdocument_dir " << document_dir().absFilename() << '\n'
                << "\ttemp_dir " << temp_dir().absFilename() << '\n'
                << "\thome_dir " << home_dir().absFilename() << '\n'
-               << "</package>\n" << std::endl;
+               << "</package>\n");
+}
+
+
+void Package::set_temp_dir(FileName const & temp_dir) const
+{
+       if (temp_dir.empty())
+               temp_dir_ = system_temp_dir_;
+       else
+               temp_dir_ = temp_dir;
 }
 
 
@@ -247,7 +249,7 @@ FileName buildSupportDir(string const & binary_dir,
                indirection = "../../lib";
                break;
        }
-       return FileName(normalizePath(addPath(binary_dir, indirection)));
+       return FileName(addPath(binary_dir, indirection));
 }
 
 
@@ -269,9 +271,6 @@ void buildDirs(FileName const & abs_binary,
        while (true) {
                // Try and find "lyxrc.defaults".
                string binary_dir = onlyPath(binary.absFilename());
-               // We may be using libtools with static linking.
-               if (suffixIs(binary_dir, ".libs/"))
-                       binary_dir = addPath(binary_dir, "../");
                build_support_dir = buildSupportDir(binary_dir, top_build_dir_location);
                if (!fileSearch(build_support_dir.absFilename(), "Makefile").empty()) {
                        // Try and find "chkconfig.ltx".
@@ -279,8 +278,7 @@ void buildDirs(FileName const & abs_binary,
                                FileName(addPath(Package::top_srcdir().absFilename(), "lib"));
 
                        if (!fileSearch(system_support_dir.absFilename(), "chkconfig.ltx").empty()) {
-                               lyxerr[Debug::INIT] << check_text << " yes"
-                                                   << std::endl;
+                               LYXERR(Debug::INIT, check_text << " yes");
                                return;
                        }
                }
@@ -299,7 +297,7 @@ void buildDirs(FileName const & abs_binary,
                }
        }
 
-       lyxerr[Debug::INIT] << check_text << " no" << std::endl;
+       LYXERR(Debug::INIT, check_text << " no");
        system_support_dir = FileName();
        build_support_dir = FileName();
 }
@@ -346,8 +344,8 @@ FileName const get_locale_dir(FileName const & system_support_dir)
        // 2. Search for system_support_dir / <relative locale dir>
        // The <relative locale dir> is OS-dependent. (On Unix, it will
        // be "../locale/".)
-       FileName path(normalizePath(addPath(system_support_dir.absFilename(),
-                                           relative_locale_dir())));
+       FileName path(addPath(system_support_dir.absFilename(),
+               relative_locale_dir()));
 
        if (path.exists() && path.isDirectory())
                return path;
@@ -361,29 +359,14 @@ FileName const get_locale_dir(FileName const & system_support_dir)
 }
 
 
-// Specification of temp_dir_ may be reset by LyXRC,
-// but the default is fixed for a given OS.
-FileName const get_temp_dir()
-{
-#if defined (USE_WINDOWS_PACKAGING)
-       // Typical example: C:/TEMP/.
-       char path[MAX_PATH];
-       GetTempPath(MAX_PATH, path);
-       return FileName(os::internal_path(to_utf8(from_local8bit(path))));
-#else // Posix-like.
-       return FileName("/tmp");
-#endif
-}
-
-
 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
 FileName const abs_path_from_command_line(string const & command_line)
 {
        if (command_line.empty())
                return FileName();
 
-       string const path = fix_dir_name(command_line);
-       return os::is_absolute_path(path) ? FileName(path) : makeAbsPath(path);
+       string const str_path = fix_dir_name(command_line);
+       return makeAbsPath(str_path);
 }
 
 
@@ -400,7 +383,7 @@ FileName const get_binary_path(string const & exe)
 #else
        string const exe_path = os::internal_path(exe);
 #endif
-       if (os::is_absolute_path(exe_path))
+       if (FileName::isAbsolute(exe_path))
                return FileName(exe_path);
 
        // Two possibilities present themselves.
@@ -415,9 +398,9 @@ FileName const get_binary_path(string const & exe)
        if (exe_name != exe_path)
                return FileName();
 
-       std::vector<string> const path = getEnvPath("PATH");
-       std::vector<string>::const_iterator it = path.begin();
-       std::vector<string>::const_iterator const end = path.end();
+       vector<string> const path = getEnvPath("PATH");
+       vector<string>::const_iterator it = path.begin();
+       vector<string>::const_iterator const end = path.end();
        for (; it != end; ++it) {
                // This will do nothing if *it is already absolute.
                string const exe_dir = makeAbsPath(*it).absFilename();
@@ -458,7 +441,7 @@ get_system_support_dir(FileName const & abs_binary,
 
        // searched_dirs is used for diagnostic purposes only in the case
        // that "chkconfig.ltx" is not found.
-       std::list<FileName> searched_dirs;
+       list<FileName> searched_dirs;
 
        // 1. Use the -sysdir command line parameter.
        FileName path = abs_path_from_command_line(command_line_system_support_dir);
@@ -468,11 +451,11 @@ get_system_support_dir(FileName const & abs_binary,
                        return path;
        }
 
-       // 2. Use the "LYX_DIR_15x" environment variable.
-       path = extract_env_var_dir("LYX_DIR_15x");
+       // 2. Use the "LYX_DIR_${major}${minor}x" environment variable.
+       path = extract_env_var_dir(LYX_DIR_VER);
        if (!path.empty()) {
                searched_dirs.push_back(path);
-               if (check_env_var_dir(path, chkconfig_ltx, "LYX_DIR_15x"))
+               if (check_env_var_dir(path, chkconfig_ltx, LYX_DIR_VER))
                        return path;
        }
 
@@ -490,8 +473,7 @@ get_system_support_dir(FileName const & abs_binary,
                // Try and find "chkconfig.ltx".
                string const binary_dir = onlyPath(binary.absFilename());
 
-               FileName const lyxdir(
-                       normalizePath(addPath(binary_dir, relative_lyxdir)));
+               FileName const lyxdir(addPath(binary_dir, relative_lyxdir));
                searched_dirs.push_back(lyxdir);
 
                if (!fileSearch(lyxdir.absFilename(), chkconfig_ltx).empty()) {
@@ -531,8 +513,8 @@ get_system_support_dir(FileName const & abs_binary,
                }
 
                // Try and find "chkconfig.ltx".
-               FileName const lyxdir(
-                       normalizePath(addPath(binary_dir.absFilename(), relative_lyxdir)));
+               FileName const lyxdir(addPath(binary_dir.absFilename(),
+                       relative_lyxdir));
                searched_dirs.push_back(lyxdir);
 
                if (!fileSearch(lyxdir.absFilename(), chkconfig_ltx).empty()) {
@@ -549,7 +531,7 @@ get_system_support_dir(FileName const & abs_binary,
        // Everything has failed :-(
        // So inform the user and exit.
        string searched_dirs_str;
-       typedef std::list<FileName>::const_iterator iterator;
+       typedef list<FileName>::const_iterator iterator;
        iterator const begin = searched_dirs.begin();
        iterator const end = searched_dirs.end();
        for (iterator it = begin; it != end; ++it) {
@@ -561,13 +543,13 @@ get_system_support_dir(FileName const & abs_binary,
        // FIXME UNICODE
        throw ExceptionMessage(ErrorException, _("No system directory"),
                bformat(_("Unable to determine the system directory "
-                                        "having searched\n"
-                                        "\t%1$s\n"
-                                        "Use the '-sysdir' command line parameter or "
-                                        "set the environment variable LYX_DIR_15x to "
-                                        "the LyX system directory containing the file "
-                                        "`chkconfig.ltx'."),
-                         from_utf8(searched_dirs_str)));
+                               "having searched\n"
+                               "\t%1$s\n"
+                               "Use the '-sysdir' command line parameter or "
+                               "set the environment variable\n%2$s "
+                               "to the LyX system directory containing the "
+                               "file `chkconfig.ltx'."),
+                         from_utf8(searched_dirs_str), from_ascii(LYX_DIR_VER)));
 
        // Keep the compiler happy.
        return FileName();
@@ -585,8 +567,8 @@ bool userSupportDir(FileName const & default_user_support_dir,
        if (!result.empty())
                return true;
 
-       // 2. Use the LYX_USERDIR_15x environment variable.
-       result = extract_env_var_dir("LYX_USERDIR_15x");
+       // 2. Use the LYX_USERDIR_${major}${minor}x environment variable.
+       result = extract_env_var_dir(LYX_USERDIR_VER);
        if (!result.empty())
                return true;
 
@@ -633,7 +615,7 @@ FileName const get_default_user_support_dir(FileName const & home_dir)
 
 
 // Check that directory @c dir contains @c file.
-// Else emit a warning about an invalid @c command_line_switch.
+// Else emit an error message about an invalid @c command_line_switch.
 bool check_command_line_dir(string const & dir,
                            string const & file,
                            string const & command_line_switch)
@@ -641,7 +623,7 @@ bool check_command_line_dir(string const & dir,
        FileName const abs_path = fileSearch(dir, file);
        if (abs_path.empty()) {
                // FIXME UNICODE
-               throw ExceptionMessage(WarningException, _("File not found"), bformat(
+               throw ExceptionMessage(ErrorException, _("File not found"), bformat(
                        _("Invalid %1$s switch.\nDirectory %2$s does not contain %3$s."),
                        from_utf8(command_line_switch), from_utf8(dir),
                        from_utf8(file)));