]> git.lyx.org Git - lyx.git/blobdiff - src/support/filetools.C
fix crossref label list, some debug messages + various
[lyx.git] / src / support / filetools.C
index 2ae0400dc5676c00663598b094354531743bff94..2c816a913c1dc509b101926897ca8a62229acbd4 100644 (file)
@@ -17,6 +17,7 @@
 #include <config.h>
 
 #include <cctype>
+
 #include <utility>
 using std::make_pair;
 using std::pair;
@@ -97,10 +98,11 @@ string QuoteName(string const & name)
 #ifdef WITH_WARNINGS
 #warning Add proper emx support here!
 #endif
-       string qname = name;
-       while (qname.find("'") != string::npos) 
-               LSubstring(qname, "'") = "\\'";
-       return '\'' + qname + '\'';
+#ifndef __EMX__
+       return '\'' + name + '\'';
+#else
+       return name; 
+#endif
 }
 
 
@@ -152,17 +154,12 @@ bool IsFileReadable (string const & path)
 //      -1 error (doesn't exist, no access, anything else) 
 int IsFileWriteable (string const & path)
 {
-       FilePtr fp(path, FilePtr::update);
-       if (!fp()) {
-               if ((errno == EACCES) || (errno == EROFS)) {
-                       fp.reopen(path, FilePtr::read);
-                       if (fp()) {
-                               return 0;
-                       }
-               }
-               return -1;
-       }
-       return 1;
+       FileInfo fi(path);
+       if (fi.access(FileInfo::wperm|FileInfo::rperm)) // read-write
+               return 1;
+       if (fi.readable()) // read-only
+               return 0;
+       return -1; // everything else.
 }
 
 
@@ -178,23 +175,10 @@ int IsDirWriteable (string const & path)
                             _("Could not test if directory is writeable"));
                return -1;
        } else {
-       FilePtr fp(tmpfl, FilePtr::truncate);
-       if (!fp()) {
-               if (errno == EACCES) {
-                       return 0;
-               } else { 
-                       WriteFSAlert(_("LyX Internal Error!"), 
-                                    _("Cannot open directory test file"));
-                       return -1;
-               }
-               }
+               FileInfo fi(path);
+               if (fi.writable()) return 1;
+               return 0;
        }
-               if (remove(tmpfl.c_str())) {
-                       WriteFSAlert(_("LyX Internal Error!"), 
-                                   _("Created test file but cannot remove it?"));
-                       return -1;
-       }
-       return 1;
 }
 
 
@@ -204,7 +188,7 @@ int IsDirWriteable (string const & path)
 // If path entry begins with $$User/, use user_lyxdir
 // Example: "$$User/doc;$$LyX/doc"
 string FileOpenSearch (string const & path, string const & name, 
-                       string const & ext)
+                      string const & ext)
 {
        string real_file, path_element;
        bool notfound = true;
@@ -218,13 +202,13 @@ string FileOpenSearch (string const & path, string const & name,
                path_element = subst(path_element, "$$User", user_lyxdir);
                
                real_file = FileSearch(path_element, name, ext);
-
+               
                if (real_file.empty()) {
-                 do {
-                   tmppath = split(tmppath, path_element, ';');
-                 } while(!tmppath.empty() && path_element.empty());
+                       do {
+                               tmppath = split(tmppath, path_element, ';');
+                       } while(!tmppath.empty() && path_element.empty());
                } else {
-                 notfound = false;
+                       notfound = false;
                }
        }
 #ifdef __EMX__
@@ -274,13 +258,13 @@ string LibFileSearch(string const & dir, string const & name,
                                     name, ext); 
        if (!fullname.empty())
                return fullname;
-
+       
        if (!build_lyxdir.empty()) 
                fullname = FileSearch(AddPath(build_lyxdir, dir), 
                                      name, ext);
        if (!fullname.empty())
                return fullname;
-
+       
        return FileSearch(AddPath(system_lyxdir, dir), name, ext);
 }
 
@@ -289,12 +273,12 @@ string i18nLibFileSearch(string const & dir, string const & name,
                         string const & ext)
 {
        string lang = token(string(GetEnv("LANG")), '_', 0);
-
+       
        if (lang.empty() || lang == "C")
                return LibFileSearch(dir, name, ext);
        else {
                string tmp = LibFileSearch(dir, lang + '_' + name,
-                                           ext);
+                                          ext);
                if (!tmp.empty())
                        return tmp;
                else
@@ -333,7 +317,24 @@ bool PutEnv(string const & envstr)
         // this leaks, but what can we do about it?
         //   Is doing a getenv() and a free() of the older value 
         //   a good idea? (JMarc)
-        int retval = putenv((new string(envstr))->c_str());
+       // Actually we don't have to leak...calling putenv like this
+       // should be enough: ... and this is obviously not enough if putenv
+       // does not make a copy of the string. It is also not very wise to
+       // put a string on the free store. If we have to leak we should do it
+       // like this:
+       char * leaker = new char[envstr.length() + 1];
+       envstr.copy(leaker, envstr.length());
+       leaker[envstr.length()] = '\0';
+       int retval = lyx::putenv(leaker);
+
+       // If putenv does not make a copy of the char const * this
+       // is very dangerous. OTOH if it does take a copy this is the
+       // best solution.
+       // The  only implementation of putenv that I have seen does not
+       // allocate memory. _And_ after testing the putenv in glibc it
+       // seems that we need to make a copy of the string contents.
+       // I will enable the above.
+       //int retval = lyx::putenv(envstr.c_str());
 #else
 #ifdef HAVE_SETENV 
         string varname;
@@ -354,6 +355,24 @@ bool PutEnvPath(string const & envstr)
 static
 int DeleteAllFilesInDir (string const & path)
 {
+       // I have decided that we will be using parts from the boost
+       // library. Check out http://www.boost.org/
+       // For directory access we will then use the directory_iterator.
+       // Then the code will be something like:
+       // directory_iterator dit(path.c_str());
+       // if (<some way to detect failure>) {
+       //         WriteFSAlert(_("Error! Cannot open directory:"), path);
+       //         return -1;
+       // }
+       // for (; dit != <someend>; ++dit) {
+       //         if ((*dit) == 2." || (*dit) == "..")
+       //                 continue;
+       //         string unlinkpath = AddName(path, temp);
+       //         if (remove(unlinkpath.c_str()))
+       //                 WriteFSAlert(_("Error! Could not remove file:"),
+       //                              unlinkpath);
+       // }
+       // return 0;
        DIR * dir = opendir(path.c_str());
        if (!dir) {
                WriteFSAlert (_("Error! Cannot open directory:"), path);
@@ -382,7 +401,7 @@ string CreateTmpDir (string const & tempdir, string const & mask)
 {
        string tmpfl = TmpFileName(tempdir, mask);
        
-       if ((tmpfl.empty()) || mkdir (tmpfl.c_str(), 0777)) {
+       if ((tmpfl.empty()) || lyx::mkdir (tmpfl.c_str(), 0777)) {
                WriteFSAlert(_("Error! Couldn't create temporary directory:"),
                             tempdir);
                return string();
@@ -422,7 +441,7 @@ int DestroyBufferTmpDir (string const & tmpdir)
 string CreateLyXTmpDir (string const & deflt)
 {
        if ((!deflt.empty()) && (deflt  != "/tmp")) {
-               if (mkdir(deflt.c_str(), 0777)) {
+               if (lyx::mkdir(deflt.c_str(), 0777)) {
 #ifdef __EMX__
                         Path p(user_lyxdir);
 #endif
@@ -457,7 +476,7 @@ bool createDirectory(string const & path, int permission)
                return false;
        }
 
-       if (mkdir(temp.c_str(), permission)) {
+       if (lyx::mkdir(temp.c_str(), permission)) {
                WriteFSAlert (_("Error! Couldn't create directory:"), temp);
                return false;
        }
@@ -832,7 +851,7 @@ string MakeRelPath(string const & abspath0, string const & basepath0)
        int j = i;
        while (j < baselen) {
                if (basepath[j] == '/') {
-                       if (j+1 == baselen) break;
+                       if (j + 1 == baselen) break;
                        buf += "../";
                }
                ++j;
@@ -978,7 +997,7 @@ static cmdret do_popen(string const & cmd)
        // create our own popen based on fork, exec, pipe
        // of course the best would be to have a
        // pstream (process stream), with the
-       // variants ipstream, opstream and
+       // variants ipstream, opstream
        FILE * inf = popen(cmd.c_str(), "r");
        string ret;
        int c = fgetc(inf);
@@ -1010,7 +1029,8 @@ string findtexfile(string const & fil, string const & format)
                return OnlyFilename(fil);
        
         // No we try to find it using kpsewhich.
-        string kpsecmd = "kpsewhich --format= " + format + " " + OnlyFilename(fil);
+        string kpsecmd = "kpsewhich --format=" + format + " " + OnlyFilename(fil);
+
         cmdret c = do_popen(kpsecmd);
        
         lyxerr[Debug::LATEX] << "kpse status = " << c.first << "\n"