Blender V2.61 - r43446
|
00001 /* 00002 * Copyright 2011, Blender Foundation. 00003 * 00004 * This program is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU General Public License 00006 * as published by the Free Software Foundation; either version 2 00007 * of the License, or (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software Foundation, 00016 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include "util_debug.h" 00020 #include "util_md5.h" 00021 #include "util_path.h" 00022 #include "util_string.h" 00023 00024 #include <OpenImageIO/sysutil.h> 00025 OIIO_NAMESPACE_USING 00026 00027 #include <stdio.h> 00028 00029 #define BOOST_FILESYSTEM_VERSION 2 00030 00031 #include <boost/filesystem.hpp> 00032 #include <boost/algorithm/string.hpp> 00033 00034 CCL_NAMESPACE_BEGIN 00035 00036 static string cached_path = ""; 00037 static string cached_user_path = ""; 00038 00039 void path_init(const string& path, const string& user_path) 00040 { 00041 cached_path = path; 00042 cached_user_path = user_path; 00043 } 00044 00045 string path_get(const string& sub) 00046 { 00047 if(cached_path == "") 00048 cached_path = path_dirname(Sysutil::this_program_path()); 00049 00050 return path_join(cached_path, sub); 00051 } 00052 00053 string path_user_get(const string& sub) 00054 { 00055 if(cached_user_path == "") 00056 cached_user_path = path_dirname(Sysutil::this_program_path()); 00057 00058 return path_join(cached_user_path, sub); 00059 } 00060 00061 string path_filename(const string& path) 00062 { 00063 return boost::filesystem::path(path).filename(); 00064 } 00065 00066 string path_dirname(const string& path) 00067 { 00068 return boost::filesystem::path(path).parent_path().string(); 00069 } 00070 00071 string path_join(const string& dir, const string& file) 00072 { 00073 return (boost::filesystem::path(dir) / boost::filesystem::path(file)).string(); 00074 } 00075 00076 string path_escape(const string& path) 00077 { 00078 string result = path; 00079 boost::replace_all(result, " ", "\\ "); 00080 return result; 00081 } 00082 00083 bool path_exists(const string& path) 00084 { 00085 return boost::filesystem::exists(path); 00086 } 00087 00088 static void path_files_md5_hash_recursive(MD5Hash& hash, const string& dir) 00089 { 00090 if(boost::filesystem::exists(dir)) { 00091 boost::filesystem::directory_iterator it(dir), it_end; 00092 00093 for(; it != it_end; it++) { 00094 if(boost::filesystem::is_directory(it->status())) { 00095 path_files_md5_hash_recursive(hash, it->path().string()); 00096 } 00097 else { 00098 string filepath = it->path().string(); 00099 00100 hash.append((const uint8_t*)filepath.c_str(), filepath.size()); 00101 hash.append_file(filepath); 00102 } 00103 } 00104 } 00105 } 00106 00107 string path_files_md5_hash(const string& dir) 00108 { 00109 /* computes md5 hash of all files in the directory */ 00110 MD5Hash hash; 00111 00112 path_files_md5_hash_recursive(hash, dir); 00113 00114 return hash.get_hex(); 00115 } 00116 00117 void path_create_directories(const string& path) 00118 { 00119 boost::filesystem::create_directories(path_dirname(path)); 00120 } 00121 00122 bool path_write_binary(const string& path, const vector<uint8_t>& binary) 00123 { 00124 path_create_directories(path); 00125 00126 /* write binary file from memory */ 00127 FILE *f = fopen(path.c_str(), "wb"); 00128 00129 if(!f) 00130 return false; 00131 00132 if(binary.size() > 0) 00133 fwrite(&binary[0], sizeof(uint8_t), binary.size(), f); 00134 00135 fclose(f); 00136 00137 return true; 00138 } 00139 00140 bool path_read_binary(const string& path, vector<uint8_t>& binary) 00141 { 00142 binary.resize(boost::filesystem::file_size(path)); 00143 00144 /* read binary file into memory */ 00145 FILE *f = fopen(path.c_str(), "rb"); 00146 00147 if(!f) 00148 return false; 00149 00150 if(binary.size() == 0) { 00151 fclose(f); 00152 return false; 00153 } 00154 00155 if(fread(&binary[0], sizeof(uint8_t), binary.size(), f) != binary.size()) { 00156 fclose(f); 00157 return false; 00158 } 00159 00160 fclose(f); 00161 00162 return true; 00163 } 00164 00165 static bool path_read_text(const string& path, string& text) 00166 { 00167 vector<uint8_t> binary; 00168 00169 if(!path_exists(path) || !path_read_binary(path, binary)) 00170 return false; 00171 00172 const char *str = (const char*)&binary[0]; 00173 size_t size = binary.size(); 00174 text = string(str, size); 00175 00176 return true; 00177 } 00178 00179 string path_source_replace_includes(const string& source_, const string& path) 00180 { 00181 /* our own little c preprocessor that replaces #includes with the file 00182 contents, to work around issue of opencl drivers not supporting 00183 include paths with spaces in them */ 00184 string source = source_; 00185 const string include = "#include \""; 00186 size_t n, pos = 0; 00187 00188 while((n = source.find(include, pos)) != string::npos) { 00189 size_t n_start = n + include.size(); 00190 size_t n_end = source.find("\"", n_start); 00191 string filename = source.substr(n_start, n_end - n_start); 00192 00193 string text, filepath = path_join(path, filename); 00194 00195 if(path_read_text(filepath, text)) { 00196 text = path_source_replace_includes(text, path_dirname(filepath)); 00197 source.replace(n, n_end + 1 - n, "\n" + text + "\n"); 00198 } 00199 else 00200 pos = n_end; 00201 } 00202 00203 return source; 00204 } 00205 00206 CCL_NAMESPACE_END 00207