]> git.lyx.org Git - lyx.git/blobdiff - src/support/filetools.C
small changes read ChangeLog
[lyx.git] / src / support / filetools.C
index 6523d87cda367ee435e6f711a728c081fcba026d..52e642203bf262f1fe9295f5ee27aabd6d891ece 100644 (file)
 #include <cctype>
 
 #include <utility>
+#include <fstream>
+
+#ifdef HAVE_SSTREAM
+#include <sstream>
+#else
+#include <strstream>
+#endif
 
 #ifdef __GNUG__
 #pragma implementation "filetools.h"
@@ -53,6 +60,7 @@
 using std::make_pair;
 using std::pair;
 using std::endl;
+using std::ifstream;
 
 extern string system_lyxdir;
 extern string build_lyxdir;
@@ -713,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