]> git.lyx.org Git - lyx.git/blobdiff - src/support/filetools.cpp
Now that exceptions are allowed, handle gracefully the case where a Buffer temp direc...
[lyx.git] / src / support / filetools.cpp
index cb2ab4a8def7d6041a2deec77df9076d50560048..aa9e8429b968333d15ca1b05bf96b39162e56cce 100644 (file)
 
 #include <config.h>
 
+#include "support/filetools.h"
+
 #include "support/convert.h"
+#include "support/debug.h"
 #include "support/environment.h"
-#include "support/filetools.h"
-#include "support/fs_extras.h"
+#include "support/gettext.h"
 #include "support/lstrings.h"
-#include "support/lyxlib.h"
 #include "support/os.h"
 #include "support/Package.h"
 #include "support/Path.h"
 #include "support/Systemcall.h"
+#include "support/qstring_helpers.h"
 
-// FIXME Interface violation
-#include "gettext.h"
-#include "debug.h"
+#include <QDir>
 
 #include <boost/assert.hpp>
-#include <boost/filesystem/operations.hpp>
 #include <boost/regex.hpp>
 
 #include <fcntl.h>
 #include <fstream>
 #include <sstream>
 
-using std::endl;
-using std::getline;
-using std::make_pair;
-using std::string;
-using std::ifstream;
-using std::ostringstream;
-using std::vector;
-using std::pair;
-
-namespace fs = boost::filesystem;
+using namespace std;
 
 namespace lyx {
 namespace support {
@@ -113,12 +103,14 @@ string const latex_path(string const & original_path,
 
 
 // Substitutes spaces with underscores in filename (and path)
-string const makeLatexName(string const & file)
+FileName const makeLatexName(FileName const & file)
 {
-       string name = onlyFilename(file);
-       string const path = onlyPath(file);
+       string name = file.onlyFileName();
+       string const path = file.onlyPath().absFilename() + "/";
 
        // ok so we scan through the string twice, but who cares.
+       // FIXME: in Unicode time this will break for sure! There is
+       // a non-latin world out there...
        string const keep = "abcdefghijklmnopqrstuvwxyz"
                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                "@!'()*+,-./0123456789:;<=>?[]`|";
@@ -127,7 +119,9 @@ string const makeLatexName(string const & file)
        while ((pos = name.find_first_not_of(keep, pos)) != string::npos)
                name[pos++] = '_';
 
-       return addName(path, name);
+       FileName latex_name(path + name);
+       latex_name.changeExtension(".tex");
+       return latex_name;
 }
 
 
@@ -192,37 +186,6 @@ FileName const fileOpenSearch(string const & path, string const & name,
 #endif
 
 
-/// Returns a vector of all files in directory dir having extension ext.
-vector<FileName> const dirList(FileName const & dir, string const & ext)
-{
-       // EXCEPTIONS FIXME. Rewrite needed when we turn on exceptions. (Lgb)
-       vector<FileName> dirlist;
-
-       if (!(dir.exists() && dir.isDirectory())) {
-               LYXERR(Debug::FILES)
-                       << "Directory \"" << dir
-                       << "\" does not exist to DirList." << endl;
-               return dirlist;
-       }
-
-       string extension;
-       if (!ext.empty() && ext[0] != '.')
-               extension += '.';
-       extension += ext;
-
-       string const encoded_dir = dir.toFilesystemEncoding();
-       fs::directory_iterator dit(encoded_dir);
-       fs::directory_iterator end;
-       for (; dit != end; ++dit) {
-               string const & fil = dit->leaf();
-               if (suffixIs(fil, extension))
-                       dirlist.push_back(FileName::fromFilesystemEncoding(
-                                       encoded_dir + '/' + fil));
-       }
-       return dirlist;
-}
-
-
 // Returns the real name of file name in directory path, with optional
 // extension ext.
 FileName const fileSearch(string const & path, string const & name,
@@ -233,7 +196,7 @@ FileName const fileSearch(string const & path, string const & name,
        string const tmpname = replaceEnvironmentPath(name);
        FileName fullname(makeAbsPath(tmpname, path));
        // search first without extension, then with it.
-       if (fullname.isReadable())
+       if (fullname.isReadableFile())
                return fullname;
        if (ext.empty())
                // We are done.
@@ -242,7 +205,7 @@ FileName const fileSearch(string const & path, string const & name,
        // fullname.
        if (getExtension(fullname.absFilename()) != ext)
                fullname = FileName(addExtension(fullname.absFilename(), ext));
-       if (fullname.isReadable() || mode == allow_unreadable)
+       if (fullname.isReadableFile() || mode == allow_unreadable)
                return fullname;
        return FileName();
 }
@@ -277,7 +240,7 @@ FileName const i18nLibFileSearch(string const & dir, string const & name,
           variable. But we don't use the value if the currently
           selected locale is the C locale. This is a GNU extension.
 
-          Otherwise, w use a trick to guess what gettext has done:
+          Otherwise, w use a trick to guess what support/gettext.has done:
           each po file is able to tell us its name. (JMarc)
        */
 
@@ -355,18 +318,18 @@ string const libScriptSearch(string const & command_in, quote_style style)
 
 static FileName createTmpDir(FileName const & tempdir, string const & mask)
 {
-       LYXERR(Debug::FILES)
-               << "createTmpDir: tempdir=`" << tempdir << "'\n"
-               << "createTmpDir:    mask=`" << mask << '\'' << endl;
+       LYXERR(Debug::FILES, "createTmpDir: tempdir=`" << tempdir << "'\n"
+               << "createTmpDir:    mask=`" << mask << '\'');
 
-       FileName const tmpfl(tempName(tempdir, mask));
-       // lyx::tempName actually creates a file to make sure that it
+       FileName const tmpfl = FileName::tempName(tempdir.absFilename()
+               + "/" + mask);
+       // FileName::tempName actually creates a file to make sure that it
        // stays unique. So we have to delete it before we can create
        // a dir with the same name. Note also that we are not thread
        // safe because of the gap between unlink and mkdir. (Lgb)
-       unlink(tmpfl);
+       tmpfl.removeFile();
 
-       if (tmpfl.empty() || mkdir(tmpfl, 0700)) {
+       if (tmpfl.empty() || !tmpfl.createDirectory(0700)) {
                lyxerr << "LyX could not create the temporary directory '"
                       << tmpfl << "'" << endl;
                return FileName();
@@ -375,64 +338,27 @@ static FileName createTmpDir(FileName const & tempdir, string const & mask)
        return tmpfl;
 }
 
-bool destroyDir(FileName const & tmpdir)
-{
-       try {
-               return fs::remove_all(tmpdir.toFilesystemEncoding()) > 0;
-       } catch (fs::filesystem_error const & fe){
-               lyxerr << "Could not delete " << tmpdir << ". (" << fe.what() << ")" << std::endl;
-               return false;
-       }
-}
-
 
-string const createBufferTmpDir()
+FileName const createLyXTmpDir(FileName const & deflt)
 {
-       static int count;
-       // 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 const tmpfl =
-               package().temp_dir().absFilename() + "/lyx_tmpbuf" +
-               convert<string>(count++);
-
-       if (mkdir(FileName(tmpfl), 0777)) {
-               lyxerr << "LyX could not create the temporary directory '"
-                      << tmpfl << "'" << endl;
-               return string();
-       }
-       return tmpfl;
-}
+       if (deflt.empty() || deflt.absFilename() == "/tmp")
+               return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
 
+       if (deflt.createDirectory(0777)) 
+               return deflt;
 
-FileName const createLyXTmpDir(FileName const & deflt)
-{
-       if (!deflt.empty() && deflt.absFilename() != "/tmp") {
-               if (mkdir(deflt, 0777)) {
-                       if (deflt.isDirWritable()) {
-                               // deflt could not be created because it
-                               // did exist already, so let's create our own
-                               // dir inside deflt.
-                               return createTmpDir(deflt, "lyx_tmpdir");
-                       } else {
-                               // some other error occured.
-                               return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
-                       }
-               } else
-                       return deflt;
+       if (deflt.isDirWritable()) {
+               // deflt could not be created because it
+               // did exist already, so let's create our own
+               // dir inside deflt.
+               return createTmpDir(deflt, "lyx_tmpdir");
        } else {
+               // some other error occured.
                return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
        }
 }
 
 
-bool createDirectory(FileName const & path, int permission)
-{
-       BOOST_ASSERT(!path.empty());
-       return mkdir(path, permission) == 0;
-}
-
-
 // Strip filename from path name
 string const onlyPath(string const & filename)
 {
@@ -441,7 +367,7 @@ string const onlyPath(string const & filename)
                return filename;
 
        // Find last / or start of filename
-       string::size_type j = filename.rfind('/');
+       size_t j = filename.rfind('/');
        return j == string::npos ? "./" : filename.substr(0, j + 1);
 }
 
@@ -451,9 +377,10 @@ string const onlyPath(string const & filename)
 // If basepath is empty, use CWD as base.
 FileName const makeAbsPath(string const & relPath, string const & basePath)
 {
+       FileName relative_path(relPath);
        // checks for already absolute path
-       if (os::is_absolute_path(relPath))
-               return FileName(relPath);
+       if (relative_path.isAbsolute())
+               return relative_path;
 
        // Copies given paths
        string tempRel = os::internal_path(relPath);
@@ -462,10 +389,11 @@ FileName const makeAbsPath(string const & relPath, string const & basePath)
 
        string tempBase;
 
-       if (os::is_absolute_path(basePath))
+       FileName base_path(basePath);
+       if (base_path.isAbsolute())
                tempBase = basePath;
        else
-               tempBase = addPath(getcwd().absFilename(), basePath);
+               tempBase = addPath(FileName::getcwd().absFilename(), basePath);
 
        // Handle /./ at the end of the path
        while (suffixIs(tempBase, "/./"))
@@ -503,7 +431,7 @@ FileName const makeAbsPath(string const & relPath, string const & basePath)
        }
 
        // returns absolute path
-       return FileName(os::internal_path(tempBase));
+       return FileName(tempBase);
 }
 
 
@@ -540,20 +468,14 @@ string const onlyFilename(string const & fname)
 }
 
 
-/// Returns true is path is absolute
-bool absolutePath(string const & path)
-{
-       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 (os::is_absolute_path(rTemp))
+       FileName abs_path(rTemp);
+       if (abs_path.isAbsolute())
                return rTemp;
 
        string temp;
@@ -563,7 +485,7 @@ string const expandPath(string const & path)
        rTemp = split(rTemp, temp, '/');
 
        if (temp == ".")
-               return getcwd().absFilename() + '/' + rTemp;
+               return FileName::getcwd().absFilename() + '/' + rTemp;
 
        if (temp == "~")
                return package().home_dir().absFilename() + '/' + rTemp;
@@ -576,41 +498,6 @@ string const expandPath(string const & path)
 }
 
 
-// Normalize a path. Constracts path/../path
-// Can't handle "../../" or "/../" (Asger)
-// Also converts paths like /foo//bar ==> /foo/bar
-string const normalizePath(string const & path)
-{
-       // Normalize paths like /foo//bar ==> /foo/bar
-       static boost::regex regex("/{2,}");
-       string const tmppath = boost::regex_merge(path, regex, "/");
-
-       fs::path const npath = fs::path(tmppath, fs::no_check).normalize();
-
-       if (!npath.is_complete())
-               return "./" + npath.string() + '/';
-
-       return npath.string() + '/';
-}
-
-
-string const getFileContents(FileName const & fname)
-{
-       if (fname.exists()) {
-               string const encodedname = fname.toFilesystemEncoding();
-               ifstream ifs(encodedname.c_str());
-               ostringstream ofs;
-               if (ifs && ofs) {
-                       ofs << ifs.rdbuf();
-                       ifs.close();
-                       return ofs.str();
-               }
-       }
-       lyxerr << "LyX was not able to read file '" << fname << '\'' << endl;
-       return string();
-}
-
-
 // Search the string for ${VAR} and $VAR and replace VAR using getenv.
 string const replaceEnvironmentPath(string const & path)
 {
@@ -753,195 +640,6 @@ string const getExtension(string const & name)
 }
 
 
-// the different filetypes and what they contain in one of the first lines
-// (dots are any characters).          (Herbert 20020131)
-// AGR Grace...
-// BMP BM...
-// EPS %!PS-Adobe-3.0 EPSF...
-// FIG #FIG...
-// FITS ...BITPIX...
-// GIF GIF...
-// JPG JFIF
-// PDF %PDF-...
-// PNG .PNG...
-// PBM P1... or P4     (B/W)
-// PGM P2... or P5     (Grayscale)
-// PPM P3... or P6     (color)
-// PS  %!PS-Adobe-2.0 or 1.0,  no "EPSF"!
-// SGI \001\332...     (decimal 474)
-// TGIF        %TGIF...
-// TIFF        II... or MM...
-// XBM ..._bits[]...
-// XPM /* XPM */    sometimes missing (f.ex. tgif-export)
-//      ...static char *...
-// XWD \000\000\000\151        (0x00006900) decimal 105
-//
-// GZIP        \037\213        http://www.ietf.org/rfc/rfc1952.txt
-// ZIP PK...                   http://www.halyava.ru/document/ind_arch.htm
-// Z   \037\235                UNIX compress
-
-string const getFormatFromContents(FileName const & filename)
-{
-       // paranoia check
-       if (filename.empty() || !filename.isReadable())
-               return string();
-
-       ifstream ifs(filename.toFilesystemEncoding().c_str());
-       if (!ifs)
-               // Couldn't open file...
-               return string();
-
-       // gnuzip
-       static string const gzipStamp = "\037\213";
-
-       // PKZIP
-       static string const zipStamp = "PK";
-
-       // compress
-       static string const compressStamp = "\037\235";
-
-       // Maximum strings to read
-       int const max_count = 50;
-       int count = 0;
-
-       string str;
-       string format;
-       bool firstLine = true;
-       while ((count++ < max_count) && format.empty()) {
-               if (ifs.eof()) {
-                       LYXERR(Debug::GRAPHICS)
-                               << "filetools(getFormatFromContents)\n"
-                               << "\tFile type not recognised before EOF!"
-                               << endl;
-                       break;
-               }
-
-               getline(ifs, str);
-               string const stamp = str.substr(0, 2);
-               if (firstLine && str.size() >= 2) {
-                       // at first we check for a zipped file, because this
-                       // information is saved in the first bytes of the file!
-                       // also some graphic formats which save the information
-                       // in the first line, too.
-                       if (prefixIs(str, gzipStamp)) {
-                               format =  "gzip";
-
-                       } else if (stamp == zipStamp) {
-                               format =  "zip";
-
-                       } else if (stamp == compressStamp) {
-                               format =  "compress";
-
-                       // the graphics part
-                       } else if (stamp == "BM") {
-                               format =  "bmp";
-
-                       } else if (stamp == "\001\332") {
-                               format =  "sgi";
-
-                       // PBM family
-                       // Don't need to use str.at(0), str.at(1) because
-                       // we already know that str.size() >= 2
-                       } else if (str[0] == 'P') {
-                               switch (str[1]) {
-                               case '1':
-                               case '4':
-                                       format =  "pbm";
-                                   break;
-                               case '2':
-                               case '5':
-                                       format =  "pgm";
-                                   break;
-                               case '3':
-                               case '6':
-                                       format =  "ppm";
-                               }
-                               break;
-
-                       } else if ((stamp == "II") || (stamp == "MM")) {
-                               format =  "tiff";
-
-                       } else if (prefixIs(str,"%TGIF")) {
-                               format =  "tgif";
-
-                       } else if (prefixIs(str,"#FIG")) {
-                               format =  "fig";
-
-                       } else if (prefixIs(str,"GIF")) {
-                               format =  "gif";
-
-                       } else if (str.size() > 3) {
-                               int const c = ((str[0] << 24) & (str[1] << 16) &
-                                              (str[2] << 8)  & str[3]);
-                               if (c == 105) {
-                                       format =  "xwd";
-                               }
-                       }
-
-                       firstLine = false;
-               }
-
-               if (!format.empty())
-                   break;
-               else if (contains(str,"EPSF"))
-                       // dummy, if we have wrong file description like
-                       // %!PS-Adobe-2.0EPSF"
-                       format = "eps";
-
-               else if (contains(str, "Grace"))
-                       format = "agr";
-
-               else if (contains(str, "JFIF"))
-                       format = "jpg";
-
-               else if (contains(str, "%PDF"))
-                       format = "pdf";
-
-               else if (contains(str, "PNG"))
-                       format = "png";
-
-               else if (contains(str, "%!PS-Adobe")) {
-                       // eps or ps
-                       ifs >> str;
-                       if (contains(str,"EPSF"))
-                               format = "eps";
-                       else
-                           format = "ps";
-               }
-
-               else if (contains(str, "_bits[]"))
-                       format = "xbm";
-
-               else if (contains(str, "XPM") || contains(str, "static char *"))
-                       format = "xpm";
-
-               else if (contains(str, "BITPIX"))
-                       format = "fits";
-       }
-
-       if (!format.empty()) {
-               LYXERR(Debug::GRAPHICS)
-                       << "Recognised Fileformat: " << format << endl;
-               return format;
-       }
-
-       LYXERR(Debug::GRAPHICS)
-               << "filetools(getFormatFromContents)\n"
-               << "\tCouldn't find a known format!\n";
-       return string();
-}
-
-
-/// check for zipped file
-bool zippedFile(FileName const & name)
-{
-       string const type = getFormatFromContents(name);
-       if (contains("gzip zip compress", type) && !type.empty())
-               return true;
-       return false;
-}
-
-
 string const unzippedFileName(string const & zipped_file)
 {
        string const ext = getExtension(zipped_file);
@@ -1116,12 +814,10 @@ FileName const findtexfile(string const & fil, string const & /*format*/)
 
        cmd_ret const c = runCommand(kpsecmd);
 
-       LYXERR(Debug::LATEX) << "kpse status = " << c.first << '\n'
-                << "kpse result = `" << rtrim(c.second, "\n\r")
-                << '\'' << endl;
+       LYXERR(Debug::LATEX, "kpse status = " << c.first << '\n'
+                << "kpse result = `" << rtrim(c.second, "\n\r") << '\'');
        if (c.first != -1)
-               return FileName(os::internal_path(rtrim(to_utf8(from_filesystem8bit(c.second)),
-                                                       "\n\r")));
+               return FileName(rtrim(to_utf8(from_filesystem8bit(c.second)), "\n\r"));
        else
                return FileName();
 }
@@ -1135,18 +831,17 @@ void removeAutosaveFile(string const & filename)
        a += '#';
        FileName const autosave(a);
        if (autosave.exists())
-               unlink(autosave);
+               autosave.removeFile();
 }
 
 
 void readBB_lyxerrMessage(FileName const & file, bool & zipped,
        string const & message)
 {
-       LYXERR(Debug::GRAPHICS) << "[readBB_from_PSFile] "
-               << message << std::endl;
+       LYXERR(Debug::GRAPHICS, "[readBB_from_PSFile] " << message);
        // FIXME: Why is this func deleting a file? (Lgb)
        if (zipped)
-               unlink(file);
+               file.removeFile();
 }
 
 
@@ -1159,9 +854,9 @@ string const readBB_from_PSFile(FileName const & file)
        // end of the file. Than we have in the header:
        // %%BoundingBox: (atend)
        // In this case we must check the end.
-       bool zipped = zippedFile(file);
+       bool zipped = file.isZippedFile();
        FileName const file_ = zipped ? unzipFile(file) : file;
-       string const format = getFormatFromContents(file_);
+       string const format = file_.guessFormatFromContents();
 
        if (format != "eps" && format != "ps") {
                readBB_lyxerrMessage(file_, zipped,"no(e)ps-format");
@@ -1170,7 +865,7 @@ string const readBB_from_PSFile(FileName const & file)
 
        static boost::regex bbox_re(
                "^%%BoundingBox:\\s*([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)");
-       std::ifstream is(file_.toFilesystemEncoding().c_str());
+       ifstream is(file_.toFilesystemEncoding().c_str());
        while (is) {
                string s;
                getline(is,s);
@@ -1213,5 +908,6 @@ int compare_timestamps(FileName const & file1, FileName const & file2)
        return cmp;
 }
 
+
 } //namespace support
 } // namespace lyx