#include "support/environment.h"
#include "support/gettext.h"
#include "support/lstrings.h"
-#include "support/lyxlib.h"
#include "support/os.h"
#include "support/Package.h"
#include "support/Path.h"
#include "support/Systemcall.h"
+#include "support/qstring_helpers.h"
+
+#include <QDir>
#include <boost/assert.hpp>
#include <boost/regex.hpp>
#include <fstream>
#include <sstream>
-using std::endl;
-using std::getline;
-using std::make_pair;
-using std::string;
-using std::ifstream;
-using std::ostringstream;
-using std::vector;
-using std::pair;
+using namespace std;
namespace lyx {
namespace support {
return fullname;
if (ext.empty())
// We are done.
- return mode == allow_unreadable ? fullname : FileName();
+ return mode == may_not_exist ? fullname : FileName();
// Only add the extension if it is not already the extension of
// fullname.
if (getExtension(fullname.absFilename()) != ext)
fullname = FileName(addExtension(fullname.absFilename(), ext));
- if (fullname.isReadableFile() || mode == allow_unreadable)
+ if (fullname.isReadableFile() || mode == may_not_exist)
return fullname;
return FileName();
}
LYXERR(Debug::FILES, "createTmpDir: tempdir=`" << tempdir << "'\n"
<< "createTmpDir: mask=`" << mask << '\'');
- FileName const tmpfl(tempName(tempdir, mask));
- // lyx::tempName actually creates a file to make sure that it
+ FileName const tmpfl = FileName::tempName(tempdir.absFilename()
+ + "/" + mask);
+ // FileName::tempName actually creates a file to make sure that it
// stays unique. So we have to delete it before we can create
// a dir with the same name. Note also that we are not thread
// safe because of the gap between unlink and mkdir. (Lgb)
tmpfl.removeFile();
- if (tmpfl.empty() || mkdir(tmpfl, 0700)) {
+ if (tmpfl.empty() || !tmpfl.createDirectory(0700)) {
lyxerr << "LyX could not create the temporary directory '"
<< tmpfl << "'" << endl;
return FileName();
return tmpfl;
}
-string const createBufferTmpDir()
-{
- static int count;
- // We are in our own directory. Why bother to mangle name?
- // In fact I wrote this code to circumvent a problematic behaviour
- // (bug?) of EMX mkstemp().
- string const tmpfl =
- package().temp_dir().absFilename() + "/lyx_tmpbuf" +
- convert<string>(count++);
-
- if (mkdir(FileName(tmpfl), 0777)) {
- lyxerr << "LyX could not create the temporary directory '"
- << tmpfl << "'" << endl;
- return string();
- }
- return tmpfl;
-}
-
FileName const createLyXTmpDir(FileName const & deflt)
{
if (deflt.empty() || deflt.absFilename() == "/tmp")
return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
- if (!mkdir(deflt, 0777))
+ if (deflt.createDirectory(0777))
return deflt;
if (deflt.isDirWritable()) {
// Convert relative path into absolute path based on a basepath.
// If relpath is absolute, just use that.
// If basepath is empty, use CWD as base.
+// Note that basePath can be a relative path, in the sense that it may
+// not begin with "/" (e.g.), but it should NOT contain such constructs
+// as "/../".
+// FIXME It might be nice if the code didn't simply assume that.
FileName const makeAbsPath(string const & relPath, string const & basePath)
{
+ FileName relative_path(relPath);
// checks for already absolute path
- if (os::is_absolute_path(relPath))
- return FileName(relPath);
+ if (relative_path.isAbsolute())
+ return relative_path;
// Copies given paths
string tempRel = os::internal_path(relPath);
string tempBase;
- if (os::is_absolute_path(basePath))
+ FileName base_path(basePath);
+ if (base_path.isAbsolute())
tempBase = basePath;
else
- tempBase = addPath(getcwd().absFilename(), basePath);
+ tempBase = addPath(FileName::getcwd().absFilename(), basePath);
// Handle /./ at the end of the path
while (suffixIs(tempBase, "/./"))
string rTemp = tempRel;
string temp;
+ // Check for a leading "~"
+ // Split by first /
+ rTemp = split(rTemp, temp, '/');
+ if (temp == "~") {
+ tempBase = package().home_dir().absFilename();
+ tempRel = rTemp;
+ }
+
+ rTemp = tempRel;
while (!rTemp.empty()) {
// Split by next /
rTemp = split(rTemp, temp, '/');
if (temp == ".") continue;
if (temp == "..") {
// Remove one level of TempBase
- string::difference_type i = tempBase.length() - 2;
- if (i < 0)
- i = 0;
+ if (tempBase.length() <= 1) {
+ //this is supposed to be an absolute path, so...
+ tempBase = "/";
+ continue;
+ }
+ //erase a trailing slash if there is one
+ if (suffixIs(tempBase, "/"))
+ tempBase.erase(tempBase.length() - 1, string::npos);
+
+ string::size_type i = tempBase.length() - 1;
while (i > 0 && tempBase[i] != '/')
--i;
if (i > 0)
tempBase.erase(i, string::npos);
else
- tempBase += '/';
+ tempBase = '/';
} else if (temp.empty() && !rTemp.empty()) {
tempBase = os::current_root() + rTemp;
rTemp.erase();
}
-/// Returns true is path is absolute
-bool absolutePath(string const & path)
-{
- return os::is_absolute_path(path);
-}
-
-
// Create absolute path. If impossible, don't do anything
// Supports ./ and ~/. Later we can add support for ~logname/. (Asger)
string const expandPath(string const & path)
{
// checks for already absolute path
string rTemp = replaceEnvironmentPath(path);
- if (os::is_absolute_path(rTemp))
+ FileName abs_path(rTemp);
+ if (abs_path.isAbsolute())
return rTemp;
string temp;
rTemp = split(rTemp, temp, '/');
if (temp == ".")
- return getcwd().absFilename() + '/' + rTemp;
+ return FileName::getcwd().absFilename() + '/' + rTemp;
if (temp == "~")
return package().home_dir().absFilename() + '/' + rTemp;
static boost::regex bbox_re(
"^%%BoundingBox:\\s*([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)");
- std::ifstream is(file_.toFilesystemEncoding().c_str());
+ ifstream is(file_.toFilesystemEncoding().c_str());
while (is) {
string s;
getline(is,s);
}
-std::vector<FileName> dirList(FileName const & filename, std::string const & ext)
-{
- std::vector<FileName> dirlist;
- if (!filename.isDirectory()) {
- LYXERR0("Directory '" << filename << "' does not exist!");
- return dirlist;
- }
-
- QDir dir(toqstr(filename.absoluteFilePath()));
-
- if (!ext.empty()) {
- QString filter;
- switch (ext[0]) {
- case '.': filter = "*" + toqstr(ext); break;
- case '*': filter = toqstr(ext); break;
- default: filter = "*." + toqstr(ext);
- }
- dir.setNameFilters(QStringList(filter));
- LYXERR(Debug::FILES, "filtering on extension "
- << fromqstr(filter) << " is requested.");
- }
-
- QFileInfoList list = dir.entryInfoList();
- for (int i = 0; i != list.size(); ++i) {
- FileName fi(fromqstr(list.at(i).absoluteFilePath()));
- dirlist.push_back(fi);
- LYXERR(Debug::FILES, "found file " << fi);
- }
-
- return dirlist;
-}
-
-
} //namespace support
} // namespace lyx