2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "util_debug.h"
|
2011-09-03 10:49:54 +00:00
|
|
|
#include "util_md5.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
#include "util_path.h"
|
|
|
|
#include "util_string.h"
|
|
|
|
|
2014-01-10 23:47:58 +00:00
|
|
|
#include <OpenImageIO/strutil.h>
|
2011-04-27 11:58:34 +00:00
|
|
|
#include <OpenImageIO/sysutil.h>
|
|
|
|
OIIO_NAMESPACE_USING
|
|
|
|
|
2011-09-09 12:04:39 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2012-07-29 13:52:38 +00:00
|
|
|
#include <boost/version.hpp>
|
|
|
|
|
2012-07-25 20:25:47 +00:00
|
|
|
#if (BOOST_VERSION < 104400)
|
|
|
|
# define BOOST_FILESYSTEM_VERSION 2
|
|
|
|
#endif
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
2011-09-08 18:58:07 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
static string cached_path = "";
|
2011-09-09 12:04:39 +00:00
|
|
|
static string cached_user_path = "";
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2014-01-10 23:47:58 +00:00
|
|
|
static boost::filesystem::path to_boost(const string& path)
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
2014-01-14 18:15:58 +00:00
|
|
|
std::wstring path_utf16 = Strutil::utf8_to_utf16(path.c_str());
|
|
|
|
return boost::filesystem::path(path_utf16.c_str());
|
2014-01-10 23:47:58 +00:00
|
|
|
#else
|
2014-01-14 18:15:58 +00:00
|
|
|
return boost::filesystem::path(path.c_str());
|
2014-01-10 23:47:58 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static string from_boost(const boost::filesystem::path& path)
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
2014-01-14 18:15:58 +00:00
|
|
|
return Strutil::utf16_to_utf8(path.wstring().c_str());
|
2014-01-10 23:47:58 +00:00
|
|
|
#else
|
2014-01-14 18:15:58 +00:00
|
|
|
return path.string().c_str();
|
2014-01-10 23:47:58 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-09-09 12:04:39 +00:00
|
|
|
void path_init(const string& path, const string& user_path)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
cached_path = path;
|
2011-09-09 12:04:39 +00:00
|
|
|
cached_user_path = user_path;
|
2013-12-27 21:38:46 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
// fix for https://svn.boost.org/trac/boost/ticket/6320
|
2014-01-10 23:47:58 +00:00
|
|
|
boost::filesystem::path::imbue( std::locale( "" ) );
|
2013-12-27 21:38:46 +00:00
|
|
|
#endif
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string path_get(const string& sub)
|
|
|
|
{
|
|
|
|
if(cached_path == "")
|
|
|
|
cached_path = path_dirname(Sysutil::this_program_path());
|
|
|
|
|
|
|
|
return path_join(cached_path, sub);
|
|
|
|
}
|
|
|
|
|
2011-09-09 12:04:39 +00:00
|
|
|
string path_user_get(const string& sub)
|
|
|
|
{
|
|
|
|
if(cached_user_path == "")
|
|
|
|
cached_user_path = path_dirname(Sysutil::this_program_path());
|
|
|
|
|
|
|
|
return path_join(cached_user_path, sub);
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
string path_filename(const string& path)
|
|
|
|
{
|
2012-07-25 20:25:47 +00:00
|
|
|
#if (BOOST_FILESYSTEM_VERSION == 2)
|
2014-01-10 23:47:58 +00:00
|
|
|
return to_boost(path).filename();
|
2012-07-25 20:25:47 +00:00
|
|
|
#else
|
2014-01-10 23:47:58 +00:00
|
|
|
return from_boost(to_boost(path).filename());
|
2012-07-25 20:25:47 +00:00
|
|
|
#endif
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string path_dirname(const string& path)
|
|
|
|
{
|
2014-01-10 23:47:58 +00:00
|
|
|
return from_boost(to_boost(path).parent_path());
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string path_join(const string& dir, const string& file)
|
|
|
|
{
|
2014-01-10 23:47:58 +00:00
|
|
|
return from_boost((to_boost(dir) / to_boost(file)));
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2011-09-08 18:58:07 +00:00
|
|
|
string path_escape(const string& path)
|
|
|
|
{
|
|
|
|
string result = path;
|
|
|
|
boost::replace_all(result, " ", "\\ ");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-03-21 16:22:41 +00:00
|
|
|
bool path_is_relative(const string& path)
|
|
|
|
{
|
|
|
|
return to_boost(path).is_relative();
|
|
|
|
}
|
|
|
|
|
2011-09-08 18:58:07 +00:00
|
|
|
bool path_exists(const string& path)
|
|
|
|
{
|
2014-01-10 23:47:58 +00:00
|
|
|
return boost::filesystem::exists(to_boost(path));
|
2011-09-08 18:58:07 +00:00
|
|
|
}
|
|
|
|
|
2011-09-27 19:35:41 +00:00
|
|
|
static void path_files_md5_hash_recursive(MD5Hash& hash, const string& dir)
|
2011-09-03 10:49:54 +00:00
|
|
|
{
|
2014-01-10 23:47:58 +00:00
|
|
|
boost::filesystem::path dirpath = to_boost(dir);
|
|
|
|
|
|
|
|
if(boost::filesystem::exists(dirpath)) {
|
|
|
|
boost::filesystem::directory_iterator it(dirpath), it_end;
|
2011-09-03 10:49:54 +00:00
|
|
|
|
|
|
|
for(; it != it_end; it++) {
|
|
|
|
if(boost::filesystem::is_directory(it->status())) {
|
2014-01-10 23:47:58 +00:00
|
|
|
path_files_md5_hash_recursive(hash, from_boost(it->path()));
|
2011-09-03 10:49:54 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-01-10 23:47:58 +00:00
|
|
|
string filepath = from_boost(it->path());
|
2011-09-03 10:49:54 +00:00
|
|
|
|
|
|
|
hash.append((const uint8_t*)filepath.c_str(), filepath.size());
|
|
|
|
hash.append_file(filepath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-27 19:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string path_files_md5_hash(const string& dir)
|
|
|
|
{
|
|
|
|
/* computes md5 hash of all files in the directory */
|
|
|
|
MD5Hash hash;
|
|
|
|
|
|
|
|
path_files_md5_hash_recursive(hash, dir);
|
2011-09-03 10:49:54 +00:00
|
|
|
|
|
|
|
return hash.get_hex();
|
|
|
|
}
|
|
|
|
|
2011-09-12 13:13:56 +00:00
|
|
|
void path_create_directories(const string& path)
|
2011-09-09 12:04:39 +00:00
|
|
|
{
|
2014-01-10 23:47:58 +00:00
|
|
|
boost::filesystem::create_directories(to_boost(path_dirname(path)));
|
2011-09-12 13:13:56 +00:00
|
|
|
}
|
2011-09-09 12:04:39 +00:00
|
|
|
|
2011-09-12 13:13:56 +00:00
|
|
|
bool path_write_binary(const string& path, const vector<uint8_t>& binary)
|
|
|
|
{
|
|
|
|
path_create_directories(path);
|
|
|
|
|
|
|
|
/* write binary file from memory */
|
2014-01-10 23:47:58 +00:00
|
|
|
FILE *f = path_fopen(path, "wb");
|
2011-09-09 12:04:39 +00:00
|
|
|
|
|
|
|
if(!f)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(binary.size() > 0)
|
|
|
|
fwrite(&binary[0], sizeof(uint8_t), binary.size(), f);
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-27 16:21:07 +00:00
|
|
|
bool path_write_text(const string& path, string& text)
|
|
|
|
{
|
|
|
|
vector<uint8_t> binary(text.length(), 0);
|
|
|
|
std::copy(text.begin(), text.end(), binary.begin());
|
|
|
|
|
|
|
|
return path_write_binary(path, binary);
|
|
|
|
}
|
|
|
|
|
2011-09-09 12:04:39 +00:00
|
|
|
bool path_read_binary(const string& path, vector<uint8_t>& binary)
|
|
|
|
{
|
2014-01-10 23:47:58 +00:00
|
|
|
binary.resize(boost::filesystem::file_size(to_boost(path)));
|
2011-09-09 12:04:39 +00:00
|
|
|
|
|
|
|
/* read binary file into memory */
|
2014-01-10 23:47:58 +00:00
|
|
|
FILE *f = path_fopen(path, "rb");
|
2011-09-09 12:04:39 +00:00
|
|
|
|
|
|
|
if(!f)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(binary.size() == 0) {
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fread(&binary[0], sizeof(uint8_t), binary.size(), f) != binary.size()) {
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-03 14:32:35 +00:00
|
|
|
bool path_read_text(const string& path, string& text)
|
2011-11-22 16:38:58 +00:00
|
|
|
{
|
|
|
|
vector<uint8_t> binary;
|
|
|
|
|
|
|
|
if(!path_exists(path) || !path_read_binary(path, binary))
|
|
|
|
return false;
|
2013-05-27 16:21:07 +00:00
|
|
|
|
2011-11-22 16:38:58 +00:00
|
|
|
const char *str = (const char*)&binary[0];
|
|
|
|
size_t size = binary.size();
|
|
|
|
text = string(str, size);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-03 14:32:35 +00:00
|
|
|
uint64_t path_modified_time(const string& path)
|
|
|
|
{
|
2014-01-10 23:47:58 +00:00
|
|
|
if(boost::filesystem::exists(to_boost(path)))
|
|
|
|
return (uint64_t)boost::filesystem::last_write_time(to_boost(path));
|
2012-11-03 14:32:35 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-22 16:38:58 +00:00
|
|
|
string path_source_replace_includes(const string& source_, const string& path)
|
|
|
|
{
|
|
|
|
/* our own little c preprocessor that replaces #includes with the file
|
2012-06-09 17:22:52 +00:00
|
|
|
* contents, to work around issue of opencl drivers not supporting
|
|
|
|
* include paths with spaces in them */
|
2011-11-22 16:38:58 +00:00
|
|
|
string source = source_;
|
|
|
|
const string include = "#include \"";
|
|
|
|
size_t n, pos = 0;
|
|
|
|
|
|
|
|
while((n = source.find(include, pos)) != string::npos) {
|
|
|
|
size_t n_start = n + include.size();
|
|
|
|
size_t n_end = source.find("\"", n_start);
|
|
|
|
string filename = source.substr(n_start, n_end - n_start);
|
|
|
|
|
|
|
|
string text, filepath = path_join(path, filename);
|
|
|
|
|
|
|
|
if(path_read_text(filepath, text)) {
|
|
|
|
text = path_source_replace_includes(text, path_dirname(filepath));
|
|
|
|
source.replace(n, n_end + 1 - n, "\n" + text + "\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pos = n_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
2014-01-10 23:47:58 +00:00
|
|
|
FILE *path_fopen(const string& path, const string& mode)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
std::wstring path_utf16 = Strutil::utf8_to_utf16(path);
|
|
|
|
std::wstring mode_utf16 = Strutil::utf8_to_utf16(mode);
|
|
|
|
|
|
|
|
return _wfopen(path_utf16.c_str(), mode_utf16.c_str());
|
|
|
|
#else
|
|
|
|
return fopen(path.c_str(), mode.c_str());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-01-23 00:13:09 +00:00
|
|
|
void path_cache_clear_except(const string& name, const set<string>& except)
|
|
|
|
{
|
|
|
|
string dir = path_user_get("cache");
|
|
|
|
|
|
|
|
if(boost::filesystem::exists(dir)) {
|
|
|
|
boost::filesystem::directory_iterator it(dir), it_end;
|
|
|
|
|
|
|
|
for(; it != it_end; it++) {
|
|
|
|
#if (BOOST_FILESYSTEM_VERSION == 2)
|
|
|
|
string filename = from_boost(it->path().filename());
|
|
|
|
#else
|
|
|
|
string filename = from_boost(it->path().filename().string());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(boost::starts_with(filename, name))
|
|
|
|
if(except.find(filename) == except.end())
|
|
|
|
boost::filesystem::remove(to_boost(filename));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|