Clean up read_file() by adding more versions of the function...

...depending on how they treat relative paths. I found the current
behaviour unintuitive and bug-friendly for some purposes (namely the
editor). This should fix editor2's save/load inconsistencies with the
--load option that fendrin spotted.
This commit is contained in:
Tomasz Śniatowski 2008-10-18 23:47:54 +01:00
parent 53399e927d
commit 24299b8109
3 changed files with 27 additions and 9 deletions

View file

@ -60,7 +60,7 @@ editor_map::editor_map(const gamemap& map)
editor_map editor_map::load_from_file(const config& game_config, const std::string& filename)
{
std::string map_string = read_file(filename);
std::string map_string = read_file(filename, false);
if (map_string.empty()) {
std::string message = _("Empty map file or file not found");
throw editor_map_load_exception(filename, message);

View file

@ -607,7 +607,13 @@ static std::string read_stream(std::istream& s)
return ss.str();
}
std::istream *istream_file(std::string fname)
std::istream *istream_file(const std::string& fname, bool relative_from_game_path /*=true*/)
{
std::string fname2(fname);
return istream_file(fname2, relative_from_game_path);
}
std::istream *istream_file(std::string& fname, bool relative_from_game_path /*=true*/)
{
LOG_FS << "streaming " << fname << " for reading.\n";
if (fname.empty())
@ -616,10 +622,10 @@ std::istream *istream_file(std::string fname)
return new std::ifstream();
}
#ifndef _WIN32
if (fname[0] != '/') {
if (relative_from_game_path && fname[0] != '/') {
#else
// Check if not start with driver letter
if (!std::isalpha(fname[0])) {
if (relative_from_game_path && !std::isalpha(fname[0])) {
#endif
if (!game_config::path.empty())
fname = game_config::path + "/" + fname;
@ -634,9 +640,15 @@ std::istream *istream_file(std::string fname)
}
std::string read_file(std::string const &fname)
std::string read_file(const std::string &fname, bool relative_from_game_path /*=true*/)
{
scoped_istream s = istream_file(fname);
scoped_istream s = istream_file(fname, relative_from_game_path);
return read_stream(*s);
}
std::string read_file(std::string &fname, bool relative_from_game_path /*=true*/)
{
scoped_istream s = istream_file(fname, relative_from_game_path);
return read_stream(*s);
}

View file

@ -95,9 +95,15 @@ bool delete_directory(const std::string& dirname);
// Basic disk I/O:
/** Basic disk I/O - read file. */
std::string read_file(const std::string& fname);
std::istream *istream_file(std::string fname);
/** Basic disk I/O - read file.
* The bool relative_from_game_path determines whether relative paths should be treated as relative
* to the game path (true) or to the current directory from which Wesnoth was run (false).
* The non-const version will update the string if the game path is prepended.
*/
std::string read_file(const std::string &fname, bool relative_from_game_path = true);
std::string read_file(std::string &fname, bool relative_from_game_path = true);
std::istream *istream_file(const std::string& fname, bool relative_from_game_path = true);
std::istream *istream_file(std::string& fname, bool relative_from_game_path = true);
std::ostream *ostream_file(std::string const &fname);
/** Throws io_exception if an error occurs. */
void write_file(const std::string& fname, const std::string& data);