]> git.lyx.org Git - lyx.git/blobdiff - src/support/filetools.C
small changes read ChangeLog
[lyx.git] / src / support / filetools.C
index 996b064852b95eb8d0de3b4d18d7f95fa3c6c6ae..52e642203bf262f1fe9295f5ee27aabd6d891ece 100644 (file)
 #include <cctype>
 
 #include <utility>
-using std::make_pair;
-using std::pair;
+#include <fstream>
+
+#ifdef HAVE_SSTREAM
+#include <sstream>
+#else
+#include <strstream>
+#endif
 
 #ifdef __GNUG__
 #pragma implementation "filetools.h"
@@ -52,6 +57,11 @@ using std::pair;
 # endif
 #endif
 
+using std::make_pair;
+using std::pair;
+using std::endl;
+using std::ifstream;
+
 extern string system_lyxdir;
 extern string build_lyxdir;
 extern string user_lyxdir;
@@ -89,9 +99,7 @@ string MakeLatexName(string const & file)
 // Substitutes spaces with underscores in filename (and path)
 string QuoteName(string const & name)
 {
-#ifdef WITH_WARNINGS
-#warning Add proper emx support here!
-#endif
+       // CHECK Add proper emx support here!
 #ifndef __EMX__
        return '\'' + name + '\'';
 #else
@@ -303,10 +311,9 @@ string GetEnvPath(string const & name)
 
 bool PutEnv(string const & envstr)
 {
-#ifdef WITH_WARNINGS
-#warning Look at and fix this.
-#endif
+       // CHECK Look at and fix this.
         // f.ex. what about error checking?
+
 #if HAVE_PUTENV
         // this leaks, but what can we do about it?
         //   Is doing a getenv() and a free() of the older value 
@@ -334,6 +341,9 @@ bool PutEnv(string const & envstr)
         string varname;
         string str = envstr.split(varname,'=');
         int retval = setenv(varname.c_str(), str.c_str(), true);
+#else
+       // No environment setting function. Can this happen?
+       int retval = 1; //return an error condition.
 #endif
 #endif
         return retval == 0;
@@ -711,6 +721,34 @@ string CleanupPath(string const & path)
 #endif
 }
 
+string 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;
+       return string();
+}
+
 
 //
 // Search ${...} as Variable-Name inside the string and replace it with
@@ -750,7 +788,7 @@ string ReplaceEnvironmentPath(string const & path)
                if (!regexMatch(copy1, RegExp)) {
                        // No EndChar inside. So we are finished
                        result1 += CompareString + result0;
-                       result0.clear();
+                       result0.erase();
                        continue;
                }
 
@@ -894,8 +932,7 @@ string AddPath(string const & path, string const & path_2)
  Strips path off if no_path == true.
  If no extension on oldname, just appends.
  */
-string ChangeExtension(string const & oldname, string const & extension, 
-                       bool no_path) 
+string ChangeExtension(string const & oldname, string const & extension)
 {
        string::size_type last_slash = oldname.rfind('/');
        string::size_type last_dot = oldname.rfind('.');
@@ -908,14 +945,8 @@ string ChangeExtension(string const & oldname, string const & extension,
                ext= '.' + extension;
        else
                ext = extension;
-       string ret_str;
-       if (no_path && last_slash != string::npos) {
-               ++last_slash; // step it
-               ret_str = oldname.substr(last_slash,
-                                        last_dot - last_slash) + ext;
-       } else
-               ret_str = oldname.substr(0, last_dot) + ext;
-       return CleanupPath(ret_str);
+
+       return CleanupPath(oldname.substr(0, last_dot) + ext);
 }