]> git.lyx.org Git - features.git/blobdiff - src/support/filetools.cpp
more use of support::FileName.
[features.git] / src / support / filetools.cpp
index 2d1ad7781897b0d4201239eb4777f3ceec17e21e..7901119a6f7bebe19c54c98e2bd92d1f4b30610e 100644 (file)
@@ -24,7 +24,6 @@
 #include "support/convert.h"
 #include "support/environment.h"
 #include "support/filetools.h"
-#include "support/fs_extras.h"
 #include "support/lstrings.h"
 #include "support/lyxlib.h"
 #include "support/os.h"
@@ -79,10 +78,7 @@ bool isSGMLFilename(string const & filename)
 bool isValidLaTeXFilename(string const & filename)
 {
        string const invalid_chars("#$%{}()[]\"^");
-       if (filename.find_first_of(invalid_chars) != string::npos)
-               return false;
-       else
-               return true;
+       return filename.find_first_of(invalid_chars) == string::npos;
 }
 
 
@@ -157,29 +153,6 @@ string const quoteName(string const & name, quote_style style)
 }
 
 
-bool isFileReadable(FileName const & filename)
-{
-       std::string const path = filename.toFilesystemEncoding();
-       return fs::exists(path) && !fs::is_directory(path) && fs::is_readable(path);
-}
-
-
-//returns true: dir writeable
-//       false: not writeable
-bool isDirWriteable(FileName const & path)
-{
-       LYXERR(Debug::FILES) << "isDirWriteable: " << path << endl;
-
-       FileName const tmpfl(tempName(path, "lyxwritetest"));
-
-       if (tmpfl.empty())
-               return false;
-
-       unlink(tmpfl);
-       return true;
-}
-
-
 #if 0
 // Uses a string of paths separated by ";"s to find a file to open.
 // Can't cope with pathnames with a ';' in them. Returns full path to file.
@@ -259,7 +232,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 (isFileReadable(fullname))
+       if (fullname.isReadable())
                return fullname;
        if (ext.empty())
                // We are done.
@@ -268,7 +241,7 @@ FileName const fileSearch(string const & path, string const & name,
        // fullname.
        if (getExtension(fullname.absFilename()) != ext)
                fullname = FileName(addExtension(fullname.absFilename(), ext));
-       if (isFileReadable(fullname) || mode == allow_unreadable)
+       if (fullname.isReadable() || mode == allow_unreadable)
                return fullname;
        return FileName();
 }
@@ -379,9 +352,7 @@ string const libScriptSearch(string const & command_in, quote_style style)
 }
 
 
-namespace {
-
-FileName const createTmpDir(FileName const & tempdir, string const & mask)
+static FileName createTmpDir(FileName const & tempdir, string const & mask)
 {
        LYXERR(Debug::FILES)
                << "createTmpDir: tempdir=`" << tempdir << "'\n"
@@ -403,20 +374,6 @@ FileName const createTmpDir(FileName const & tempdir, string const & mask)
        return tmpfl;
 }
 
-} // namespace anon
-
-
-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()
 {
        static int count;
@@ -438,29 +395,21 @@ string const createBufferTmpDir()
 
 FileName const createLyXTmpDir(FileName const & deflt)
 {
-       if (!deflt.empty() && deflt.absFilename() != "/tmp") {
-               if (mkdir(deflt, 0777)) {
-                       if (isDirWriteable(deflt)) {
-                               // 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;
-       } else {
+       if (deflt.empty() || deflt.absFilename() == "/tmp")
                return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
-       }
-}
 
+       if (!mkdir(deflt, 0777)) 
+               return deflt;
 
-bool createDirectory(FileName const & path, int permission)
-{
-       BOOST_ASSERT(!path.empty());
-       return mkdir(path, permission) == 0;
+       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");
+       }
 }
 
 
@@ -472,7 +421,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);
 }
 
@@ -625,23 +574,6 @@ string const normalizePath(string const & path)
 }
 
 
-string const getFileContents(FileName const & fname)
-{
-       string const encodedname = fname.toFilesystemEncoding();
-       if (fs::exists(encodedname)) {
-               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)
 {
@@ -784,195 +716,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() || !isFileReadable(filename))
-               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);
@@ -1120,7 +863,7 @@ FileName const findtexfile(string const & fil, string const & /*format*/)
        // If the file can be found directly, we just return a
        // absolute path version of it.
        FileName const absfile(makeAbsPath(fil));
-       if (fs::exists(absfile.toFilesystemEncoding()))
+       if (absfile.exists())
                return absfile;
 
        // No we try to find it using kpsewhich.
@@ -1165,7 +908,7 @@ void removeAutosaveFile(string const & filename)
        a += onlyFilename(filename);
        a += '#';
        FileName const autosave(a);
-       if (fs::exists(autosave.toFilesystemEncoding()))
+       if (autosave.exists())
                unlink(autosave);
 }
 
@@ -1190,9 +933,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");