mail[Wesnoth-commits] r33310 - in /trunk/src: filesystem.cpp filesystem.hpp serialization/preprocessor.cpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by guillaume . melquiond on March 03, 2009 - 18:44:
Author: silene
Date: Tue Mar  3 18:44:11 2009
New Revision: 33310

URL: http://svn.gna.org/viewcvs/wesnoth?rev=33310&view=rev
Log:
Moved WML file/dir locating to filesystem.cpp.

Modified:
    trunk/src/filesystem.cpp
    trunk/src/filesystem.hpp
    trunk/src/serialization/preprocessor.cpp

Modified: trunk/src/filesystem.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/filesystem.cpp?rev=33310&r1=33309&r2=33310&view=diff
==============================================================================
--- trunk/src/filesystem.cpp (original)
+++ trunk/src/filesystem.cpp Tue Mar  3 18:44:11 2009
@@ -55,6 +55,7 @@
 
 #include "config.hpp"
 #include "filesystem.hpp"
+#include "foreach.hpp"
 #include "game_config.hpp"
 #include "log.hpp"
 #include "loadscreen.hpp"
@@ -996,21 +997,72 @@
 
        if (filename.empty()) {
                DBG_FS << "  invalid filename ( type: " << type <<")\n";
-               return "";
-       }
-
-       const std::vector<std::string>& paths = get_binary_paths(type);
-       for(std::vector<std::string>::const_iterator i = paths.begin(); i != 
paths.end(); ++i) {
-               const std::string file = *i + filename;
-               DBG_FS << "  Checking " << *i << "\n";
+               return std::string();
+       }
+
+       foreach (const std::string &path, get_binary_paths(type))
+       {
+               const std::string file = path + filename;
+               DBG_FS << "  Checking " << path << "\n";
                if(file_exists(file)) {
-                 DBG_FS << "  Found at " << file << "\n";
+                       DBG_FS << "  Found at " << file << "\n";
                        return file;
                }
        }
 
-       DBG_FS << "  " << filename << " not found.\n";
-       return "";
+       DBG_FS << "  not found.\n";
+       return std::string();
+}
+
+std::string get_wml_location(const std::string &filename, const std::string 
&current_dir)
+{
+       LOG_FS << "Looking for " << filename << "\n";
+
+       std::string result;
+
+       if (filename.empty()) {
+               DBG_FS << "  invalid filename\n";
+               return result;
+       }
+
+       if (filename.find("..") != std::string::npos) {
+               ERR_FS << "Illegal path '" << filename << "' found (\"..\" 
not allowed).\n";
+               return result;
+       }
+
+       bool already_found = false;
+
+       if (filename[0] == '~' || filename[0] == '@')
+       {
+               // If the filename starts with '~' or '@', look in the user 
data directory.
+               result = get_user_data_dir() + "/data/" + filename.substr(1);
+               LOG_FS << "got relative name '" << filename << "' -> '" << 
result << "'\n";
+
+               already_found = file_exists(result) || is_directory(result);
+
+               // If it starts with '@', look also in the standard data 
directory.
+               if (filename[0] == '@' && !already_found && 
!game_config::path.empty())
+                       result = game_config::path + "/data/" + 
filename.substr(1);
+       }
+       else if (filename.size() >= 2 && filename[0] == '.' && filename[1] == 
'/' )
+       {
+               // If the filename begins with a "./", look in the same 
directory
+               // as the file currrently being preprocessed.
+               result = current_dir + filename.substr(2);
+       }
+       else if (!game_config::path.empty())
+               result = game_config::path + "/data/" + filename;
+
+       if (result.empty() ||
+           (!already_found && !file_exists(result) && !is_directory(result)))
+       {
+               LOG_FS << "  not found.\n";
+               result.clear();
+       }
+       else
+               LOG_FS << "  found: '" << result << "'\n";
+
+       return result;
 }
 
 scoped_istream& scoped_istream::operator=(std::istream *s)
@@ -1022,9 +1074,7 @@
 
 scoped_istream::~scoped_istream()
 {
-       DBG_FS <<"deleting stream";
        delete stream;
-       DBG_FS << " ok\n";
 }
 
 scoped_ostream& scoped_ostream::operator=(std::ostream *s)

Modified: trunk/src/filesystem.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/filesystem.hpp?rev=33310&r1=33309&r2=33310&view=diff
==============================================================================
--- trunk/src/filesystem.hpp (original)
+++ trunk/src/filesystem.hpp Tue Mar  3 18:44:11 2009
@@ -211,6 +211,12 @@
  */
 std::string get_binary_file_location(const std::string& type, const 
std::string& filename);
 
+/**
+ * Returns a complete path to the actual WML file or directory
+ * or an empty string if the file isn't present.
+ */
+std::string get_wml_location(const std::string &filename, const std::string 
&current_dir);
+
 class scoped_istream {
        std::istream *stream;
 public:

Modified: trunk/src/serialization/preprocessor.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/serialization/preprocessor.cpp?rev=33310&r1=33309&r2=33310&view=diff
==============================================================================
--- trunk/src/serialization/preprocessor.cpp (original)
+++ trunk/src/serialization/preprocessor.cpp Tue Mar  3 18:44:11 2009
@@ -947,42 +947,8 @@
                                LOG_CF << "Macro definition not found for " 
<< symbol << " , attempting to open as file.\n";
                                pop_token();
                                std::string prefix;
-                               std::string nfname;
-                               std::string const &newfilename = symbol;
-                               // If the filename begins with a '~',
-                               //  then look in the user's data directory.
-                               // If the filename begins with a '@',
-                               //  then we look in the user's data directory,
-                               // but default to the standard data directory 
if it's not found there.
-                               if(newfilename != "" && (newfilename[0] == 
'~' || newfilename[0] == '@')) {
-                                       nfname = newfilename;
-                                       
nfname.erase(nfname.begin(),nfname.begin()+1);
-                                       nfname = get_user_data_dir() + 
"/data/" + nfname;
-
-                                       LOG_CF << "got relative name '" << 
newfilename
-                                               << "' -> '" << nfname << 
"'\n";
-
-                                       if(newfilename[0] == '@' && 
file_exists(nfname) == false
-                                          && is_directory(nfname) == false)
-                                       {
-                                               nfname = "data/" + 
newfilename.substr(1);
-                                       }
-                               } else if(newfilename.size() >= 2 && 
newfilename[0] == '.'
-                                       && newfilename[1] == '/' )
-                               {
-                                       // If the filename begins with a "./",
-                                       // then look in the same directory as 
the file
-                                       // currrently being preprocessed.
-                                       nfname = newfilename;
-                                       
nfname.erase(nfname.begin(),nfname.begin()+2);
-                                       nfname = directory_ + nfname;
-                               } else {
-                                       nfname = "data/" + newfilename;
-                               }
-
-                               // Ignore filenames that start with '../' or 
contain '/../'.
-                               if (newfilename.rfind("../", 0) == 
std::string::npos
-                                       && newfilename.find("/../") == 
std::string::npos)
+                               std::string nfname = get_wml_location(symbol, 
directory_);
+                               if (!nfname.empty())
                                {
                                        if (!slowpath_)
                                                new 
preprocessor_file(target_, called_macros_, nfname);
@@ -996,10 +962,9 @@
                                                delete buf;
                                                strings_.back() += res.str();
                                        }
-                               } else {
-                                       ERR_CF << "Illegal path '" << 
newfilename
-                                               << "' found (../ not 
allowed).\n";
                                }
+                               else
+                                       ERR_CF << "File not found '" << 
symbol << "'\n";
                        } else {
                                ERR_CF << "Too much nested preprocessing 
inclusions at "
                                       << linenum_ << ' ' << target_.location_




Related Messages


Powered by MHonArc, Updated Tue Mar 03 19:00:07 2009