]> git.lyx.org Git - lyx.git/blobdiff - src/support/filetools.cpp
* get rid of support::absolutePath()
[lyx.git] / src / support / filetools.cpp
index 4b80d824fe4c63eb6004195917d392f14dfdc843..a7cf272e6ba3cce252bd6b368cbf1c7c18e8774b 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/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 {
@@ -112,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:;<=>?[]`|";
@@ -126,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;
 }
 
 
@@ -245,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)
        */
 
@@ -326,14 +321,15 @@ static FileName createTmpDir(FileName const & tempdir, string const & mask)
        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();
@@ -352,7 +348,7 @@ string const createBufferTmpDir()
                package().temp_dir().absFilename() + "/lyx_tmpbuf" +
                convert<string>(count++);
 
-       if (mkdir(FileName(tmpfl), 0777)) {
+       if (!FileName(tmpfl).createDirectory(0777)) {
                lyxerr << "LyX could not create the temporary directory '"
                       << tmpfl << "'" << endl;
                return string();
@@ -366,7 +362,7 @@ FileName const createLyXTmpDir(FileName const & deflt)
        if (deflt.empty() || deflt.absFilename() == "/tmp")
                return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
 
-       if (!mkdir(deflt, 0777)) 
+       if (deflt.createDirectory(0777)) 
                return deflt;
 
        if (deflt.isDirWritable()) {
@@ -399,9 +395,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);
@@ -410,10 +407,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, "/./"))
@@ -451,7 +449,7 @@ FileName const makeAbsPath(string const & relPath, string const & basePath)
        }
 
        // returns absolute path
-       return FileName(os::internal_path(tempBase));
+       return FileName(tempBase);
 }
 
 
@@ -488,20 +486,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;
@@ -511,7 +503,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;
@@ -524,24 +516,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() + '/';
-}
-
-
 // Search the string for ${VAR} and $VAR and replace VAR using getenv.
 string const replaceEnvironmentPath(string const & path)
 {
@@ -861,8 +835,7 @@ FileName const findtexfile(string const & fil, string const & /*format*/)
        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();
 }
@@ -876,7 +849,7 @@ void removeAutosaveFile(string const & filename)
        a += '#';
        FileName const autosave(a);
        if (autosave.exists())
-               unlink(autosave);
+               autosave.removeFile();
 }
 
 
@@ -886,7 +859,7 @@ void readBB_lyxerrMessage(FileName const & file, bool & zipped,
        LYXERR(Debug::GRAPHICS, "[readBB_from_PSFile] " << message);
        // FIXME: Why is this func deleting a file? (Lgb)
        if (zipped)
-               unlink(file);
+               file.removeFile();
 }
 
 
@@ -910,7 +883,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);
@@ -953,5 +926,6 @@ int compare_timestamps(FileName const & file1, FileName const & file2)
        return cmp;
 }
 
+
 } //namespace support
 } // namespace lyx