[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Wesnoth-cvs-commits] wesnoth/src filesystem.cpp
From: |
Guillaume Melquiond |
Subject: |
[Wesnoth-cvs-commits] wesnoth/src filesystem.cpp |
Date: |
Thu, 21 Apr 2005 16:42:41 -0400 |
CVSROOT: /cvsroot/wesnoth
Module name: wesnoth
Branch:
Changes by: Guillaume Melquiond <address@hidden> 05/04/21 20:42:41
Modified files:
src : filesystem.cpp
Log message:
Now that we have streams, let's clean file reading. Should be a lot
more readable.
CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/filesystem.cpp.diff?tr1=1.69&tr2=1.70&r1=text&r2=text
Patches:
Index: wesnoth/src/filesystem.cpp
diff -u wesnoth/src/filesystem.cpp:1.69 wesnoth/src/filesystem.cpp:1.70
--- wesnoth/src/filesystem.cpp:1.69 Thu Apr 21 20:16:32 2005
+++ wesnoth/src/filesystem.cpp Thu Apr 21 20:42:41 2005
@@ -1,4 +1,4 @@
-/* $Id: filesystem.cpp,v 1.69 2005/04/21 20:16:32 silene Exp $ */
+/* $Id: filesystem.cpp,v 1.70 2005/04/21 20:42:41 silene Exp $ */
/*
Copyright (C) 2003 by David White <address@hidden>
Part of the Battle for Wesnoth Project http://wesnoth.whitevine.net
@@ -409,44 +409,6 @@
#endif
}
-namespace {
-void read_file_internal(const std::string& fname, std::string& res)
-{
- const int size = file_size(fname);
- if(size < 0) {
- return;
- }
-
- std::vector<char> v;
- v.reserve(size);
-
- //const util::scoped_resource<FILE*,close_FILE>
file(fopen(fname.c_str(),"rb"));
- const util::scoped_FILE file(fopen(fname.c_str(),"rb"));
-
- if(file == NULL) {
- return;
- }
-
- const int block_size = 65536;
-
- while(v.size() < size) {
- const size_t expected = minimum<size_t>(block_size,size -
v.size());
-
- if(expected > 0) {
- v.resize(v.size() + expected);
- const size_t nbytes = fread(&v[v.size() -
expected],1,expected,file);
- if(nbytes < expected) {
- v.resize(v.size() - (expected - nbytes));
- break;
- }
- }
- }
-
- res.resize(v.size());
- std::copy(v.begin(),v.end(),res.begin());
-}
-} //end anon namespace
-
std::string read_stream(std::istream& s)
{
std::stringstream ss;
@@ -459,42 +421,6 @@
return read_stream(std::cin);
}
-std::string read_file(const std::string& fname)
-{
- //if we have a path to the data,
- //convert any filepath which is relative
- LOG_G << "Reading " << fname << "\n";
-#ifdef USE_ZIPIOS
- if(!fname.empty() && fname[0] != '/') {
- zipios::ConstEntryPointer p = the_collection->getEntry(fname);
- if (p != 0) {
- std::istream* s = the_collection->getInputStream(p);
- if (s != NULL) {
- std::string contents = read_stream(*s);
- delete s;
- return contents;
- }
- }
- }
-#else
- if(!fname.empty() && fname[0] != '/' && !game_config::path.empty()) {
- std::string res;
- read_file_internal(game_config::path + "/" + fname,res);
- if(!res.empty()) {
- return res;
- }
- }
-#endif
-
- // FIXME: why do we rely on this even with relative paths ?
- {
- // still useful with zipios, for things like cache and prefs
- std::string res;
- read_file_internal(fname,res);
- return res;
- }
-}
-
std::istream *istream_file(std::string const &fname)
{
LOG_G << "Streaming " << fname << " for reading.\n";
@@ -519,6 +445,12 @@
return new std::ifstream(fname.c_str(),std::ios_base::binary);
}
+std::string read_file(std::string const &fname)
+{
+ scoped_istream s = istream_file(fname);
+ return read_stream(*s);
+}
+
std::ostream *ostream_file(std::string const &fname)
{
LOG_G << "Streaming " << fname << " for writing.\n";