Cycles: Remove workaround for MSVC2010 and Boost
[blender.git] / intern / cycles / util / util_path.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "util_debug.h"
18 #include "util_md5.h"
19 #include "util_path.h"
20 #include "util_string.h"
21
22 #include <OpenImageIO/strutil.h>
23 #include <OpenImageIO/sysutil.h>
24 OIIO_NAMESPACE_USING
25
26 #include <stdio.h>
27
28 #include <boost/filesystem.hpp> 
29 #include <boost/algorithm/string.hpp>
30
31 CCL_NAMESPACE_BEGIN
32
33 static string cached_path = "";
34 static string cached_user_path = "";
35
36 static boost::filesystem::path to_boost(const string& path)
37 {
38         return boost::filesystem::path(path.c_str());
39 }
40
41 static string from_boost(const boost::filesystem::path& path)
42 {
43         return path.string().c_str();
44 }
45
46 static char *path_specials(const string& sub)
47 {
48         static bool env_init = false;
49         static char *env_shader_path;
50         static char *env_kernel_path;
51         if(!env_init) {
52                 env_shader_path = getenv("CYCLES_SHADER_PATH");
53                 env_kernel_path = getenv("CYCLES_KERNEL_PATH");
54                 env_init = true;
55         }
56         if(env_shader_path != NULL && sub == "shader") {
57                 return env_shader_path;
58         }
59         else if(env_shader_path != NULL && sub == "kernel") {
60                 return env_kernel_path;
61         }
62         return NULL;
63 }
64
65 void path_init(const string& path, const string& user_path)
66 {
67         cached_path = path;
68         cached_user_path = user_path;
69 }
70
71 string path_get(const string& sub)
72 {
73         char *special = path_specials(sub);
74         if(special != NULL)
75                 return special;
76
77         if(cached_path == "")
78                 cached_path = path_dirname(Sysutil::this_program_path());
79
80         return path_join(cached_path, sub);
81 }
82
83 string path_user_get(const string& sub)
84 {
85         if(cached_user_path == "")
86                 cached_user_path = path_dirname(Sysutil::this_program_path());
87
88         return path_join(cached_user_path, sub);
89 }
90
91 string path_filename(const string& path)
92 {
93         return from_boost(to_boost(path).filename());
94 }
95
96 string path_dirname(const string& path)
97 {
98         return from_boost(to_boost(path).parent_path());
99 }
100
101 string path_join(const string& dir, const string& file)
102 {
103         return from_boost((to_boost(dir) / to_boost(file)));
104 }
105
106 string path_escape(const string& path)
107 {
108         string result = path;
109         boost::replace_all(result, " ", "\\ ");
110         return result;
111 }
112
113 bool path_is_relative(const string& path)
114 {
115         return to_boost(path).is_relative();
116 }
117
118 bool path_exists(const string& path)
119 {
120         return boost::filesystem::exists(to_boost(path));
121 }
122
123 static void path_files_md5_hash_recursive(MD5Hash& hash, const string& dir)
124 {
125         boost::filesystem::path dirpath = to_boost(dir);
126
127         if(boost::filesystem::exists(dirpath)) {
128                 boost::filesystem::directory_iterator it(dirpath), it_end;
129
130                 for(; it != it_end; it++) {
131                         if(boost::filesystem::is_directory(it->status())) {
132                                 path_files_md5_hash_recursive(hash, from_boost(it->path()));
133                         }
134                         else {
135                                 string filepath = from_boost(it->path());
136
137                                 hash.append((const uint8_t*)filepath.c_str(), filepath.size());
138                                 hash.append_file(filepath);
139                         }
140                 }
141         }
142 }
143
144 string path_files_md5_hash(const string& dir)
145 {
146         /* computes md5 hash of all files in the directory */
147         MD5Hash hash;
148
149         path_files_md5_hash_recursive(hash, dir);
150
151         return hash.get_hex();
152 }
153
154 void path_create_directories(const string& path)
155 {
156         boost::filesystem::create_directories(to_boost(path_dirname(path)));
157 }
158
159 bool path_write_binary(const string& path, const vector<uint8_t>& binary)
160 {
161         path_create_directories(path);
162
163         /* write binary file from memory */
164         FILE *f = path_fopen(path, "wb");
165
166         if(!f)
167                 return false;
168
169         if(binary.size() > 0)
170                 fwrite(&binary[0], sizeof(uint8_t), binary.size(), f);
171
172         fclose(f);
173
174         return true;
175 }
176
177 bool path_write_text(const string& path, string& text)
178 {
179         vector<uint8_t> binary(text.length(), 0);
180         std::copy(text.begin(), text.end(), binary.begin());
181
182         return path_write_binary(path, binary);
183 }
184
185 bool path_read_binary(const string& path, vector<uint8_t>& binary)
186 {
187         binary.resize(boost::filesystem::file_size(to_boost(path)));
188
189         /* read binary file into memory */
190         FILE *f = path_fopen(path, "rb");
191
192         if(!f)
193                 return false;
194
195         if(binary.size() == 0) {
196                 fclose(f);
197                 return false;
198         }
199
200         if(fread(&binary[0], sizeof(uint8_t), binary.size(), f) != binary.size()) {
201                 fclose(f);
202                 return false;
203         }
204
205         fclose(f);
206
207         return true;
208 }
209
210 bool path_read_text(const string& path, string& text)
211 {
212         vector<uint8_t> binary;
213
214         if(!path_exists(path) || !path_read_binary(path, binary))
215                 return false;
216
217         const char *str = (const char*)&binary[0];
218         size_t size = binary.size();
219         text = string(str, size);
220
221         return true;
222 }
223
224 uint64_t path_modified_time(const string& path)
225 {
226         if(boost::filesystem::exists(to_boost(path)))
227                 return (uint64_t)boost::filesystem::last_write_time(to_boost(path));
228         
229         return 0;
230 }
231
232 string path_source_replace_includes(const string& source_, const string& path)
233 {
234         /* our own little c preprocessor that replaces #includes with the file
235          * contents, to work around issue of opencl drivers not supporting
236          * include paths with spaces in them */
237         string source = source_;
238         const string include = "#include \"";
239         size_t n, pos = 0;
240
241         while((n = source.find(include, pos)) != string::npos) {
242                 size_t n_start = n + include.size();
243                 size_t n_end = source.find("\"", n_start);
244                 string filename = source.substr(n_start, n_end - n_start);
245
246                 string text, filepath = path_join(path, filename);
247
248                 if(path_read_text(filepath, text)) {
249                         text = path_source_replace_includes(text, path_dirname(filepath));
250                         source.replace(n, n_end + 1 - n, "\n" + text + "\n");
251                 }
252                 else
253                         pos = n_end;
254         }
255
256         return source;
257 }
258
259 FILE *path_fopen(const string& path, const string& mode)
260 {
261         return fopen(path.c_str(), mode.c_str());
262 }
263
264 void path_cache_clear_except(const string& name, const set<string>& except)
265 {
266         string dir = path_user_get("cache");
267
268         if(boost::filesystem::exists(dir)) {
269                 boost::filesystem::directory_iterator it(dir), it_end;
270
271                 for(; it != it_end; it++) {
272                         string filename = from_boost(it->path().filename().string());
273
274                         if(boost::starts_with(filename, name))
275                                 if(except.find(filename) == except.end())
276                                         boost::filesystem::remove(to_boost(filename));
277                 }
278         }
279
280 }
281
282 CCL_NAMESPACE_END
283