shared: use more portable path_open() method
This commit is contained in:
parent
c6dac9347f
commit
2c44b805cf
@ -234,7 +234,8 @@ ConfSimple::ConfSimple(const char *fname, int readonly, bool tildexp,
|
|||||||
if (!readonly && !path_exists(fname)) {
|
if (!readonly && !path_exists(fname)) {
|
||||||
mode |= ios::trunc;
|
mode |= ios::trunc;
|
||||||
}
|
}
|
||||||
fstream input = path_open(fname, mode);
|
fstream input;
|
||||||
|
path_open(fname, mode, input);
|
||||||
if (!input.is_open()) {
|
if (!input.is_open()) {
|
||||||
LOGDEB0("ConfSimple::ConfSimple: fstream(w)(" << fname << ", " << mode <<
|
LOGDEB0("ConfSimple::ConfSimple: fstream(w)(" << fname << ", " << mode <<
|
||||||
") errno " << errno << "\n");
|
") errno " << errno << "\n");
|
||||||
@ -245,7 +246,7 @@ ConfSimple::ConfSimple(const char *fname, int readonly, bool tildexp,
|
|||||||
input.clear();
|
input.clear();
|
||||||
status = STATUS_RO;
|
status = STATUS_RO;
|
||||||
// open readonly
|
// open readonly
|
||||||
input = path_open(fname, ios::in);
|
path_open(fname, ios::in, input);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!input.is_open()) {
|
if (!input.is_open()) {
|
||||||
@ -577,7 +578,8 @@ bool ConfSimple::write()
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (m_filename.length()) {
|
if (m_filename.length()) {
|
||||||
fstream output = path_open(m_filename, ios::out | ios::trunc);
|
fstream output;
|
||||||
|
path_open(m_filename, ios::out | ios::trunc, output);
|
||||||
if (!output.is_open()) {
|
if (!output.is_open()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -836,6 +836,9 @@ bool path_makepath(const string& ipath, int mode)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined(__GNUC__) || __GNUC__ > 4
|
||||||
|
// Not sure what g++ version supports fstream assignment but 4.9
|
||||||
|
// (jessie) certainly does not
|
||||||
std::fstream path_open(const std::string& path, int mode)
|
std::fstream path_open(const std::string& path, int mode)
|
||||||
{
|
{
|
||||||
#if defined(_WIN32) && defined (_MSC_VER)
|
#if defined(_WIN32) && defined (_MSC_VER)
|
||||||
@ -853,6 +856,27 @@ std::fstream path_open(const std::string& path, int mode)
|
|||||||
return std::fstream(path, std::ios_base::openmode(mode));
|
return std::fstream(path, std::ios_base::openmode(mode));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
bool path_open(const std::string& path, int mode, std::fstream& outstream)
|
||||||
|
{
|
||||||
|
#if defined(_WIN32) && defined (_MSC_VER)
|
||||||
|
// MSC STL has support for using wide chars in fstream
|
||||||
|
// constructor. We need this if, e.g. the user name/home directory
|
||||||
|
// is not ASCII. Actually don't know how to do this with gcc
|
||||||
|
wchar_t wpath[MAX_PATH + 1];
|
||||||
|
utf8towchar(path, wpath, MAX_PATH);
|
||||||
|
outstream.open(wpath, std::ios_base::openmode(mode));
|
||||||
|
if (!outstream.is_open()) {
|
||||||
|
LOGERR("path_open("<< path << ", "<< mode <<") errno " << errno <<"\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
#else
|
||||||
|
outstream.open(path, std::ios_base::openmode(mode));
|
||||||
|
return outstream.is_open();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
bool path_isdir(const string& path, bool follow)
|
bool path_isdir(const string& path, bool follow)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -147,6 +147,7 @@ extern bool path_makepath(const std::string& path, int mode);
|
|||||||
* @param path an utf-8 file path.
|
* @param path an utf-8 file path.
|
||||||
* @param mode is an std::fstream mode (ios::in etc.) */
|
* @param mode is an std::fstream mode (ios::in etc.) */
|
||||||
extern std::fstream path_open(const std::string& path, int mode);
|
extern std::fstream path_open(const std::string& path, int mode);
|
||||||
|
extern bool path_open(const std::string& path, int mode, std::fstream& outstream);
|
||||||
|
|
||||||
/// Where we create the user data subdirs
|
/// Where we create the user data subdirs
|
||||||
extern std::string path_homedata();
|
extern std::string path_homedata();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user