]> 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 b95b23c767bfc5ba23feb4dcea4272e252e070c4..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...
@@ -1044,33 +1056,36 @@ string const getExtFromContents(string const & filename)
                }
 
                getline(ifs, str);
-               lyxerr[Debug::GRAPHICS] << "Scanstring: " << str << endl;
-               
+
+               lyxerr[Debug::GRAPHICS] << "Scanstring: " << str.substr(0,60)
+                                       << endl;
+
                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))
+                       if (prefixIs(str, gzipStamp)) {
                                format =  "gzip";
 
-                       else if (stamp == zipStamp)
+                       } else if (stamp == zipStamp) {
                                format =  "zip";
 
-                       else if (stamp == compressStamp)
+                       } else if (stamp == compressStamp) {
                                format =  "compress";
 
                        // the graphics part
-                       else if (stamp == "BM")
+                       } else if (stamp == "BM") {
                                format =  "bmp";
 
-                       else if (stamp == "\001\332")
+                       } 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') {
+                       else if (str[0] == 'P') {
                                switch (str[1]) {
                                case '1':
                                case '4':
@@ -1085,26 +1100,32 @@ string const getExtFromContents(string const & filename)
                                        format =  "ppm";
                                }
                                break;
+
+                       } else if ((stamp == "II") || (stamp == "MM")) {
+                               format =  "tiff";
+
+                       } else if (prefixIs(str,"%TGIF")) {
+                               format =  "tgif";
+
+                       } 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";
+                               }
                        }
-                       if (stamp == "\001\332")
-                           format =  "sgi";
-                       else if ((stamp == "II") || (stamp == "MM"))
-                           format =  "tiff";
-                       else if (str == "%TGIF")
-                           format =  "tgif";
-                       else if (prefixIs(str,"GIF"))
-                           format =  "gif";
-                       else if (str.size() > 3)        // get long
-                           if (((str[0] << 24) + (str[1] << 16) +
-                               (str[2] << 8) + str[3]) == 105)
-                               format =  "xwd";
+
                        firstLine = false;
                }
+
                if (!format.empty())
                    break;
                else if (contains(str,"EPSF"))
                        // dummy, if we have wrong file description like
-                       // description like "%!PS-Adobe-2.0EPSF"
+                       // %!PS-Adobe-2.0EPSF"
                        format =  "eps";
 
                else if (contains(str,"Grace"))
@@ -1122,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";
@@ -1139,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));
@@ -1344,3 +1381,47 @@ 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
+       // It seems that every command in the header has an own line,
+       // getline() should work for all files.
+       // On the other hand some plot programs write the bb at the
+       // end of the file. Than we have in the header:
+       // %%BoundingBox: (atend)
+       // In this case we must check the end.
+       bool zipped = zippedFile(file);
+       string const file_ = zipped ?
+               string(unzipFile(file)) : string(file);
+       string const format = getExtFromContents(file_);
+
+       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;
+               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();
+}