]> 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 578bca69ffbf3d6119f627fd968a7cdd8a08c9e3..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"
@@ -68,13 +74,13 @@ extern string system_tempdir;
 
 bool IsLyXFilename(string const & filename)
 {
-       return suffixIs(filename, ".lyx");
+       return suffixIs(lowercase(filename), ".lyx");
 }
 
 
 bool IsSGMLFilename(string const & filename)
 {
-       return suffixIs(filename, ".sgml");
+       return suffixIs(lowercase(filename), ".sgml");
 }
 
 
@@ -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;
 }
 
 
@@ -226,7 +229,7 @@ vector<string> const DirList(string const & dir, string const & ext)
           vector<string> dirlist;
           directory_iterator dit("dir");
           while (dit != directory_iterator()) {
-                  string fil = (*dit).filename;
+                  string fil = dit->filename;
                   if (prefixIs(fil, extension)) {
                           dirlist.push_back(fil);
                   }
@@ -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);
@@ -408,7 +426,8 @@ int DeleteAllFilesInDir (string const & path)
                        continue;
                string const unlinkpath = AddName (path, temp);
 
-               lyxerr.debug() << "Deleting file: " << unlinkpath << endl;
+               lyxerr[Debug::FILES] << "Deleting file: " << unlinkpath 
+                                    << endl;
 
                bool deleted = true;
                if (FileInfo(unlinkpath).isDir())
@@ -466,12 +485,12 @@ int DestroyTmpDir(string const & tmpdir, bool Allfiles)
 
 string const CreateBufferTmpDir(string const & pathfor)
 {
-       static int count = 0;
+       static int count;
        static string const tmpdir(pathfor.empty() ? os::getTmpDir() : pathfor);
        // We are in our own directory.  Why bother to mangle name?
        // In fact I wrote this code to circumvent a problematic behaviour (bug?)
        // of EMX mkstemp().
-       string tmpfl = tmpdir + "/lyx_tmpbuf" + tostr(count++);
+       string const tmpfl = tmpdir + "/lyx_tmpbuf" + tostr(count++);
        if (lyx::mkdir(tmpfl, 0777)) {
                WriteFSAlert(_("Error! Couldn't create temporary directory:"),
                             tmpdir);
@@ -553,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
@@ -563,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);
@@ -647,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;
@@ -694,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
@@ -730,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();
@@ -954,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;
@@ -1072,7 +1088,10 @@ findtexfile(string const & fil, string const & /*format*/)
         lyxerr[Debug::LATEX] << "kpse status = " << c.first << "\n"
                             << "kpse result = `" << strip(c.second, '\n') 
                             << "'" << endl;
-        return c.first != -1 ? strip(c.second, '\n') : string();
+       if (c.first != -1) 
+               return os::internal_path(strip(strip(c.second, '\n'), '\r'));
+       else
+               return string();
 }