boost::filesystem::path make_relative_path( const boost::filesystem::path& p, const boost::filesystem::path& from) { QDir dir( QString( from.string().c_str())); QString fname( QString( p.string().c_str())); QString rel_path( dir.relativeFilePath( fname)); return boost::filesystem::path( rel_path.toStdString()); }
// Resolution order for ambiguous imports: // (1) filename as given // (2) underscore + given // (3) underscore + given + extension // (4) given + extension std::vector<Sass_Queued> resolve_includes(const std::string& root, const std::string& file) { std::string filename = join_paths(root, file); // supported extensions const std::vector<std::string> exts = { ".scss", ".sass", ".css" }; // split the filename std::string base(dir_name(file)); std::string name(base_name(file)); std::vector<Sass_Queued> includes; // create full path (maybe relative) std::string rel_path(join_paths(base, name)); std::string abs_path(join_paths(root, rel_path)); if (file_exists(abs_path)) includes.push_back(Sass_Queued(rel_path, abs_path, 0)); // next test variation with underscore rel_path = join_paths(base, "_" + name); abs_path = join_paths(root, rel_path); if (file_exists(abs_path)) includes.push_back(Sass_Queued(rel_path, abs_path, 0)); // next test exts plus underscore for(auto ext : exts) { rel_path = join_paths(base, "_" + name + ext); abs_path = join_paths(root, rel_path); if (file_exists(abs_path)) includes.push_back(Sass_Queued(rel_path, abs_path, 0)); } // next test plain name with exts for(auto ext : exts) { rel_path = join_paths(base, name + ext); abs_path = join_paths(root, rel_path); if (file_exists(abs_path)) includes.push_back(Sass_Queued(rel_path, abs_path, 0)); } // nothing found return includes; }
String Get(Key key, const String& relative_path) { boost::filesystem::path rel_path(relative_path); // If they actually gave us a relative path, just hand it back if (rel_path.is_complete()) return relative_path; boost::filesystem::path base_path(Get(key)); return (base_path / rel_path).string(); }
void deprecated(std::string msg, ParserState pstate) { std::string cwd(Sass::File::get_cwd()); std::cerr << "DEPRECATION WARNING: " << msg << std::endl; std::cerr << "will be an error in future versions of Sass." << std::endl; std::string rel_path(Sass::File::resolve_relative_path(pstate.path, cwd, cwd)); std::cerr << " on line " << pstate.line+1 << " of " << rel_path << std::endl; }
std::string Reference::makeOutputName(std::string const &name) { std::string path(name); #if defined(_WINDOWS) std::replace(path.begin(), path.end(), '\\', '/'); #endif return std::string("content/") + rel_path(path); }
boost::filesystem::path make_relative_path( const boost::filesystem::path& p, const boost::filesystem::path& from) { RAMEN_ASSERT( p.is_absolute()); RAMEN_ASSERT( from.is_absolute()); QDir dir( QString( file_cstring( from))); QString fname( QString( file_cstring( p))); QString rel_path( dir.relativeFilePath( fname)); return boost::filesystem::path( rel_path.toStdString()); }
void deprecated_bind(std::string msg, ParserState pstate) { std::string cwd(Sass::File::get_cwd()); std::string abs_path(Sass::File::rel2abs(pstate.path, cwd, cwd)); std::string rel_path(Sass::File::abs2rel(pstate.path, cwd, cwd)); std::string output_path(Sass::File::path_for_console(rel_path, abs_path, pstate.path)); std::cerr << "WARNING: " << msg << std::endl; std::cerr << " on line " << pstate.line+1 << " of " << output_path << std::endl; std::cerr << "This will be an error in future versions of Sass." << std::endl; }
std::string Reference::build(std::string const &dir, std::string const &name) { std::string path(dir); path += "/"; path += name; #if defined(_WINDOWS) std::replace(path.begin(), path.end(), '\\', '/'); #endif builtItems_.insert(path); return rel_path(path); }
bool CONFIG::get_path_of_new_resource(POOL_MEM &path, POOL_MEM &extramsg, const char *component, const char *resourcetype, const char *name, bool error_if_exists, bool create_directories) { POOL_MEM rel_path(PM_FNAME); POOL_MEM directory(PM_FNAME); POOL_MEM resourcetype_lowercase(resourcetype); resourcetype_lowercase.toLower(); if (!get_path_of_resource(path, component, resourcetype, name, false)) { return false; } path_get_directory(directory, path); if (create_directories) { path_create(directory); } if (!path_exists(directory)) { extramsg.bsprintf("Resource config directory \"%s\" does not exist.\n", directory.c_str()); return false; } /* * Store name for temporary file in extramsg. * Can be used, if result is true. * Otherwise it contains an error message. */ extramsg.bsprintf("%s.tmp", path.c_str()); if (!error_if_exists) { return true; } /* * File should not exists, as it is going to be created. */ if (path_exists(path)) { extramsg.bsprintf("Resource config file \"%s\" already exists.\n", path.c_str()); return false; } if (path_exists(extramsg)) { extramsg.bsprintf("Temporary resource config file \"%s.tmp\" already exists.\n", path.c_str()); return false; } return true; }
std::string OutputFileHandler::MakeFoldersAndReturnFullPath(const std::string& rDirectory) const { fs::path output_root(GetChasteTestOutputDirectory()); fs::path rel_path(rDirectory); if (!rel_path.empty() && (*(--rel_path.end())) == ".") { // rDirectory has a trailing slash, which gives an unhelpful last component rel_path.remove_leaf(); } // Make master wait (because other processes may be checking whether a directory exists) PetscTools::Barrier("OutputFileHandler::MakeFoldersAndReturnFullPathBeforeCreation"); // Are we the master process? Only the master should make any new directories if (PetscTools::AmMaster()) { try { // If necessary make the ChasteTestOutputDirectory - don't make it deleteable by Chaste fs::create_directories(output_root); // Note that this is a no-op if the folder exists already // Now make all the sub-folders requested one-by-one and add the .chaste_deletable_folder file to them fs::path next_folder(output_root); for (fs::path::iterator path_iter = rel_path.begin(); path_iter != rel_path.end(); ++path_iter) { next_folder /= *path_iter; bool created_dir = fs::create_directory(next_folder); if (created_dir) { // Add the Chaste signature file fs::ofstream sig_file(next_folder / SIG_FILE_NAME); sig_file.close(); } } } // LCOV_EXCL_START catch (const fs::filesystem_error& e) { TERMINATE("Error making test output folder: " << e.what()); } // LCOV_EXCL_STOP } // Wait for master to finish before going on to use the directory. PetscTools::Barrier("OutputFileHandler::MakeFoldersAndReturnFullPath"); std::string path_with_slash = (output_root / rel_path).string(); AddTrailingSlash(path_with_slash); return path_with_slash; }
void deprecated(std::string msg, std::string msg2, ParserState pstate) { std::string cwd(Sass::File::get_cwd()); std::string abs_path(Sass::File::rel2abs(pstate.path, cwd, cwd)); std::string rel_path(Sass::File::abs2rel(pstate.path, cwd, cwd)); std::string output_path(Sass::File::path_for_console(rel_path, pstate.path, pstate.path)); std::cerr << "DEPRECATION WARNING on line " << pstate.line + 1; if (output_path.length()) std::cerr << " of " << output_path; std::cerr << ":" << std::endl; std::cerr << msg << " and will be an error in future versions of Sass." << std::endl; if (msg2.length()) std::cerr << msg2 << std::endl; std::cerr << std::endl; }
bool findFileWrapper(const String& file, void* ctx) { auto const context = static_cast<ResolveIncludeContext*>(ctx); assert(context->path.isNull()); Stream::Wrapper* w = Stream::getWrapperFromURI(file); if (w && !dynamic_cast<FileStreamWrapper*>(w)) { if (w->stat(file, context->s) == 0) { context->path = file; return true; } } // handle file:// if (StringUtil::IsFileUrl(file)) { return findFileWrapper(file.substr(7), ctx); } if (!w) return false; // TranslatePath() will canonicalize the path and also check // whether the file is in an allowed directory. String translatedPath = File::TranslatePathKeepRelative(file); if (file[0] != '/') { if (findFile(translatedPath.get(), context->s, context->allow_dir)) { context->path = translatedPath; return true; } return false; } if (RuntimeOption::SandboxMode || !RuntimeOption::AlwaysUseRelativePath) { if (findFile(translatedPath.get(), context->s, context->allow_dir)) { context->path = translatedPath; return true; } } std::string server_root(SourceRootInfo::GetCurrentSourceRoot()); if (server_root.empty()) { server_root = std::string(g_context->getCwd().data()); if (server_root.empty() || server_root[server_root.size() - 1] != '/') { server_root += "/"; } } String rel_path(FileUtil::relativePath(server_root, translatedPath.data())); if (findFile(rel_path.get(), context->s, context->allow_dir)) { context->path = rel_path; return true; } return false; }
Variant include_impl_invoke(const String& file, bool once, const char *currentDir) { if (file[0] == '/') { if (RuntimeOption::SandboxMode || !RuntimeOption::AlwaysUseRelativePath) { try { return invoke_file(file, once, currentDir); } catch(PhpFileDoesNotExistException &e) {} } String rel_path(FileUtil::relativePath(RuntimeOption::SourceRoot, string(file.data()))); // Don't try/catch - We want the exception to be passed along return invoke_file(rel_path, once, currentDir); } else { // Don't try/catch - We want the exception to be passed along return invoke_file(file, once, currentDir); } }
const std::string traces_to_string(Backtraces traces, std::string indent) { std::stringstream ss; std::string cwd(File::get_cwd()); bool first = true; size_t i_beg = traces.size() - 1; size_t i_end = std::string::npos; for (size_t i = i_beg; i != i_end; i --) { const Backtrace& trace = traces[i]; // make path relative to the current directory std::string rel_path(File::abs2rel(trace.pstate.path, cwd, cwd)); // skip functions on error cases (unsure why ruby sass does this) // if (trace.caller.substr(0, 6) == ", in f") continue; if (first) { ss << indent; ss << "on line "; ss << trace.pstate.line + 1; ss << " of " << rel_path; // ss << trace.caller; first = false; } else { ss << trace.caller; ss << std::endl; ss << indent; ss << "from line "; ss << trace.pstate.line + 1; ss << " of " << rel_path; } } ss << std::endl; return ss.str(); }
bool CONFIG::get_path_of_resource(POOL_MEM &path, const char *component, const char *resourcetype, const char *name, bool set_wildcards) { POOL_MEM rel_path(PM_FNAME); POOL_MEM directory(PM_FNAME); POOL_MEM resourcetype_lowercase(resourcetype); resourcetype_lowercase.toLower(); if (!component) { if (m_config_include_dir) { component = m_config_include_dir; } else { return false; } } if (resourcetype_lowercase.strlen() <= 0) { if (set_wildcards) { resourcetype_lowercase.strcpy("*"); } else { return false; } } if (!name) { if (set_wildcards) { name = "*"; } else { return false; } } path.strcpy(m_config_dir); rel_path.bsprintf(m_config_include_naming_format, component, resourcetype_lowercase.c_str(), name); path_append(path, rel_path); return true; }
static bool findFileWrapper(CStrRef file, void* ctx) { ResolveIncludeContext* context = (ResolveIncludeContext*)ctx; assert(context->path.isNull()); // TranslatePath() will canonicalize the path and also check // whether the file is in an allowed directory. String translatedPath = File::TranslatePath(file, false, true); if (file[0] != '/') { if (HPHP::Eval::FileRepository::findFile(translatedPath.get(), context->s)) { context->path = translatedPath; return true; } return false; } if (RuntimeOption::SandboxMode || !RuntimeOption::AlwaysUseRelativePath) { if (HPHP::Eval::FileRepository::findFile(translatedPath.get(), context->s)) { context->path = translatedPath; return true; } } string server_root(SourceRootInfo::GetCurrentSourceRoot()); if (server_root.empty()) { server_root = string(g_vmContext->getCwd()->data()); if (server_root.empty() || server_root[server_root.size() - 1] != '/') { server_root += "/"; } } String rel_path(Util::relativePath(server_root, translatedPath.data())); if (HPHP::Eval::FileRepository::findFile(rel_path.get(), context->s)) { context->path = rel_path; return true; } return false; }
static int handle_error(Sass_Context* c_ctx) { try { throw; } catch (Exception::Base& e) { std::stringstream msg_stream; std::string cwd(Sass::File::get_cwd()); std::string msg_prefix(e.errtype()); bool got_newline = false; msg_stream << msg_prefix << ": "; const char* msg = e.what(); while (msg && *msg) { if (*msg == '\r') { got_newline = true; } else if (*msg == '\n') { got_newline = true; } else if (got_newline) { msg_stream << std::string(msg_prefix.size() + 2, ' '); got_newline = false; } msg_stream << *msg; ++msg; } if (!got_newline) msg_stream << "\n"; if (e.import_stack) { for (size_t i = 1; i < e.import_stack->size() - 1; ++i) { std::string path((*e.import_stack)[i]->imp_path); std::string rel_path(Sass::File::abs2rel(path, cwd, cwd)); msg_stream << std::string(msg_prefix.size() + 2, ' '); msg_stream << (i == 1 ? " on line " : " from line "); msg_stream << e.pstate.line + 1 << " of " << rel_path << "\n"; } } else { std::string rel_path(Sass::File::abs2rel(e.pstate.path, cwd, cwd)); msg_stream << std::string(msg_prefix.size() + 2, ' '); msg_stream << " on line " << e.pstate.line + 1 << " of " << rel_path << "\n"; } // now create the code trace (ToDo: maybe have util functions?) if (e.pstate.line != std::string::npos && e.pstate.column != std::string::npos) { size_t line = e.pstate.line; const char* line_beg = e.pstate.src; while (line_beg && *line_beg && line) { if (*line_beg == '\n') --line; ++line_beg; } const char* line_end = line_beg; while (line_end && *line_end && *line_end != '\n') { if (*line_end == '\n') break; if (*line_end == '\r') break; line_end++; } size_t max_left = 42; size_t max_right = 78; size_t move_in = e.pstate.column > max_left ? e.pstate.column - max_left : 0; size_t shorten = (line_end - line_beg) - move_in > max_right ? (line_end - line_beg) - move_in - max_right : 0; msg_stream << ">> " << std::string(line_beg + move_in, line_end - shorten) << "\n"; msg_stream << " " << std::string(e.pstate.column - move_in, '-') << "^\n"; } JsonNode* json_err = json_mkobject(); json_append_member(json_err, "status", json_mknumber(1)); json_append_member(json_err, "file", json_mkstring(e.pstate.path)); json_append_member(json_err, "line", json_mknumber((double)(e.pstate.line + 1))); json_append_member(json_err, "column", json_mknumber((double)(e.pstate.column + 1))); json_append_member(json_err, "message", json_mkstring(e.what())); json_append_member(json_err, "formatted", json_mkstream(msg_stream)); try { c_ctx->error_json = json_stringify(json_err, " "); } catch (...) {} c_ctx->error_message = sass_copy_string(msg_stream.str()); c_ctx->error_text = sass_copy_c_string(e.what()); c_ctx->error_status = 1; c_ctx->error_file = sass_copy_c_string(e.pstate.path); c_ctx->error_line = e.pstate.line + 1; c_ctx->error_column = e.pstate.column + 1; c_ctx->error_src = e.pstate.src; c_ctx->output_string = 0; c_ctx->source_map_string = 0; json_delete(json_err); } catch (std::bad_alloc& ba) { std::stringstream msg_stream; JsonNode* json_err = json_mkobject(); msg_stream << "Unable to allocate memory: " << ba.what() << std::endl; json_append_member(json_err, "status", json_mknumber(2)); json_append_member(json_err, "message", json_mkstring(ba.what())); json_append_member(json_err, "formatted", json_mkstream(msg_stream)); try { c_ctx->error_json = json_stringify(json_err, " "); } catch (...) {} c_ctx->error_message = sass_copy_string(msg_stream.str()); c_ctx->error_text = sass_copy_c_string(ba.what()); c_ctx->error_status = 2; c_ctx->output_string = 0; c_ctx->source_map_string = 0; json_delete(json_err); } catch (std::exception& e) { std::stringstream msg_stream; JsonNode* json_err = json_mkobject(); msg_stream << "Internal Error: " << e.what() << std::endl; json_append_member(json_err, "status", json_mknumber(3)); json_append_member(json_err, "message", json_mkstring(e.what())); json_append_member(json_err, "formatted", json_mkstream(msg_stream)); try { c_ctx->error_json = json_stringify(json_err, " "); } catch (...) {} c_ctx->error_message = sass_copy_string(msg_stream.str()); c_ctx->error_text = sass_copy_c_string(e.what()); c_ctx->error_status = 3; c_ctx->output_string = 0; c_ctx->source_map_string = 0; json_delete(json_err); } catch (std::string& e) { std::stringstream msg_stream; JsonNode* json_err = json_mkobject(); msg_stream << "Internal Error: " << e << std::endl; json_append_member(json_err, "status", json_mknumber(4)); json_append_member(json_err, "message", json_mkstring(e.c_str())); json_append_member(json_err, "formatted", json_mkstream(msg_stream)); try { c_ctx->error_json = json_stringify(json_err, " "); } catch (...) {} c_ctx->error_message = sass_copy_string(msg_stream.str()); c_ctx->error_text = sass_copy_c_string(e.c_str()); c_ctx->error_status = 4; c_ctx->output_string = 0; c_ctx->source_map_string = 0; json_delete(json_err); } catch (const char* e) { std::stringstream msg_stream; JsonNode* json_err = json_mkobject(); msg_stream << "Internal Error: " << e << std::endl; json_append_member(json_err, "status", json_mknumber(4)); json_append_member(json_err, "message", json_mkstring(e)); json_append_member(json_err, "formatted", json_mkstream(msg_stream)); try { c_ctx->error_json = json_stringify(json_err, " "); } catch (...) {} c_ctx->error_message = sass_copy_string(msg_stream.str()); c_ctx->error_text = sass_copy_c_string(e); c_ctx->error_status = 4; c_ctx->output_string = 0; c_ctx->source_map_string = 0; json_delete(json_err); } catch (...) { std::stringstream msg_stream; JsonNode* json_err = json_mkobject(); msg_stream << "Unknown error occurred" << std::endl; json_append_member(json_err, "status", json_mknumber(5)); json_append_member(json_err, "message", json_mkstring("unknown")); try { c_ctx->error_json = json_stringify(json_err, " "); } catch (...) {} c_ctx->error_message = sass_copy_string(msg_stream.str()); c_ctx->error_text = sass_copy_c_string("unknown"); c_ctx->error_status = 5; c_ctx->output_string = 0; c_ctx->source_map_string = 0; json_delete(json_err); } return c_ctx->error_status; }
bool findFileWrapper(const String& file, void* ctx) { auto const context = static_cast<ResolveIncludeContext*>(ctx); assertx(context->path.isNull()); Stream::Wrapper* w = Stream::getWrapperFromURI(file); if (w && !w->isNormalFileStream()) { // Stream wrappers can reenter PHP via user defined callbacks. Roll this // operation into a single event rqtrace::EventGuard trace{"STREAM_WRAPPER_STAT"}; rqtrace::DisableTracing disable; if (w->stat(file, context->s) == 0) { context->path = file; return true; } } // handle file:// if (StringUtil::IsFileUrl(file)) { return findFileWrapper(file.substr(7), ctx); } if (!w) return false; auto passW = RuntimeOption::RepoAuthoritative || dynamic_cast<FileStreamWrapper*>(w) || !w->isNormalFileStream() ? nullptr : w; // TranslatePath() will canonicalize the path and also check // whether the file is in an allowed directory. String translatedPath = File::TranslatePathKeepRelative(file); if (!FileUtil::isAbsolutePath(file.toCppString())) { if (findFile(translatedPath.get(), context->s, context->allow_dir, passW, context->nativeFuncs)) { context->path = translatedPath; return true; } return false; } if (RuntimeOption::SandboxMode || !RuntimeOption::AlwaysUseRelativePath) { if (findFile(translatedPath.get(), context->s, context->allow_dir, passW, context->nativeFuncs)) { context->path = translatedPath; return true; } } std::string server_root(SourceRootInfo::GetCurrentSourceRoot()); if (server_root.empty()) { server_root = std::string(g_context->getCwd().data()); if (server_root.empty() || FileUtil::isDirSeparator(server_root[server_root.size() - 1])) { server_root += FileUtil::getDirSeparator(); } } String rel_path(FileUtil::relativePath(server_root, translatedPath.data())); if (findFile(rel_path.get(), context->s, context->allow_dir, passW, context->nativeFuncs)) { context->path = rel_path; return true; } return false; }