]> git.lyx.org Git - lyx.git/blobdiff - src/support/filetools.C
Create a grfx::Loader class and so move large chunks of code out of
[lyx.git] / src / support / filetools.C
index 09e5c2bf0bc2ede3cc20a20a7c3d08f07eaf7a0d..9e17a3faab9af1fbc55f963c24ffcd66efc686e6 100644 (file)
 
 #include <config.h>
 
-#include <cctype>
-
-#include <utility>
-#include <fstream>
-
-#include "Lsstream.h"
-
 #ifdef __GNUG__
 #pragma implementation "filetools.h"
 #endif
 
-#include <cstdlib>
-#include <cstdio>
-#include <fcntl.h>
-#include <cerrno>
 #include "debug.h"
 #include "support/lstrings.h"
 #include "support/systemcall.h"
 
 #include "filetools.h"
-#include "LSubstring.h"
+#include "lstrings.h"
 #include "frontends/Alert.h"
 #include "FileInfo.h"
 #include "support/path.h"        // I know it's OS/2 specific (SMiyata)
 #include "lyxlib.h"
 #include "os.h"
 
+#include "Lsstream.h"
+
+#include <cctype>
+#include <cstdlib>
+#include <cstdio>
+#include <fcntl.h>
+#include <cerrno>
+
+#include <utility>
+#include <fstream>
+
+
 // Which part of this is still necessary? (JMarc).
 #if HAVE_DIRENT_H
 # include <dirent.h>
 # endif
 #endif
 
+#ifndef CXX_GLOBAL_CSTD
+using std::fgetc;
+using std::isalpha;
+using std::isalnum;
+#endif
+
 using std::make_pair;
 using std::pair;
 using std::endl;
 using std::ifstream;
 using std::vector;
+using std::getline;
 
 extern string system_lyxdir;
 extern string build_lyxdir;
@@ -550,9 +558,10 @@ string const CreateLyXTmpDir(string const & deflt)
 }
 
 
+// FIXME: no need for separate method like this ... 
 int DestroyLyXTmpDir(string const & tmpdir)
 {
-       return DestroyTmpDir (tmpdir, false); // Why false?
+       return DestroyTmpDir(tmpdir, true); 
 }
 
 
@@ -984,6 +993,8 @@ string const GetExtension(string const & name)
 // AGR Grace...
 // BMP BM...
 // EPS %!PS-Adobe-3.0 EPSF...
+// EPSI like EPS and with
+//      %%BeginPreview...
 // FITS ...BITPIX...
 // GIF GIF...
 // JPG JFIF
@@ -1014,6 +1025,7 @@ string const getExtFromContents(string const & filename)
        if (filename.empty() || !IsFileReadable(filename))
                return string();
 
+       
        ifstream ifs(filename.c_str());
        if (!ifs)
                // Couldn't open file...
@@ -1043,8 +1055,10 @@ string const getExtFromContents(string const & filename)
                        break;
                }
 
-               std::getline(ifs, str);
-               lyxerr[Debug::GRAPHICS] << "Scanstring: " << str << endl;
+               getline(ifs, str);
+
+               lyxerr[Debug::GRAPHICS] << "Scanstring: " << str.substr(0,60)
+                                       << endl;
 
                string const stamp = str.substr(0,2);
                if (firstLine && str.size() >= 2) {
@@ -1129,7 +1143,7 @@ string const getExtFromContents(string const & filename)
                else if (contains(str,"%!PS-Adobe")) {
                        // eps or ps
                        ifs >> str;
-                       if (contains(str,"EPSF"))
+                       if (contains(str,"EPSF")) 
                                format = "eps";
                        else
                            format = "ps";
@@ -1146,9 +1160,25 @@ string const getExtFromContents(string const & filename)
        }
 
        if (!format.empty()) {
-           lyxerr[Debug::GRAPHICS]
-               << "Recognised Fileformat: " << format << endl;
-           return format;
+               // if we have eps than epsi is also possible
+               // we have to check for a preview
+               if (format == "eps") {
+                       lyxerr[Debug::GRAPHICS]
+                               << "\teps detected -> test for an epsi ..."
+                               << endl;
+                       while (count++ < max_count) {
+                               if (ifs.eof())
+                                       break;
+                               getline(ifs, str);
+                               if (contains(str, "BeginPreview")) {
+                                       format = "epsi";
+                                       count = max_count;
+                               }
+                       }
+               }
+               lyxerr[Debug::GRAPHICS]
+                       << "Recognised Fileformat: " << format << endl;
+               return format;
        }
 
        string const ext(GetExtension(filename));
@@ -1353,6 +1383,16 @@ void removeAutosaveFile(string const & filename)
 }
 
 
+void readBB_lyxerrMessage(string const & file, bool & zipped, 
+       string const & message) 
+{
+       lyxerr[Debug::GRAPHICS] << "[readBB_from_PSFile] " 
+               << message << std::endl;
+       if (zipped)
+               lyx::unlink(file);
+}
+
+
 string const readBB_from_PSFile(string const & file)
 {
        // in a (e)ps-file it's an entry like %%BoundingBox:23 45 321 345
@@ -1362,18 +1402,26 @@ string const readBB_from_PSFile(string const & file)
        // end of the file. Than we have in the header:
        // %%BoundingBox: (atend)
        // In this case we must check the end.
-       string const file_ = zippedFile(file) ?
+       bool zipped = zippedFile(file);
+       string const file_ = zipped ?
                string(unzipFile(file)) : string(file);
        string const format = getExtFromContents(file_);
-       if (format != "eps" && format != "ps")
+
+       if (format != "eps" && format != "ps") {
+               readBB_lyxerrMessage(file_, zipped,"no(e)ps-format"); 
                return string();
+       }
 
        std::ifstream is(file_.c_str());
        while (is) {
                string s;
-               std::getline(is,s);
-               if (contains(s,"%%BoundingBox:") && !contains(s,"atend"))
-                       return (frontStrip(s.substr(14)));
+               getline(is,s);
+               if (contains(s,"%%BoundingBox:") && !contains(s,"atend")) {
+                       string const bb = frontStrip(s.substr(14));
+                       readBB_lyxerrMessage(file_, zipped, bb);
+                       return bb;
+               }
        }
+       readBB_lyxerrMessage(file_, zipped, "no bb found");
        return string();
 }