]> git.lyx.org Git - lyx.git/blobdiff - src/support/filetools.C
remove commented HAVE_SSTREAM code
[lyx.git] / src / support / filetools.C
index 7d65bd4ad903e4bc491f8fe5d27073828653d1a5..287a7e5860feac6f67e59f036beadfb6197866d3 100644 (file)
 #include <utility>
 #include <fstream>
 
-#ifdef HAVE_SSTREAM
-#include <sstream>
-#else
-#include <strstream>
-#endif
+#include "Lsstream.h"
 
 #ifdef __GNUG__
 #pragma implementation "filetools.h"
@@ -387,6 +383,7 @@ int DeleteAllFilesInDir (string const & path)
                return -1;
        }
        struct dirent * de;
+       int return_value = 0;
        while ((de = readdir(dir))) {
                string temp = de->d_name;
                if (temp == "." || temp == "..") 
@@ -395,12 +392,18 @@ int DeleteAllFilesInDir (string const & path)
 
                lyxerr.debug() << "Deleting file: " << unlinkpath << endl;
 
-               if (remove(unlinkpath.c_str()))
+               bool deleted = true;
+               if (FileInfo(unlinkpath).isDir())
+                       deleted = (DeleteAllFilesInDir(unlinkpath) == 0);
+               deleted &= (remove(unlinkpath.c_str()) == 0);
+               if (!deleted) {
                        WriteFSAlert (_("Error! Could not remove file:"), 
                                      unlinkpath);
+                       return_value = -1;
+               }
         }
        closedir(dir);
-       return 0;
+       return return_value;
 }
 
 
@@ -732,24 +735,11 @@ string const GetFileContents(string const & fname)
        FileInfo finfo(fname);
        if (finfo.exist()) {
                ifstream ifs(fname.c_str());
-#ifdef HAVE_SSTREAM
                std::ostringstream ofs;
-#else
-#warning The rumour goes that this might leak, but who really cares?
-               ostrstream ofs;
-#endif
                if (ifs && ofs) {
                        ofs << ifs.rdbuf();
                        ifs.close();
-#ifdef HAVE_SSTREAM
                        return ofs.str().c_str();
-#else
-                       ofs << '\0';
-                       char const * tmp = ofs.str();
-                       string ret(tmp);
-                       delete[] tmp;
-                       return ret;
-#endif
                }
        }
        lyxerr << "LyX was not able to read file '" << fname << "'" << endl;
@@ -958,6 +948,20 @@ ChangeExtension(string const & oldname, string const & extension)
 }
 
 
+/// Return the extension of the file (not including the .)
+string const GetExtension(string const & name)
+{
+       string::size_type last_slash = name.rfind('/');
+       string::size_type last_dot = name.rfind('.');
+       if (last_dot != string::npos &&
+           (last_slash == string::npos || last_dot > last_slash))
+               return name.substr(last_dot + 1,
+                                  name.length() - (last_dot + 1));
+       else
+               return string();
+}
+
+
 // Creates a nice compact path for displaying
 string const
 MakeDisplayPath (string const & path, unsigned int threshold)
@@ -1089,3 +1093,18 @@ findtexfile(string const & fil, string const & /*format*/)
                             << "'" << endl;
         return c.first != -1 ? strip(c.second, '\n') : string();
 }
+
+
+void removeAutosaveFile(string const & filename)
+{
+       string a = OnlyPath(filename);
+       a += '#';
+       a += OnlyFilename(filename);
+       a += '#';
+       FileInfo fileinfo(a);
+       if (fileinfo.exist()) {
+               if (::remove(a.c_str()) != 0) {
+                       WriteFSAlert(_("Could not delete auto-save file!"), a);
+               }
+       }
+}