]> git.lyx.org Git - lyx.git/blobdiff - src/support/filetools.C
lyxserver cleanup patch + andre's small patches
[lyx.git] / src / support / filetools.C
index c247043bc34f6ace27376f27651bb7bd30a41920..e21eb0da48ea38efb9e676111929fdc332b3beb2 100644 (file)
@@ -6,7 +6,7 @@
         Parts Copyright 1985, 1990, 1993 Free Software Foundation, Inc.
        Parts Copyright 1996 Asger Alstrup
        
-       See also filetools.H.
+       See also filetools.h.
 
        lyx-filetool.C : tools functions for file/path handling
        this file is part of LyX, the High Level Word Processor
 #pragma implementation "filetools.h"
 #endif
 
+#include <cstdlib>
+#include <fcntl.h>
+#include <cerrno>
+#include "debug.h"
+#include "support/lstrings.h"
+
 #include "filetools.h"
 #include "LSubstring.h"
 #include "lyx_gui_misc.h"
@@ -128,6 +134,8 @@ bool IsFileReadable (string const & path)
 int IsFileWriteable (string const & path)
 {
        FileInfo fi(path);
+       //lyxerr << "fi : " << fi << endl; 
+       //lyxerr << "fi.exists" << fi.exist() << endl;
        if (fi.access(FileInfo::wperm|FileInfo::rperm)) // read-write
                return 1;
        if (fi.readable()) // read-only
@@ -136,24 +144,19 @@ int IsFileWriteable (string const & path)
 }
 
 
-//returns 1: dir writeable
-//       0: not writeable
-//      -1: error- couldn't find out
-int IsDirWriteable (string const & path)
+//returns true: dir writeable
+//       false: not writeable
+bool IsDirWriteable (string const & path)
 {
+       lyxerr[Debug::FILES] << "IsDirWriteable: " << path << endl;
         string const tmpfl(lyx::tempName(path, "lyxwritetest"));
-       // We must unlink the tmpfl.
+
+       if (tmpfl.empty())
+               return false;
        lyx::unlink(tmpfl);
-       
-       if (tmpfl.empty()) {
-               WriteFSAlert(_("LyX Internal Error!"), 
-                            _("Could not test if directory is writeable"));
-               return -1;
-       } else {
-               FileInfo fi(path);
-               if (fi.writable()) return 1;
-               return 0;
-       }
+       return true;
 }
 
 
@@ -289,7 +292,22 @@ string const
 i18nLibFileSearch(string const & dir, string const & name, 
                  string const & ext)
 {
-       string const lang = token(string(GetEnv("LANG")), '_', 0);
+       // this comment is from intl/dcigettext.c. We try to mimick this
+       // behaviour here.  
+       /* The highest priority value is the `LANGUAGE' environment
+           variable. But we don't use the value if the currently
+           selected locale is the C locale. This is a GNU extension. */
+
+       string const lc_all = GetEnv("LC_ALL");
+       string lang = GetEnv("LANGUAGE");
+       if (lang.empty() || lc_all == "C") {
+               lang = lc_all;
+               if (lang.empty()) {
+                       lang = GetEnv("LANG");
+               }
+       }
+       
+       lang = token(lang, '_', 0);
        
        if (lang.empty() || lang == "C")
                return LibFileSearch(dir, name, ext);
@@ -554,7 +572,7 @@ string const OnlyPath(string const & Filename)
 string const MakeAbsPath(string const & RelPath, string const & BasePath)
 {
        // checks for already absolute path
-       if (AbsolutePath(RelPath))
+       if (os::is_absolute_path(RelPath))
                return RelPath;
 
        // Copies given paths
@@ -564,7 +582,7 @@ string const MakeAbsPath(string const & RelPath, string const & BasePath)
 
        string TempBase;
 
-       if (AbsolutePath(BasePath))
+       if (os::is_absolute_path(BasePath))
                TempBase = BasePath;
        else
                TempBase = AddPath(lyx::getcwd(), BasePath);
@@ -648,24 +666,21 @@ string const OnlyFilename(string const & fname)
 }
 
 
-// Is a filename/path absolute?
+/// Returns true is path is absolute
 bool AbsolutePath(string const & path)
 {
-#ifndef __EMX__
-       return (!path.empty() && path[0] == '/');
-#else
-       return (!path.empty() && isalpha(static_cast<unsigned char>(path[0])) && path.length() > 1 && path[1] == ':');
-#endif
+       return os::is_absolute_path(path);
 }
 
 
+
 // Create absolute path. If impossible, don't do anything
 // Supports ./ and ~/. Later we can add support for ~logname/. (Asger)
 string const ExpandPath(string const & path)
 {
        // checks for already absolute path
        string RTemp(ReplaceEnvironmentPath(path));
-       if (AbsolutePath(RTemp))
+       if (os::is_absolute_path(RTemp))
                return RTemp;
 
        string Temp;
@@ -695,7 +710,7 @@ string const NormalizePath(string const & path)
        string RTemp;
        string Temp;
 
-       if (AbsolutePath(path))
+       if (os::is_absolute_path(path))
                RTemp = path;
        else
                // Make implicit current directory explicit
@@ -731,7 +746,7 @@ string const GetFileContents(string const & fname)
        FileInfo finfo(fname);
        if (finfo.exist()) {
                ifstream ifs(fname.c_str());
-               std::ostringstream ofs;
+               ostringstream ofs;
                if (ifs && ofs) {
                        ofs << ifs.rdbuf();
                        ifs.close();
@@ -955,7 +970,7 @@ MakeDisplayPath (string const & path, unsigned int threshold)
 
        // If we backup from home or don't have a relative path,
        // this try is no good
-       if (prefixIs(relhome, "../") || AbsolutePath(relhome)) {
+       if (prefixIs(relhome, "../") || os::is_absolute_path(relhome)) {
                // relative path was no good, just use the original path
                relhome = path;
                l2 = l1;
@@ -1074,7 +1089,7 @@ findtexfile(string const & fil, string const & /*format*/)
                             << "kpse result = `" << strip(c.second, '\n') 
                             << "'" << endl;
        if (c.first != -1) 
-               return strip(strip(c.second, '\n'), '\r');
+               return os::internal_path(strip(strip(c.second, '\n'), '\r'));
        else
                return string();
 }